Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Richard Townsend | 5d0b477 | 2019-03-06 13:33:30 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Gabriel Charette | 1894708 | 2019-08-23 02:05:33 | [diff] [blame] | 5 | #include "base/timer/lap_timer.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 6 | |
| 7 | #include "base/test/task_environment.h" |
Gabriel Charette | b69fcd4 | 2019-08-23 02:13:29 | [diff] [blame] | 8 | #include "base/time/time.h" |
Richard Townsend | 5d0b477 | 2019-03-06 13:33:30 | [diff] [blame] | 9 | #include "build/build_config.h" |
| 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | // This file contains a minimal unit test for LapTimer, used for benchmarking. |
| 13 | // This file is supposed to match closely with the example code, documented in |
| 14 | // lap_timer.h. Please update that documentation if you need to change things. |
| 15 | |
Peter Kasting | 811504a7 | 2025-01-09 03:18:50 | [diff] [blame] | 16 | namespace base::test { |
Richard Townsend | 5d0b477 | 2019-03-06 13:33:30 | [diff] [blame] | 17 | |
| 18 | namespace { |
| 19 | |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 20 | constexpr base::TimeDelta kTimeLimit = base::Milliseconds(15); |
| 21 | constexpr base::TimeDelta kTimeAdvance = base::Milliseconds(1); |
Richard Townsend | 5d0b477 | 2019-03-06 13:33:30 | [diff] [blame] | 22 | constexpr int kWarmupRuns = 5; |
| 23 | constexpr int kTimeCheckInterval = 10; |
| 24 | |
| 25 | } // namespace |
| 26 | |
| 27 | TEST(LapTimer, UsageExample) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 28 | TaskEnvironment task_environment(TaskEnvironment::TimeSource::MOCK_TIME); |
Richard Townsend | 5d0b477 | 2019-03-06 13:33:30 | [diff] [blame] | 29 | |
Richard Townsend | 5d0b477 | 2019-03-06 13:33:30 | [diff] [blame] | 30 | LapTimer timer(kWarmupRuns, kTimeLimit, kTimeCheckInterval); |
| 31 | |
| 32 | EXPECT_FALSE(timer.HasTimeLimitExpired()); |
| 33 | EXPECT_FALSE(timer.IsWarmedUp()); |
| 34 | |
| 35 | do { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 36 | task_environment.FastForwardBy(kTimeAdvance); |
Richard Townsend | 5d0b477 | 2019-03-06 13:33:30 | [diff] [blame] | 37 | timer.NextLap(); |
| 38 | } while (!timer.HasTimeLimitExpired()); |
| 39 | |
| 40 | EXPECT_NEAR(timer.LapsPerSecond(), 1000, 0.1); |
| 41 | EXPECT_NEAR(timer.TimePerLap().InMillisecondsF(), 1.0f, 0.1); |
| 42 | // Output number of laps is 20, because the warm up runs are ignored and the |
| 43 | // timer is only checked every kTimeInterval laps. |
| 44 | EXPECT_EQ(timer.NumLaps(), 20); |
| 45 | |
| 46 | EXPECT_TRUE(timer.HasTimeLimitExpired()); |
| 47 | EXPECT_TRUE(timer.IsWarmedUp()); |
| 48 | } |
| 49 | |
Xiaohan Wang | 7940de2 | 2022-01-15 14:47:55 | [diff] [blame] | 50 | #if !BUILDFLAG(IS_IOS) |
Richard Townsend | 5d0b477 | 2019-03-06 13:33:30 | [diff] [blame] | 51 | // iOS simulator does not support using ThreadTicks. |
| 52 | TEST(LapTimer, ThreadTicksUsageExample) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 53 | TaskEnvironment task_environment(TaskEnvironment::TimeSource::MOCK_TIME); |
Richard Townsend | 5d0b477 | 2019-03-06 13:33:30 | [diff] [blame] | 54 | LapTimer timer(kWarmupRuns, kTimeLimit, kTimeCheckInterval, |
| 55 | LapTimer::TimerMethod::kUseThreadTicks); |
| 56 | |
| 57 | EXPECT_FALSE(timer.HasTimeLimitExpired()); |
| 58 | EXPECT_FALSE(timer.IsWarmedUp()); |
| 59 | |
| 60 | do { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 61 | task_environment.FastForwardBy(kTimeAdvance); |
Richard Townsend | 5d0b477 | 2019-03-06 13:33:30 | [diff] [blame] | 62 | timer.NextLap(); |
| 63 | } while (!timer.HasTimeLimitExpired()); |
| 64 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 65 | // Because advancing the TaskEnvironment time won't affect the |
Richard Townsend | 5d0b477 | 2019-03-06 13:33:30 | [diff] [blame] | 66 | // ThreadTicks, laps will be much faster than the regular UsageExample. |
| 67 | EXPECT_GT(timer.LapsPerSecond(), 1000); |
| 68 | EXPECT_LT(timer.TimePerLap().InMillisecondsF(), 1.0f); |
| 69 | EXPECT_GT(timer.NumLaps(), 20); |
| 70 | |
| 71 | EXPECT_TRUE(timer.HasTimeLimitExpired()); |
| 72 | EXPECT_TRUE(timer.IsWarmedUp()); |
| 73 | } |
| 74 | #endif |
| 75 | |
Peter Kasting | 811504a7 | 2025-01-09 03:18:50 | [diff] [blame] | 76 | } // namespace base::test |