// Copyright 2020 Google LLC. All Rights Reserved. #include "benchmarking/measurements.h" #include #include "base/logging.h" namespace widevine { namespace { size_t RoundUp(size_t num, size_t denum) { return (num + denum - 1) / denum; } } // namespace void Timer::Reset() { start_ = Clock::now(); } Period Timer::Get() const { const auto now = Clock::now(); return std::chrono::duration_cast(now - start_); } Throughput::Throughput() : bytes(0), microseconds(0), bits_per_second(0) {} Throughput::Throughput(const Period& duration, size_t bytes) { this->bytes = bytes; microseconds = duration.count(); bits_per_second = 0; if (microseconds > 0) { // Make sure to only do one division to ensure that we are not loosing too // much information as we are avoiding floating-point values. bits_per_second = (bytes * 8 * 1000 * 1000) / microseconds; } } const Period& Sampler::Percentile::Get(size_t percentile) const { // Nearest-rank method: // https://en.wikipedia.org/wiki/Percentile#The_nearest-rank_method const size_t index = RoundUp(samples_.size() * percentile, 100); // The index will be from 1 to N, but we need it to be 0 to N-1. return samples_[index - 1]; } void Sampler::Push(const Period& period) { samples_.push_back(period); } void PrettyPrint(const std::string& title, const Throughput& throughput, size_t bytes_per_call) { LOG(INFO) << title; LOG(INFO) << " bytes per call: " << bytes_per_call; LOG(INFO) << " bytes: " << throughput.bytes; LOG(INFO) << " microseconds: " << throughput.microseconds; LOG(INFO) << " bits per second: " << throughput.bits_per_second; } void PrettyPrint(const std::string& title, const Sampler& samples, size_t per_sample_size) { const auto& percentiles = samples.Percentiles(); LOG(INFO) << title; LOG(INFO) << " Sample Size: " << samples.SampleSize() << " (" << per_sample_size << " bytes)"; LOG(INFO) << " Min (0%): " << percentiles.Get(0).count() << " us"; LOG(INFO) << " 25%: " << percentiles.Get(25).count() << " us"; LOG(INFO) << " Median (50%): " << percentiles.Get(50).count() << " us"; LOG(INFO) << " 75%: " << percentiles.Get(75).count() << " us"; LOG(INFO) << " Max (100%): " << percentiles.Get(100).count() << " us"; } } // namespace widevine