Skip to content

Commit ee6b920

Browse files
committed
feat(example): add bench with setup
1 parent 98e6fd1 commit ee6b920

File tree

3 files changed

+157
-49
lines changed

3 files changed

+157
-49
lines changed

example/helper.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package example
2+
3+
import "time"
4+
5+
//go:noinline
6+
func recursiveFib(n int) uint64 {
7+
if n <= 1 {
8+
return uint64(n)
9+
}
10+
return recursiveFib(n-1) + recursiveFib(n-2)
11+
}
12+
13+
//go:noinline
14+
func expensiveOperation() uint64 {
15+
// Large memory allocation
16+
data := make([]uint64, 1024*1024) // 8 MiB allocation
17+
for i := range data {
18+
data[i] = 42
19+
}
20+
21+
// Expensive recursive computation that will dominate flamegraph
22+
fibResult := recursiveFib(30)
23+
24+
// More expensive work - sum the data
25+
sum := uint64(0)
26+
for _, v := range data {
27+
sum += v
28+
}
29+
30+
return sum + fibResult
31+
}
32+
33+
//go:noinline
34+
func doWork(n int) uint64 {
35+
if n <= 1 {
36+
return uint64(n)
37+
}
38+
return doWork(n-1) + doWork(n-2)
39+
}
40+
41+
func actualWork() uint64 {
42+
time.Sleep(1 * time.Millisecond)
43+
result := doWork(30)
44+
return 42 + result
45+
}

example/setup_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package example
2+
3+
import (
4+
"runtime"
5+
"testing"
6+
)
7+
8+
func BenchmarkLargeSetup(b *testing.B) {
9+
expensiveOperation()
10+
b.ResetTimer()
11+
12+
var result uint64
13+
for i := 0; i < b.N; i++ {
14+
result = actualWork()
15+
}
16+
runtime.KeepAlive(result)
17+
}
18+
19+
func BenchmarkLargeSetupInLoop(b *testing.B) {
20+
var result uint64
21+
for i := 0; i < b.N; i++ {
22+
b.StopTimer()
23+
expensiveOperation()
24+
b.StartTimer()
25+
26+
result = actualWork()
27+
}
28+
runtime.KeepAlive(result)
29+
}

go-runner/src/snapshots/codspeed_go_runner__integration_tests__assert_results_snapshots@example.snap

