Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: net/base/bandwidth_metrics.h

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef NET_BASE_BANDWIDTH_METRICS_H_ 5 #ifndef NET_BASE_BANDWIDTH_METRICS_H_
6 #define NET_BASE_BANDWIDTH_METRICS_H_ 6 #define NET_BASE_BANDWIDTH_METRICS_H_
7 7
8 #include <list> 8 #include <list>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 // tracker.StopStream(); 50 // tracker.StopStream();
51 // 51 //
52 // NOTE: This class is not thread safe. 52 // NOTE: This class is not thread safe.
53 // 53 //
54 class BandwidthMetrics { 54 class BandwidthMetrics {
55 public: 55 public:
56 BandwidthMetrics() 56 BandwidthMetrics()
57 : num_streams_in_progress_(0), 57 : num_streams_in_progress_(0),
58 num_data_samples_(0), 58 num_data_samples_(0),
59 data_sum_(0.0), 59 data_sum_(0.0),
60 bytes_since_last_start_(0) { 60 bytes_since_last_start_(0) {}
61 }
62 61
63 // Get the bandwidth. Returns Kbps (kilo-bits-per-second). 62 // Get the bandwidth. Returns Kbps (kilo-bits-per-second).
64 double bandwidth() const { 63 double bandwidth() const { return data_sum_ / num_data_samples_; }
65 return data_sum_ / num_data_samples_;
66 }
67 64
68 // Record that we've started a stream. 65 // Record that we've started a stream.
69 void StartStream() { 66 void StartStream() {
70 // If we're the only stream, we've finished some idle time. Record a new 67 // If we're the only stream, we've finished some idle time. Record a new
71 // timestamp to indicate the start of data flow. 68 // timestamp to indicate the start of data flow.
72 if (++num_streams_in_progress_ == 1) { 69 if (++num_streams_in_progress_ == 1) {
73 last_start_ = base::TimeTicks::HighResNow(); 70 last_start_ = base::TimeTicks::HighResNow();
74 bytes_since_last_start_ = 0; 71 bytes_since_last_start_ = 0;
75 } 72 }
76 } 73 }
77 74
78 // Track that we've completed a stream. 75 // Track that we've completed a stream.
79 void StopStream() { 76 void StopStream() {
80 if (--num_streams_in_progress_ == 0) { 77 if (--num_streams_in_progress_ == 0) {
81 // We don't use small streams when tracking bandwidth because they are not 78 // We don't use small streams when tracking bandwidth because they are not
82 // precise; imagine a 25 byte stream. The sample is too small to make 79 // precise; imagine a 25 byte stream. The sample is too small to make
83 // a good measurement. 80 // a good measurement.
84 // 20KB is an arbitrary value. We might want to use a lesser value. 81 // 20KB is an arbitrary value. We might want to use a lesser value.
85 static const int64 kRecordSizeThreshold = 20 * 1024; 82 static const int64 kRecordSizeThreshold = 20 * 1024;
86 if (bytes_since_last_start_ < kRecordSizeThreshold) 83 if (bytes_since_last_start_ < kRecordSizeThreshold)
87 return; 84 return;
88 85
89 base::TimeDelta delta = base::TimeTicks::HighResNow() - last_start_; 86 base::TimeDelta delta = base::TimeTicks::HighResNow() - last_start_;
90 double ms = delta.InMillisecondsF(); 87 double ms = delta.InMillisecondsF();
91 if (ms > 0.0) { 88 if (ms > 0.0) {
92 double kbps = static_cast<double>(bytes_since_last_start_) * 8 / ms; 89 double kbps = static_cast<double>(bytes_since_last_start_) * 8 / ms;
93 ++num_data_samples_; 90 ++num_data_samples_;
94 data_sum_ += kbps; 91 data_sum_ += kbps;
95 VLOG(1) << "Bandwidth: " << kbps 92 VLOG(1) << "Bandwidth: " << kbps << "Kbps (avg " << bandwidth()
96 << "Kbps (avg " << bandwidth() << "Kbps)"; 93 << "Kbps)";
97 int kbps_int = static_cast<int>(kbps); 94 int kbps_int = static_cast<int>(kbps);
98 UMA_HISTOGRAM_COUNTS_10000("Net.DownloadBandwidth", kbps_int); 95 UMA_HISTOGRAM_COUNTS_10000("Net.DownloadBandwidth", kbps_int);
99 } 96 }
100 } 97 }
101 } 98 }
102 99
103 // Add a sample of the number of bytes read from the network into the tracker. 100 // Add a sample of the number of bytes read from the network into the tracker.
104 void RecordBytes(int bytes) { 101 void RecordBytes(int bytes) {
105 DCHECK(num_streams_in_progress_); 102 DCHECK(num_streams_in_progress_);
106 bytes_since_last_start_ += static_cast<int64>(bytes); 103 bytes_since_last_start_ += static_cast<int64>(bytes);
107 } 104 }
108 105
109 private: 106 private:
110 int num_streams_in_progress_; // The number of streams in progress. 107 int num_streams_in_progress_; // The number of streams in progress.
111 // TODO(mbelshe): Use a rolling buffer of 30 samples instead of an average. 108 // TODO(mbelshe): Use a rolling buffer of 30 samples instead of an average.
112 int num_data_samples_; // The number of samples collected. 109 int num_data_samples_; // The number of samples collected.
113 double data_sum_; // The sum of all samples collected. 110 double data_sum_; // The sum of all samples collected.
114 int64 bytes_since_last_start_; // Bytes tracked during this "session". 111 int64 bytes_since_last_start_; // Bytes tracked during this "session".
115 base::TimeTicks last_start_; // Timestamp of the begin of this "session". 112 base::TimeTicks last_start_; // Timestamp of the begin of this "session".
116 }; 113 };
117 114
118 // A utility class for managing the lifecycle of a measured stream. 115 // A utility class for managing the lifecycle of a measured stream.
119 // It is important that we not leave unclosed streams, and this class helps 116 // It is important that we not leave unclosed streams, and this class helps
120 // ensure we always stop them. 117 // ensure we always stop them.
121 class ScopedBandwidthMetrics { 118 class ScopedBandwidthMetrics {
122 public: 119 public:
123 ScopedBandwidthMetrics(); 120 ScopedBandwidthMetrics();
124 ~ScopedBandwidthMetrics(); 121 ~ScopedBandwidthMetrics();
125 122
126 void StartStream(); 123 void StartStream();
127 void StopStream(); 124 void StopStream();
128 void RecordBytes(int bytes); 125 void RecordBytes(int bytes);
129 126
130 private: 127 private:
131 bool started_; 128 bool started_;
132 }; 129 };
133 130
134 } // namespace net 131 } // namespace net
135 132
136 #endif // NET_BASE_BANDWIDTH_METRICS_H_ 133 #endif // NET_BASE_BANDWIDTH_METRICS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698