如何使用Bencher跟踪文件大小


Bencher supports the most popular benchmarking harnesses out-of-the-box. Sometimes though, you want to measure the size of your deliverables themselves, such as the binary size of an executable. Lucky for you, Bencher also supports tracking file size. The easiest way to track file size is to use the bencher run CLI subcommand with the --file-size option. The --file-size option expects a file path to the file who’s size will be measured. 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 at the path ./path/to/my_binary, then you could track the size of this binary file with bencher run and the json adapter. This would work both with a benchmark command and without one.

With a benchmark command:

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

Without a benchmark command:

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

In either case, the generated BMF JSON would look like this, if my_binary had a size of 42 bytes:

{
"my_binary": {
"file-size": {
"value": 42.0
}
}
}

In this example, the key my_binary is the binary file name. It is used as the name of the Benchmark. The my_binary object contains the file-size key. file-size is the slug for the built-in File Size Measure. The File Size Measure is not created by default for all Projects. However, when you use the File Size Measure, it will be automatically created for your Project. The File Size Measure object contains a Metric with the file size value in bytes, 42.0.

The File Size will always be a whole byte increment. That is, it will never have a decimal like 42.5. However, BMF JSON needs to support a wide range of possible values, so it uses floats instead of integers.

多文件大小

如果你想跟踪多个文件的大小,你只需在同一个 bencher run 命令中多次使用 --file-size 选项。 例如,你可以跟踪 my_binary 和一个 Windows 可执行文件版本 my_binary.exe

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

如果 my_binary.exe 的大小是 48 字节,生成的 BMF JSON 会是这样的:

{
"my_binary": {
"file-size": {
"value": 42.0
}
},
"my_binary.exe": {
"file-size": {
"value": 48.0
}
}
}

跟踪构建时间

除了跟踪文件大小之外,您可能还想要跟踪交付物的构建时间bencher run 支持同时跟踪文件大小和构建时间。

您可以使用以下命令跟踪 my_binary 的二进制大小和编译时间:

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

如果您的构建脚本花了 87.0 秒完成, 生成的 BMF JSON 将如下所示:

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

🐰 恭喜!您已经学会了如何跟踪您的文件大小!🎉


继续学习:如何在CI中跟踪基准 ➡

🤖 该文档由 OpenAI GPT-4 自动生成。 它可能不准确并且可能包含错误。 如果您发现任何错误,请在 GitHub 上提出问题.


Published: Mon, May 13, 2024 at 7:14:00 AM UTC | Last Updated: Sat, November 9, 2024 at 7:17:00 AM UTC