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

Side by Side Diff: remoting/base/running_average.cc

Issue 6736009: Measure bandwidth for chromoting video channel (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 9 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/logging.h"
6 #include "remoting/base/running_average.h"
7
8 namespace remoting {
9
10 RunningAverage::RunningAverage(int window_size)
11 : window_size_(window_size),
12 sum_(0) {
13 CHECK(window_size_);
14 }
15
16 RunningAverage::~RunningAverage() {
17 }
18
19 void RunningAverage::Record(int64 value) {
20 base::AutoLock auto_lock(lock_);
21
22 data_points_.push_back(value);
23 sum_ += value;
24
25 if (data_points_.size() > window_size_) {
26 sum_ -= data_points_[0];
27 data_points_.pop_front();
28 }
29 }
30
31 int64 RunningAverage::Average() {
32 base::AutoLock auto_lock(lock_);
33
34 if (data_points_.empty())
35 return 0;
36 return sum_ / data_points_.size();
37 }
38
39 TimedRunningAverage::TimedRunningAverage(int window_size)
40 : counter_(window_size),
41 window_size_(window_size) {
42 }
43
44 TimedRunningAverage::~TimedRunningAverage() {
45 }
46
47 void TimedRunningAverage::Record(int64 value) {
48 {
49 base::AutoLock auto_lock(lock_);
50 time_.push_back(base::Time::Now());
51
52 if (time_.size() > window_size_)
53 time_.pop_front();
54 }
55 counter_.Record(value);
56 }
57
58 double TimedRunningAverage::Rate() {
59 int64 average = counter_.Average();
60 base::TimeDelta delta;
61
62 {
63 base::AutoLock auto_lock(lock_);
64 if (!time_.empty())
65 delta = *time_.rbegin() - *time_.begin();
66 }
67
68 if (delta.InMilliseconds())
69 return 1000.0 * average / delta.InMilliseconds();
simonmorris 2011/03/25 09:24:59 I'm not sure the math is right here. For a rate, d
Wez 2011/03/25 10:43:19 Yes. The rate (bandwidth in our case) would norma
70 return 0;
71 }
72
73 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698