Benchmark tool library for c++ code
Benchmark是什么?
Wikipedia解釋
如下幾類:
-
Business and economics(商業和經濟)
- Benchmarking, evaluating performance within organizations(基準測試是將業務流程和績效指標與其他公司的行業最佳和最佳實踐進行比較的做法。 通常測量的維度是質量、時間和成本)
- Benchmark price (基準價格 (BP) 是國際市場特定部分中每單位數量的價格,由在倫敦金屬交易所等市場上一貫出口最大數量或數量的國家或生產者組織設定。 [1] [2] 這個價格是定期設定的,通常是每月一次,作為國際貿易的指導方針。)
- Benchmark (crude oil), oil-specific practices (基準原油或標記原油是用作原油買賣雙方參考價格的原油。有三個主要基準,即西德克薩斯中質原油 (WTI)、布倫特混合原油和迪拜原油。)
- Benchmark, an investment performance attribution (投資業績歸因是一組技術,業績分析師用來解釋為什么投資組合的業績與基準不同)
-
Science and technology (科學與技術)
- Benchmark (surveying), a point of known elevation marked for the purpose of surveying (基準、基準或測量基準這一術語起源于測量員在石頭結構中鑿出的水平標記)
- Benchmarking (geolocating), an activity involving finding benchmarks (是參與者尋找基準(也稱為調查標記或大地控制點)的業余愛好活動)
- Benchmark (computing), the result of running a computer program to assess performance (在計算中,基準是運行一個計算機程序、一組程序或其他操作的行為,以評估一個對象的相對性能,通常是通過對它運行許多標準測試和試驗。 [1] 術語基準也通常用于精心設計的基準測試程序本身。)
- Benchmark, a best-performing, or gold standard test in medicine and statistics (醫學和統計學中表現最佳或黃金標準的測試)
和我們相關的是Science and technology中的Benchmark (computing),主要通過考察一個計算機程序、一組程序、其他操作行為,然后對一個對象進行性能評估。
Benchmark (computing)
-
Benchmark原則
- Relevance: Benchmarks should measure relatively vital features.(基準應該衡量相對重要的特征)
- Representativeness: Benchmark performance metrics should be broadly accepted by industry and academia.(代表性:基準性能指標應被業界和學術界廣泛接受)
- Equity: All systems should be fairly compared.(公平:所有系統都應該被公平地比較)
- Repeatability: Benchmark results can be verified.(重復性:可以驗證基準測試結果)
- Cost-effectiveness: Benchmark tests are economical.(成本效益:基準測試是經濟的)
- Scalability: Benchmark tests should work across systems possessing a range of resources from low to high.(可擴展性:基準測試應該適用于擁有從低到高的一系列資源的系統)
- Transparency: Benchmark metrics should be easy to understand.(透明度:基準指標應該易于理解)
-
Benchmark類型
- Real program (真正的程序)
- word processing software(文本處理程序)
- tool software of CAD(CAD軟件工具)
- user’s application software (i.e.: MIS) (用戶應用程序)
- 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. (可用于自動檢測計算機的硬件參數)
- Kernel(內核測試)
- 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測試工具
內存、文件系統benchmark工具
- Iometer – I/O subsystem measurement and characterization tool for single and clustered systems.
- IOzone – Filesystem benchmark
- 更多的參考Wikipedia鏈接
-
一些個人想法
這里主要介紹了benchmark的原則(做基準測試要遵循的規則)、benchmark的類型(主要存在哪些類型的benchmark,當我們要進行benchmark測試時,首先要知道我們測試的主體是什么,在上述的類型中應該有它的歸屬)、一些常用的benchmark工具(針對那些通用的測試主體,已經前人開發的各個工具);那么我認為benchmark測試的流程應該如下:
針對C++代碼進行Benchmark測試
回到我們主題,如何對C++代碼進行Benchmark測試,這里以C++編寫的logger日志庫為例;按照我們上述Benchmark測試流程:
Benchmark主體是什么?
C++編寫的logger日志庫,應該屬于Component Benchmark / Microbenchmark這一類;一段相對較小且特定的代碼組成的核心例程
ps: 多數我們用戶態的C++代碼,都能歸屬在Real program、Component Benchmark / Microbenchmark這兩類。
是否存在現有的測試工具?
目前沒有。
需要自己編寫測試工具
按照我們梳理的流程,需要自己編寫測試工具。
那么針對我們C++代碼Benchmark,自己可以編寫簡單的代碼進行測試,比如如下代碼,Timer用于測量shared_ptr使用make_shared和new兩種方式初始化的執行時間:
#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; }避免重復造輪子及使用好的輪子,是否存在一些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
- 運行效果
輸出內容較少。
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(默認會使用gtest對代碼進行測試,也可以使用cmake設置變量去掉);
一些指標
| google/benchmark | 依賴自身庫,需額外引入pthread | 包含兩個時間time: 時鐘默認是std::chrono::high_resolution_clock,反饋時間精度是nscpu_time: 時鐘根據參數設定,是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 |
查看源碼,上述軟件框架模型都很類似:用戶將要測試的函數對象注冊到容器中,然后三方庫再對容器進行遍歷執行,最后統計結果;
總結
以上是生活随笔為你收集整理的Benchmark tool library for c++ code的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unity 工具 之 XChart UG
- 下一篇: matlab程序设计课件,《MATLAB