-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark.go
77 lines (63 loc) · 1.58 KB
/
benchmark.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
package main
import (
"fmt"
"os"
"sort"
"time"
"github.com/olekukonko/tablewriter"
)
type BenchmarkResult struct {
CacheName string
Duration time.Duration
Hits int64
Misses int64
}
func (br *BenchmarkResult) hitRate() float64 {
return float64(br.Hits) / float64(br.Hits+br.Misses) * 100
}
type Benchmark struct {
ItemSize int
CacheSizeMultiplier float64
ZipfAlpha float64
Concurrency int
Results []*BenchmarkResult
}
func (b *Benchmark) AddResult(r *BenchmarkResult) {
b.Results = append(b.Results, r)
}
func (b *Benchmark) WriteToConsole() {
b.sortResults()
workloads := b.ItemSize * workloadMultiplier
fmt.Printf("itemSize=%d, workloads=%d, cacheSize=%.2f%%, zipf's alpha=%.2f, concurrency=%d\n\n",
b.ItemSize,
workloads,
b.CacheSizeMultiplier*100,
b.ZipfAlpha,
b.Concurrency)
headers := []string{"Cache", "HitRate", "QPS", "Hits", "Misses"}
table := tablewriter.NewWriter(os.Stdout)
for _, ret := range b.Results {
qps := float64(ret.Hits+ret.Misses) / float64(ret.Duration.Milliseconds())
qps = qps * 1000
table.Append([]string{
ret.CacheName,
fmt.Sprintf("%.2f%%", ret.hitRate()),
fmt.Sprintf("%.f", qps),
fmt.Sprintf("%d", ret.Hits),
fmt.Sprintf("%d", ret.Misses),
})
}
table.SetHeader(headers)
table.SetBorder(false)
table.Render()
fmt.Printf("\n\n")
}
func (b *Benchmark) Clean() {
b.Results = []*BenchmarkResult{}
}
func (b *Benchmark) sortResults() {
// sort by hit rate
sort.Slice(b.Results, func(i, j int) bool {
return b.Results[i].hitRate() > b.Results[j].hitRate()
})
}