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