-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBenchmarking.go
55 lines (47 loc) · 1.12 KB
/
Benchmarking.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
package Misc
import (
"github.com/RENCI/GoUtils/Collections"
"time"
)
type BenchmarkResult struct {
Name string
Iterations Collections.List[time.Duration]
Average time.Duration
Median time.Duration
Min time.Duration
Max time.Duration
}
func Benchmark(name string, iterations int, f func()) *BenchmarkResult {
var br = &BenchmarkResult{
Name: name,
Iterations: Collections.NewList[time.Duration](),
}
for i := 0; i < iterations; i++ {
start := time.Now()
f()
br.Iterations.Add(time.Since(start))
}
return br
}
func (this *BenchmarkResult) Calculate() {
if this.Iterations.Size() == 0 {
return
}
var sum time.Duration
this.Iterations.ForEach(func(v time.Duration) {
sum += v
})
this.Average = sum / time.Duration(this.Iterations.Size())
this.Iterations.Sort(func(item1 time.Duration, item2 time.Duration) int {
if item1 > item2 {
return 1
} else if item1 == item2 {
return 0
} else {
return -1
}
})
this.Median = this.Iterations.Get(this.Iterations.Size() / 2)
this.Min = this.Iterations.Get(0)
this.Max = this.Iterations.Get(this.Iterations.Size() - 1)
}