| 54 | const double kNumSecondsPerNanosecond = 1.e-9; |
| 55 | |
| 56 | struct StatisticsMapValue { |
| 57 | static const int kWindowSize = 100; |
| 58 | |
| 59 | inline StatisticsMapValue() { time_last_called_ = std::chrono::system_clock::now(); } |
| 60 | |
| 61 | inline void AddValue(double sample) { |
| 62 | std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now(); |
| 63 | double dt = |
| 64 | static_cast<double>(std::chrono::duration_cast<std::chrono::nanoseconds>(now - time_last_called_).count()) * |
| 65 | kNumSecondsPerNanosecond; |
| 66 | time_last_called_ = now; |
| 67 | |
| 68 | values_.Add(sample); |
| 69 | time_deltas_.Add(dt); |
| 70 | } |
| 71 | inline double GetLastDeltaTime() const { |
| 72 | if (time_deltas_.total_samples()) { |
| 73 | return time_deltas_.GetMostRecent(); |
| 74 | } else { |
| 75 | return 0; |
| 76 | } |
| 77 | } |
| 78 | inline double GetLastValue() const { |
| 79 | if (values_.total_samples()) { |
| 80 | return values_.GetMostRecent(); |
| 81 | } else { |
| 82 | return 0; |
| 83 | } |
| 84 | } |
| 85 | inline double Sum() const { return values_.sum(); } |
| 86 | int TotalSamples() const { return values_.total_samples(); } |
| 87 | double Mean() const { return values_.Mean(); } |
| 88 | double RollingMean() const { return values_.RollingMean(); } |
| 89 | double Max() const { return values_.max(); } |
| 90 | double Min() const { return values_.min(); } |
| 91 | double Median() const { return values_.median(); } |
| 92 | double Q1() const { return values_.q1(); } |
| 93 | double Q3() const { return values_.q3(); } |
| 94 | double LazyVariance() const { return values_.LazyVariance(); } |
| 95 | double MeanCallsPerSec() const { |
| 96 | double mean_dt = time_deltas_.Mean(); |
| 97 | if (mean_dt != 0) { |
| 98 | return 1.0 / mean_dt; |
| 99 | } else { |
| 100 | return -1.0; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | double MeanDeltaTime() const { return time_deltas_.Mean(); } |
| 105 | double RollingMeanDeltaTime() const { return time_deltas_.RollingMean(); } |
| 106 | double MaxDeltaTime() const { return time_deltas_.max(); } |
| 107 | double MinDeltaTime() const { return time_deltas_.min(); } |
| 108 | double LazyVarianceDeltaTime() const { return time_deltas_.LazyVariance(); } |
| 109 | std::vector<double> GetAllValues() const { return values_.GetAllSamples(); } |
| 110 | |
| 111 | private: |
| 112 | // Create an accumulator with specified window size. |
| 113 | Accumulator<double, double, kWindowSize> values_; |