@@ -489,8 +489,7 @@ func BenchmarkIntentScan(b *testing.B) {
489489 if err != nil {
490490 b .Fatal (err )
491491 }
492- b .ResetTimer ()
493- for i := 0 ; i < b .N ; i ++ {
492+ for b .Loop () {
494493 valid , err := iter .Valid ()
495494 if err != nil {
496495 b .Fatal (err )
@@ -1144,9 +1143,7 @@ func runMVCCScan(ctx context.Context, b *testing.B, opts benchScanOptions) {
11441143 endKeyBuf := append (make ([]byte , 0 , 1024 ), []byte ("key-" )... )
11451144
11461145 b .SetBytes (int64 (opts .numRows * opts .valueBytes ))
1147- b .ResetTimer ()
1148-
1149- for i := 0 ; i < b .N ; i ++ {
1146+ for b .Loop () {
11501147 // Choose a random key to start scan.
11511148 if opts .numColumnFamilies == 0 {
11521149 keyIdx := rand .Int31n (int32 (opts .numKeys - opts .numRows ))
@@ -1191,8 +1188,6 @@ func runMVCCScan(ctx context.Context, b *testing.B, opts benchScanOptions) {
11911188 b .Fatalf ("failed to scan garbage: found %d keys" , len (res .KVs ))
11921189 }
11931190 }
1194-
1195- b .StopTimer ()
11961191}
11971192
11981193// runMVCCGet first creates test data (and resets the benchmarking
@@ -1218,10 +1213,8 @@ func runMVCCGet(ctx context.Context, b *testing.B, opts mvccBenchData, useBatch
12181213 }
12191214
12201215 b .SetBytes (int64 (opts .valueBytes ))
1221- b .ResetTimer ()
1222-
12231216 keyBuf := append (make ([]byte , 0 , 64 ), []byte ("key-" )... )
1224- for i := 0 ; i < b . N ; i ++ {
1217+ for b . Loop () {
12251218 // Choose a random key to retrieve.
12261219 keyIdx := rand .Int31n (int32 (opts .numKeys ))
12271220 key := roachpb .Key (encoding .EncodeUvarintAscending (keyBuf [:4 ], uint64 (keyIdx )))
@@ -1241,8 +1234,6 @@ func runMVCCGet(ctx context.Context, b *testing.B, opts mvccBenchData, useBatch
12411234 b .Fatalf ("unexpected value size: %d" , len (valueBytes ))
12421235 }
12431236 }
1244-
1245- b .StopTimer ()
12461237}
12471238
12481239func runMVCCBlindPut (ctx context.Context , b * testing.B , emk engineMaker , valueSize int ) {
@@ -1254,9 +1245,7 @@ func runMVCCBlindPut(ctx context.Context, b *testing.B, emk engineMaker, valueSi
12541245 defer eng .Close ()
12551246
12561247 b .SetBytes (int64 (valueSize ))
1257- b .ResetTimer ()
1258-
1259- for i := 0 ; i < b .N ; i ++ {
1248+ for i := 0 ; b .Loop (); i ++ {
12601249 key := roachpb .Key (encoding .EncodeUvarintAscending (keyBuf [:4 ], uint64 (i )))
12611250 ts := hlc.Timestamp {WallTime : timeutil .Now ().UnixNano ()}
12621251 batch := eng .NewWriteBatch ()
@@ -1267,8 +1256,6 @@ func runMVCCBlindPut(ctx context.Context, b *testing.B, emk engineMaker, valueSi
12671256 b .Fatalf ("failed commit: %v" , err )
12681257 }
12691258 }
1270-
1271- b .StopTimer ()
12721259}
12731260
12741261func runMVCCConditionalPut (
@@ -1303,9 +1290,7 @@ func runMVCCConditionalPut(
13031290 expected = value .TagAndDataBytes ()
13041291 }
13051292
1306- b .ResetTimer ()
1307-
1308- for i := 0 ; i < b .N ; i ++ {
1293+ for i := 0 ; b .Loop (); i ++ {
13091294 key := roachpb .Key (encoding .EncodeUvarintAscending (keyBuf [:4 ], uint64 (i )))
13101295 ts := hlc.Timestamp {WallTime : timeutil .Now ().UnixNano ()}
13111296 batch := eng .NewBatch ()
@@ -1324,8 +1309,6 @@ func runMVCCConditionalPut(
13241309 }
13251310 batch .Close ()
13261311 }
1327-
1328- b .StopTimer ()
13291312}
13301313
13311314func runMVCCBlindConditionalPut (ctx context.Context , b * testing.B , emk engineMaker , valueSize int ) {
@@ -1337,9 +1320,7 @@ func runMVCCBlindConditionalPut(ctx context.Context, b *testing.B, emk engineMak
13371320 defer eng .Close ()
13381321
13391322 b .SetBytes (int64 (valueSize ))
1340- b .ResetTimer ()
1341-
1342- for i := 0 ; i < b .N ; i ++ {
1323+ for i := 0 ; b .Loop (); i ++ {
13431324 key := roachpb .Key (encoding .EncodeUvarintAscending (keyBuf [:4 ], uint64 (i )))
13441325 ts := hlc.Timestamp {WallTime : timeutil .Now ().UnixNano ()}
13451326 batch := eng .NewWriteBatch ()
@@ -1360,8 +1341,6 @@ func runMVCCBlindConditionalPut(ctx context.Context, b *testing.B, emk engineMak
13601341 }
13611342 batch .Close ()
13621343 }
1363-
1364- b .StopTimer ()
13651344}
13661345
13671346func runMVCCBatchPut (ctx context.Context , b * testing.B , emk engineMaker , valueSize , batchSize int ) {
@@ -1374,13 +1353,10 @@ func runMVCCBatchPut(ctx context.Context, b *testing.B, emk engineMaker, valueSi
13741353
13751354 b .SetBytes (int64 (valueSize ))
13761355 b .ResetTimer ()
1356+ defer b .StopTimer ()
13771357
13781358 for i := 0 ; i < b .N ; i += batchSize {
1379- end := i + batchSize
1380- if end > b .N {
1381- end = b .N
1382- }
1383-
1359+ end := min (i + batchSize , b .N )
13841360 batch := eng .NewBatch ()
13851361
13861362 for j := i ; j < end ; j ++ {
@@ -1401,8 +1377,6 @@ func runMVCCBatchPut(ctx context.Context, b *testing.B, emk engineMaker, valueSi
14011377
14021378 batch .Close ()
14031379 }
1404-
1405- b .StopTimer ()
14061380}
14071381
14081382// Benchmark batch time series merge operations. This benchmark does not
@@ -1433,10 +1407,8 @@ func runMVCCBatchTimeSeries(ctx context.Context, b *testing.B, emk engineMaker,
14331407 eng := emk (b , fmt .Sprintf ("batch_merge_%d" , batchSize ))
14341408 defer eng .Close ()
14351409
1436- b .ResetTimer ()
1437-
14381410 var ts hlc.Timestamp
1439- for i := 0 ; i < b . N ; i ++ {
1411+ for b . Loop () {
14401412 batch := eng .NewBatch ()
14411413
14421414 for j := 0 ; j < batchSize ; j ++ {
@@ -1445,14 +1417,11 @@ func runMVCCBatchTimeSeries(ctx context.Context, b *testing.B, emk engineMaker,
14451417 b .Fatalf ("failed put: %+v" , err )
14461418 }
14471419 }
1448-
14491420 if err := batch .Commit (false /* sync */ ); err != nil {
14501421 b .Fatal (err )
14511422 }
14521423 batch .Close ()
14531424 }
1454-
1455- b .StopTimer ()
14561425}
14571426
14581427// runMVCCGetMergedValue reads merged values for numKeys separate keys and mergesPerKey
@@ -1492,8 +1461,7 @@ func runMVCCGetMergedValue(
14921461 }
14931462 }
14941463
1495- b .ResetTimer ()
1496- for i := 0 ; i < b .N ; i ++ {
1464+ for b .Loop () {
14971465 _ , err := MVCCGet (ctx , eng , keys [rand .Intn (numKeys )], timestamp , MVCCGetOptions {
14981466 // NB: BatchEvalReadCategory is considered latency sensitive and
14991467 // exempted from open-iterator tracking.
@@ -1503,7 +1471,6 @@ func runMVCCGetMergedValue(
15031471 b .Fatal (err )
15041472 }
15051473 }
1506- b .StopTimer ()
15071474}
15081475
15091476func runMVCCDeleteRange (ctx context.Context , b * testing.B , valueBytes int ) {
@@ -1718,9 +1685,7 @@ func runClearRange(
17181685 defer eng .Close ()
17191686
17201687 b .SetBytes (rangeBytes )
1721- b .ResetTimer ()
1722-
1723- for i := 0 ; i < b .N ; i ++ {
1688+ for b .Loop () {
17241689 batch := eng .NewUnindexedBatch ()
17251690 if err := clearRange (eng , batch , MVCCKey {Key : keys .LocalMax }, MVCCKeyMax ); err != nil {
17261691 b .Fatal (err )
@@ -1731,8 +1696,6 @@ func runClearRange(
17311696 // to take an exceptionally long time since ClearRange is very fast.
17321697 batch .Close ()
17331698 }
1734-
1735- b .StopTimer ()
17361699}
17371700
17381701// runMVCCComputeStats benchmarks computing MVCC stats on a 64MB range of data.
@@ -1748,18 +1711,14 @@ func runMVCCComputeStats(ctx context.Context, b *testing.B, valueBytes int, numR
17481711 defer eng .Close ()
17491712
17501713 b .SetBytes (rangeBytes )
1751- b .ResetTimer ()
1752-
17531714 var stats enginepb.MVCCStats
17541715 var err error
1755- for i := 0 ; i < b . N ; i ++ {
1716+ for b . Loop () {
17561717 stats , err = ComputeStats (ctx , eng , fs .BatchEvalReadCategory , keys .LocalMax , keys .MaxKey , 0 )
17571718 if err != nil {
17581719 b .Fatal (err )
17591720 }
17601721 }
1761-
1762- b .StopTimer ()
17631722 log .Dev .Infof (ctx , "live_bytes: %d" , stats .LiveBytes )
17641723}
17651724
@@ -1776,18 +1735,14 @@ func runMVCCFindSplitKey(ctx context.Context, b *testing.B, valueBytes int) {
17761735 defer eng .Close ()
17771736
17781737 b .SetBytes (rangeBytes )
1779- b .ResetTimer ()
1780-
17811738 var err error
1782- for i := 0 ; i < b . N ; i ++ {
1739+ for b . Loop () {
17831740 _ , err = MVCCFindSplitKey (ctx , eng , roachpb .RKeyMin ,
17841741 roachpb .RKeyMax , rangeBytes / 2 )
17851742 if err != nil {
17861743 b .Fatal (err )
17871744 }
17881745 }
1789-
1790- b .StopTimer ()
17911746}
17921747
17931748type benchGarbageCollectOptions struct {
@@ -1907,8 +1862,7 @@ func runMVCCGarbageCollect(
19071862 if opts .updateStats {
19081863 ms = & enginepb.MVCCStats {}
19091864 }
1910- b .ResetTimer ()
1911- for i := 0 ; i < b .N ; i ++ {
1865+ for b .Loop () {
19121866 batch := eng .NewBatch ()
19131867 if err := MVCCGarbageCollect (ctx , batch , ms , gcKeys , now ); err != nil {
19141868 b .Fatal (err )
@@ -1957,9 +1911,7 @@ func runBatchApplyBatchRepr(
19571911 }
19581912
19591913 b .SetBytes (int64 (len (repr )))
1960- b .ResetTimer ()
1961-
1962- for i := 0 ; i < b .N ; i ++ {
1914+ for b .Loop () {
19631915 var batch WriteBatch
19641916 if ! indexed {
19651917 batch = eng .NewWriteBatch ()
@@ -1971,8 +1923,6 @@ func runBatchApplyBatchRepr(
19711923 }
19721924 batch .Close ()
19731925 }
1974-
1975- b .StopTimer ()
19761926}
19771927
19781928func runMVCCCheckForAcquireLock (
@@ -2059,9 +2009,7 @@ func runMVCCAcquireLockCommon(
20592009 }
20602010 ms := & enginepb.MVCCStats {}
20612011
2062- b .ResetTimer ()
2063-
2064- for i := 0 ; i < b .N ; i ++ {
2012+ for i := 0 ; b .Loop (); i ++ {
20652013 key := makeKey (i )
20662014 txn := & txn1
20672015 var err error
@@ -2078,8 +2026,6 @@ func runMVCCAcquireLockCommon(
20782026 b .Fatal (err )
20792027 }
20802028 }
2081-
2082- b .StopTimer ()
20832029}
20842030
20852031type mvccExportToSSTOpts struct {
@@ -2335,8 +2281,7 @@ func runCheckSSTConflicts(
23352281 }
23362282 sstWriter .Close ()
23372283
2338- b .ResetTimer ()
2339- for i := 0 ; i < b .N ; i ++ {
2284+ for b .Loop () {
23402285 _ , err := CheckSSTConflicts (context .Background (), sstFile .Data (), eng , sstStart , sstEnd , sstStart .Key , sstEnd .Key .Next (), hlc.Timestamp {}, hlc.Timestamp {}, math .MaxInt64 , 0 , usePrefixSeek )
23412286 if err != nil {
23422287 b .Fatal (err )
@@ -2360,8 +2305,7 @@ func runSSTIterator(b *testing.B, numKeys int, verify bool) {
23602305 }
23612306 sstWriter .Close ()
23622307
2363- b .ResetTimer ()
2364- for i := 0 ; i < b .N ; i ++ {
2308+ for b .Loop () {
23652309 iter , err := NewMemSSTIterator (sstFile .Bytes (), verify , IterOptions {
23662310 KeyTypes : IterKeyTypePointsAndRanges ,
23672311 LowerBound : keys .MinKey ,
@@ -2539,7 +2483,8 @@ func BenchmarkMVCCScannerWithIntentsAndVersions(b *testing.B) {
25392483 batch .Close ()
25402484 require .NoError (b , eng .IngestLocalFiles (ctx , []string {sstFileName }))
25412485 }
2542- for i := 0 ; i < b .N ; i ++ {
2486+ var printed bool
2487+ for b .Loop () {
25432488 ro := eng .NewReader (StandardDurability )
25442489 ts := hlc.Timestamp {WallTime : int64 (numVersions ) + 5 }
25452490 startKey := makeKey (nil , 0 )
@@ -2566,10 +2511,11 @@ func BenchmarkMVCCScannerWithIntentsAndVersions(b *testing.B) {
25662511 if res .NumKeys != totalNumKeys {
25672512 b .Fatalf ("expected %d keys, and found %d" , totalNumKeys , res .NumKeys )
25682513 }
2569- if i == 0 {
2514+ if ! printed {
25702515 // This is to understand the results.
25712516 stats := iter .Stats ()
25722517 fmt .Printf ("stats: %s\n " , stats .Stats .String ())
2518+ printed = true
25732519 }
25742520 iter .Close ()
25752521 ro .Close ()
@@ -2642,7 +2588,7 @@ func BenchmarkMVCCPutDelete(b *testing.B) {
26422588 value := roachpb .MakeValueFromBytes (randutil .RandBytes (r , 10 ))
26432589 var blockNum int64
26442590
2645- for i := 0 ; i < b . N ; i ++ {
2591+ for b . Loop () {
26462592 blockID := r .Int63 ()
26472593 blockNum ++
26482594 key := encoding .EncodeVarintAscending (nil , blockID )
@@ -2957,11 +2903,7 @@ func BenchmarkBatchBuilderPut(b *testing.B) {
29572903
29582904 const batchSize = 1000
29592905 for i := 0 ; i < b .N ; i += batchSize {
2960- end := i + batchSize
2961- if end > b .N {
2962- end = b .N
2963- }
2964-
2906+ end := min (b .N , i + batchSize )
29652907 for j := i ; j < end ; j ++ {
29662908 key := roachpb .Key (encoding .EncodeUvarintAscending (keyBuf [:4 ], uint64 (j )))
29672909 ts := hlc.Timestamp {WallTime : int64 (j + 1 )} // j+1 to avoid zero timestamp
0 commit comments