Repository navigation
Expand file tree
/
Copy pathtimer_test.cc
More file actions
102 lines (85 loc) · 3.03 KB
/
Copy pathtimer_test.cc
File metadata and controls
102 lines (85 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2010-2025 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/base/timer.h"
#include <cstdint>
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "gtest/gtest.h"
namespace {
// Consumes user-mode CPU time until `timer` advances by at least `target`
// (bounded by a wall-time cap so the test can never hang).
void BurnUserCpu(const UserTimer& timer, absl::Duration target) {
const absl::Duration initial = timer.GetDuration();
WallTimer wall;
wall.Start();
volatile uint64_t sink = 0;
while (timer.GetDuration() - initial < target &&
wall.GetDuration() < absl::Seconds(2)) {
for (int i = 0; i < 100000; ++i) {
sink = sink * 6364136223846793005ULL + 1442695040888963407ULL;
}
}
}
TEST(UserTimerTest, InitialAndResetState) {
UserTimer timer;
EXPECT_FALSE(timer.IsRunning());
EXPECT_EQ(timer.GetDuration(), absl::ZeroDuration());
// Stopping a stopped timer is a no-op.
timer.Stop();
EXPECT_FALSE(timer.IsRunning());
EXPECT_EQ(timer.GetDuration(), absl::ZeroDuration());
timer.Start();
EXPECT_TRUE(timer.IsRunning());
BurnUserCpu(timer, absl::Milliseconds(30));
EXPECT_GE(timer.GetDuration(), absl::Milliseconds(30));
timer.Reset();
EXPECT_FALSE(timer.IsRunning());
EXPECT_EQ(timer.GetDuration(), absl::ZeroDuration());
}
TEST(UserTimerTest, MeasuresCpuWorkAndAccumulates) {
UserTimer timer;
timer.Start();
BurnUserCpu(timer, absl::Milliseconds(50));
timer.Stop();
EXPECT_FALSE(timer.IsRunning());
const absl::Duration first_elapsed = timer.GetDuration();
EXPECT_GE(first_elapsed, absl::Milliseconds(50));
// While stopped, CPU work must not advance the timer.
{
UserTimer dummy;
dummy.Start();
BurnUserCpu(dummy, absl::Milliseconds(30));
}
EXPECT_EQ(timer.GetDuration(), first_elapsed);
// Starting again accumulates on top of `first_elapsed`.
timer.Start();
BurnUserCpu(timer, absl::Milliseconds(50));
timer.Stop();
EXPECT_GE(timer.GetDuration(), first_elapsed + absl::Milliseconds(50));
// Restart resets the accumulated time and starts running.
timer.Restart();
EXPECT_TRUE(timer.IsRunning());
EXPECT_LT(timer.GetDuration(), first_elapsed);
}
TEST(UserTimerTest, DoesNotCountSleepTime) {
UserTimer user_timer;
WallTimer wall_timer;
user_timer.Start();
wall_timer.Start();
absl::SleepFor(absl::Milliseconds(200));
wall_timer.Stop();
user_timer.Stop();
EXPECT_GE(wall_timer.GetDuration(), absl::Milliseconds(180));
EXPECT_LT(user_timer.GetDuration(), absl::Milliseconds(100));
}
} // namespace