Lines changed: 83 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ expression: results
7070
"stats": "[stats]"
7171
},
7272
{
73-
"name": "BenchmarkSleep100ns",
74-
"uri": "example/sleep_test.go::BenchmarkSleep100ns",
73+
"name": "BenchmarkLargeSetup",
74+
"uri": "example/setup_test.go::BenchmarkLargeSetup",
7575
"config": {
7676
"warmup_time_ns": null,
7777
"min_round_time_ns": null,
@@ -81,8 +81,31 @@ expression: results
8181
"stats": "[stats]"
8282
},
8383
{
84-
"name": "BenchmarkSleep100ns_Loop",
85-
"uri": "example/sleep_test.go::BenchmarkSleep100ns_Loop",
84+
"name": "BenchmarkLargeSetupInLoop",
85+
"uri": "example/setup_test.go::BenchmarkLargeSetupInLoop",
86+
"config": {
87+
"warmup_time_ns": null,
88+
"min_round_time_ns": null,
89+
"max_time_ns": null,
90+
"max_rounds": null
91+
},
92+
"stats": "[stats]"
93+
}
94+
]
95+
},
96+
{
97+
"creator": {
98+
"name": "codspeed-go",
99+
"version": "[version]",
100+
"pid": "[pid]"
101+
},
102+
"instrument": {
103+
"type": "walltime"
104+
},
105+
"benchmarks": [
106+
{
107+
"name": "BenchmarkQuicktest",
108+
"uri": "example/compat/quicktest_test.go::BenchmarkQuicktest",
86109
"config": {
87110
"warmup_time_ns": null,
88111
"min_round_time_ns": null,
@@ -92,8 +115,8 @@ expression: results
92115
"stats": "[stats]"
93116
},
94117
{
95-
"name": "BenchmarkSleep100us",
96-
"uri": "example/sleep_test.go::BenchmarkSleep100us",
118+
"name": "BenchmarkTestifyWithNew",
119+
"uri": "example/compat/testify_test.go::BenchmarkTestifyWithNew",
97120
"config": {
98121
"warmup_time_ns": null,
99122
"min_round_time_ns": null,
@@ -103,8 +126,8 @@ expression: results
103126
"stats": "[stats]"
104127
},
105128
{
106-
"name": "BenchmarkSleep100us_Loop",
107-
"uri": "example/sleep_test.go::BenchmarkSleep100us_Loop",
129+
"name": "BenchmarkTestifyWithT",
130+
"uri": "example/compat/testify_test.go::BenchmarkTestifyWithT",
108131
"config": {
109132
"warmup_time_ns": null,
110133
"min_round_time_ns": null,
@@ -114,8 +137,31 @@ expression: results
114137
"stats": "[stats]"
115138
},
116139
{
117-
"name": "BenchmarkSleep10ms",
118-
"uri": "example/sleep_test.go::BenchmarkSleep10ms",
140+
"name": "BenchmarkWithSlogAssert",
141+
"uri": "example/compat/slogassert_test.go::BenchmarkWithSlogAssert",
142+
"config": {
143+
"warmup_time_ns": null,
144+
"min_round_time_ns": null,
145+
"max_time_ns": null,
146+
"max_rounds": null
147+
},
148+
"stats": "[stats]"
149+
}
150+
]
151+
},
152+
{
153+
"creator": {
154+
"name": "codspeed-go",
155+
"version": "[version]",
156+
"pid": "[pid]"
157+
},
158+
"instrument": {
159+
"type": "walltime"
160+
},
161+
"benchmarks": [
162+
{
163+
"name": "BenchmarkSleep100ns",
164+
"uri": "example/timing/sleep_test.go::BenchmarkSleep100ns",
119165
"config": {
120166
"warmup_time_ns": null,
121167
"min_round_time_ns": null,
@@ -125,8 +171,8 @@ expression: results
125171
"stats": "[stats]"
126172
},
127173
{
128-
"name": "BenchmarkSleep10ms_Loop",
129-
"uri": "example/sleep_test.go::BenchmarkSleep10ms_Loop",
174+
"name": "BenchmarkSleep100ns_Loop",
175+
"uri": "example/timing/sleep_test.go::BenchmarkSleep100ns_Loop",
130176
"config": {
131177
"warmup_time_ns": null,
132178
"min_round_time_ns": null,
@@ -136,8 +182,8 @@ expression: results
136182
"stats": "[stats]"
137183
},
138184
{
139-
"name": "BenchmarkSleep10us",
140-
"uri": "example/sleep_test.go::BenchmarkSleep10us",
185+
"name": "BenchmarkSleep100us",
186+
"uri": "example/timing/sleep_test.go::BenchmarkSleep100us",
141187
"config": {
142188
"warmup_time_ns": null,
143189
"min_round_time_ns": null,
@@ -147,8 +193,8 @@ expression: results
147193
"stats": "[stats]"
148194
},
149195
{
150-
"name": "BenchmarkSleep10us_Loop",
151-
"uri": "example/sleep_test.go::BenchmarkSleep10us_Loop",
196+
"name": "BenchmarkSleep100us_Loop",
197+
"uri": "example/timing/sleep_test.go::BenchmarkSleep100us_Loop",
152198
"config": {
153199
"warmup_time_ns": null,
154200
"min_round_time_ns": null,
@@ -158,8 +204,8 @@ expression: results
158204
"stats": "[stats]"
159205
},
160206
{
161-
"name": "BenchmarkSleep1ms",
162-
"uri": "example/sleep_test.go::BenchmarkSleep1ms",
207+
"name": "BenchmarkSleep10ms",
208+
"uri": "example/timing/sleep_test.go::BenchmarkSleep10ms",
163209
"config": {
164210
"warmup_time_ns": null,
165211
"min_round_time_ns": null,
@@ -169,8 +215,8 @@ expression: results
169215
"stats": "[stats]"
170216
},
171217
{
172-
"name": "BenchmarkSleep1ms_Loop",
173-
"uri": "example/sleep_test.go::BenchmarkSleep1ms_Loop",
218+
"name": "BenchmarkSleep10ms_Loop",
219+
"uri": "example/timing/sleep_test.go::BenchmarkSleep10ms_Loop",
174220
"config": {
175221
"warmup_time_ns": null,
176222
"min_round_time_ns": null,
@@ -180,8 +226,8 @@ expression: results
180226
"stats": "[stats]"
181227
},
182228
{
183-
"name": "BenchmarkSleep1us",
184-
"uri": "example/sleep_test.go::BenchmarkSleep1us",
229+
"name": "BenchmarkSleep10us",
230+
"uri": "example/timing/sleep_test.go::BenchmarkSleep10us",
185231
"config": {
186232
"warmup_time_ns": null,
187233
"min_round_time_ns": null,
@@ -191,8 +237,8 @@ expression: results
191237
"stats": "[stats]"
192238
},
193239
{
194-
"name": "BenchmarkSleep1us_Loop",
195-
"uri": "example/sleep_test.go::BenchmarkSleep1us_Loop",
240+
"name": "BenchmarkSleep10us_Loop",
241+
"uri": "example/timing/sleep_test.go::BenchmarkSleep10us_Loop",
196242
"config": {
197243
"warmup_time_ns": null,
198244
"min_round_time_ns": null,
@@ -202,8 +248,8 @@ expression: results
202248
"stats": "[stats]"
203249
},
204250
{
205-
"name": "BenchmarkSleep50ms",
206-
"uri": "example/sleep_test.go::BenchmarkSleep50ms",
251+
"name": "BenchmarkSleep1ms",
252+
"uri": "example/timing/sleep_test.go::BenchmarkSleep1ms",
207253
"config": {
208254
"warmup_time_ns": null,
209255
"min_round_time_ns": null,
@@ -213,31 +259,19 @@ expression: results
213259
"stats": "[stats]"
214260
},
215261
{
216-
"name": "BenchmarkSleep50ms_Loop",
217-
"uri": "example/sleep_test.go::BenchmarkSleep50ms_Loop",
262+
"name": "BenchmarkSleep1ms_Loop",
263+
"uri": "example/timing/sleep_test.go::BenchmarkSleep1ms_Loop",
218264
"config": {
219265
"warmup_time_ns": null,
220266
"min_round_time_ns": null,
221267
"max_time_ns": null,
222268
"max_rounds": null
223269
},
224270
"stats": "[stats]"
225-
}
226-
]
227-
},
228-
{
229-
"creator": {
230-
"name": "codspeed-go",
231-
"version": "[version]",
232-
"pid": "[pid]"
233-
},
234-
"instrument": {
235-
"type": "walltime"
236-
},
237-
"benchmarks": [
271+
},
238272
{
239-
"name": "BenchmarkQuicktest",
240-
"uri": "example/compat/quicktest_test.go::BenchmarkQuicktest",
273+
"name": "BenchmarkSleep1us",
274+
"uri": "example/timing/sleep_test.go::BenchmarkSleep1us",
241275
"config": {
242276
"warmup_time_ns": null,
243277
"min_round_time_ns": null,
@@ -247,8 +281,8 @@ expression: results
247281
"stats": "[stats]"
248282
},
249283
{
250-
"name": "BenchmarkTestifyWithNew",
251-
"uri": "example/compat/testify_test.go::BenchmarkTestifyWithNew",
284+
"name": "BenchmarkSleep1us_Loop",
285+
"uri": "example/timing/sleep_test.go::BenchmarkSleep1us_Loop",
252286
"config": {
253287
"warmup_time_ns": null,
254288
"min_round_time_ns": null,
@@ -258,8 +292,8 @@ expression: results
258292
"stats": "[stats]"
259293
},
260294
{
261-
"name": "BenchmarkTestifyWithT",
262-
"uri": "example/compat/testify_test.go::BenchmarkTestifyWithT",
295+
"name": "BenchmarkSleep50ms",
296+
"uri": "example/timing/sleep_test.go::BenchmarkSleep50ms",
263297
"config": {
264298
"warmup_time_ns": null,
265299
"min_round_time_ns": null,
@@ -269,8 +303,8 @@ expression: results
269303
"stats": "[stats]"
270304
},
271305
{
272-
"name": "BenchmarkWithSlogAssert",
273-
"uri": "example/compat/slogassert_test.go::BenchmarkWithSlogAssert",
306+
"name": "BenchmarkSleep50ms_Loop",
307+
"uri": "example/timing/sleep_test.go::BenchmarkSleep50ms_Loop",
274308
"config": {
275309
"warmup_time_ns": null,
276310
"min_round_time_ns": null,

0 commit comments

Comments
 (0)