构建集成
在robotic_arm的third_party已经集成了benchmark,只需在相关测试用例代码的CmakeLists.txt添加如下内容:
target_link_libraries(xxx PRIVATE benchmark pthread)
Demo样例
存在以下模式样例:
1. 使用BENCHMARK、BENCHMARK_MAIN宏
#include <benchmark/benchmark.h>
#include <chrono>
#include <thread>
void BM_DemoSleep(benchmark::State& state) {
for (auto _ : state){
//待测试的代码
}
}
BENCHMARK(BM_DemoSleep); // 注册要测试的函数对象
BENCHMARK_MAIN(); // main函数,运行benchmark初始化和执行
2. 直接使用Benchmark相应的接口
#include <benchmark/benchmark.h>
#include <chrono>
#include <thread>
void BM_DemoSleep(benchmark::State& state) {
for (auto _ : state){
std::this_thread::sleep_for(std::chrono::nanoseconds(1000)); //待测试的代码
}
}
void BM_DemoSleep1(benchmark::State& state, int id) {
std::cout << "id:"<< id << std::endl;
for (auto _ : state){
std::this_thread::sleep_for(std::chrono::nanoseconds(1000));
}
}
int main(int argc, char** argv) {
::benchmark::Initialize(&argc, argv); // 初始化Benchmark
if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1;
// 使用函数指针注册