Skip to content

Commit e93255d

Browse files
committed
Update README.md
1 parent ec49c2b commit e93255d

File tree

3 files changed

+2143
-0
lines changed

3 files changed

+2143
-0
lines changed

README.md

+81
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,87 @@ in order to generate and analyze latency histogram:
636636
[sleep]: https://github.com/chronoxor/CppBenchmark/raw/master/images/sleep.png "Sleep HDR Histogram"
637637

638638
## Example 9: Benchmark latency with manual update
639+
```C++
640+
#include "benchmark/cppbenchmark.h"
641+
642+
#include <chrono>
643+
#include <limits>
644+
645+
const uint64_t iterations = 10000000;
646+
const auto settings = CppBenchmark::Settings().Iterations(iterations).Latency(1, 1000000000, 5, false);
647+
648+
BENCHMARK("high_resolution_clock", settings)
649+
{
650+
static uint64_t minresolution = std::numeric_limits<uint64_t>::max();
651+
static uint64_t maxresolution = std::numeric_limits<uint64_t>::min();
652+
static auto latency_timestamp = std::chrono::high_resolution_clock::now();
653+
static auto resolution_timestamp = std::chrono::high_resolution_clock::now();
654+
static uint64_t count = 0;
655+
656+
// Get the current timestamp
657+
auto current = std::chrono::high_resolution_clock::now();
658+
659+
// Update iterations counter
660+
++count;
661+
662+
// Register latency metrics
663+
uint64_t latency = std::chrono::duration_cast<std::chrono::nanoseconds>(current - latency_timestamp).count();
664+
if (latency > 0)
665+
{
666+
context.metrics().AddLatency(latency / count);
667+
latency_timestamp = current;
668+
count = 0;
669+
}
670+
671+
// Register resolution metrics
672+
uint64_t resolution = std::chrono::duration_cast<std::chrono::nanoseconds>(current - resolution_timestamp).count();
673+
if (resolution > 0)
674+
{
675+
if (resolution < minresolution)
676+
{
677+
minresolution = resolution;
678+
context.metrics().SetCustom("resolution-min", minresolution);
679+
}
680+
if (resolution > maxresolution)
681+
{
682+
maxresolution = resolution;
683+
context.metrics().SetCustom("resolution-max", maxresolution);
684+
}
685+
resolution_timestamp = current;
686+
}
687+
}
688+
```
689+
690+
Report fragment is the following:
691+
```
692+
===============================================================================
693+
Benchmark: high_resolution_clock
694+
Attempts: 5
695+
Iterations: 10000000
696+
-------------------------------------------------------------------------------
697+
Phase: high_resolution_clock
698+
Latency (Min): 38 ns / iteration
699+
Latency (Max): 1.037 ms / iteration
700+
Latency (Mean): 53.0462
701+
Latency (StDv): 1136.37
702+
Total time: 468.924 ms
703+
Total iterations: 10000000
704+
Iterations throughput: 21325385 / second
705+
Custom values:
706+
resolution-max: 7262968
707+
resolution-min: 311
708+
===============================================================================
709+
```
710+
711+
If the benchmark is launched with **--histograms=100** parameter then a file
712+
with [High Dynamic Range (HDR) Histogram](http://hdrhistogram.github.io/HdrHistogram/)
713+
will be created - [clock.hdr](https://github.com/chronoxor/CppBenchmark/raw/master/images/clock.hdr)
714+
715+
Finally you can use [HdrHistogram Plotter](http://hdrhistogram.github.io/HdrHistogram/plotFiles.html)
716+
in order to generate and analyze latency histogram:
717+
718+
![High resolution clock HDR Histogram][clock]
719+
[clock]: https://github.com/chronoxor/CppBenchmark/raw/master/images/clock.png "High resolution clock HDR Histogram"
639720
640721
## Example 10: Benchmark threads
641722
```C++

0 commit comments

Comments
 (0)