Chromium Code Reviews| 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) |
|
pbos-webrtc
2015/12/21 13:44:43
consider const char* here as well
åsapersson
2015/12/21 16:45:32
Acknowledged.
| |
| 26 : last(sample), total(1) {} | 27 : name_(name), last_(-1), total_(0) {} |
| 27 int last; // Last added sample. | 28 const std::string name_; |
| 28 int total; // Total number of added samples. | 29 int last_; // Last added sample. |
| 30 int total_; // Total number of added samples. | |
| 29 }; | 31 }; |
| 30 | 32 |
| 31 rtc::CriticalSection histogram_crit_; | 33 rtc::CriticalSection histogram_crit_; |
| 32 // Map holding info about added samples to a histogram (mapped by the histogram | 34 // Map holding info about added samples to a histogram (mapped by the histogram |
| 33 // name). | 35 // name). |
| 34 std::map<std::string, SampleInfo> histograms_ GUARDED_BY(histogram_crit_); | 36 std::map<std::string, SampleInfo> histograms_ GUARDED_BY(histogram_crit_); |
| 35 } // namespace | 37 } // namespace |
| 36 | 38 |
| 37 namespace metrics { | 39 namespace metrics { |
| 38 Histogram* HistogramFactoryGetCounts(const std::string& name, int min, int max, | 40 Histogram* HistogramFactoryGetCounts(const std::string& name, int min, int max, |
| 39 int bucket_count) { return NULL; } | 41 int bucket_count) { |
| 42 rtc::CritScope cs(&histogram_crit_); | |
| 43 if (histograms_.find(name) == histograms_.end()) { | |
| 44 histograms_.insert(std::make_pair(name, SampleInfo(name))); | |
| 45 } | |
| 46 auto it = histograms_.find(name); | |
| 47 return reinterpret_cast<Histogram*>(&it->second); | |
| 48 } | |
| 40 | 49 |
| 41 Histogram* HistogramFactoryGetEnumeration(const std::string& name, | 50 Histogram* HistogramFactoryGetEnumeration(const std::string& name, |
| 42 int boundary) { return NULL; } | 51 int boundary) { |
| 52 rtc::CritScope cs(&histogram_crit_); | |
| 53 if (histograms_.find(name) == histograms_.end()) { | |
| 54 histograms_.insert(std::make_pair(name, SampleInfo(name))); | |
| 55 } | |
| 56 auto it = histograms_.find(name); | |
| 57 return reinterpret_cast<Histogram*>(&it->second); | |
| 58 } | |
| 43 | 59 |
| 44 void HistogramAdd( | 60 void HistogramAdd( |
| 45 Histogram* histogram_pointer, const std::string& name, int sample) { | 61 Histogram* histogram_pointer, const std::string& name, int sample) { |
| 46 rtc::CritScope cs(&histogram_crit_); | 62 rtc::CritScope cs(&histogram_crit_); |
| 47 auto it = histograms_.find(name); | 63 SampleInfo* ptr = reinterpret_cast<SampleInfo*>(histogram_pointer); |
| 48 if (it == histograms_.end()) { | 64 // The name should not vary. |
| 49 histograms_.insert(std::make_pair(name, SampleInfo(sample))); | 65 RTC_CHECK(ptr->name_ == name); |
| 50 return; | 66 ptr->last_ = sample; |
| 51 } | 67 ++ptr->total_; |
| 52 it->second.last = sample; | |
| 53 ++it->second.total; | |
| 54 } | 68 } |
| 55 } // namespace metrics | 69 } // namespace metrics |
| 56 | 70 |
| 57 namespace test { | 71 namespace test { |
| 58 int LastHistogramSample(const std::string& name) { | 72 int LastHistogramSample(const std::string& name) { |
| 59 rtc::CritScope cs(&histogram_crit_); | 73 rtc::CritScope cs(&histogram_crit_); |
| 60 const auto it = histograms_.find(name); | 74 const auto it = histograms_.find(name); |
| 61 if (it == histograms_.end()) { | 75 if (it == histograms_.end()) { |
| 62 return -1; | 76 return -1; |
| 63 } | 77 } |
| 64 return it->second.last; | 78 return it->second.last_; |
| 65 } | 79 } |
| 66 | 80 |
| 67 int NumHistogramSamples(const std::string& name) { | 81 int NumHistogramSamples(const std::string& name) { |
| 68 rtc::CritScope cs(&histogram_crit_); | 82 rtc::CritScope cs(&histogram_crit_); |
| 69 const auto it = histograms_.find(name); | 83 const auto it = histograms_.find(name); |
| 70 if (it == histograms_.end()) { | 84 if (it == histograms_.end()) { |
| 71 return 0; | 85 return 0; |
| 72 } | 86 } |
| 73 return it->second.total; | 87 return it->second.total_; |
| 74 } | 88 } |
| 75 | 89 |
| 76 void ClearHistograms() { | 90 void ClearHistograms() { |
| 77 rtc::CritScope cs(&histogram_crit_); | 91 rtc::CritScope cs(&histogram_crit_); |
| 78 histograms_.clear(); | 92 for (auto& it : histograms_) { |
| 93 it.second.last_ = -1; | |
| 94 it.second.total_ = 0; | |
| 95 } | |
| 79 } | 96 } |
| 80 } // namespace test | 97 } // namespace test |
| 81 } // namespace webrtc | 98 } // namespace webrtc |
| 82 | |
| OLD | NEW |