Benchmark tool library for c++ code
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.
- 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測(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
| 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
- 運(yùn)行效果
需要cpp-optparse HdrHistogram兩個(gè)庫(kù)的額外依賴。
sltbench
- CMakeLists.txt
- example code
- 運(yùn)行效果
輸出內(nèi)容較少。
nonius
- CMakeLists.txt
- example code
運(yùn)行效果
只有頭文件,輕量級(jí);配置項(xiàng)好像有點(diǎn)少。
hayai
- CmakeLists.txt
- example code
- 運(yùn)行效果
輸出很像gtest(如它所說(shuō))。
Celero
- CmakeLists.txt
- example code
- 運(yùn)行效果
需要添加一個(gè)BASELINE,只添加BENCHMARK會(huì)crash。
google/benchmark
- CmakeLists.txt
- example code
- 運(yùn)行效果
源碼編譯需要依賴gtest(默認(rèn)會(huì)使用gtest對(duì)代碼進(jìn)行測(cè)試,也可以使用cmake設(shè)置變量去掉);
一些指標(biāo)
| 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 |
| nonius | header 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)題。
- 上一篇: Unity 工具 之 XChart UG
- 下一篇: qt 当前窗口句柄_QT获取窗口句柄