-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreport.go
84 lines (72 loc) · 1.49 KB
/
report.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
package traces
import (
"context"
"fmt"
"io"
"github.com/Comcast/goburrow-cache"
)
type Reporter interface {
Report(cache.Stats, options)
}
type Provider interface {
Provide(ctx context.Context, keys chan<- interface{})
}
type reporter struct {
w io.Writer
headerPrinted bool
}
func NewReporter(w io.Writer) Reporter {
return &reporter{w: w}
}
func (r *reporter) Report(st cache.Stats, opt options) {
if !r.headerPrinted {
fmt.Fprintf(r.w, "Requets,Hits,HitRate,Evictions,CacheSize\n")
r.headerPrinted = true
}
fmt.Fprintf(r.w, "%d,%d,%.04f,%d,%d\n",
st.RequestCount(), st.HitCount, st.HitRate(), st.EvictionCount,
opt.cacheSize)
}
type options struct {
policy string
cacheSize int
reportInterval int
maxItems int
}
var policies = []string{
"lru",
"slru",
"tinylfu",
}
func benchmarkCache(p Provider, r Reporter, opt options) {
c := cache.New(cache.WithMaximumSize(opt.cacheSize), cache.WithPolicy(opt.policy))
defer c.Close()
keys := make(chan interface{}, 100)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go p.Provide(ctx, keys)
stats := cache.Stats{}
i := 0
for {
if opt.maxItems > 0 && i >= opt.maxItems {
break
}
k, ok := <-keys
if !ok {
break
}
_, ok = c.GetIfPresent(k)
if !ok {
c.Put(k, k)
}
i++
if opt.reportInterval > 0 && i%opt.reportInterval == 0 {
c.Stats(&stats)
r.Report(stats, opt)
}
}
if opt.reportInterval == 0 {
c.Stats(&stats)
r.Report(stats, opt)
}
}