OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/test/histogram.h" | 11 #include "webrtc/test/histogram.h" |
12 | 12 |
13 #include <map> | 13 #include <map> |
14 | 14 |
| 15 #include "webrtc/base/checks.h" |
15 #include "webrtc/base/criticalsection.h" | 16 #include "webrtc/base/criticalsection.h" |
16 #include "webrtc/base/thread_annotations.h" | 17 #include "webrtc/base/thread_annotations.h" |
17 #include "webrtc/system_wrappers/include/metrics.h" | 18 #include "webrtc/system_wrappers/include/metrics.h" |
18 | 19 |
19 // Test implementation of histogram methods in | 20 // Test implementation of histogram methods in |
20 // webrtc/system_wrappers/include/metrics.h. | 21 // webrtc/system_wrappers/include/metrics.h. |
21 | 22 |
22 namespace webrtc { | 23 namespace webrtc { |
23 namespace { | 24 namespace { |
24 struct SampleInfo { | 25 struct SampleInfo { |
25 SampleInfo(int sample) | 26 SampleInfo(const std::string& name) : name_(name), last_(-1), total_(0) {} |
26 : last(sample), total(1) {} | 27 const std::string name_; |
27 int last; // Last added sample. | 28 int last_; // Last added sample. |
28 int total; // Total number of added samples. | 29 int total_; // Total number of added samples. |
29 }; | 30 }; |
30 | 31 |
31 rtc::CriticalSection histogram_crit_; | 32 rtc::CriticalSection histogram_crit_; |
32 // Map holding info about added samples to a histogram (mapped by the histogram | 33 // Map holding info about added samples to a histogram (mapped by the histogram |
33 // name). | 34 // name). |
34 std::map<std::string, SampleInfo> histograms_ GUARDED_BY(histogram_crit_); | 35 std::map<std::string, SampleInfo> histograms_ GUARDED_BY(histogram_crit_); |
35 } // namespace | 36 } // namespace |
36 | 37 |
37 namespace metrics { | 38 namespace metrics { |
38 Histogram* HistogramFactoryGetCounts(const std::string& name, int min, int max, | 39 Histogram* HistogramFactoryGetCounts(const std::string& name, int min, int max, |
39 int bucket_count) { return NULL; } | 40 int bucket_count) { |
| 41 rtc::CritScope cs(&histogram_crit_); |
| 42 if (histograms_.find(name) == histograms_.end()) { |
| 43 histograms_.insert(std::make_pair(name, SampleInfo(name))); |
| 44 } |
| 45 auto it = histograms_.find(name); |
| 46 return reinterpret_cast<Histogram*>(&it->second); |
| 47 } |
40 | 48 |
41 Histogram* HistogramFactoryGetEnumeration(const std::string& name, | 49 Histogram* HistogramFactoryGetEnumeration(const std::string& name, |
42 int boundary) { return NULL; } | 50 int boundary) { |
| 51 rtc::CritScope cs(&histogram_crit_); |
| 52 if (histograms_.find(name) == histograms_.end()) { |
| 53 histograms_.insert(std::make_pair(name, SampleInfo(name))); |
| 54 } |
| 55 auto it = histograms_.find(name); |
| 56 return reinterpret_cast<Histogram*>(&it->second); |
| 57 } |
43 | 58 |
44 void HistogramAdd( | 59 void HistogramAdd( |
45 Histogram* histogram_pointer, const std::string& name, int sample) { | 60 Histogram* histogram_pointer, const std::string& name, int sample) { |
46 rtc::CritScope cs(&histogram_crit_); | 61 rtc::CritScope cs(&histogram_crit_); |
47 auto it = histograms_.find(name); | 62 SampleInfo* ptr = reinterpret_cast<SampleInfo*>(histogram_pointer); |
48 if (it == histograms_.end()) { | 63 // The name should not vary. |
49 histograms_.insert(std::make_pair(name, SampleInfo(sample))); | 64 RTC_CHECK(ptr->name_ == name); |
50 return; | 65 ptr->last_ = sample; |
51 } | 66 ++ptr->total_; |
52 it->second.last = sample; | |
53 ++it->second.total; | |
54 } | 67 } |
55 } // namespace metrics | 68 } // namespace metrics |
56 | 69 |
57 namespace test { | 70 namespace test { |
58 int LastHistogramSample(const std::string& name) { | 71 int LastHistogramSample(const std::string& name) { |
59 rtc::CritScope cs(&histogram_crit_); | 72 rtc::CritScope cs(&histogram_crit_); |
60 const auto it = histograms_.find(name); | 73 const auto it = histograms_.find(name); |
61 if (it == histograms_.end()) { | 74 if (it == histograms_.end()) { |
62 return -1; | 75 return -1; |
63 } | 76 } |
64 return it->second.last; | 77 return it->second.last_; |
65 } | 78 } |
66 | 79 |
67 int NumHistogramSamples(const std::string& name) { | 80 int NumHistogramSamples(const std::string& name) { |
68 rtc::CritScope cs(&histogram_crit_); | 81 rtc::CritScope cs(&histogram_crit_); |
69 const auto it = histograms_.find(name); | 82 const auto it = histograms_.find(name); |
70 if (it == histograms_.end()) { | 83 if (it == histograms_.end()) { |
71 return 0; | 84 return 0; |
72 } | 85 } |
73 return it->second.total; | 86 return it->second.total_; |
74 } | 87 } |
75 | 88 |
76 void ClearHistograms() { | 89 void ClearHistograms() { |
77 rtc::CritScope cs(&histogram_crit_); | 90 rtc::CritScope cs(&histogram_crit_); |
78 histograms_.clear(); | 91 for (auto& it : histograms_) { |
| 92 it.second.last_ = -1; |
| 93 it.second.total_ = 0; |
| 94 } |
79 } | 95 } |
80 } // namespace test | 96 } // namespace test |
81 } // namespace webrtc | 97 } // namespace webrtc |
82 | |
OLD | NEW |