-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchclock.go
More file actions
349 lines (291 loc) · 8.14 KB
/
benchclock.go
File metadata and controls
349 lines (291 loc) · 8.14 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
339
340
341
342
343
344
345
346
347
348
349
// Package benchclock provides a precision benchmarking utility that corrects for
// clock drift, CPU scaling, and warm-up bias, providing statistically meaningful
// measurements with confidence intervals and variance metrics.
package benchclock
import (
"fmt"
"math"
"sort"
"time"
)
// Result holds the statistical results of a benchmark run.
type Result struct {
// Name of the benchmark
Name string
// Number of iterations performed
Iterations int
// Mean duration per operation
Mean time.Duration
// Median duration per operation
Median time.Duration
// Standard deviation
StdDev time.Duration
// Variance
Variance float64
// 95% Confidence Interval
ConfidenceInterval95 struct {
Lower time.Duration
Upper time.Duration
}
// 99% Confidence Interval
ConfidenceInterval99 struct {
Lower time.Duration
Upper time.Duration
}
// Minimum duration observed
Min time.Duration
// Maximum duration observed
Max time.Duration
// Clock drift correction applied (nanoseconds)
ClockDriftCorrection int64
// CPU scaling factor detected
CPUScalingFactor float64
// Number of warm-up iterations discarded
WarmupIterationsDiscarded int
}
// Config holds configuration for benchmark execution.
type Config struct {
// Minimum number of iterations to run
MinIterations int
// Maximum number of iterations to run
MaxIterations int
// Minimum duration to run the benchmark
MinDuration time.Duration
// Number of warm-up iterations to discard
WarmupIterations int
// Enable clock drift correction
CorrectClockDrift bool
// Enable CPU scaling detection
DetectCPUScaling bool
}
// DefaultConfig returns a default configuration suitable for most benchmarks.
func DefaultConfig() *Config {
return &Config{
MinIterations: 100,
MaxIterations: 100000,
MinDuration: time.Second,
WarmupIterations: 10,
CorrectClockDrift: true,
DetectCPUScaling: true,
}
}
// Benchmark represents a benchmark to be executed.
type Benchmark struct {
Name string
Fn func()
Config *Config
}
// New creates a new Benchmark with the given name and function.
func New(name string, fn func()) *Benchmark {
return &Benchmark{
Name: name,
Fn: fn,
Config: DefaultConfig(),
}
}
// WithConfig sets a custom configuration for the benchmark.
func (b *Benchmark) WithConfig(cfg *Config) *Benchmark {
b.Config = cfg
return b
}
// Run executes the benchmark and returns the statistical results.
func (b *Benchmark) Run() (*Result, error) {
if b.Fn == nil {
return nil, fmt.Errorf("benchmark function cannot be nil")
}
cfg := b.Config
if cfg == nil {
cfg = DefaultConfig()
}
// Perform warm-up runs to eliminate warm-up bias
for i := 0; i < cfg.WarmupIterations; i++ {
b.Fn()
}
// Detect clock drift
var clockDrift int64
if cfg.CorrectClockDrift {
clockDrift = measureClockDrift()
}
// Detect CPU scaling
var cpuScaling float64 = 1.0
if cfg.DetectCPUScaling {
cpuScaling = detectCPUScaling()
}
// Collect timing measurements
durations := make([]time.Duration, 0, cfg.MaxIterations)
startTime := time.Now()
iterations := 0
for iterations < cfg.MinIterations || (time.Since(startTime) < cfg.MinDuration && iterations < cfg.MaxIterations) {
start := time.Now()
b.Fn()
elapsed := time.Since(start)
// Apply clock drift correction
corrected := elapsed - time.Duration(clockDrift)
// Ensure we don't have negative durations
if corrected < 0 {
corrected = elapsed
}
durations = append(durations, corrected)
iterations++
}
// Calculate statistics
result := &Result{
Name: b.Name,
Iterations: iterations,
ClockDriftCorrection: clockDrift,
CPUScalingFactor: cpuScaling,
WarmupIterationsDiscarded: cfg.WarmupIterations,
}
// Sort durations for percentile calculations
sorted := make([]time.Duration, len(durations))
copy(sorted, durations)
sort.Slice(sorted, func(i, j int) bool {
return sorted[i] < sorted[j]
})
// Calculate mean
var sum int64
for _, d := range durations {
sum += int64(d)
}
result.Mean = time.Duration(sum / int64(len(durations)))
// Calculate median
if len(sorted)%2 == 0 {
result.Median = (sorted[len(sorted)/2-1] + sorted[len(sorted)/2]) / 2
} else {
result.Median = sorted[len(sorted)/2]
}
// Calculate variance and standard deviation
var varianceSum float64
for _, d := range durations {
diff := float64(d - result.Mean)
varianceSum += diff * diff
}
result.Variance = varianceSum / float64(len(durations))
result.StdDev = time.Duration(math.Sqrt(result.Variance))
// Calculate confidence intervals
// Using t-distribution critical values for 95% and 99% confidence
// For large samples (n > 30), we can use z-scores
n := float64(len(durations))
stdErr := float64(result.StdDev) / math.Sqrt(n)
// 95% confidence interval (z = 1.96)
margin95 := 1.96 * stdErr
lower95 := result.Mean - time.Duration(margin95)
if lower95 < 0 {
lower95 = 0
}
result.ConfidenceInterval95.Lower = lower95
result.ConfidenceInterval95.Upper = result.Mean + time.Duration(margin95)
// 99% confidence interval (z = 2.576)
margin99 := 2.576 * stdErr
lower99 := result.Mean - time.Duration(margin99)
if lower99 < 0 {
lower99 = 0
}
result.ConfidenceInterval99.Lower = lower99
result.ConfidenceInterval99.Upper = result.Mean + time.Duration(margin99)
// Min and Max
result.Min = sorted[0]
result.Max = sorted[len(sorted)-1]
return result, nil
}
// measureClockDrift detects clock drift by measuring the overhead of time measurements.
func measureClockDrift() int64 {
// Measure the overhead of calling time.Now() repeatedly
samples := 100
var totalOverhead int64
for i := 0; i < samples; i++ {
start := time.Now()
_ = time.Now()
overhead := time.Since(start)
totalOverhead += int64(overhead)
}
// Return average overhead (clock drift)
return totalOverhead / int64(samples)
}
// detectCPUScaling attempts to detect CPU frequency scaling by running
// a simple computational benchmark.
func detectCPUScaling() float64 {
// Run a simple computation benchmark multiple times
// If CPU scaling is active, we'll see variance in execution time
samples := 10
durations := make([]float64, samples)
for i := 0; i < samples; i++ {
start := time.Now()
// Simple computational task
sum := 0
for j := 0; j < 100000; j++ {
sum += j
}
durations[i] = float64(time.Since(start))
}
// Calculate coefficient of variation (CV = stddev / mean)
var sum, mean float64
for _, d := range durations {
sum += d
}
mean = sum / float64(len(durations))
var varianceSum float64
for _, d := range durations {
diff := d - mean
varianceSum += diff * diff
}
variance := varianceSum / float64(len(durations))
stddev := math.Sqrt(variance)
cv := stddev / mean
// CPU scaling factor: 1.0 means stable, higher means more scaling
return 1.0 + cv
}
// String returns a formatted string representation of the benchmark results.
func (r *Result) String() string {
return fmt.Sprintf(`Benchmark: %s
Iterations: %d
Mean: %v
Median: %v
StdDev: %v
Variance: %.2f ns²
Min: %v
Max: %v
95%% Confidence Interval: [%v, %v]
99%% Confidence Interval: [%v, %v]
Clock Drift Correction: %v ns
CPU Scaling Factor: %.4f
Warmup Iterations Discarded: %d`,
r.Name,
r.Iterations,
r.Mean,
r.Median,
r.StdDev,
r.Variance,
r.Min,
r.Max,
r.ConfidenceInterval95.Lower,
r.ConfidenceInterval95.Upper,
r.ConfidenceInterval99.Lower,
r.ConfidenceInterval99.Upper,
r.ClockDriftCorrection,
r.CPUScalingFactor,
r.WarmupIterationsDiscarded,
)
}
// RunMultiple runs multiple benchmarks and returns their results.
func RunMultiple(benchmarks []*Benchmark) ([]*Result, error) {
results := make([]*Result, 0, len(benchmarks))
for _, b := range benchmarks {
result, err := b.Run()
if err != nil {
return nil, fmt.Errorf("benchmark %s failed: %w", b.Name, err)
}
results = append(results, result)
}
return results, nil
}
// Compare compares two benchmark results and returns a comparison string.
func Compare(r1, r2 *Result) string {
diff := float64(r2.Mean-r1.Mean) / float64(r1.Mean) * 100
direction := "slower"
if diff < 0 {
direction = "faster"
diff = -diff
}
return fmt.Sprintf("%s vs %s: %.2f%% %s", r2.Name, r1.Name, diff, direction)
}