How to Track Build Time with Bencher


Bencher supports the most popular benchmarking harnesses out-of-the-box. Sometimes though, you want to measure how long it takes to build your deliverables themselves, such as the compile time of an executable. Lucky for you, Bencher also supports tracking build times. The easiest way to track build times is to use the bencher run CLI subcommand with the --build-time flag. Under the hood, bencher run outputs the results as Bencher Metric Format (BMF) JSON. It is therefore good practice to explicitly use the json adapter.

If you had a script located at ./my_build_script.sh that built your binary, then you could track the build time of this binary with bencher run and the json adapter. The --build-time flag requires a benchmark command.

Terminal window
bencher run --build-time --adapter json ./my_build_script.sh

The generated BMF JSON would look like this, if your build script took 87.0 seconds to complete:

{
"/bin/sh -c ./my_build_script.sh": {
"build-time": {
"value": 87.0
}
}
}

Note that the Benchmark name is /bin/sh -c ./my_build_script.sh. This is because our command is only a single argument, so bencher run defaults to the shell form for execution. Alternatively, if you would like to force the use of the exec form, you can either provide multiple arguments or use the --exec flag.

Terminal window
bencher run --build-time --adapter json --exec ./my_build_script.sh

If this exec form build took 86.98 seconds, then the generated BMF JSON would look like this:

{
"./my_build_script.sh": {
"build-time": {
"value": 86.98
}
}
}

Now, the Benchmark name is simply ./my_build_script.sh. The ./my_build_script.sh object contains the build-time key. build-time is the slug for the built-in Build Time Measure. The Build Time Measure is not created by default for all Projects. However, when you use the Build Time Measure, it will be automatically created for your Project. The Build Time Measure object contains a Metric with the build time value in seconds, 87.0 and 86.98 in our examples.

The Build Time will always be rounded to the nearest two decimal places. That is, it will never be a long decimal like 42.666666.

Track File Size

You may want to track the file size of your deliverables, in addition to tracking how long it takes to build them. bencher run supports tracking both build times and file sizes.

If the output of your ./my_build_script.sh build script was a binary at the path ./path/to/my_binary, then you could track both the compile time and the binary size with this command:

Terminal window
bencher run --build-time --file-size ./path/to/my_binary --adapter json ./my_build_script.sh

If my_binary had a size of 42 bytes, the generated BMF JSON would look like this:

{
"/bin/sh -c ./my_build_script.sh": {
"build-time": {
"value": 87.0
}
},
"my_binary": {
"file-size": {
"value": 42.0
}
}
}

🐰 Congrats! You have learned how to track your build time! 🎉


Keep Going: How to Track Benchmarks in CI ➡



Published: Sat, November 9, 2024 at 10:00:00 AM UTC