-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchclock_test.go
More file actions
338 lines (272 loc) · 7.69 KB
/
benchclock_test.go
File metadata and controls
338 lines (272 loc) · 7.69 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package benchclock
import (
"testing"
"time"
)
func TestNew(t *testing.T) {
fn := func() {
time.Sleep(1 * time.Microsecond)
}
b := New("TestBenchmark", fn)
if b.Name != "TestBenchmark" {
t.Errorf("Expected name 'TestBenchmark', got '%s'", b.Name)
}
if b.Fn == nil {
t.Error("Expected non-nil function")
}
if b.Config == nil {
t.Error("Expected default config to be set")
}
}
func TestDefaultConfig(t *testing.T) {
cfg := DefaultConfig()
if cfg.MinIterations <= 0 {
t.Error("MinIterations should be positive")
}
if cfg.MaxIterations <= cfg.MinIterations {
t.Error("MaxIterations should be greater than MinIterations")
}
if cfg.MinDuration <= 0 {
t.Error("MinDuration should be positive")
}
if cfg.WarmupIterations < 0 {
t.Error("WarmupIterations should be non-negative")
}
}
func TestBenchmarkRun(t *testing.T) {
// Simple benchmark that does minimal work
counter := 0
fn := func() {
counter++
time.Sleep(10 * time.Microsecond)
}
cfg := &Config{
MinIterations: 10,
MaxIterations: 100,
MinDuration: 50 * time.Millisecond,
WarmupIterations: 5,
CorrectClockDrift: true,
DetectCPUScaling: true,
}
b := New("TestBenchmark", fn).WithConfig(cfg)
result, err := b.Run()
if err != nil {
t.Fatalf("Benchmark failed: %v", err)
}
// Check that warm-up iterations were performed
if counter < cfg.WarmupIterations {
t.Errorf("Expected at least %d warm-up iterations", cfg.WarmupIterations)
}
// Verify result fields
if result.Name != "TestBenchmark" {
t.Errorf("Expected name 'TestBenchmark', got '%s'", result.Name)
}
if result.Iterations < cfg.MinIterations {
t.Errorf("Expected at least %d iterations, got %d", cfg.MinIterations, result.Iterations)
}
if result.Mean <= 0 {
t.Error("Mean should be positive")
}
if result.Median <= 0 {
t.Error("Median should be positive")
}
if result.StdDev < 0 {
t.Error("StdDev should be non-negative")
}
if result.Variance < 0 {
t.Error("Variance should be non-negative")
}
if result.Min <= 0 {
t.Error("Min should be positive")
}
if result.Max <= 0 {
t.Error("Max should be positive")
}
if result.Min > result.Max {
t.Error("Min should be less than or equal to Max")
}
if result.ConfidenceInterval95.Lower <= 0 {
t.Error("95% CI lower bound should be positive")
}
if result.ConfidenceInterval95.Upper <= 0 {
t.Error("95% CI upper bound should be positive")
}
if result.ConfidenceInterval95.Lower > result.ConfidenceInterval95.Upper {
t.Error("95% CI lower bound should be less than upper bound")
}
if result.ConfidenceInterval99.Lower <= 0 {
t.Error("99% CI lower bound should be positive")
}
if result.ConfidenceInterval99.Upper <= 0 {
t.Error("99% CI upper bound should be positive")
}
if result.ConfidenceInterval99.Lower > result.ConfidenceInterval99.Upper {
t.Error("99% CI lower bound should be less than upper bound")
}
// 99% CI should be wider than 95% CI
if result.ConfidenceInterval99.Upper-result.ConfidenceInterval99.Lower <
result.ConfidenceInterval95.Upper-result.ConfidenceInterval95.Lower {
t.Error("99% CI should be wider than 95% CI")
}
if result.CPUScalingFactor < 1.0 {
t.Error("CPU scaling factor should be at least 1.0")
}
if result.WarmupIterationsDiscarded != cfg.WarmupIterations {
t.Errorf("Expected %d warmup iterations discarded, got %d",
cfg.WarmupIterations, result.WarmupIterationsDiscarded)
}
}
func TestBenchmarkRunNilFunction(t *testing.T) {
b := &Benchmark{
Name: "NilTest",
Fn: nil,
Config: DefaultConfig(),
}
_, err := b.Run()
if err == nil {
t.Error("Expected error for nil function")
}
}
func TestMeasureClockDrift(t *testing.T) {
drift := measureClockDrift()
// Clock drift should be non-negative and reasonable (< 1ms)
if drift < 0 {
t.Error("Clock drift should be non-negative")
}
if drift > 1000000 { // 1ms in nanoseconds
t.Errorf("Clock drift seems unreasonably high: %d ns", drift)
}
}
func TestDetectCPUScaling(t *testing.T) {
scaling := detectCPUScaling()
// CPU scaling factor should be at least 1.0
if scaling < 1.0 {
t.Errorf("CPU scaling factor should be at least 1.0, got %.4f", scaling)
}
// Should be reasonable (not more than 2.0 for normal systems)
if scaling > 3.0 {
t.Logf("Warning: CPU scaling factor is high: %.4f", scaling)
}
}
func TestResultString(t *testing.T) {
result := &Result{
Name: "TestBenchmark",
Iterations: 100,
Mean: 1000 * time.Nanosecond,
Median: 950 * time.Nanosecond,
StdDev: 100 * time.Nanosecond,
Variance: 10000,
Min: 800 * time.Nanosecond,
Max: 1200 * time.Nanosecond,
ClockDriftCorrection: 50,
CPUScalingFactor: 1.05,
WarmupIterationsDiscarded: 10,
}
result.ConfidenceInterval95.Lower = 900 * time.Nanosecond
result.ConfidenceInterval95.Upper = 1100 * time.Nanosecond
result.ConfidenceInterval99.Lower = 850 * time.Nanosecond
result.ConfidenceInterval99.Upper = 1150 * time.Nanosecond
str := result.String()
if str == "" {
t.Error("Result string should not be empty")
}
// Check that the string contains key information
if len(str) < 100 {
t.Error("Result string seems too short")
}
}
func TestRunMultiple(t *testing.T) {
benchmarks := []*Benchmark{
New("Benchmark1", func() { time.Sleep(1 * time.Microsecond) }).WithConfig(&Config{
MinIterations: 10,
MaxIterations: 50,
MinDuration: 20 * time.Millisecond,
WarmupIterations: 2,
CorrectClockDrift: true,
DetectCPUScaling: false,
}),
New("Benchmark2", func() { time.Sleep(2 * time.Microsecond) }).WithConfig(&Config{
MinIterations: 10,
MaxIterations: 50,
MinDuration: 20 * time.Millisecond,
WarmupIterations: 2,
CorrectClockDrift: true,
DetectCPUScaling: false,
}),
}
results, err := RunMultiple(benchmarks)
if err != nil {
t.Fatalf("RunMultiple failed: %v", err)
}
if len(results) != 2 {
t.Errorf("Expected 2 results, got %d", len(results))
}
if results[0].Name != "Benchmark1" {
t.Errorf("Expected first result name 'Benchmark1', got '%s'", results[0].Name)
}
if results[1].Name != "Benchmark2" {
t.Errorf("Expected second result name 'Benchmark2', got '%s'", results[1].Name)
}
}
func TestCompare(t *testing.T) {
r1 := &Result{
Name: "Benchmark1",
Mean: 1000 * time.Nanosecond,
}
r2 := &Result{
Name: "Benchmark2",
Mean: 1200 * time.Nanosecond,
}
comparison := Compare(r1, r2)
if comparison == "" {
t.Error("Comparison string should not be empty")
}
// Should indicate that r2 is slower
if len(comparison) < 10 {
t.Error("Comparison string seems too short")
}
}
func TestWithConfig(t *testing.T) {
fn := func() { time.Sleep(1 * time.Microsecond) }
customCfg := &Config{
MinIterations: 50,
MaxIterations: 500,
MinDuration: 2 * time.Second,
WarmupIterations: 20,
CorrectClockDrift: false,
DetectCPUScaling: false,
}
b := New("TestBenchmark", fn).WithConfig(customCfg)
if b.Config != customCfg {
t.Error("Config was not set correctly")
}
if b.Config.MinIterations != 50 {
t.Errorf("Expected MinIterations 50, got %d", b.Config.MinIterations)
}
}
// Benchmark using standard Go testing
func BenchmarkBenchclock(b *testing.B) {
fn := func() {
// Simple operation
sum := 0
for i := 0; i < 100; i++ {
sum += i
}
}
cfg := &Config{
MinIterations: 50,
MaxIterations: 1000,
MinDuration: 100 * time.Millisecond,
WarmupIterations: 5,
CorrectClockDrift: true,
DetectCPUScaling: true,
}
bench := New("BenchmarkTest", fn).WithConfig(cfg)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := bench.Run()
if err != nil {
b.Fatal(err)
}
}
}