This repository was archived by the owner on Aug 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.go
More file actions
59 lines (50 loc) · 1.37 KB
/
context.go
File metadata and controls
59 lines (50 loc) · 1.37 KB
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
package bench
import (
"bytes"
"fmt"
"time"
"github.com/codahale/hdrhistogram"
)
// Context for each benchmarking run
type Context struct {
ID int
Iteration int64
timers map[string]*hdrhistogram.Histogram
counters map[string]int64
// user values
Values map[string]interface{}
}
func newContext(id int) *Context {
c := Context{ID: id}
c.timers = make(map[string]*hdrhistogram.Histogram)
c.counters = make(map[string]int64)
c.Values = make(map[string]interface{})
return &c
}
// Timer records the time for the specified event
func (c *Context) Timer(name string, t time.Duration) {
if _, ok := c.timers[name]; ok == false {
c.timers[name] = hdrhistogram.New(min, max, precision)
}
c.timers[name].RecordValue(t.Nanoseconds())
}
// Incr increments a counter
func (c *Context) Incr(name string, v int64) {
c.counters[name] += v
}
func (c *Context) String() string {
prefix := " "
var buf bytes.Buffer
percentiles := []float64{50, 99.9, 100}
fmt.Fprintf(&buf, "Task: %d, Total runs: %d\n", c.ID, c.Iteration)
for n, h := range c.timers {
fmt.Fprintf(&buf, "%sTimer: %s \n", prefix, n)
for _, p := range percentiles {
fmt.Fprintf(&buf, "%s%2.1fth percentile: %.2fms\n", prefix, p, float64(h.ValueAtQuantile(p))/1000000.0)
}
}
for n, count := range c.counters {
fmt.Fprintf(&buf, "%sCounter: %s, value: %d \n", prefix, n, count)
}
return buf.String()
}