1 // Copyright 2018 The Chromium Authors
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/profiler/periodic_sampling_scheduler.h"
6
7 #include "base/test/bind.h"
8 #include "base/test/simple_test_tick_clock.h"
9 #include "base/time/time.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace base {
13 namespace {
14
15 class TestScheduler : public PeriodicSamplingScheduler {
16 public:
TestScheduler(TimeDelta sampling_duration,double fraction_of_execution_time_to_sample)17 TestScheduler(TimeDelta sampling_duration,
18 double fraction_of_execution_time_to_sample)
19 : PeriodicSamplingScheduler(sampling_duration,
20 fraction_of_execution_time_to_sample,
21 kStartTime) {
22 tick_clock_.SetNowTicks(kStartTime);
23 }
24
25 TestScheduler(const TestScheduler&) = delete;
26 TestScheduler& operator=(const TestScheduler&) = delete;
27
RandDouble() const28 double RandDouble() const override { return rand_double_value_; }
Now() const29 TimeTicks Now() const override { return tick_clock_.NowTicks(); }
30
SetRandDouble(double value)31 void SetRandDouble(double value) { rand_double_value_ = value; }
tick_clock()32 SimpleTestTickClock& tick_clock() { return tick_clock_; }
33
34 private:
35 static constexpr TimeTicks kStartTime = TimeTicks();
36 SimpleTestTickClock tick_clock_;
37 double rand_double_value_ = 0.0;
38 };
39
40 constexpr TimeTicks TestScheduler::kStartTime;
41
TEST(PeriodicSamplingSchedulerTest,ScheduleCollections)42 TEST(PeriodicSamplingSchedulerTest, ScheduleCollections) {
43 const TimeDelta sampling_duration = Seconds(30);
44 const double fraction_of_execution_time_to_sample = 0.01;
45
46 const TimeDelta expected_period =
47 sampling_duration / fraction_of_execution_time_to_sample;
48
49 TestScheduler scheduler(sampling_duration,
50 fraction_of_execution_time_to_sample);
51
52 // The first collection should be exactly at the start time, since the random
53 // value is 0.0.
54 scheduler.SetRandDouble(0.0);
55 EXPECT_EQ(Seconds(0), scheduler.GetTimeToNextCollection());
56
57 // With a random value of 1.0 the second collection should be at the end of
58 // the second period.
59 scheduler.SetRandDouble(1.0);
60 EXPECT_EQ(2 * expected_period - sampling_duration,
61 scheduler.GetTimeToNextCollection());
62
63 // With a random value of 0.25 the second collection should be a quarter into
64 // the third period exclusive of the sampling duration.
65 scheduler.SetRandDouble(0.25);
66 EXPECT_EQ(2 * expected_period + 0.25 * (expected_period - sampling_duration),
67 scheduler.GetTimeToNextCollection());
68 }
69
TEST(PeriodicSamplingSchedulerTest,ScheduleWithJumpInTimeTicks)70 TEST(PeriodicSamplingSchedulerTest, ScheduleWithJumpInTimeTicks) {
71 const TimeDelta sampling_duration = Seconds(30);
72 const double fraction_of_execution_time_to_sample = 0.01;
73
74 const TimeDelta expected_period =
75 sampling_duration / fraction_of_execution_time_to_sample;
76
77 TestScheduler scheduler(sampling_duration,
78 fraction_of_execution_time_to_sample);
79
80 // The first collection should be exactly at the start time, since the random
81 // value is 0.0.
82 scheduler.SetRandDouble(0.0);
83 EXPECT_EQ(Seconds(0), scheduler.GetTimeToNextCollection());
84
85 // Simulate a non-continuous jump in the current TimeTicks such that the next
86 // period would start before the current time. In this case the
87 // period start should be reset to the current time, and the next collection
88 // chosen within that period.
89 scheduler.tick_clock().Advance(expected_period + Seconds(1));
90 scheduler.SetRandDouble(0.5);
91 EXPECT_EQ(0.5 * (expected_period - sampling_duration),
92 scheduler.GetTimeToNextCollection());
93 }
94
95 } // namespace
96 } // namespace base
97