@@ -66,11 +66,6 @@ var profileFlag = flag.String("profile", "",
66
66
func BenchmarkTPCC (b * testing.B ) {
67
67
defer log .Scope (b ).Close (b )
68
68
69
- // Setup the cluster once for all benchmarks.
70
- ctx := context .Background ()
71
- tc , pgURLs := startCluster (b , ctx )
72
- defer tc .Stopper ().Stop (ctx )
73
-
74
69
for _ , impl := range []struct { name , flag string }{
75
70
{"literal" , "--literal-implementation=true" },
76
71
{"optimized" , "--literal-implementation=false" },
@@ -84,9 +79,37 @@ func BenchmarkTPCC(b *testing.B) {
84
79
{"stock_level" , "--mix=stockLevel=1" },
85
80
{"default" , "--mix=newOrder=10,payment=10,orderStatus=1,delivery=1,stockLevel=1" },
86
81
} {
82
+ var tc serverutils.TestClusterInterface
83
+ var pgURLs [nodes ]string
87
84
b .Run (mix .name , func (b * testing.B ) {
85
+ ctx := context .Background ()
86
+ // TODO(mgartner): This is a hack to avoid repeatedly
87
+ // setting up the cluster for a single benchmark run. Go's
88
+ // benchmarking tooling will run a benchmark with b.N=1
89
+ // first, and ramp up b.N until the benchmark hits a time
90
+ // threshold. This means that the setup code will run
91
+ // multiple times for a single benchmark result. To avoid
92
+ // the high latency this would incur, we only run the setup
93
+ // code when on the first execution of each iteration of the
94
+ // benchmark, when b.N=1. If the benchmark is run with
95
+ // --count greater than 1, then b.N will be reset to 1 for
96
+ // each iteration, and a new cluster will be created,
97
+ // ensuring benchmark results across interations remain
98
+ // independent. This won't be necessary in Go 1.24+ when
99
+ // b.Loop can be used instead.
100
+ if b .N == 1 && tc != nil {
101
+ tc .Stopper ().Stop (ctx )
102
+ tc = nil
103
+ }
104
+ // Setup the cluster.
105
+ if tc == nil {
106
+ tc , pgURLs = startCluster (b , ctx )
107
+ }
88
108
run (b , ctx , pgURLs , []string {impl .flag , mix .flag })
89
109
})
110
+ if tc != nil {
111
+ tc .Stopper ().Stop (context .Background ())
112
+ }
90
113
}
91
114
})
92
115
}
0 commit comments