Last active
August 29, 2015 14:03
-
-
Save rjeczalik/da25b74997f1c2712cac to your computer and use it in GitHub Desktop.
fixture: licstat/scheduler/all_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"math/rand" | |
"os" | |
"strconv" | |
"text/template" | |
"time" | |
) | |
func init() { | |
rand.Seed(time.Now().UnixNano()) | |
} | |
const usage = "usage: all_test <count> <density> <event>..." | |
func die(v interface{}) { | |
fmt.Fprintln(os.Stderr, v) | |
os.Exit(1) | |
} | |
type Event struct { | |
Level int | |
Priority int | |
Value int | |
} | |
type Tmpl struct { | |
E map[string][]Event | |
C map[int]map[string]int | |
} | |
func BuildTmpl(m map[string][]Event) *Tmpl { | |
t := &Tmpl{ | |
E: m, | |
C: make(map[int]map[string]int), | |
} | |
for k, v := range t.E { | |
for _, e := range v { | |
if c, ok := t.C[e.Level]; ok { | |
c[k]++ | |
} else { | |
t.C[e.Level] = map[string]int{k: 1} | |
} | |
} | |
} | |
return t | |
} | |
func GenEvents(c int, d float64, s []string) map[string][]Event { | |
m, n := make(map[string][]Event, len(s)), int32(d*float64(c))+1 | |
for _, s := range s { | |
m[s] = make([]Event, c) | |
for i := range m[s] { | |
m[s][i].Level, m[s][i].Priority, m[s][i].Value = int(rand.Int31n(n)), int(rand.Int31n(n)), 1 | |
} | |
} | |
return m | |
} | |
var tmpl = template.Must(template.New("egen").Parse(`0: { | |
m: events{ {{range $type, $events := .E}} | |
{{$type}}: { | |
d: t, | |
e: []Event{ {{range $_, $event := $events}} | |
{Level: {{$event.Level}}, Priority: {{$event.Priority}}, Value: {{$event.Value}}},{{end}} | |
}, | |
},{{end}} | |
}, | |
n: count{ {{range $key, $value := .C}} | |
{{$key}}: { | |
{{range $typ, $count := $value}} {{$typ}}: {{$count}}, | |
{{end}} },{{end}} | |
}, | |
}, | |
`)) | |
func ExecTemplate(e map[string][]Event, w io.Writer) error { | |
return tmpl.Execute(w, BuildTmpl(e)) | |
} | |
func main() { | |
if len(os.Args) < 4 { | |
die(usage) | |
} | |
var ( | |
count int | |
density float64 | |
err error | |
) | |
if count, err = strconv.Atoi(os.Args[1]); err != nil { | |
die(err) | |
} | |
if density, err = strconv.ParseFloat(os.Args[2], 64); err != nil { | |
die(err) | |
} | |
if ExecTemplate(GenEvents(count, density, os.Args[3:]), os.Stdout); err != nil { | |
die(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment