rate_counter_unittest.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2013 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. #include "remoting/base/rate_counter.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include "base/test/simple_test_tick_clock.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace remoting {
  10. static const int64_t kTestValues[] = { 10, 20, 30, 10, 25, 16, 15 };
  11. // One second window and one sample per second, so rate equals each sample.
  12. TEST(RateCounterTest, OneSecondWindow) {
  13. RateCounter rate_counter(base::Seconds(1));
  14. EXPECT_EQ(0, rate_counter.Rate());
  15. base::SimpleTestTickClock tick_clock;
  16. rate_counter.set_tick_clock_for_tests(&tick_clock);
  17. for (size_t i = 0; i < std::size(kTestValues); ++i) {
  18. tick_clock.Advance(base::Seconds(1));
  19. rate_counter.Record(kTestValues[i]);
  20. EXPECT_EQ(static_cast<double>(kTestValues[i]), rate_counter.Rate());
  21. }
  22. }
  23. // Record all samples instantaneously, so the rate is the total of the samples.
  24. TEST(RateCounterTest, OneSecondWindowAllSamples) {
  25. RateCounter rate_counter(base::Seconds(1));
  26. EXPECT_EQ(0, rate_counter.Rate());
  27. base::SimpleTestTickClock tick_clock;
  28. rate_counter.set_tick_clock_for_tests(&tick_clock);
  29. double expected = 0.0;
  30. for (size_t i = 0; i < std::size(kTestValues); ++i) {
  31. rate_counter.Record(kTestValues[i]);
  32. expected += kTestValues[i];
  33. }
  34. EXPECT_EQ(expected, rate_counter.Rate());
  35. }
  36. // Two second window, one sample per second. For all but the first sample, the
  37. // rate should be the average of it and the preceding one. For the first it
  38. // will be the average of the sample with zero.
  39. TEST(RateCounterTest, TwoSecondWindow) {
  40. RateCounter rate_counter(base::Seconds(2));
  41. EXPECT_EQ(0, rate_counter.Rate());
  42. base::SimpleTestTickClock tick_clock;
  43. rate_counter.set_tick_clock_for_tests(&tick_clock);
  44. for (size_t i = 0; i < std::size(kTestValues); ++i) {
  45. tick_clock.Advance(base::Seconds(1));
  46. rate_counter.Record(kTestValues[i]);
  47. double expected = kTestValues[i];
  48. if (i > 0)
  49. expected += kTestValues[i-1];
  50. expected /= 2;
  51. EXPECT_EQ(expected, rate_counter.Rate());
  52. }
  53. }
  54. // Sample over a window one second shorter than the number of samples.
  55. // Rate should be the average of all but the first sample.
  56. TEST(RateCounterTest, LongWindow) {
  57. const size_t kWindowSeconds = std::size(kTestValues) - 1;
  58. RateCounter rate_counter(base::Seconds(kWindowSeconds));
  59. EXPECT_EQ(0, rate_counter.Rate());
  60. base::SimpleTestTickClock tick_clock;
  61. rate_counter.set_tick_clock_for_tests(&tick_clock);
  62. double expected = 0.0;
  63. for (size_t i = 0; i < std::size(kTestValues); ++i) {
  64. tick_clock.Advance(base::Seconds(1));
  65. rate_counter.Record(kTestValues[i]);
  66. if (i != 0)
  67. expected += kTestValues[i];
  68. }
  69. expected /= kWindowSeconds;
  70. EXPECT_EQ(expected, rate_counter.Rate());
  71. }
  72. } // namespace remoting