| OLD | NEW |
| 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 // Get the bandwidth. Returns Kbps (kilo-bits-per-second). | 63 // Get the bandwidth. Returns Kbps (kilo-bits-per-second). |
| 64 double bandwidth() const { | 64 double bandwidth() const { |
| 65 return data_sum_ / num_data_samples_; | 65 return data_sum_ / num_data_samples_; |
| 66 } | 66 } |
| 67 | 67 |
| 68 // Record that we've started a stream. | 68 // Record that we've started a stream. |
| 69 void StartStream() { | 69 void StartStream() { |
| 70 // If we're the only stream, we've finished some idle time. Record a new | 70 // If we're the only stream, we've finished some idle time. Record a new |
| 71 // timestamp to indicate the start of data flow. | 71 // timestamp to indicate the start of data flow. |
| 72 if (++num_streams_in_progress_ == 1) { | 72 if (++num_streams_in_progress_ == 1) { |
| 73 last_start_ = base::TimeTicks::HighResNow(); | 73 last_start_ = base::TimeTicks::Now(); |
| 74 bytes_since_last_start_ = 0; | 74 bytes_since_last_start_ = 0; |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 | 77 |
| 78 // Track that we've completed a stream. | 78 // Track that we've completed a stream. |
| 79 void StopStream() { | 79 void StopStream() { |
| 80 if (--num_streams_in_progress_ == 0) { | 80 if (--num_streams_in_progress_ == 0) { |
| 81 // We don't use small streams when tracking bandwidth because they are not | 81 // 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 | 82 // precise; imagine a 25 byte stream. The sample is too small to make |
| 83 // a good measurement. | 83 // a good measurement. |
| 84 // 20KB is an arbitrary value. We might want to use a lesser value. | 84 // 20KB is an arbitrary value. We might want to use a lesser value. |
| 85 static const int64 kRecordSizeThreshold = 20 * 1024; | 85 static const int64 kRecordSizeThreshold = 20 * 1024; |
| 86 if (bytes_since_last_start_ < kRecordSizeThreshold) | 86 if (bytes_since_last_start_ < kRecordSizeThreshold) |
| 87 return; | 87 return; |
| 88 | 88 |
| 89 base::TimeDelta delta = base::TimeTicks::HighResNow() - last_start_; | 89 base::TimeDelta delta = base::TimeTicks::Now() - last_start_; |
| 90 double ms = delta.InMillisecondsF(); | 90 double ms = delta.InMillisecondsF(); |
| 91 if (ms > 0.0) { | 91 if (ms > 0.0) { |
| 92 double kbps = static_cast<double>(bytes_since_last_start_) * 8 / ms; | 92 double kbps = static_cast<double>(bytes_since_last_start_) * 8 / ms; |
| 93 ++num_data_samples_; | 93 ++num_data_samples_; |
| 94 data_sum_ += kbps; | 94 data_sum_ += kbps; |
| 95 VLOG(1) << "Bandwidth: " << kbps | 95 VLOG(1) << "Bandwidth: " << kbps |
| 96 << "Kbps (avg " << bandwidth() << "Kbps)"; | 96 << "Kbps (avg " << bandwidth() << "Kbps)"; |
| 97 int kbps_int = static_cast<int>(kbps); | 97 int kbps_int = static_cast<int>(kbps); |
| 98 UMA_HISTOGRAM_COUNTS_10000("Net.DownloadBandwidth", kbps_int); | 98 UMA_HISTOGRAM_COUNTS_10000("Net.DownloadBandwidth", kbps_int); |
| 99 } | 99 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 127 void StopStream(); | 127 void StopStream(); |
| 128 void RecordBytes(int bytes); | 128 void RecordBytes(int bytes); |
| 129 | 129 |
| 130 private: | 130 private: |
| 131 bool started_; | 131 bool started_; |
| 132 }; | 132 }; |
| 133 | 133 |
| 134 } // namespace net | 134 } // namespace net |
| 135 | 135 |
| 136 #endif // NET_BASE_BANDWIDTH_METRICS_H_ | 136 #endif // NET_BASE_BANDWIDTH_METRICS_H_ |
| OLD | NEW |