first commit.

way too late.
This commit is contained in:
didier amyot committed 2015-01-31 10:19:31 -08:00
commit dbbfaf99b6
15 files changed
+757

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
package main
import (
"testing"
"github.com/didiercrunch/doorman"
"gopkg.in/mgo.v2/bson"
)
func TestDoormanDistribution(t *testing.T) {
wab = doorman.NewDoorman(bson.NewObjectId(), []float64{0.25, 0.5, 0.10, 0.05, 0.1})
n := 10000
cases := make([]float64, wab.Length())
for i := 0; i <= n; i++ {
cases[wab.GetRandomCase()] += 1
}
expt := make([]float64, wab.Length())
for i, p := range wab.Probabilities {
expt[i] = p * float64(n)
}
// Todo: use chi square test to assert the results are not absurde
//if err := AssertTwoDistributionAreProbabliNotDifferent(expt, cases, 0.001); err != nil {
// t.Error(err)
//}
}
+78
View File
@@ -0,0 +1,78 @@
package main
import (
"html/template"
"log"
"net/http"
"os"
"github.com/didiercrunch/doorman"
"gopkg.in/mgo.v2/bson"
)
var wab = doorman.NewDoorman(bson.ObjectIdHex("54b33d5ef6fb5e3b77000001"), []float64{0.25, 0.5, 0.10, 0.05, 0.1})
func init() {
if err := wab.NanoMsgSubscriber("tcp://127.0.0.1:40899"); err != nil {
panic(err)
}
}
func GetDoormanValue(r *http.Request) (string, int) {
switch wab.GetCaseFromData([]byte(r.URL.String())) {
case 0:
return "blue", 0
case 1:
return "red", 1
case 2:
return "pink", 2
case 3:
return "yellow", 3
case 4:
return "magenta", 4
}
log.Panic("impossible value returned by doorman")
return "", 0
}
var tmpl = `
<!DOCTYPE html>
<html>
<head></head>
<body style="background:{{ .color }};width:100%;height:100%">
<h1 style="text-align:center;">the doorman value is {{ .value }}</h1>
<p style="text-align:center;">
this doorman depend solely on the url. If you hit different url you
will have different doorman results.
</p>
</body>
</html>
`
func Handler(w http.ResponseWriter, r *http.Request) {
t, err := template.New("foo").Parse(tmpl)
if err != nil {
panic(err)
}
m := make(map[string]interface{})
m["color"], m["value"] = GetDoormanValue(r)
if err := t.Execute(w, m); err != nil {
panic(err)
}
}
func getPort() string {
if len(os.Args) > 1 {
return os.Args[1]
} else {
return "8080"
}
}
func main() {
http.HandleFunc("/", Handler)
port := getPort()
log.Println("serving on port", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}