blob: 6fcdb6864f6ee856638999bb5ede895991a98e20 [file] [log] [blame]
Åsa Persson352b2d72015-04-15 18:00:40 +02001/*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/test/histogram.h"
12
13#include <map>
14
Åsa Perssona96f02b2015-04-24 08:52:11 +020015#include "webrtc/base/criticalsection.h"
16#include "webrtc/base/thread_annotations.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010017#include "webrtc/system_wrappers/include/metrics.h"
Åsa Persson352b2d72015-04-15 18:00:40 +020018
19// Test implementation of histogram methods in
Henrik Kjellander98f53512015-10-28 18:17:40 +010020// webrtc/system_wrappers/include/metrics.h.
Åsa Persson352b2d72015-04-15 18:00:40 +020021
22namespace webrtc {
23namespace {
Åsa Perssona96f02b2015-04-24 08:52:11 +020024struct SampleInfo {
25 SampleInfo(int sample)
26 : last(sample), total(1) {}
27 int last; // Last added sample.
28 int total; // Total number of added samples.
29};
30
31rtc::CriticalSection histogram_crit_;
32// Map holding info about added samples to a histogram (mapped by the histogram
33// name).
34std::map<std::string, SampleInfo> histograms_ GUARDED_BY(histogram_crit_);
Åsa Persson352b2d72015-04-15 18:00:40 +020035} // namespace
36
37namespace metrics {
38Histogram* HistogramFactoryGetCounts(const std::string& name, int min, int max,
39 int bucket_count) { return NULL; }
40
41Histogram* HistogramFactoryGetEnumeration(const std::string& name,
42 int boundary) { return NULL; }
43
44void HistogramAdd(
45 Histogram* histogram_pointer, const std::string& name, int sample) {
Åsa Perssona96f02b2015-04-24 08:52:11 +020046 rtc::CritScope cs(&histogram_crit_);
47 auto it = histograms_.find(name);
48 if (it == histograms_.end()) {
49 histograms_.insert(std::make_pair(name, SampleInfo(sample)));
50 return;
51 }
52 it->second.last = sample;
53 ++it->second.total;
Åsa Persson352b2d72015-04-15 18:00:40 +020054}
55} // namespace metrics
56
57namespace test {
58int LastHistogramSample(const std::string& name) {
Åsa Perssona96f02b2015-04-24 08:52:11 +020059 rtc::CritScope cs(&histogram_crit_);
60 const auto it = histograms_.find(name);
Åsa Persson352b2d72015-04-15 18:00:40 +020061 if (it == histograms_.end()) {
62 return -1;
63 }
Åsa Perssona96f02b2015-04-24 08:52:11 +020064 return it->second.last;
65}
66
67int NumHistogramSamples(const std::string& name) {
68 rtc::CritScope cs(&histogram_crit_);
69 const auto it = histograms_.find(name);
70 if (it == histograms_.end()) {
71 return 0;
72 }
73 return it->second.total;
74}
75
76void ClearHistograms() {
77 rtc::CritScope cs(&histogram_crit_);
78 histograms_.clear();
Åsa Persson352b2d72015-04-15 18:00:40 +020079}
80} // namespace test
81} // namespace webrtc
82