File tree Expand file tree Collapse file tree 2 files changed +74
-0
lines changed Expand file tree Collapse file tree 2 files changed +74
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments