-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_test.go
86 lines (75 loc) · 2.08 KB
/
bench_test.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
package bandit
import (
"runtime"
"runtime/debug"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func measureMemoryUsageDuringOperation(bm Benchmarker, prefix string, body func()) float64 {
var m1, m2 runtime.MemStats
bm.Time(prefix+"_gc", func() {
runtime.GC()
runtime.GC()
})
runtime.ReadMemStats(&m1)
body()
runtime.ReadMemStats(&m2)
return float64(m2.Alloc-m1.Alloc) / 1024 / 1024
}
func createTestTrie(n, capacity, offset, stride int) *IntervalSet {
r := NewIntervalSetWithCapacity(uint(capacity), Above(0))
for i := 0; i < n; i++ {
s := Above(uint64(i*stride + offset)).AsIntervalSet()
r = r.SymmetricDifference(r, s)
}
return r
}
var _ = Describe("Bench", func() {
var (
n int = 100000
m int = 100000
t int = 5
)
// Disable GC so we get better measurements
debug.SetGCPercent(-1)
Measure("or", func(bm Benchmarker) {
var a, b *IntervalSet
bm.Time("total", func() {
alloc := measureMemoryUsageDuringOperation(bm, "creation", func() {
creation := bm.Time("creation", func() {
a = createTestTrie(n, m, 0, 2)
b = createTestTrie(n, m, 1, 2)
})
Ω(creation.Seconds()).Should(BeNumerically("<", 15))
})
bm.RecordValue("creationAlloc", alloc)
alloc = measureMemoryUsageDuringOperation(bm, "runtime", func() {
runtime := bm.Time("runtime", func() {
a.Union(a, b)
})
Ω(runtime.Seconds()).Should(BeNumerically("<", 2))
})
bm.RecordValue("execAlloc", alloc)
})
}, t)
Measure("and", func(bm Benchmarker) {
var a, b *IntervalSet
bm.Time("total", func() {
alloc := measureMemoryUsageDuringOperation(bm, "creation", func() {
creation := bm.Time("creation", func() {
a = createTestTrie(n, m, 0, 2)
b = createTestTrie(n, m, 1, 2)
})
Ω(creation.Seconds()).Should(BeNumerically("<", 15))
})
bm.RecordValue("creationAlloc", alloc)
alloc = measureMemoryUsageDuringOperation(bm, "runtime", func() {
runtime := bm.Time("runtime", func() {
a.Intersection(a, b)
})
Ω(runtime.Seconds()).Should(BeNumerically("<", 2))
})
bm.RecordValue("execAlloc", alloc)
})
}, t)
})