summaryrefslogtreecommitdiffstats
path: root/remoting/base
diff options
context:
space:
mode:
authoryuweih <yuweih@chromium.org>2016-03-23 15:41:05 -0700
committerCommit bot <commit-bot@chromium.org>2016-03-23 22:43:12 +0000
commitd2a0c5cb1217570e7b3da52415f7326f48928053 (patch)
tree5e09f3caa5a6e6824e0b1ae9ec5b320532422b39 /remoting/base
parent7d9e04b799588c9cce868eae6ab7570514600895 (diff)
downloadchromium_src-d2a0c5cb1217570e7b3da52415f7326f48928053.zip
chromium_src-d2a0c5cb1217570e7b3da52415f7326f48928053.tar.gz
chromium_src-d2a0c5cb1217570e7b3da52415f7326f48928053.tar.bz2
Show max latency on client's status bar
Will later also consider storing max latency data in server BUG=560950 Committed: https://crrev.com/e37a467453f3814a9ecb0eb6769bd7c58b61cbae Cr-Commit-Position: refs/heads/master@{#382663} Review URL: https://codereview.chromium.org/1811833002 Cr-Commit-Position: refs/heads/master@{#382957}
Diffstat (limited to 'remoting/base')
-rw-r--r--remoting/base/BUILD.gn2
-rw-r--r--remoting/base/running_average_unittest.cc60
-rw-r--r--remoting/base/running_samples.cc (renamed from remoting/base/running_average.cc)26
-rw-r--r--remoting/base/running_samples.h (renamed from remoting/base/running_average.h)30
-rw-r--r--remoting/base/running_samples_unittest.cc92
5 files changed, 129 insertions, 81 deletions
diff --git a/remoting/base/BUILD.gn b/remoting/base/BUILD.gn
index 8a039e1..b8577c1 100644
--- a/remoting/base/BUILD.gn
+++ b/remoting/base/BUILD.gn
@@ -65,7 +65,7 @@ source_set("unit_tests") {
"rate_counter_unittest.cc",
"rsa_key_pair_unittest.cc",
"run_all_unittests.cc",
- "running_average_unittest.cc",
+ "running_samples_unittest.cc",
"test_rsa_key_pair.h",
"typed_buffer_unittest.cc",
"util_unittest.cc",
diff --git a/remoting/base/running_average_unittest.cc b/remoting/base/running_average_unittest.cc
deleted file mode 100644
index e731573..0000000
--- a/remoting/base/running_average_unittest.cc
+++ /dev/null
@@ -1,60 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include <stddef.h>
-#include <stdint.h>
-
-#include "base/macros.h"
-#include "remoting/base/running_average.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace remoting {
-
-static const int64_t kTestValues[] = { 10, 20, 30, 10, 25, 16, 15 };
-
-// Average across a single element, i.e. just return the most recent.
-TEST(RunningAverageTest, OneElementWindow) {
- RunningAverage running_average(1);
- EXPECT_EQ(0, running_average.Average());
-
- for (size_t i = 0; i < arraysize(kTestValues); ++i) {
- running_average.Record(kTestValues[i]);
- EXPECT_EQ(static_cast<double>(kTestValues[i]), running_average.Average());
- }
-}
-
-// Average the two most recent elements.
-TEST(RunningAverageTest, TwoElementWindow) {
- RunningAverage running_average(2);
- EXPECT_EQ(0, running_average.Average());
-
- for (size_t i = 0; i < arraysize(kTestValues); ++i) {
- running_average.Record(kTestValues[i]);
-
- double expected = kTestValues[i];
- if (i > 0)
- expected = (expected + kTestValues[i-1]) / 2;
-
- EXPECT_EQ(expected, running_average.Average());
- }
-}
-
-// Average across all the elements if the window size exceeds the element count.
-TEST(RunningAverageTest, LongWindow) {
- RunningAverage running_average(arraysize(kTestValues) + 1);
- EXPECT_EQ(0, running_average.Average());
-
- for (size_t i = 0; i < arraysize(kTestValues); ++i) {
- running_average.Record(kTestValues[i]);
-
- double expected = 0.0;
- for (size_t j = 0; j <= i; ++j)
- expected += kTestValues[j];
- expected /= i + 1;
-
- EXPECT_EQ(expected, running_average.Average());
- }
-}
-
-} // namespace remoting
diff --git a/remoting/base/running_average.cc b/remoting/base/running_samples.cc
index 5e24c0d..d76a1a6 100644
--- a/remoting/base/running_average.cc
+++ b/remoting/base/running_samples.cc
@@ -1,22 +1,23 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "remoting/base/running_average.h"
+#include "remoting/base/running_samples.h"
+
+#include <algorithm>
#include "base/logging.h"
namespace remoting {
-RunningAverage::RunningAverage(int window_size)
- : window_size_(window_size),
- sum_(0) {
+RunningSamples::RunningSamples(int window_size)
+ : window_size_(window_size) {
DCHECK_GT(window_size, 0);
}
-RunningAverage::~RunningAverage() {}
+RunningSamples::~RunningSamples() {}
-void RunningAverage::Record(int64_t value) {
+void RunningSamples::Record(int64_t value) {
DCHECK(thread_checker_.CalledOnValidThread());
data_points_.push_back(value);
@@ -28,7 +29,7 @@ void RunningAverage::Record(int64_t value) {
}
}
-double RunningAverage::Average() {
+double RunningSamples::Average() const {
DCHECK(thread_checker_.CalledOnValidThread());
if (data_points_.empty())
@@ -36,4 +37,13 @@ double RunningAverage::Average() {
return static_cast<double>(sum_) / data_points_.size();
}
+int64_t RunningSamples::Max() const {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ if (data_points_.empty())
+ return 0;
+
+ return *std::max_element(data_points_.begin(), data_points_.end());
+}
+
} // namespace remoting
diff --git a/remoting/base/running_average.h b/remoting/base/running_samples.h
index e41234e..3f5078d 100644
--- a/remoting/base/running_average.h
+++ b/remoting/base/running_samples.h
@@ -1,9 +1,9 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef REMOTING_BASE_RUNNING_AVERAGE_H_
-#define REMOTING_BASE_RUNNING_AVERAGE_H_
+#ifndef REMOTING_BASE_RUNNING_SAMPLES_H_
+#define REMOTING_BASE_RUNNING_SAMPLES_H_
#include <stddef.h>
#include <stdint.h>
@@ -15,20 +15,26 @@
namespace remoting {
-// Calculates the average of the most recent N recorded samples.
+// Calculates the maximum or average of the most recent N recorded samples.
// This is typically used to smooth out random variation in point samples
// over bandwidth, frame rate, etc.
-class RunningAverage {
+class RunningSamples {
public:
- // Constructs a helper to average over the |window_size| most recent samples.
- explicit RunningAverage(int window_size);
- virtual ~RunningAverage();
+ // Constructs a running sample helper that stores |window_size| most
+ // recent samples.
+ explicit RunningSamples(int window_size);
+ virtual ~RunningSamples();
// Records a point sample.
void Record(int64_t value);
// Returns the average over up to |window_size| of the most recent samples.
- double Average();
+ // 0 if no sample available
+ double Average() const;
+
+ // Returns the max over up to |window_size| of the most recent samples.
+ // 0 if no sample available
+ int64_t Max() const;
private:
// Stores the desired window size, as size_t to avoid casting when comparing
@@ -39,13 +45,13 @@ class RunningAverage {
std::deque<int64_t> data_points_;
// Holds the sum of the samples in |data_points_|.
- int64_t sum_;
+ int64_t sum_ = 0;
base::ThreadChecker thread_checker_;
- DISALLOW_COPY_AND_ASSIGN(RunningAverage);
+ DISALLOW_COPY_AND_ASSIGN(RunningSamples);
};
} // namespace remoting
-#endif // REMOTING_BASE_RUNNING_AVERAGE_H_
+#endif // REMOTING_BASE_RUNNING_SAMPLES_H_
diff --git a/remoting/base/running_samples_unittest.cc b/remoting/base/running_samples_unittest.cc
new file mode 100644
index 0000000..2c2c7f5
--- /dev/null
+++ b/remoting/base/running_samples_unittest.cc
@@ -0,0 +1,92 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "base/macros.h"
+#include "remoting/base/running_samples.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace remoting {
+
+typedef void (*TestFunction)(size_t i, RunningSamples& samples);
+
+static const int64_t kTestValues[] = { 10, 20, 30, 10, 25, 16, 15 };
+
+// Test framework that verifies average() and max() at beginning, iterates
+// through all elements and meanwhile calls your own test function
+static void TestFramework(int windowSize, TestFunction testFn) {
+ RunningSamples samples(windowSize);
+ EXPECT_EQ(0, samples.Average());
+ EXPECT_EQ(0, samples.Max());
+
+ for (size_t i = 0; i < arraysize(kTestValues); ++i) {
+ samples.Record(kTestValues[i]);
+ testFn(i, samples);
+ }
+}
+
+// Average across a single element, i.e. just return the most recent.
+TEST(RunningSamplesTest, AverageOneElementWindow) {
+ TestFramework(1, [](size_t i, RunningSamples& samples) {
+ EXPECT_EQ(static_cast<double>(kTestValues[i]), samples.Average());
+ });
+}
+
+// Average the two most recent elements.
+TEST(RunningSamplesTest, AverageTwoElementWindow) {
+ TestFramework(2, [](size_t i, RunningSamples& samples) {
+ double expected = kTestValues[i];
+ if (i > 0)
+ expected = (expected + kTestValues[i-1]) / 2;
+
+ EXPECT_EQ(expected, samples.Average());
+ });
+}
+
+// Average across all the elements if the window size exceeds the element count.
+TEST(RunningSamplesTest, AverageLongWindow) {
+ TestFramework(arraysize(kTestValues) + 1,
+ [](size_t i, RunningSamples& samples) {
+ double expected = 0.0;
+ for (size_t j = 0; j <= i; ++j)
+ expected += kTestValues[j];
+ expected /= i + 1;
+
+ EXPECT_EQ(expected, samples.Average());
+ });
+}
+
+// Max of a single element, i.e. just return the most recent.
+TEST(RunningSamplesTest, MaxOneElementWindow) {
+ TestFramework(1, [](size_t i, RunningSamples& samples) {
+ EXPECT_EQ(static_cast<double>(kTestValues[i]), samples.Max());
+ });
+}
+
+// Max of the two most recent elements.
+TEST(RunningSamplesTest, MaxTwoElementWindow) {
+ TestFramework(2, [](size_t i, RunningSamples& samples) {
+ double expected = kTestValues[i];
+ if (i > 0)
+ expected = expected > kTestValues[i-1] ? expected : kTestValues[i-1];
+
+ EXPECT_EQ(expected, samples.Max());
+ });
+}
+
+// Max of all the elements if the window size exceeds the element count.
+TEST(RunningSamplesTest, MaxLongWindow) {
+ TestFramework(arraysize(kTestValues) + 1,
+ [](size_t i, RunningSamples& samples) {
+ int64_t expected = -1;
+ for (size_t j = 0; j <= i; ++j)
+ expected = expected > kTestValues[j] ? expected : kTestValues[j];
+
+ EXPECT_EQ(expected, samples.Max());
+ });
+}
+
+} // namespace remoting \ No newline at end of file