blob: 2f143da629464dbb494f0e471795bd5baf8a395a [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2019 The Chromium Authors
Richard Townsend5d0b4772019-03-06 13:33:302// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Gabriel Charette18947082019-08-23 02:05:335#include "base/timer/lap_timer.h"
Gabriel Charettec7108742019-08-23 03:31:406
7#include "base/test/task_environment.h"
Gabriel Charetteb69fcd42019-08-23 02:13:298#include "base/time/time.h"
Richard Townsend5d0b4772019-03-06 13:33:309#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 Kasting811504a72025-01-09 03:18:5016namespace base::test {
Richard Townsend5d0b4772019-03-06 13:33:3017
18namespace {
19
Peter Kastinge5a38ed2021-10-02 03:06:3520constexpr base::TimeDelta kTimeLimit = base::Milliseconds(15);
21constexpr base::TimeDelta kTimeAdvance = base::Milliseconds(1);
Richard Townsend5d0b4772019-03-06 13:33:3022constexpr int kWarmupRuns = 5;
23constexpr int kTimeCheckInterval = 10;
24
25} // namespace
26
27TEST(LapTimer, UsageExample) {
Gabriel Charette694c3c332019-08-19 14:53:0528 TaskEnvironment task_environment(TaskEnvironment::TimeSource::MOCK_TIME);
Richard Townsend5d0b4772019-03-06 13:33:3029
Richard Townsend5d0b4772019-03-06 13:33:3030 LapTimer timer(kWarmupRuns, kTimeLimit, kTimeCheckInterval);
31
32 EXPECT_FALSE(timer.HasTimeLimitExpired());
33 EXPECT_FALSE(timer.IsWarmedUp());
34
35 do {
Gabriel Charette694c3c332019-08-19 14:53:0536 task_environment.FastForwardBy(kTimeAdvance);
Richard Townsend5d0b4772019-03-06 13:33:3037 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 Wang7940de22022-01-15 14:47:5550#if !BUILDFLAG(IS_IOS)
Richard Townsend5d0b4772019-03-06 13:33:3051// iOS simulator does not support using ThreadTicks.
52TEST(LapTimer, ThreadTicksUsageExample) {
Gabriel Charette694c3c332019-08-19 14:53:0553 TaskEnvironment task_environment(TaskEnvironment::TimeSource::MOCK_TIME);
Richard Townsend5d0b4772019-03-06 13:33:3054 LapTimer timer(kWarmupRuns, kTimeLimit, kTimeCheckInterval,
55 LapTimer::TimerMethod::kUseThreadTicks);
56
57 EXPECT_FALSE(timer.HasTimeLimitExpired());
58 EXPECT_FALSE(timer.IsWarmedUp());
59
60 do {
Gabriel Charette694c3c332019-08-19 14:53:0561 task_environment.FastForwardBy(kTimeAdvance);
Richard Townsend5d0b4772019-03-06 13:33:3062 timer.NextLap();
63 } while (!timer.HasTimeLimitExpired());
64
Gabriel Charette694c3c332019-08-19 14:53:0565 // Because advancing the TaskEnvironment time won't affect the
Richard Townsend5d0b4772019-03-06 13:33:3066 // 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 Kasting811504a72025-01-09 03:18:5076} // namespace base::test