cpp-common
Public Member Functions | Private Attributes | Static Private Attributes | List of all members
nvsl::Clock Class Reference

Clock object based on std::chrono::high_resolution_clock. More...

#include <clock.hh>

Public Member Functions

void tick ()
 Start the timer.
 
void tock ()
 Stop the timer.
 
void reset ()
 
size_t ns () const
 
const std::string summarize () const
 Total time elapsed.
 
void reconcile ()
 Prepare clock object to calculate percentile/summary. More...
 
size_t percentile (const size_t pc) const
 Calculate the percentile value. More...
 
std::string summarize (size_t total_ops, bool distribution=false) const
 Summary with operations per second. More...
 
size_t ns_per_op (size_t total_ops) const
 
size_t percentile_per_op (const size_t total_ops, const size_t pc) const
 

Private Attributes

std::chrono::system_clock::time_point start_clk
 
bool running = false
 
size_t total_ns = 0
 
std::vector< size_t > raw_values
 
std::vector< size_t > sorted_raw_values
 

Static Private Attributes

static constexpr size_t RAW_VAL_CNT = 1024*1024*100
 

Detailed Description

Clock object based on std::chrono::high_resolution_clock.

Definition at line 38 of file clock.hh.

Member Function Documentation

◆ percentile()

size_t nvsl::Clock::percentile ( const size_t  pc) const
inline

Calculate the percentile value.

Parameters
[in]pcPercentile out of 100

Definition at line 125 of file clock.hh.

125  {
126  if (sorted_raw_values.size() == 0) {
127 #if defined(NVSL_ERROR)
128  NVSL_ERROR("Clock not reconcile. Call reconcile()");
129 #else
130  assert(0 && "Clock not reconciled. Call reconcile()");
131 #endif
132  }
133 
134  const auto sz = sorted_raw_values.size();
135  const auto idx = std::max(0UL, (size_t)((sz*pc)/100.0)-1);
136 
137  return sorted_raw_values[idx];
138  }

◆ reconcile()

void nvsl::Clock::reconcile ( )
inline

Prepare clock object to calculate percentile/summary.

non-const function to update the internal state so that other functions can be const

Definition at line 114 of file clock.hh.

114  {
115  sorted_raw_values.reserve(raw_values.size());
116  std::copy(raw_values.begin(), raw_values.end(),
117  std::back_inserter(sorted_raw_values));
118  std::sort(sorted_raw_values.begin(), sorted_raw_values.end());
119  }

◆ summarize()

std::string nvsl::Clock::summarize ( size_t  total_ops,
bool  distribution = false 
) const
inline

Summary with operations per second.

Parameters
[in]total_opsTotal number of operations performed
[in]distributionGenerate distribution in summary

Definition at line 145 of file clock.hh.

145  {
146  std::stringstream ss;
147 
148 #ifdef NVSL_ASSERT
149  NVSL_ASSERT(total_ops != 0, "total ops cannot be zero");
150 #else
151  assert(total_ops != 0 && "Total ops cannot be zero");
152 #endif
153 
154  size_t ops_per_iter = total_ops/raw_values.size();
155  ss << this->summarize() << std::endl
156  << "ops/s: " << (total_ops * (1000000000)) / ((double)this->ns())
157  << "\n"
158  << "time/op: "
159  << ns_to_hr_clk((size_t)(this->ns() / (double)total_ops))
160  << "\np50/op: "
161  << ns_to_hr_clk((size_t)(this->percentile(50))/ops_per_iter)
162  << "\np90/op: "
163  << ns_to_hr_clk((size_t)(this->percentile(90))/ops_per_iter)
164  << "\np99/op: "
165  << ns_to_hr_clk((size_t)(this->percentile(99))/ops_per_iter)
166  << "\ntime/op: "
167  << ns_to_hr_clk((size_t)(this->ns() / (double)total_ops));
168 
169  return ss.str();
170  }

Member Data Documentation

◆ sorted_raw_values

std::vector<size_t> nvsl::Clock::sorted_raw_values
private

Total number of values to store

Definition at line 44 of file clock.hh.


The documentation for this class was generated from the following file:
nvsl::Clock::summarize
const std::string summarize() const
Total time elapsed.
Definition: clock.hh:98
nvsl::Clock::percentile
size_t percentile(const size_t pc) const
Calculate the percentile value.
Definition: clock.hh:125
NVSL_ERROR
#define NVSL_ERROR(msg)
Throw exception with msg if val is NULL.
Definition: error.hh:53
nvsl::Clock::sorted_raw_values
std::vector< size_t > sorted_raw_values
Definition: clock.hh:44
NVSL_ASSERT
#define NVSL_ASSERT(cond, msg)
Assert a condition w/ msg and generate backtrace on fail.
Definition: error.hh:73