-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstats.go
More file actions
118 lines (89 loc) · 2.28 KB
/
stats.go
File metadata and controls
118 lines (89 loc) · 2.28 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
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
111
112
113
114
115
116
117
118
package main
import (
"code.google.com/p/plotinum/plot"
"code.google.com/p/plotinum/plotter"
"github.com/patrick-higgins/summstat"
"image/color"
"log"
"time"
)
type TimeInfo []time.Duration
type Stats [4]TimeInfo
type StatPacket struct {
statOp int
latency time.Duration
}
var statAdd chan StatPacket
func (s *Stats) StatsManager() {
for {
p := <-statAdd
s[p.statOp] = append(s[p.statOp], p.latency)
}
}
func (s *Stats) calcStats(timeInfo []time.Duration, opType string) {
if len(timeInfo) == 0 {
return
}
var sum int64 = 0
stats := summstat.NewStats()
for _, x := range timeInfo {
sample := summstat.Sample(float64(x.Nanoseconds()) / 1000.0)
stats.AddSample(sample)
sum += x.Nanoseconds()
}
log.Println("**********************************************")
log.Printf("Stats for %s", opType)
log.Printf("Total Ops for %s : %d", opType, stats.Count())
log.Printf("Total time taken : %f seconds", float64(sum)/1000000000)
for _, percentile := range []float64{0.8, 0.9, 0.95, 0.99} {
value := stats.Percentile(percentile)
log.Printf("Ops per second %vth percentile: %v\n", percentile*100, 1000000/value)
}
mean := stats.Mean()
log.Printf("Ops per second Mean: %v\n", 1000000/mean)
}
func (s *Stats) drawPlot(timeInfo []time.Duration, opType string) {
if len(timeInfo) == 0 {
return
}
pts := make(plotter.XYs, len(timeInfo))
for i, x := range timeInfo {
if x.Nanoseconds() > 1 && x.Nanoseconds() < 10000000 {
pts[i].X = float64(i)
pts[i].Y = float64(x.Nanoseconds()) / 1000.0
}
}
// Create a new plot, set its title and
// axis labels.
p, err := plot.New()
if err != nil {
panic(err)
}
p.Title.Text = opType + "Performance"
p.X.Label.Text = "Number of Operations"
p.Y.Label.Text = "Latency in Microseconds"
// Draw a grid behind the data
p.Add(plotter.NewGrid())
// Make a scatter plotter and set its style.
sc, e := plotter.NewScatter(pts)
if e != nil {
panic(e)
}
sc.GlyphStyle.Color = color.RGBA{R: 128, A: 255}
sc.GlyphStyle.Radius = 1
p.Add(sc)
// Save the plot to a PNG file.
if err := p.Save(20, 12, opType+".png"); err != nil {
panic(err)
}
}
func (s *Stats) ReportSummary(drawPlot bool) {
for i, o := range opName {
s.calcStats(s[i], o)
}
if drawPlot {
for i, o := range opName {
s.drawPlot(s[i], o)
}
}
}