google/benchmark

GitHub: google/benchmark

Google开源的C++微基准测试库,用于精确测量和分析代码片段的执行性能。

Stars: 10061 | Forks: 1751

# 基准测试 [![build-and-test](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/1f3c93fa41184710.svg)](https://github.com/google/benchmark/actions?query=workflow%3Abuild-and-test) [![bazel](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/952d3c928f184711.svg)](https://github.com/google/benchmark/actions/workflows/bazel.yml) [![test-bindings](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/cfc934cb63184712.svg)](https://github.com/google/benchmark/actions?query=workflow%3Atest-bindings) [![Coverage Status](https://coveralls.io/repos/google/benchmark/badge.svg)](https://coveralls.io/r/google/benchmark) [![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/google/benchmark/badge)](https://securityscorecards.dev/viewer/?uri=github.com/google/benchmark) [![Discord](https://discordapp.com/api/guilds/1125694995928719494/widget.png?style=shield)](https://discord.gg/cz7UX7wKC2) 一个用于对代码片段进行基准测试的库,类似于单元测试。示例: ``` #include #include static void BM_SomeFunction(benchmark::State& state) { // Perform setup here for (auto _ : state) { // This code gets timed SomeFunction(); } } // Register the function as a benchmark BENCHMARK(BM_SomeFunction); // Run the benchmark BENCHMARK_MAIN(); ``` ## 快速开始 要开始使用,请参阅 [Requirements](#requirements) 和 [Installation](#installation)。有关完整示例,请参阅 [Usage](#usage); 如需更全面的功能概览,请参阅 [User Guide](docs/user_guide.md)。 阅读 [Google Test documentation](https://github.com/google/googletest/blob/main/docs/primer.md) 也可能会有所帮助, 因为 API 的某些结构方面是相似的。 ## 资源 [Discussion group](https://groups.google.com/d/forum/benchmark-discuss) IRC 频道: * [libera](https://libera.chat) #benchmark [Additional Tooling Documentation](docs/tools.md) [Assembly Testing Documentation](docs/AssemblyTests.md) [Building and installing Python bindings](docs/python_bindings.md) ## 系统要求 该库可以在 C++11 环境下使用。但是,构建它需要 C++17, 包括编译器和标准库的支持。 _有关支持的编译器和标准的更多详细信息,请参阅 [dependencies.md](docs/dependencies.md)。_ 如果您需要支持特定的编译器,非常欢迎提交补丁。 请参阅 [Platform-Specific Build Instructions](docs/platform_specific_build_instructions.md)。 ## 安装说明 本节介绍使用 cmake 的安装过程。作为前置条件,您需要安装 git 和 cmake。 _有关支持的构建工具版本的更多详细信息,请参阅 [dependencies.md](docs/dependencies.md)。_ ``` # 检出库。 $ git clone https://github.com/google/benchmark.git # 进入库根目录 $ cd benchmark # 创建一个 build 目录用于存放构建输出。 $ cmake -E make_directory "build" # 使用 cmake 生成构建系统文件,并下载所有依赖。 $ cmake -E chdir "build" cmake -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release ../ # 或者,从 CMake 3.13 开始,使用更简单的形式: # cmake -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release -S . -B "build" # 构建库。 $ cmake --build "build" --config Release ``` 这将构建 `benchmark` 和 `benchmark_main` 库以及测试。 在 unix 系统上,构建目录现在看起来应该像这样: ``` /benchmark /build /src /libbenchmark.a /libbenchmark_main.a /test ... ``` 接下来,您可以运行测试来检查构建结果。 ``` $ cmake -E chdir "build" ctest --build-config Release ``` 如果您想将库全局安装,还需运行: ``` sudo cmake --build "build" --config Release --target install ``` 请注意,Google Benchmark 需要 Google Test 来构建和运行测试。此 依赖项可以通过两种方式提供: * 将 Google Test 源代码检出到 `benchmark/googletest` 中。 * 或者,如果在配置期间指定了 `-DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON`(如上所示), 该库将自动下载并构建任何所需的依赖项。 如果您不希望构建和运行测试,请将 `-DBENCHMARK_ENABLE_GTEST_TESTS=OFF` 添加到 `CMAKE_ARGS` 中。 ### Debug vs Release 默认情况下,benchmark 构建为 debug 库。在这种情况下,输出中会显示警告。 要将其构建为 release 库,请在生成构建系统文件时添加 `-DCMAKE_BUILD_TYPE=Release`,如上所示。在构建命令中使用 `--config Release` 是为了正确支持多配置工具(例如 Visual Studio),对于其他构建系统(如 Makefile) 可以跳过。 要启用链接时优化 (link-time optimisation),还需在生成构建系统文件时 添加 `-DBENCHMARK_ENABLE_LTO=true`。 如果您使用的是 gcc,如果自动检测失败,您可能需要设置 `GCC_AR` 和 `GCC_RANLIB` cmake 缓存变量。 如果您使用的是 clang,您可能需要设置 `LLVMAR_EXECUTABLE`、 `LLVMNM_EXECUTABLE` 和 `LLVMRANLIB_EXECUTABLE` cmake 缓存变量。 要启用 sanitizer 检查(例如 `asan` 和 `tsan`),请添加: ``` -DCMAKE_C_FLAGS="-g -O2 -fno-omit-frame-pointer -fsanitize=address -fsanitize=thread -fno-sanitize-recover=all" -DCMAKE_CXX_FLAGS="-g -O2 -fno-omit-frame-pointer -fsanitize=address -fsanitize=thread -fno-sanitize-recover=all " ``` ### 稳定版和实验性库版本 主分支包含基准测试库的最新稳定版本; 其 API 可以被认为是基本稳定的,只有在发布新的主要版本时 才会进行破坏源代码兼容性的更改。 较新的、实验性的功能在 [`v2` branch](https://github.com/google/benchmark/tree/v2) 上实现和测试。希望 使用、测试并对新功能提供反馈的用户鼓励尝试 该分支。但是,该分支不提供稳定性保证,并保留 随时更改和破坏 API 的权利。 ## 使用方法 ### 基本用法 定义一个执行要测量的代码的函数,使用 `BENCHMARK` 宏将其注册为 benchmark 函数,并确保有合适的 `main` 函数可用: ``` #include static void BM_StringCreation(benchmark::State& state) { for (auto _ : state) std::string empty_string; } // Register the function as a benchmark BENCHMARK(BM_StringCreation); // Define another benchmark static void BM_StringCopy(benchmark::State& state) { std::string x = "hello"; for (auto _ : state) std::string copy(x); } BENCHMARK(BM_StringCopy); BENCHMARK_MAIN(); ``` 要运行 benchmark,请编译并链接 `benchmark` 库 (libbenchmark.a/.so)。如果您遵循上面的构建步骤,该库将 位于您创建的构建目录下。 ``` # 在 Linux 上执行上述构建步骤后的示例。假设 # `benchmark` 和 `build` 目录位于当前目录下。 $ g++ mybenchmark.cc -std=c++11 -isystem benchmark/include \ -Lbenchmark/build/src -lbenchmark -lpthread -o mybenchmark ``` 或者,链接 `benchmark_main` 库并删除 上面的 `BENCHMARK_MAIN();` 以获得相同的行为。 编译后的可执行文件默认将运行所有 benchmark。传递 `--help` 标志以获取选项信息,或参阅 [User Guide](docs/user_guide.md)。 ### 配合 CMake 使用 如果使用 CMake,建议使用 `target_link_libraries` 链接项目提供的 `benchmark::benchmark` 和 `benchmark::benchmark_main` 目标。 可以使用 ```find_package``` 来导入库的已安装版本。 ``` find_package(benchmark REQUIRED) ``` 或者,```add_subdirectory``` 将直接把该库包含在 一个人的 CMake 项目中。 ``` add_subdirectory(benchmark) ``` 无论哪种方式,都要按如下方式链接到该库。 ``` target_link_libraries(MyTarget benchmark::benchmark) ```
标签:Bash脚本, Bazel, Benchmark, C++, CMake, Google, Google Benchmark, Nuclei, 二进制发布, 代码片段测试, 单元测试, 压测, 底层优化, 开发库, 开源工具, 微基准测试, 性能分析, 性能测试, 数据擦除, 数据管道, 测试框架, 计算机科学, 软件工程, 逆向工具, 高精度计时