forked from bpowers/radix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.go
110 lines (93 loc) · 2.24 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
106
107
108
109
110
package main
import (
"flag"
"fmt"
"github.com/fzzbt/radix/redis"
"log"
"os"
"runtime"
"runtime/pprof"
"strings"
"time"
)
var connections *int = flag.Int("c", 50, "number of connections")
var requests *int = flag.Int("n", 10000, "number of request")
var dsize *int = flag.Int("d", 3, "data size")
var cpuprof *string = flag.String("cpuprof", "", "filename for cpuprof")
var gomaxprocs *int = flag.Int("p", 8, "GOMAXPROCS value")
// handleReplyError prints an error message for the given reply.
func handleReplyError(rep *redis.Reply) {
if rep.Err != nil {
log.Println("redis: " + rep.Err.Error())
} else {
log.Println("redis: unexpected reply type")
}
}
// benchmark benchmarks the given function.
func benchmark(data string, handle func(string, *redis.Client, chan struct{})) time.Duration {
conf := redis.DefaultConfig()
conf.Database = 8
conf.PoolCapacity = *connections
c := redis.NewClient(conf)
defer c.Close()
rep := c.Flushdb()
if rep.Err != nil {
handleReplyError(rep)
os.Exit(1)
}
ch := make(chan struct{})
start := time.Now()
for i := 0; i < *connections; i++ {
go handle(data, c, ch)
}
for i := 0; i < *requests; i++ {
ch <- struct{}{}
}
dur := time.Now().Sub(start)
return dur
}
func run(name string, handle func(string, *redis.Client, chan struct{}), data string) {
fmt.Printf("===== %s =====\n", strings.ToUpper(name))
duration := benchmark(data, handle)
rps := float64(*requests) / duration.Seconds()
fmt.Println("Requests per second: ", rps)
fmt.Println("Duration: ", duration)
fmt.Println()
}
func main() {
var data string
flag.Parse()
runtime.GOMAXPROCS(*gomaxprocs)
if *cpuprof != "" {
f, err := os.Create(*cpuprof)
if err != nil {
log.Fatalln(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
for i := 0; i < *dsize; i++ {
data += "x"
}
fmt.Printf(
"Connections: %d, Requests: %d, Payload: %d bytes, GOMAXPROCS: %d\n\n",
*connections,
*requests,
*dsize,
*gomaxprocs)
args := flag.Args()
if len(args) == 0 {
// run all tests by default
for i, name := range testNames {
run(name, testHandles[i], data)
}
} else {
for i, name := range testNames {
for _, arg := range args {
if arg == name {
run(name, testHandles[i], data)
}
}
}
}
}