-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_bench_test.go
More file actions
170 lines (142 loc) · 3.27 KB
/
Copy patharray_bench_test.go
File metadata and controls
170 lines (142 loc) · 3.27 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// SPDX-License-Identifier: EUPL-1.2
// Benchmarks for the Array[T comparable] primitive in array.go.
// Per AX-11 — Array sits on every typed-collection path consumers reach
// for: agent lists, vocab sets, registered protocols, route tables.
// Each public method gets a perf contract here.
//
// Run: go test -bench='BenchmarkArray' -benchmem -run='^$' .
package core_test
import (
. "dappco.re/go"
)
var benchArrayStrings = []string{
"apple", "banana", "cherry", "date", "elderberry",
"fig", "grape", "honeydew",
}
// --- Constructors ---
func BenchmarkNewArray_Empty(b *B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = NewArray[string]()
}
}
func BenchmarkNewArray_WithItems(b *B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = NewArray(benchArrayStrings...)
}
}
// --- Add / AddUnique ---
func BenchmarkArray_Add(b *B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
a := NewArray[string]()
a.Add(benchArrayStrings...)
}
}
func BenchmarkArray_AddUnique_AllNew(b *B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
a := NewArray[string]()
a.AddUnique(benchArrayStrings...)
}
}
func BenchmarkArray_AddUnique_AllDup(b *B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
a := NewArray[string]("apple")
a.AddUnique("apple", "apple", "apple", "apple")
}
}
// --- Lookup ---
func BenchmarkArray_Contains_Hit(b *B) {
a := NewArray(benchArrayStrings...)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = a.Contains("elderberry")
}
}
func BenchmarkArray_Contains_Miss(b *B) {
a := NewArray(benchArrayStrings...)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = a.Contains("nope")
}
}
func BenchmarkArray_IndexOf_Hit(b *B) {
a := NewArray(benchArrayStrings...)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = a.IndexOf("elderberry")
}
}
func BenchmarkArray_IndexOf_Miss(b *B) {
a := NewArray(benchArrayStrings...)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = a.IndexOf("nope")
}
}
// --- Filter / Each ---
func BenchmarkArray_Filter_HalfHit(b *B) {
a := NewArray(benchArrayStrings...)
pred := func(s string) bool { return len(s) >= 5 }
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = a.Filter(pred)
}
}
func BenchmarkArray_Each(b *B) {
a := NewArray(benchArrayStrings...)
sink := 0
b.ReportAllocs()
for i := 0; i < b.N; i++ {
a.Each(func(s string) { sink += len(s) })
}
_ = sink
}
// --- Remove ---
func BenchmarkArray_Remove_Found(b *B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
a := NewArray(benchArrayStrings...)
a.Remove("cherry")
}
}
// --- Deduplicate ---
func BenchmarkArray_Deduplicate_NoDups(b *B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
a := NewArray(benchArrayStrings...)
a.Deduplicate()
}
}
func BenchmarkArray_Deduplicate_AllDups(b *B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
a := NewArray("a", "a", "a", "a", "a", "a", "a", "a")
a.Deduplicate()
}
}
// --- Size / Clear / AsSlice ---
func BenchmarkArray_Len(b *B) {
a := NewArray(benchArrayStrings...)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = a.Len()
}
}
func BenchmarkArray_AsSlice(b *B) {
a := NewArray(benchArrayStrings...)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = a.AsSlice()
}
}
func BenchmarkArray_Clear(b *B) {
a := NewArray(benchArrayStrings...)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
a.Clear()
}
}