Benchmark tool library for c++ code
Benchmark是什么?
Wikipedia解釋
如下幾類:
-
Business and economics(商業(yè)和經(jīng)濟)
- Benchmarking, evaluating performance within organizations(基準(zhǔn)測試是將業(yè)務(wù)流程和績效指標(biāo)與其他公司的行業(yè)最佳和最佳實踐進行比較的做法。 通常測量的維度是質(zhì)量、時間和成本)
- Benchmark price (基準(zhǔn)價格 (BP) 是國際市場特定部分中每單位數(shù)量的價格,由在倫敦金屬交易所等市場上一貫出口最大數(shù)量或數(shù)量的國家或生產(chǎn)者組織設(shè)定。 [1] [2] 這個價格是定期設(shè)定的,通常是每月一次,作為國際貿(mào)易的指導(dǎo)方針。)
- Benchmark (crude oil), oil-specific practices (基準(zhǔn)原油或標(biāo)記原油是用作原油買賣雙方參考價格的原油。有三個主要基準(zhǔn),即西德克薩斯中質(zhì)原油 (WTI)、布倫特混合原油和迪拜原油。)
- Benchmark, an investment performance attribution (投資業(yè)績歸因是一組技術(shù),業(yè)績分析師用來解釋為什么投資組合的業(yè)績與基準(zhǔn)不同)
-
Science and technology (科學(xué)與技術(shù))
- Benchmark (surveying), a point of known elevation marked for the purpose of surveying (基準(zhǔn)、基準(zhǔn)或測量基準(zhǔn)這一術(shù)語起源于測量員在石頭結(jié)構(gòu)中鑿出的水平標(biāo)記)
- Benchmarking (geolocating), an activity involving finding benchmarks (是參與者尋找基準(zhǔn)(也稱為調(diào)查標(biāo)記或大地控制點)的業(yè)余愛好活動)
- Benchmark (computing), the result of running a computer program to assess performance (在計算中,基準(zhǔn)是運行一個計算機程序、一組程序或其他操作的行為,以評估一個對象的相對性能,通常是通過對它運行許多標(biāo)準(zhǔn)測試和試驗。 [1] 術(shù)語基準(zhǔn)也通常用于精心設(shè)計的基準(zhǔn)測試程序本身。)
- Benchmark, a best-performing, or gold standard test in medicine and statistics (醫(yī)學(xué)和統(tǒng)計學(xué)中表現(xiàn)最佳或黃金標(biāo)準(zhǔn)的測試)
和我們相關(guān)的是Science and technology中的Benchmark (computing),主要通過考察一個計算機程序、一組程序、其他操作行為,然后對一個對象進行性能評估。
Benchmark (computing)
-
Benchmark原則
- Relevance: Benchmarks should measure relatively vital features.(基準(zhǔn)應(yīng)該衡量相對重要的特征)
- 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ù)性:可以驗證基準(zhǔn)測試結(jié)果)
- Cost-effectiveness: Benchmark tests are economical.(成本效益:基準(zhǔn)測試是經(jīng)濟的)
- Scalability: Benchmark tests should work across systems possessing a range of resources from low to high.(可擴展性:基準(zhǔn)測試應(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. (由一段相對較小且特定的代碼組成的核心例程)
- measure performance of a computer’s basic components (測量計算機基本組件的性能)
- may be used for automatic detection of computer’s hardware parameters like number of registers, cache size, memory latency, etc. (可用于自動檢測計算機的硬件參數(shù))
- Kernel(內(nèi)核測試)
- 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.
- Procedure for programming synthetic benchmark:
- 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測試工具
內(nèi)存、文件系統(tǒng)benchmark工具
- Iometer – I/O subsystem measurement and characterization tool for single and clustered systems.
- IOzone – Filesystem benchmark
- 更多的參考Wikipedia鏈接
-
一些個人想法
這里主要介紹了benchmark的原則(做基準(zhǔn)測試要遵循的規(guī)則)、benchmark的類型(主要存在哪些類型的benchmark,當(dāng)我們要進行benchmark測試時,首先要知道我們測試的主體是什么,在上述的類型中應(yīng)該有它的歸屬)、一些常用的benchmark工具(針對那些通用的測試主體,已經(jīng)前人開發(fā)的各個工具);那么我認(rèn)為benchmark測試的流程應(yīng)該如下:
針對C++代碼進行Benchmark測試
回到我們主題,如何對C++代碼進行Benchmark測試,這里以C++編寫的logger日志庫為例;按照我們上述Benchmark測試流程:
Benchmark主體是什么?
C++編寫的logger日志庫,應(yīng)該屬于Component Benchmark / Microbenchmark這一類;一段相對較小且特定的代碼組成的核心例程
ps: 多數(shù)我們用戶態(tài)的C++代碼,都能歸屬在Real program、Component Benchmark / Microbenchmark這兩類。
是否存在現(xiàn)有的測試工具?
目前沒有。
需要自己編寫測試工具
按照我們梳理的流程,需要自己編寫測試工具。
那么針對我們C++代碼Benchmark,自己可以編寫簡單的代碼進行測試,比如如下代碼,Timer用于測量shared_ptr使用make_shared和new兩種方式初始化的執(zhí)行時間:
#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來幫助我們進行測試呢?
C++ Benchmark tool library
存在哪些c++ Benchmark庫呢,通過google搜索和github搜索, 列出如下Benchmark library
| google/benchmark | A microbenchmark support library | https://github.com/google/benchmark |
| Celero | C++ Benchmark Authoring Library/Framework | https://github.com/DigitalInBlue/Celero |
| hayai | C++ benchmarking framework | https://github.com/nickbruun/hayai |
| nonius | A C++ micro-benchmarking framework | https://github.com/libnonius/nonius |
| sltbench | C++ benchmark tool. Practical, stable and fast performance testing framework. | https://github.com/ivafanas/sltbench |
| CppBenchmark | Performance benchmark framework for C++ with nanoseconds measure precision | https://github.com/chronoxor/CppBenchmark |
Benchmark tool library使用
CppBenchmark
- CMakeLists.txt
- example code
- 運行效果
需要cpp-optparse HdrHistogram兩個庫的額外依賴。
sltbench
- CMakeLists.txt
- example code
- 運行效果
輸出內(nèi)容較少。
nonius
- CMakeLists.txt
- example code
運行效果
只有頭文件,輕量級;配置項好像有點少。
hayai
- CmakeLists.txt
- example code
- 運行效果
輸出很像gtest(如它所說)。
Celero
- CmakeLists.txt
- example code
- 運行效果
需要添加一個BASELINE,只添加BENCHMARK會crash。
google/benchmark
- CmakeLists.txt
- example code
- 運行效果
源碼編譯需要依賴gtest(默認(rèn)會使用gtest對代碼進行測試,也可以使用cmake設(shè)置變量去掉);
一些指標(biāo)
| google/benchmark | 依賴自身庫,需額外引入pthread | 包含兩個時間time: 時鐘默認(rèn)是std::chrono::high_resolution_clock,反饋時間精度是nscpu_time: 時鐘根據(jù)參數(shù)設(shè)定,是CLOCK_PROCESS_CPUTIME_ID 或者 CLOCK_THREAD_CPUTIME_ID(clock_gettime) |
| Celero | 依賴自身庫,無額外引入 | 時鐘是std::chrono::high_resolution_clock,反饋時間精度是std::chrono::microseconds |
| hayai | 依賴自身庫,無額外引入 | 時鐘是CLOCK_MONOTONIC_RAW->CLOCK_MONOTONIC->CLOCK_REALTIME(查看宏定義,按照這個順序進行退化),反饋時間精度是ns |
| nonius | header only,需額外引入pthread | 時鐘是std::chrono::high_resolution_clock, 反饋時間精度是ns |
| sltbench | 依賴自身庫,無額外引入 | 時鐘是std::chrono::high_resolution_clock, 反饋時間精度是ns |
| CppBenchmark | 依賴自身庫,需額外引入cpp-optparse、HdrHistogram | 時鐘是CLOCK_MONOTONIC,反饋時間精度是ns |
查看源碼,上述軟件框架模型都很類似:用戶將要測試的函數(shù)對象注冊到容器中,然后三方庫再對容器進行遍歷執(zhí)行,最后統(tǒng)計結(jié)果;
總結(jié)
以上是生活随笔為你收集整理的Benchmark tool library for c++ code的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unity 工具 之 XChart UG
- 下一篇: matlab程序设计课件,《MATLAB