日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

Benchmark tool library for c++ code

發(fā)布時(shí)間:2024/8/1 c/c++ 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Benchmark tool library for c++ code 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Benchmark是什么?

Wikipedia解釋

如下幾類:

  • Business and economics(商業(yè)和經(jīng)濟(jì))

    • Benchmarking, evaluating performance within organizations(基準(zhǔn)測(cè)試是將業(yè)務(wù)流程和績(jī)效指標(biāo)與其他公司的行業(yè)最佳和最佳實(shí)踐進(jìn)行比較的做法。 通常測(cè)量的維度是質(zhì)量、時(shí)間和成本)
    • Benchmark price (基準(zhǔn)價(jià)格 (BP) 是國(guó)際市場(chǎng)特定部分中每單位數(shù)量的價(jià)格,由在倫敦金屬交易所等市場(chǎng)上一貫出口最大數(shù)量或數(shù)量的國(guó)家或生產(chǎn)者組織設(shè)定。 [1] [2] 這個(gè)價(jià)格是定期設(shè)定的,通常是每月一次,作為國(guó)際貿(mào)易的指導(dǎo)方針。)
    • Benchmark (crude oil), oil-specific practices (基準(zhǔn)原油或標(biāo)記原油是用作原油買(mǎi)賣雙方參考價(jià)格的原油。有三個(gè)主要基準(zhǔn),即西德克薩斯中質(zhì)原油 (WTI)、布倫特混合原油和迪拜原油。)
    • Benchmark, an investment performance attribution (投資業(yè)績(jī)歸因是一組技術(shù),業(yè)績(jī)分析師用來(lái)解釋為什么投資組合的業(yè)績(jī)與基準(zhǔn)不同)
  • Science and technology (科學(xué)與技術(shù))

    • Benchmark (surveying), a point of known elevation marked for the purpose of surveying (基準(zhǔn)、基準(zhǔn)或測(cè)量基準(zhǔn)這一術(shù)語(yǔ)起源于測(cè)量員在石頭結(jié)構(gòu)中鑿出的水平標(biāo)記)
    • Benchmarking (geolocating), an activity involving finding benchmarks (是參與者尋找基準(zhǔn)(也稱為調(diào)查標(biāo)記或大地控制點(diǎn))的業(yè)余愛(ài)好活動(dòng))
    • Benchmark (computing), the result of running a computer program to assess performance (在計(jì)算中,基準(zhǔn)是運(yùn)行一個(gè)計(jì)算機(jī)程序、一組程序或其他操作的行為,以評(píng)估一個(gè)對(duì)象的相對(duì)性能,通常是通過(guò)對(duì)它運(yùn)行許多標(biāo)準(zhǔn)測(cè)試和試驗(yàn)。 [1] 術(shù)語(yǔ)基準(zhǔn)也通常用于精心設(shè)計(jì)的基準(zhǔn)測(cè)試程序本身。)
    • Benchmark, a best-performing, or gold standard test in medicine and statistics (醫(yī)學(xué)和統(tǒng)計(jì)學(xué)中表現(xiàn)最佳或黃金標(biāo)準(zhǔn)的測(cè)試)

    和我們相關(guān)的是Science and technology中的Benchmark (computing),主要通過(guò)考察一個(gè)計(jì)算機(jī)程序、一組程序、其他操作行為,然后對(duì)一個(gè)對(duì)象進(jìn)行性能評(píng)估。

