google benchmark安装编译测试一条龙服务

google benchmark
首先还是建议大家认真阅读github上的描述,我也是除了很多差错,最后发现还是官网真香
环境:Ubuntu20.04.1

安装编译benchmark

  1. 安装git和cmake
  2. $ git clone https://github.com/google/benchmark.git
  3. $ cd benchmark
  4. $ cmake -E make_directory "build"
  5. $ cmake -E chdir "build" cmake -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release ../
  6. $ cmake --build "build" --config Release

测试benchmark

  1. 创建demo.cpp文件,如下
#include <benchmark/benchmark.h>
#include <iostream>
#include <string>
using namespace std;

void demo()
{
    string str = "hello world";
    str.size();
}

static void BM_demo(benchmark::State& state) {
    for (auto _ : state)
        demo();
}

BENCHMARK(BM_demo); 
BENCHMARK_MAIN(); 
  1. $ g++ demo demo.cpp -std=c++11 -lbenchmark -lpthread

这里也有可能会报错:

/usr/bin/ld: /usr/local/lib/libbenchmark.a(benchmark_runner.cc.o): in function `benchmark::internal::BenchmarkRunner::DoNIterations()':

benchmark_runner.cc:(.text+0x12dd): undefined reference to `pthread_create'

collect2: error: ld returned 1 exit status

如果报错如上,可以尝试一下:

$ g++ demo demo.cpp -std=c++11 -lbenchmark -pthread

接着你会发现多了一个demo文件,这是刚编译好的

  1. $ ./demo

就可以看到结果了 在这里插入图片描述