forked from kandoo/beehive
/
bench.go
105 lines (92 loc) · 2.31 KB
/
bench.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"runtime/pprof"
"time"
bh "github.com/kandoo/beehive"
"github.com/kandoo/beehive/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/kandoo/beehive/examples/kvstore/store"
)
var (
replFactor = flag.Int("kv.rf", 3, "replication factor")
buckets = flag.Int("kv.b", 1024, "number of buckets")
rounds = flag.Int("kv.r", 100, "rounds of benchmark")
numkeys = flag.Int("kv.n", 1024, "number of keys")
tries = flag.Int("kv.t", 1024, "number of requests sent per key")
get = flag.Bool("kv.get", false, "use gets")
cpuprofile = flag.String("kv.cpuprofile", "", "write cpu profile to file")
quiet = flag.Bool("kv.quiet", false, "no raft log")
random = flag.Bool("kv.rand", false, "whether to use random placement")
output = flag.String("kv.output", "bench.out", "the output file")
)
func main() {
flag.Parse()
rand.Seed(time.Now().UnixNano())
if *quiet {
log.SetOutput(ioutil.Discard)
}
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
panic(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
hive := bh.NewHive()
os.RemoveAll(hive.Config().StatePath)
app := hive.NewApp("kvstore", bh.Persistent(*replFactor))
kvs := &store.KVStore{
Hive: hive,
Buckets: uint64(*buckets),
}
app.Handle(store.Put{}, kvs)
app.Handle(store.Get(""), kvs)
go hive.Start()
time.Sleep(4 * time.Minute)
keys := make([]string, *numkeys)
reqs := make([]interface{}, *numkeys)
val := "val"
for i, _ := range keys {
keys[i] = fmt.Sprintf("%dkeys%d", i, i)
if *get {
reqs[i] = store.Get(keys[i])
} else {
reqs[i] = store.Put{Key: keys[i], Val: val}
}
}
for _, k := range keys {
hive.Emit(store.Put{Key: k, Val: val})
hive.Sync(context.Background(), store.Get(k))
}
ts := make([]time.Duration, *rounds)
for i := 0; i < *rounds; i++ {
start := time.Now()
for j := 0; j < *tries; j++ {
for _, r := range reqs {
hive.Emit(r)
}
}
for _, k := range keys {
hive.Sync(context.Background(), store.Get(k))
}
ts[i] = time.Since(start)
}
hive.Stop()
f, err := os.Create(*output)
if err != nil {
panic(err)
}
defer f.Close()
w := bufio.NewWriter(f)
for _, t := range ts {
fmt.Fprintf(w, "%v\n", uint64(t))
}
w.Flush()
}