diff options
Diffstat (limited to 'remoting/base')
-rw-r--r-- | remoting/base/BUILD.gn | 2 | ||||
-rw-r--r-- | remoting/base/running_average.cc (renamed from remoting/base/running_samples.cc) | 26 | ||||
-rw-r--r-- | remoting/base/running_average.h (renamed from remoting/base/running_samples.h) | 30 | ||||
-rw-r--r-- | remoting/base/running_average_unittest.cc | 60 | ||||
-rw-r--r-- | remoting/base/running_samples_unittest.cc | 92 |
5 files changed, 81 insertions, 129 deletions
diff --git a/remoting/base/BUILD.gn b/remoting/base/BUILD.gn index 23dcad1..97dc3aa 100644 --- a/remoting/base/BUILD.gn +++ b/remoting/base/BUILD.gn @@ -66,7 +66,7 @@ source_set("unit_tests") { "rate_counter_unittest.cc", "rsa_key_pair_unittest.cc", "run_all_unittests.cc", - "running_samples_unittest.cc", + "running_average_unittest.cc", "test_rsa_key_pair.h", "typed_buffer_unittest.cc", "util_unittest.cc", diff --git a/remoting/base/running_samples.cc b/remoting/base/running_average.cc index d76a1a6..5e24c0d 100644 --- a/remoting/base/running_samples.cc +++ b/remoting/base/running_average.cc @@ -1,23 +1,22 @@ -// Copyright 2016 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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_samples.h" - -#include <algorithm> +#include "remoting/base/running_average.h" #include "base/logging.h" namespace remoting { -RunningSamples::RunningSamples(int window_size) - : window_size_(window_size) { +RunningAverage::RunningAverage(int window_size) + : window_size_(window_size), + sum_(0) { DCHECK_GT(window_size, 0); } -RunningSamples::~RunningSamples() {} +RunningAverage::~RunningAverage() {} -void RunningSamples::Record(int64_t value) { +void RunningAverage::Record(int64_t value) { DCHECK(thread_checker_.CalledOnValidThread()); data_points_.push_back(value); @@ -29,7 +28,7 @@ void RunningSamples::Record(int64_t value) { } } -double RunningSamples::Average() const { +double RunningAverage::Average() { DCHECK(thread_checker_.CalledOnValidThread()); if (data_points_.empty()) @@ -37,13 +36,4 @@ double RunningSamples::Average() const { 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_samples.h b/remoting/base/running_average.h index 3f5078d..e41234e 100644 --- a/remoting/base/running_samples.h +++ b/remoting/base/running_average.h @@ -1,9 +1,9 @@ -// Copyright 2016 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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_SAMPLES_H_ -#define REMOTING_BASE_RUNNING_SAMPLES_H_ +#ifndef REMOTING_BASE_RUNNING_AVERAGE_H_ +#define REMOTING_BASE_RUNNING_AVERAGE_H_ #include <stddef.h> #include <stdint.h> @@ -15,26 +15,20 @@ namespace remoting { -// Calculates the maximum or average of the most recent N recorded samples. +// Calculates the 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 RunningSamples { +class RunningAverage { public: - // Constructs a running sample helper that stores |window_size| most - // recent samples. - explicit RunningSamples(int window_size); - virtual ~RunningSamples(); + // Constructs a helper to average over the |window_size| most recent samples. + explicit RunningAverage(int window_size); + virtual ~RunningAverage(); // Records a point sample. void Record(int64_t value); // Returns the average over up to |window_size| of the most recent samples. - // 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; + double Average(); private: // Stores the desired window size, as size_t to avoid casting when comparing @@ -45,13 +39,13 @@ class RunningSamples { std::deque<int64_t> data_points_; // Holds the sum of the samples in |data_points_|. - int64_t sum_ = 0; + int64_t sum_; base::ThreadChecker thread_checker_; - DISALLOW_COPY_AND_ASSIGN(RunningSamples); + DISALLOW_COPY_AND_ASSIGN(RunningAverage); }; } // namespace remoting -#endif // REMOTING_BASE_RUNNING_SAMPLES_H_ +#endif // REMOTING_BASE_RUNNING_AVERAGE_H_ diff --git a/remoting/base/running_average_unittest.cc b/remoting/base/running_average_unittest.cc new file mode 100644 index 0000000..e731573 --- /dev/null +++ b/remoting/base/running_average_unittest.cc @@ -0,0 +1,60 @@ +// 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_samples_unittest.cc b/remoting/base/running_samples_unittest.cc deleted file mode 100644 index 2c2c7f5..0000000 --- a/remoting/base/running_samples_unittest.cc +++ /dev/null @@ -1,92 +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_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 |