Add metrics::Samples to facilitate easier testing
Currently, tests that verify metrics use a combination of
metrics::NumSamples and metrics::NumEvents to assert which samples
were recorded and how many times they were recorded. This means
that a comprehensive tests has n + 1 assertions for n distinct
samples.
The new metrics::Samples function returns a map of sample --> num
events which can be asserted against using gmock matchers,
achieving better coverage and better test failure messages in just
one line.
Bug: None
Change-Id: I07d4a766654cfc04e414b77b6de02927683a361f
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/125486
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Steve Anton <steveanton@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26974}
diff --git a/system_wrappers/source/metrics.cc b/system_wrappers/source/metrics.cc
index f98b4cd..2383272 100644
--- a/system_wrappers/source/metrics.cc
+++ b/system_wrappers/source/metrics.cc
@@ -88,6 +88,11 @@
return (info_.samples.empty()) ? -1 : info_.samples.begin()->first;
}
+ std::map<int, int> Samples() const {
+ rtc::CritScope cs(&crit_);
+ return info_.samples;
+ }
+
private:
rtc::CriticalSection crit_;
const int min_;
@@ -162,6 +167,12 @@
return (it == map_.end()) ? -1 : it->second->MinSample();
}
+ std::map<int, int> Samples(const std::string& name) const {
+ rtc::CritScope cs(&crit_);
+ const auto& it = map_.find(name);
+ return (it == map_.end()) ? std::map<int, int>() : it->second->Samples();
+ }
+
private:
rtc::CriticalSection crit_;
std::map<std::string, std::unique_ptr<RtcHistogram>> map_
@@ -307,5 +318,10 @@
return map ? map->MinSample(name) : -1;
}
+std::map<int, int> Samples(const std::string& name) {
+ RtcHistogramMap* map = GetMap();
+ return map ? map->Samples(name) : std::map<int, int>();
+}
+
} // namespace metrics
} // namespace webrtc