-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.go
87 lines (67 loc) · 1.76 KB
/
stats.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
package main
import (
"fmt"
"sort"
"time"
)
// Stats encapsulates the stats for a list of time.Duration values
type Stats struct {
Count int
Total ResultTiming
Average float64
Median float64
Minimum ResultTiming
Maximum ResultTiming
}
// ResultTiming represents the time spent executing
// a query and the number of results generated for the query
type ResultTiming struct {
time float64
results int
}
func (rt *ResultTiming) stringify() string {
return fmt.Sprintf("%.2f ms (%d results)", rt.time, rt.results)
}
func (s *Stats) print() {
fmt.Println("")
fmt.Printf("Total queries:%d \n", s.Count)
fmt.Printf("Total time: %s\n", s.Total.stringify())
fmt.Printf("Average time: %.2f ms\n", s.Average)
fmt.Printf("Median Time: %.2f ms\n", s.Median)
fmt.Printf("Minimum time: %s\n", s.Minimum.stringify())
fmt.Printf("Maximum time: %s\n", s.Maximum.stringify())
fmt.Println("")
}
// GetStats takes a list of Result values, creates and returns the Stat
func GetStats(results []Result) *Stats {
stats := &Stats{}
var n int
durationToNS := func(d time.Duration) float64 {
return float64(d.Nanoseconds()) / float64(1000000)
}
resTimes := Map(results, durationToNS)
sort.Slice(resTimes, func(i, j int) bool {
return resTimes[i].time < resTimes[j].time
})
n = len(resTimes)
if n == 0 {
return stats
}
stats.Count = n
stats.Minimum = resTimes[0]
stats.Maximum = resTimes[n-1]
totalTime := 0.0
totalRes := 0
for _, rt := range resTimes {
totalTime += rt.time
totalRes += rt.results
}
stats.Total = ResultTiming{totalTime, totalRes}
stats.Average = totalTime / float64(len(resTimes))
if n%2 == 0 && n > 1 {
stats.Median = (resTimes[n/2].time + resTimes[n/2+1].time) / 2
} else {
stats.Median = resTimes[n/2].time
}
return stats
}