Use UintToString() for unsigned values.
[chromium-blink-merge.git] / base / trace_event / trace_event_synthetic_delay_unittest.cc
blob1dc0fc26f5c7a1289a52e381e262e13c7f992fc9
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/trace_event/trace_event_synthetic_delay.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace base {
10 namespace trace_event {
11 namespace {
13 const int kTargetDurationMs = 100;
14 // Allow some leeway in timings to make it possible to run these tests with a
15 // wall clock time source too.
16 const int kShortDurationMs = 10;
18 } // namespace
20 class TraceEventSyntheticDelayTest : public testing::Test,
21 public TraceEventSyntheticDelayClock {
22 public:
23 TraceEventSyntheticDelayTest() {}
24 ~TraceEventSyntheticDelayTest() override { ResetTraceEventSyntheticDelays(); }
26 // TraceEventSyntheticDelayClock implementation.
27 base::TimeTicks Now() override {
28 AdvanceTime(base::TimeDelta::FromMilliseconds(kShortDurationMs / 10));
29 return now_;
32 TraceEventSyntheticDelay* ConfigureDelay(const char* name) {
33 TraceEventSyntheticDelay* delay = TraceEventSyntheticDelay::Lookup(name);
34 delay->SetClock(this);
35 delay->SetTargetDuration(
36 base::TimeDelta::FromMilliseconds(kTargetDurationMs));
37 return delay;
40 void AdvanceTime(base::TimeDelta delta) { now_ += delta; }
42 int64 TestFunction() {
43 base::TimeTicks start = Now();
44 { TRACE_EVENT_SYNTHETIC_DELAY("test.Delay"); }
45 return (Now() - start).InMilliseconds();
48 int64 AsyncTestFunctionBegin() {
49 base::TimeTicks start = Now();
50 { TRACE_EVENT_SYNTHETIC_DELAY_BEGIN("test.AsyncDelay"); }
51 return (Now() - start).InMilliseconds();
54 int64 AsyncTestFunctionEnd() {
55 base::TimeTicks start = Now();
56 { TRACE_EVENT_SYNTHETIC_DELAY_END("test.AsyncDelay"); }
57 return (Now() - start).InMilliseconds();
60 private:
61 base::TimeTicks now_;
63 DISALLOW_COPY_AND_ASSIGN(TraceEventSyntheticDelayTest);
66 TEST_F(TraceEventSyntheticDelayTest, StaticDelay) {
67 TraceEventSyntheticDelay* delay = ConfigureDelay("test.Delay");
68 delay->SetMode(TraceEventSyntheticDelay::STATIC);
69 EXPECT_GE(TestFunction(), kTargetDurationMs);
72 TEST_F(TraceEventSyntheticDelayTest, OneShotDelay) {
73 TraceEventSyntheticDelay* delay = ConfigureDelay("test.Delay");
74 delay->SetMode(TraceEventSyntheticDelay::ONE_SHOT);
75 EXPECT_GE(TestFunction(), kTargetDurationMs);
76 EXPECT_LT(TestFunction(), kShortDurationMs);
78 delay->SetTargetDuration(
79 base::TimeDelta::FromMilliseconds(kTargetDurationMs));
80 EXPECT_GE(TestFunction(), kTargetDurationMs);
83 TEST_F(TraceEventSyntheticDelayTest, AlternatingDelay) {
84 TraceEventSyntheticDelay* delay = ConfigureDelay("test.Delay");
85 delay->SetMode(TraceEventSyntheticDelay::ALTERNATING);
86 EXPECT_GE(TestFunction(), kTargetDurationMs);
87 EXPECT_LT(TestFunction(), kShortDurationMs);
88 EXPECT_GE(TestFunction(), kTargetDurationMs);
89 EXPECT_LT(TestFunction(), kShortDurationMs);
92 TEST_F(TraceEventSyntheticDelayTest, AsyncDelay) {
93 ConfigureDelay("test.AsyncDelay");
94 EXPECT_LT(AsyncTestFunctionBegin(), kShortDurationMs);
95 EXPECT_GE(AsyncTestFunctionEnd(), kTargetDurationMs / 2);
98 TEST_F(TraceEventSyntheticDelayTest, AsyncDelayExceeded) {
99 ConfigureDelay("test.AsyncDelay");
100 EXPECT_LT(AsyncTestFunctionBegin(), kShortDurationMs);
101 AdvanceTime(base::TimeDelta::FromMilliseconds(kTargetDurationMs));
102 EXPECT_LT(AsyncTestFunctionEnd(), kShortDurationMs);
105 TEST_F(TraceEventSyntheticDelayTest, AsyncDelayNoActivation) {
106 ConfigureDelay("test.AsyncDelay");
107 EXPECT_LT(AsyncTestFunctionEnd(), kShortDurationMs);
110 TEST_F(TraceEventSyntheticDelayTest, AsyncDelayNested) {
111 ConfigureDelay("test.AsyncDelay");
112 EXPECT_LT(AsyncTestFunctionBegin(), kShortDurationMs);
113 EXPECT_LT(AsyncTestFunctionBegin(), kShortDurationMs);
114 EXPECT_LT(AsyncTestFunctionEnd(), kShortDurationMs);
115 EXPECT_GE(AsyncTestFunctionEnd(), kTargetDurationMs / 2);
118 TEST_F(TraceEventSyntheticDelayTest, AsyncDelayUnbalanced) {
119 ConfigureDelay("test.AsyncDelay");
120 EXPECT_LT(AsyncTestFunctionBegin(), kShortDurationMs);
121 EXPECT_GE(AsyncTestFunctionEnd(), kTargetDurationMs / 2);
122 EXPECT_LT(AsyncTestFunctionEnd(), kShortDurationMs);
124 EXPECT_LT(AsyncTestFunctionBegin(), kShortDurationMs);
125 EXPECT_GE(AsyncTestFunctionEnd(), kTargetDurationMs / 2);
128 TEST_F(TraceEventSyntheticDelayTest, ResetDelays) {
129 ConfigureDelay("test.Delay");
130 ResetTraceEventSyntheticDelays();
131 EXPECT_LT(TestFunction(), kShortDurationMs);
134 TEST_F(TraceEventSyntheticDelayTest, BeginParallel) {
135 TraceEventSyntheticDelay* delay = ConfigureDelay("test.AsyncDelay");
136 base::TimeTicks end_times[2];
137 base::TimeTicks start_time = Now();
139 delay->BeginParallel(&end_times[0]);
140 EXPECT_FALSE(end_times[0].is_null());
142 delay->BeginParallel(&end_times[1]);
143 EXPECT_FALSE(end_times[1].is_null());
145 delay->EndParallel(end_times[0]);
146 EXPECT_GE((Now() - start_time).InMilliseconds(), kTargetDurationMs);
148 start_time = Now();
149 delay->EndParallel(end_times[1]);
150 EXPECT_LT((Now() - start_time).InMilliseconds(), kShortDurationMs);
153 } // namespace trace_event
154 } // namespace base