Benchmark (computing)

  • Benchmark原則

  • Relevance: Benchmarks should measure relatively vital features.(基準(zhǔn)應(yīng)該衡量相對(duì)重要的特征)
  • Representativeness: Benchmark performance metrics should be broadly accepted by industry and academia.(代表性:基準(zhǔn)性能指標(biāo)應(yīng)被業(yè)界和學(xué)術(shù)界廣泛接受)
  • Equity: All systems should be fairly compared.(公平:所有系統(tǒng)都應(yīng)該被公平地比較)
  • Repeatability: Benchmark results can be verified.(重復(fù)性:可以驗(yàn)證基準(zhǔn)測(cè)試結(jié)果)
  • Cost-effectiveness: Benchmark tests are economical.(成本效益:基準(zhǔn)測(cè)試是經(jīng)濟(jì)的)
  • Scalability: Benchmark tests should work across systems possessing a range of resources from low to high.(可擴(kuò)展性:基準(zhǔn)測(cè)試應(yīng)該適用于擁有從低到高的一系列資源的系統(tǒng))
  • Transparency: Benchmark metrics should be easy to understand.(透明度:基準(zhǔn)指標(biāo)應(yīng)該易于理解)
  • Benchmark類型

  • Real program (真正的程序)
    • word processing software(文本處理程序)
    • tool software of CAD(CAD軟件工具)
    • user’s application software (i.e.: MIS) (用戶應(yīng)用程序)
  • Component Benchmark / Microbenchmark(組件benchmark,微benchmark)
    • core routine consists of a relatively small and specific piece of code. (由一段相對(duì)較小且特定的代碼組成的核心例程)
    • measure performance of a computer’s basic components (測(cè)量計(jì)算機(jī)基本組件的性能)
    • may be used for automatic detection of computer’s hardware parameters like number of registers, cache size, memory latency, etc. (可用于自動(dòng)檢測(cè)計(jì)算機(jī)的硬件參數(shù))
  • Kernel(內(nèi)核測(cè)試)
    • contains key codes
    • normally abstracted from actual program
    • popular kernel: Livermore loop
    • linpack benchmark (contains basic linear algebra subroutine written in FORTRAN language)
    • results are represented in Mflop/s.
  • Synthetic Benchmark
    • Procedure for programming synthetic benchmark:
      • take statistics of all types of operations from many application programs
      • get proportion of each operation
      • write program based on the proportion above
    • Types of Synthetic Benchmark are:
      • Whetstone
      • Dhrystone
    • These were the first general purpose industry standard computer benchmarks. They do not necessarily obtain high scores on modern pipelined computers.
  • I/O benchmarks
  • Database benchmarks
    • measure the throughput and response times of database management systems (DBMS)
  • Parallel benchmarks
    • used on machines with multiple cores and/or processors, or systems consisting of multiple machines
  • 一些常用的benchmark測(cè)試工具

    內(nèi)存、文件系統(tǒng)benchmark工具

    • Iometer – I/O subsystem measurement and characterization tool for single and clustered systems.
    • IOzone – Filesystem benchmark
    • 更多的參考Wikipedia鏈接
  • 一些個(gè)人想法

這里主要介紹了benchmark的原則(做基準(zhǔn)測(cè)試要遵循的規(guī)則)、benchmark的類型(主要存在哪些類型的benchmark,當(dāng)我們要進(jìn)行benchmark測(cè)試時(shí),首先要知道我們測(cè)試的主體是什么,在上述的類型中應(yīng)該有它的歸屬)、一些常用的benchmark工具(針對(duì)那些通用的測(cè)試主體,已經(jīng)前人開(kāi)發(fā)的各個(gè)工具);那么我認(rèn)為benchmark測(cè)試的流程應(yīng)該如下:

針對(duì)C++代碼進(jìn)行Benchmark測(cè)試

