23 inline std::string ns_to_hr_clk(
const size_t ns_total) {
26 const size_t s = ns_total / (1000000000);
27 const size_t ms = (ns_total - s * 1000000000) / (1000000);
28 const size_t us = (ns_total - s * 1000000000 - ms * 1000000) / (1000);
30 (ns_total - s * 1000000000 - ms * 1000000 - us * 1000);
32 ss << s <<
"s " << ms <<
"ms " << us <<
"us " << ns <<
"ns";
40 std::chrono::system_clock::time_point start_clk;
43 std::vector<size_t> raw_values;
47 static constexpr
size_t RAW_VAL_CNT = 1024*1024*100;
50 raw_values.reserve(RAW_VAL_CNT);
56 this->start_clk = std::chrono::high_resolution_clock::now();
61 using namespace std::chrono;
63 const auto end_clk = high_resolution_clock::now();
65 #if defined(NVSL_ERROR) && defined(DBGE)
68 std::cerr <<
"Clock not running" << std::endl;
74 const auto elapsed = duration_cast<nanoseconds>(end_clk - start_clk).count();
75 this->total_ns += elapsed;
77 #define OVERFLOW_MSG "Raw values buffer overflow, increase array size or " \
79 #if defined(NVSL_ASSERT) && defined(DBGE)
80 NVSL_ASSERT(this->raw_values.size() < RAW_VAL_CNT, OVERFLOW_MSG);
82 if (this->raw_values.size() > RAW_VAL_CNT) {
83 fprintf(stderr, OVERFLOW_MSG
"\n");
87 this->raw_values.push_back(elapsed);
91 this->running =
false;
95 size_t ns()
const {
return this->total_ns; }
101 size_t ns_total = this->ns();
103 ss <<
"Total ns: " << this->ns() << std::endl;
104 ss <<
"Total time: " << ns_to_hr_clk(ns_total);
116 std::copy(raw_values.begin(), raw_values.end(),
127 #if defined(NVSL_ERROR)
128 NVSL_ERROR(
"Clock not reconcile. Call reconcile()");
130 assert(0 &&
"Clock not reconciled. Call reconcile()");
135 const auto idx = std::max(0UL, (
size_t)((sz*pc)/100.0)-1);
145 std::string
summarize(
size_t total_ops,
bool distribution =
false)
const {
146 std::stringstream ss;
149 NVSL_ASSERT(total_ops != 0,
"total ops cannot be zero");
151 assert(total_ops != 0 &&
"Total ops cannot be zero");
154 size_t ops_per_iter = total_ops/raw_values.size();
156 <<
"ops/s: " << (total_ops * (1000000000)) / ((double)this->ns())
159 << ns_to_hr_clk((
size_t)(this->ns() / (double)total_ops))
161 << ns_to_hr_clk((
size_t)(this->
percentile(50))/ops_per_iter)
163 << ns_to_hr_clk((
size_t)(this->
percentile(90))/ops_per_iter)
165 << ns_to_hr_clk((
size_t)(this->
percentile(99))/ops_per_iter)
167 << ns_to_hr_clk((
size_t)(this->ns() / (
double)total_ops));
172 size_t ns_per_op(
size_t total_ops)
const {
return this->ns() / total_ops; }
174 size_t percentile_per_op(
const size_t total_ops,
const size_t pc)
const {
175 const auto ops_per_iter = total_ops / raw_values.size();