Åsa Persson | 352b2d7 | 2015-04-15 18:00:40 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame^] | 15 | #include "webrtc/base/checks.h" |
Åsa Persson | a96f02b | 2015-04-24 08:52:11 +0200 | [diff] [blame] | 16 | #include "webrtc/base/criticalsection.h" |
| 17 | #include "webrtc/base/thread_annotations.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 18 | #include "webrtc/system_wrappers/include/metrics.h" |
Åsa Persson | 352b2d7 | 2015-04-15 18:00:40 +0200 | [diff] [blame] | 19 | |
| 20 | // Test implementation of histogram methods in |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 21 | // webrtc/system_wrappers/include/metrics.h. |
Åsa Persson | 352b2d7 | 2015-04-15 18:00:40 +0200 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | namespace { |
Åsa Persson | a96f02b | 2015-04-24 08:52:11 +0200 | [diff] [blame] | 25 | struct SampleInfo { |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame^] | 26 | SampleInfo(const std::string& name) : name_(name), last_(-1), total_(0) {} |
| 27 | const std::string name_; |
| 28 | int last_; // Last added sample. |
| 29 | int total_; // Total number of added samples. |
Åsa Persson | a96f02b | 2015-04-24 08:52:11 +0200 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | rtc::CriticalSection histogram_crit_; |
| 33 | // Map holding info about added samples to a histogram (mapped by the histogram |
| 34 | // name). |
| 35 | std::map<std::string, SampleInfo> histograms_ GUARDED_BY(histogram_crit_); |
Åsa Persson | 352b2d7 | 2015-04-15 18:00:40 +0200 | [diff] [blame] | 36 | } // namespace |
| 37 | |
| 38 | namespace metrics { |
| 39 | Histogram* HistogramFactoryGetCounts(const std::string& name, int min, int max, |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame^] | 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 | } |
Åsa Persson | 352b2d7 | 2015-04-15 18:00:40 +0200 | [diff] [blame] | 48 | |
| 49 | Histogram* HistogramFactoryGetEnumeration(const std::string& name, |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame^] | 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 | } |
Åsa Persson | 352b2d7 | 2015-04-15 18:00:40 +0200 | [diff] [blame] | 58 | |
| 59 | void HistogramAdd( |
| 60 | Histogram* histogram_pointer, const std::string& name, int sample) { |
Åsa Persson | a96f02b | 2015-04-24 08:52:11 +0200 | [diff] [blame] | 61 | rtc::CritScope cs(&histogram_crit_); |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame^] | 62 | SampleInfo* ptr = reinterpret_cast<SampleInfo*>(histogram_pointer); |
| 63 | // The name should not vary. |
| 64 | RTC_CHECK(ptr->name_ == name); |
| 65 | ptr->last_ = sample; |
| 66 | ++ptr->total_; |
Åsa Persson | 352b2d7 | 2015-04-15 18:00:40 +0200 | [diff] [blame] | 67 | } |
| 68 | } // namespace metrics |
| 69 | |
| 70 | namespace test { |
| 71 | int LastHistogramSample(const std::string& name) { |
Åsa Persson | a96f02b | 2015-04-24 08:52:11 +0200 | [diff] [blame] | 72 | rtc::CritScope cs(&histogram_crit_); |
| 73 | const auto it = histograms_.find(name); |
Åsa Persson | 352b2d7 | 2015-04-15 18:00:40 +0200 | [diff] [blame] | 74 | if (it == histograms_.end()) { |
| 75 | return -1; |
| 76 | } |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame^] | 77 | return it->second.last_; |
Åsa Persson | a96f02b | 2015-04-24 08:52:11 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | int NumHistogramSamples(const std::string& name) { |
| 81 | rtc::CritScope cs(&histogram_crit_); |
| 82 | const auto it = histograms_.find(name); |
| 83 | if (it == histograms_.end()) { |
| 84 | return 0; |
| 85 | } |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame^] | 86 | return it->second.total_; |
Åsa Persson | a96f02b | 2015-04-24 08:52:11 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void ClearHistograms() { |
| 90 | rtc::CritScope cs(&histogram_crit_); |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame^] | 91 | for (auto& it : histograms_) { |
| 92 | it.second.last_ = -1; |
| 93 | it.second.total_ = 0; |
| 94 | } |
Åsa Persson | 352b2d7 | 2015-04-15 18:00:40 +0200 | [diff] [blame] | 95 | } |
| 96 | } // namespace test |
| 97 | } // namespace webrtc |