Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to limit iterations (b.N) in Go Benchmarking?

Tags:

go

I'm trying to benchmark an operation which requires expensive preparation which I'm excluding using StopTimer() and StartTimer(). Specifically, I'm benchmarking nth-item insertion into a sorted list.

Sample code:

n := 100

// Run the process b.N times
for i := 0; i < b.N; i++ {

    // Stop the timer for our expensive preparation work (inserting n-1 items)
    b.StopTimer()

    // ...

    // Insert n-1 items
    for j := 1; j < n; j++ {
        m.InsertItem(o)
    }

    // ...

    // Resume the timer
    b.StartTimer()

    // Insert the nth item
    m.InsertItem(o)

}

The problem is that Go's benchmarking heuristic is limiting b.N according to the benchmarked time, not the total time. It ends up asking for 5MM (5000000) iterations of 100th order insertion, and this takes more time than is reasonable (I want to benchmark up to the 10 millionth item insertion).

Is there a way to specify a maximum b.N for a specific benchmark in Go's benchmark tool? I did not find anything in the docs myself.

like image 315
gwelter Avatar asked Nov 15 '18 15:11

gwelter


2 Answers

Setting an explicit iteration count (100 times):

go test -bench=. -benchtime=100x

Specify the time (10 seconds):

go test -bench=. -benchtime=10s

To limit iterations (b.N) in Go Benchmarking:
Since Go 1.12:

The -benchtime flag now supports setting an explicit iteration count instead of a time when the value ends with an "x". For example, -benchtime=100x runs the benchmark 100 times.

like image 198
wasmup Avatar answered Oct 04 '22 20:10

wasmup


-benchtime flag can be used to specify the time for which you want to run the benchmark(source). Although I haven't tried it, providing a lower value of time than 1 sec should do the trick.

go test -bench=. -benchtime=10s

like image 32
Saty Anand Avatar answered Oct 04 '22 22:10

Saty Anand