回到我們主題,如何對(duì)C++代碼進(jìn)行Benchmark測(cè)試,這里以C++編寫(xiě)的logger日志庫(kù)為例;按照我們上述Benchmark測(cè)試流程:

  • Benchmark主體是什么?

    C++編寫(xiě)的logger日志庫(kù),應(yīng)該屬于Component Benchmark / Microbenchmark這一類;一段相對(duì)較小且特定的代碼組成的核心例程

    ps: 多數(shù)我們用戶態(tài)的C++代碼,都能歸屬在Real program、Component Benchmark / Microbenchmark這兩類。

  • 是否存在現(xiàn)有的測(cè)試工具?

    目前沒(méi)有。

  • 需要自己編寫(xiě)測(cè)試工具

  • 按照我們梳理的流程,需要自己編寫(xiě)測(cè)試工具。

    那么針對(duì)我們C++代碼Benchmark,自己可以編寫(xiě)簡(jiǎn)單的代碼進(jìn)行測(cè)試,比如如下代碼,Timer用于測(cè)量shared_ptr使用make_shared和new兩種方式初始化的執(zhí)行時(shí)間:

    #include <array> #include <chrono> #include <iostream> #include <memory>using TimePoint = std::chrono::high_resolution_clock::time_point; class Timer {public:Timer() { start_time_point_ = std::chrono::high_resolution_clock::now(); }~Timer() { Stop(); }void Stop() {TimePoint end_time_point = std::chrono::high_resolution_clock::now();auto start = std::chrono::time_point_cast<std::chrono::nanoseconds>(start_time_point_).time_since_epoch().count();auto end = std::chrono::time_point_cast<std::chrono::nanoseconds>(end_time_point).time_since_epoch().count();auto duration = end - start;std::cout << duration << "ns(" << duration * 0.001 << "us)" << std::endl;}private:TimePoint start_time_point_; };struct Point {float x{0};float y{0}; };int main() {std::cout << "shared_ptr make_shared:";{std::array<std::shared_ptr<Point>, 1000> ptr_array;Timer timer;for (int i = 0; i < ptr_array.size(); ++i) {ptr_array[i] = std::make_shared<Point>();}}std::cout << "shared_ptr new:";{std::array<std::shared_ptr<Point>, 1000> ptr_array;Timer timer;for (int i = 0; i < ptr_array.size(); ++i) {ptr_array[i] = std::shared_ptr<Point>(new Point());}}return 0; }

    避免重復(fù)造輪子及使用好的輪子,是否存在一些library來(lái)幫助我們進(jìn)行測(cè)試呢?

    C++ Benchmark tool library

    存在哪些c++ Benchmark庫(kù)呢,通過(guò)google搜索和github搜索, 列出如下Benchmark library

    名稱簡(jiǎn)介源碼地址
    google/benchmarkA microbenchmark support libraryhttps://github.com/google/benchmark
    CeleroC++ Benchmark Authoring Library/Frameworkhttps://github.com/DigitalInBlue/Celero
    hayaiC++ benchmarking frameworkhttps://github.com/nickbruun/hayai
    noniusA C++ micro-benchmarking frameworkhttps://github.com/libnonius/nonius
    sltbenchC++ benchmark tool. Practical, stable and fast performance testing framework.https://github.com/ivafanas/sltbench
    CppBenchmarkPerformance benchmark framework for C++ with nanoseconds measure precisionhttps://github.com/chronoxor/CppBenchmark

    Benchmark tool library使用

    CppBenchmark

    • CMakeLists.txt
    set(CppBenchmarkPath "/path/CppBenchmark")include_directories(${CppBenchmarkPath}/include) link_directories(${CppBenchmarkPath}/bin ${CppBenchmarkPath}/temp/modules)add_executable(function_call_benchmark function_call_benchmark.cpp) target_link_libraries(function_call_benchmark PUBLIC cppbenchmark cpp-optparse HdrHistogram)
    • example code
    #include "benchmark/cppbenchmark.h"#include <math.h>// Benchmark sin() call for 1 seconds. // Make 3 attemtps and choose one with the best time result. BENCHMARK("sin", Settings().Attempts(3).Duration(1)) {std::cout << "xxx\n";sin(123.456); }BENCHMARK_MAIN()
    • 運(yùn)行效果
    [ 33%] Launching sin. Attempt 1...Done! [ 66%] Launching sin. Attempt 2...Done! [100%] Launching sin. Attempt 3...Done! =============================================================================== CppBenchmark report. Version 1.0.1.0 =============================================================================== CPU architecture: Intel(R) Core(TM) i7-10700K CPU @ 3.80GHz CPU logical cores: 16 CPU physical cores: 16 CPU clock speed: 4.867 GHz CPU Hyper-Threading: disabled RAM total: 31.209 GiB RAM free: 807.604 MiB =============================================================================== OS version: Ubuntu 18.04.5 LTS OS bits: 64-bit Process bits: 64-bit Process configuration: release Local timestamp: Tue Nov 23 17:25:42 2021 UTC timestamp: Tue Nov 23 09:25:42 2021 =============================================================================== Benchmark: sin Attempts: 3 Duration: 1 seconds ------------------------------------------------------------------------------- Phase: sin Average time: 0 ns/op Minimal time: 1 ns/op Maximal time: 1 ns/op Total time: 96.781 ms Total operations: 133027371 Operations throughput: 1374517987 ops/s ===============================================================================

    需要cpp-optparse HdrHistogram兩個(gè)庫(kù)的額外依賴。

    sltbench

    • CMakeLists.txt
    set(SltBenchmarkPath "/Path/sltbench/install")include_directories(${SltBenchmarkPath}/include) link_directories(${SltBenchmarkPath}/lib)add_executable(slt_benchmark slt_benchmark.cpp) target_link_libraries(slt_benchmark PUBLIC sltbench)
    • example code
    #include "sltbench/Bench.h" #include <thread> #include <chrono>void my_function() {std::this_thread::sleep_for(std::chrono::microseconds(1000)); }SLTBENCH_FUNCTION(my_function);SLTBENCH_MAIN();
    • 運(yùn)行效果

      輸出內(nèi)容較少。

    nonius

    • CMakeLists.txt
    set(NoniusBenchmarkPath "/Path/nonius")include_directories(${NoniusBenchmarkPath}/include)add_executable(nonius_benchmark nonius_benchmark.cpp) target_link_libraries(nonius_benchmark PUBLIC pthread)
    • example code
    #define NONIUS_RUNNER #include <nonius/nonius.h++> #include <thread> #include <chrono>NONIUS_BENCHMARK("DemoSleep", []{std::this_thread::sleep_for(std::chrono::microseconds(1000)); })int main() {nonius::configuration cfg;cfg.samples = 1;cfg.resamples = 1;nonius::go(cfg);return 0; }

    運(yùn)行效果

    只有頭文件,輕量級(jí);配置項(xiàng)好像有點(diǎn)少。

    hayai

    • CmakeLists.txt
    set(HayaiBenchmarkPath "/Path/hayai/install")include_directories(${HayaiBenchmarkPath}/include) link_directories(${HayaiBenchmarkPath}/lib)add_executable(hayai_benchmark hayai_benchmark.cpp) target_link_libraries(hayai_benchmark PUBLIC hayai_main)
    • example code
    #include <hayai/hayai.hpp> #include <thread> #include <chrono>BENCHMARK(DemoSleep, DemoSleep, 1, 100) {std::this_thread::sleep_for(std::chrono::microseconds(1000)); }
    • 運(yùn)行效果

      輸出很像gtest(如它所說(shuō))。

    Celero

    • CmakeLists.txt
    set(CeleroBenchmarkPath "/sec/yms/benchmark/Celero/install")include_directories(${CeleroBenchmarkPath}/include) link_directories(${CeleroBenchmarkPath}/lib)add_executable(celero_benchmark celero_benchmark.cpp) target_link_libraries(celero_benchmark PUBLIC celero-d)
    • example code
    #include <celero/Celero.h>#include <chrono> #include <thread>CELERO_MAINBASELINE(DemoSleep, DemoSleep, 1, 100) // 這個(gè)必不可少,缺少運(yùn)行會(huì)coredump {std::this_thread::sleep_for(std::chrono::microseconds(1000)); }BENCHMARK(DemoSleep, HalfBaseline, 1, 100) {std::this_thread::sleep_for(std::chrono::microseconds(500)); }
    • 運(yùn)行效果

      需要添加一個(gè)BASELINE,只添加BENCHMARK會(huì)crash。

    google/benchmark

    • CmakeLists.txt
    # 使用的源碼安裝到了系統(tǒng)目錄,所以沒(méi)有上述的include_directories、link_directories add_executable(google_benchmark google_benchmark.cpp) target_link_libraries(google_benchmark PRIVATE benchmark pthread)
    • example code
    #include <benchmark/benchmark.h> #include <chrono> #include <thread>void BM_DemoSleep(benchmark::State& state) {for (auto _ : state){std::this_thread::sleep_for(std::chrono::microseconds(1000));} } BENCHMARK(BM_DemoSleep);BENCHMARK_MAIN();
    • 運(yùn)行效果

      源碼編譯需要依賴gtest(默認(rèn)會(huì)使用gtest對(duì)代碼進(jìn)行測(cè)試,也可以使用cmake設(shè)置變量去掉);

    一些指標(biāo)

    名稱編譯依賴時(shí)間精度(linux平臺(tái))
    google/benchmark依賴自身庫(kù),需額外引入pthread包含兩個(gè)時(shí)間time: 時(shí)鐘默認(rèn)是std::chrono::high_resolution_clock,反饋時(shí)間精度是nscpu_time: 時(shí)鐘根據(jù)參數(shù)設(shè)定,是CLOCK_PROCESS_CPUTIME_ID 或者 CLOCK_THREAD_CPUTIME_ID(clock_gettime)
    Celero依賴自身庫(kù),無(wú)額外引入時(shí)鐘是std::chrono::high_resolution_clock,反饋時(shí)間精度是std::chrono::microseconds
    hayai依賴自身庫(kù),無(wú)額外引入時(shí)鐘是CLOCK_MONOTONIC_RAW->CLOCK_MONOTONIC->CLOCK_REALTIME(查看宏定義,按照這個(gè)順序進(jìn)行退化),反饋時(shí)間精度是ns
    noniusheader only,需額外引入pthread時(shí)鐘是std::chrono::high_resolution_clock, 反饋時(shí)間精度是ns
    sltbench依賴自身庫(kù),無(wú)額外引入時(shí)鐘是std::chrono::high_resolution_clock, 反饋時(shí)間精度是ns
    CppBenchmark依賴自身庫(kù),需額外引入cpp-optparse、HdrHistogram時(shí)鐘是CLOCK_MONOTONIC,反饋時(shí)間精度是ns

    查看源碼,上述軟件框架模型都很類似:用戶將要測(cè)試的函數(shù)對(duì)象注冊(cè)到容器中,然后三方庫(kù)再對(duì)容器進(jìn)行遍歷執(zhí)行,最后統(tǒng)計(jì)結(jié)果;

    總結(jié)

    以上是生活随笔為你收集整理的Benchmark tool library for c++ code的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。