Last updated:2026-10-12
Google Benchmark Performance Testing¶
Start Your Benchmark Testing with C++那些事¶
0. Introduction¶
Goal of this section: complete a small Benchmark project!
In everyday development, how do you test the performance of your own interfaces? How do you quickly set up a Benchmark testing framework in C++?
This section will get you started with Benchmark, step by step.
1. Project Structure¶
We will use vector versus list insertion as the example to explain the Benchmark project.
This project is managed with Bazel. If you don't know how to configure the related environment yet, please refer to the earlier video courses/documentation.
Create a project structure of:
Benchmark uses Google's open source project:
In the past, using a third-party library meant compiling the source yourself, generating an so library, and then linking it — too much hassle! Here, using Bazel management, you only need to add the dependent git project in the WORKSPACE. For example, here we use version v1.7.1 of google benchmark.
git_repository(
name = "google_benchmark",
remote = "https://github.com/google/benchmark.git",
tag = "v1.7.1",
)
After that, we need to implement the main logic. Create a src directory and add a BUILD file inside: the purpose is to compile the cpp files. Note: this depends on the benchmark library, and the usage is @the name configured above//:benchmark
cc_binary(
name = "vector_list",
srcs = ["vector_list.cc"],
deps = [
"@google_benchmark//:benchmark",
]
)
With that, you can use it in the cc file:
2. Demo Time¶
Write two insertion functions — very simple. The framework is shown below, with a few things worth explaining:
- state is the runtime state information of the benchmark
- for example: sleep control, input parameter control, and so on
- state.range(0) gets the 0th parameter
- DoNotOptimize means no compiler optimization
// 测试 std::vector 的插入速度
static void BM_vectorInsert(benchmark::State& state) {
for (auto _ : state) {
std::vector<int> v;
for (size_t i = 0; i < state.range(0); ++i) {
v.insert(v.begin() + i, i);
}
benchmark::DoNotOptimize(v);
}
}
// 测试 std::list 的插入速度
static void BM_listInsert(benchmark::State& state) {
for (auto _ : state) {
std::list<int> l;
for (size_t i = 0; i < state.range(0); ++i) {
l.insert(l.begin(), i);
}
benchmark::DoNotOptimize(l);
}
}
The main entry calls:
- Range(8, 8 << 10) means starting from 8, multiplying by 8 each time, up to 8192. This corresponds to the state.range(0) above — the number is passed in as a parameter
- BENCHMARK_MAIN means using google benchmark's main function. We know every executable has a main; the reason this file can execute is that this macro expands to the main function. Have you learned it?
// 定义基准测试
BENCHMARK(BM_vectorInsert)->Range(8, 8<<10);
BENCHMARK(BM_listInsert)->Range(8, 8<<10);
// 运行基准测试
BENCHMARK_MAIN();
3. Summary¶
This section walked you step by step through google benchmark and applying it to our own project. You can do lots of interesting things with it. The source code for this section has been put into the Knowledge Planet community for everyone to learn from — welcome to join and discuss~
Related Topics¶
- Hand-Writing C++ FnOnce | C++那些事
- A Small C++ gRPC Project | C++那些事
- C++ Inter-Process Communication in Practice | C++那些事
- C++ Short URL Service Demo | C++那些事
- C++17 and SQLite Folder Size Statistics | C++那些事
- C++20 Coroutine HTTP Server | C++那些事
- Protobuf ZeroCopy Serialization | C++那些事
- Accelerating Python with pybind11 | C++那些事
- C++ Stack Calculator Design | C++那些事