-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathany-count_test.go
100 lines (83 loc) · 2.17 KB
/
any-count_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
© 2024–present Harald Rudell <harald.rudell@gmail.com> https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"sync/atomic"
"testing"
"unsafe"
)
func TestAnyCount(t *testing.T) {
//t.Error("Logging on")
const (
expOne = 1
expThree = 3
)
var (
values = []int{2, 3}
total int
)
// methods: Count() Init() Cond()
var a = AnyCount[int]{Value: 1, HasValue: true}
// Sizeof AnyCount[int]: 40
t.Logf("Sizeof AnyCount[int]: %d", unsafe.Sizeof(a))
// Count() 1
if tot := a.Count(); tot != expOne {
t.Errorf("Count %d exp %d", tot, expOne)
}
// iteration single value
total = 0
for i, v := a.Init(); a.Cond(&i, &v); {
total++
}
if total != expOne {
t.Errorf("Count %d exp %d", total, expOne)
}
// Count multiple values
a.Values = values
if tot := a.Count(); tot != expThree {
t.Errorf("FAIL Count %d exp %d", tot, expThree)
}
// iteration multiple values
total = 0
for i, v := a.Init(); a.Cond(&i, &v); {
total++
}
if total != expThree {
t.Errorf("FAIL Count %d exp %d", total, expThree)
}
}
// memory benchmark:
// - appears in pprof as: github.com/haraldrudell/parl.BenchmarkAnyCount
// - — there are no allocations. None. Zilch. Nada.
//
// F=tmp/anycount-$(hostname -s)-heap-$(date "+%FT%T%z").prof && go test -benchmem -memprofile=$F -memprofilerate=1 -run=^$ -bench ^BenchmarkAnyCount$ github.com/haraldrudell/parl && echo $F
func BenchmarkAnyCount(b *testing.B) {
var (
values = []int{2, 3}
)
// because AnyCount is allocation-free,
// do an allocation here so that it appears in the heap profile
// - an atomic must be on the heap, ie. be allocated
// - the atomic must be used to not be optimized away
// - atomic.Bool causes 96 bytes allocation
var a atomic.Bool
a.Load()
// here are some silly operations proving that no allocation occur
for i := 0; i < b.N; i++ {
// methods: Count() Init() Cond()
var a = AnyCount[int]{Value: 1, HasValue: true}
// Count() 1
a.Count()
// iteration single value
for i, v := a.Init(); a.Cond(&i, &v); {
}
// Count multiple values
a.Values = values
a.Count()
// iteration multiple values
for i, v := a.Init(); a.Cond(&i, &v); {
}
}
}