| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "base/debug/trace_event_synthetic_delay.h" | |
| 6 | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | |
| 8 | |
| 9 namespace base { | |
| 10 namespace debug { | |
| 11 namespace { | |
| 12 | |
| 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; | |
| 17 | |
| 18 } // namespace | |
| 19 | |
| 20 class TraceEventSyntheticDelayTest : public testing::Test, | |
| 21 public TraceEventSyntheticDelayClock { | |
| 22 public: | |
| 23 TraceEventSyntheticDelayTest() {} | |
| 24 ~TraceEventSyntheticDelayTest() override { ResetTraceEventSyntheticDelays(); } | |
| 25 | |
| 26 // TraceEventSyntheticDelayClock implementation. | |
| 27 base::TimeTicks Now() override { | |
| 28 AdvanceTime(base::TimeDelta::FromMilliseconds(kShortDurationMs / 10)); | |
| 29 return now_; | |
| 30 } | |
| 31 | |
| 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; | |
| 38 } | |
| 39 | |
| 40 void AdvanceTime(base::TimeDelta delta) { now_ += delta; } | |
| 41 | |
| 42 int64 TestFunction() { | |
| 43 base::TimeTicks start = Now(); | |
| 44 { TRACE_EVENT_SYNTHETIC_DELAY("test.Delay"); } | |
| 45 return (Now() - start).InMilliseconds(); | |
| 46 } | |
| 47 | |
| 48 int64 AsyncTestFunctionBegin() { | |
| 49 base::TimeTicks start = Now(); | |
| 50 { TRACE_EVENT_SYNTHETIC_DELAY_BEGIN("test.AsyncDelay"); } | |
| 51 return (Now() - start).InMilliseconds(); | |
| 52 } | |
| 53 | |
| 54 int64 AsyncTestFunctionEnd() { | |
| 55 base::TimeTicks start = Now(); | |
| 56 { TRACE_EVENT_SYNTHETIC_DELAY_END("test.AsyncDelay"); } | |
| 57 return (Now() - start).InMilliseconds(); | |
| 58 } | |
| 59 | |
| 60 private: | |
| 61 base::TimeTicks now_; | |
| 62 | |
| 63 DISALLOW_COPY_AND_ASSIGN(TraceEventSyntheticDelayTest); | |
| 64 }; | |
| 65 | |
| 66 TEST_F(TraceEventSyntheticDelayTest, StaticDelay) { | |
| 67 TraceEventSyntheticDelay* delay = ConfigureDelay("test.Delay"); | |
| 68 delay->SetMode(TraceEventSyntheticDelay::STATIC); | |
| 69 EXPECT_GE(TestFunction(), kTargetDurationMs); | |
| 70 } | |
| 71 | |
| 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); | |
| 77 | |
| 78 delay->SetTargetDuration( | |
| 79 base::TimeDelta::FromMilliseconds(kTargetDurationMs)); | |
| 80 EXPECT_GE(TestFunction(), kTargetDurationMs); | |
| 81 } | |
| 82 | |
| 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); | |
| 90 } | |
| 91 | |
| 92 TEST_F(TraceEventSyntheticDelayTest, AsyncDelay) { | |
| 93 ConfigureDelay("test.AsyncDelay"); | |
| 94 EXPECT_LT(AsyncTestFunctionBegin(), kShortDurationMs); | |
| 95 EXPECT_GE(AsyncTestFunctionEnd(), kTargetDurationMs / 2); | |
| 96 } | |
| 97 | |
| 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); | |
| 103 } | |
| 104 | |
| 105 TEST_F(TraceEventSyntheticDelayTest, AsyncDelayNoActivation) { | |
| 106 ConfigureDelay("test.AsyncDelay"); | |
| 107 EXPECT_LT(AsyncTestFunctionEnd(), kShortDurationMs); | |
| 108 } | |
| 109 | |
| 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); | |
| 116 } | |
| 117 | |
| 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); | |
| 123 | |
| 124 EXPECT_LT(AsyncTestFunctionBegin(), kShortDurationMs); | |
| 125 EXPECT_GE(AsyncTestFunctionEnd(), kTargetDurationMs / 2); | |
| 126 } | |
| 127 | |
| 128 TEST_F(TraceEventSyntheticDelayTest, ResetDelays) { | |
| 129 ConfigureDelay("test.Delay"); | |
| 130 ResetTraceEventSyntheticDelays(); | |
| 131 EXPECT_LT(TestFunction(), kShortDurationMs); | |
| 132 } | |
| 133 | |
| 134 TEST_F(TraceEventSyntheticDelayTest, BeginParallel) { | |
| 135 TraceEventSyntheticDelay* delay = ConfigureDelay("test.AsyncDelay"); | |
| 136 base::TimeTicks end_times[2]; | |
| 137 base::TimeTicks start_time = Now(); | |
| 138 | |
| 139 delay->BeginParallel(&end_times[0]); | |
| 140 EXPECT_FALSE(end_times[0].is_null()); | |
| 141 | |
| 142 delay->BeginParallel(&end_times[1]); | |
| 143 EXPECT_FALSE(end_times[1].is_null()); | |
| 144 | |
| 145 delay->EndParallel(end_times[0]); | |
| 146 EXPECT_GE((Now() - start_time).InMilliseconds(), kTargetDurationMs); | |
| 147 | |
| 148 start_time = Now(); | |
| 149 delay->EndParallel(end_times[1]); | |
| 150 EXPECT_LT((Now() - start_time).InMilliseconds(), kShortDurationMs); | |
| 151 } | |
| 152 | |
| 153 } // namespace debug | |
| 154 } // namespace base | |
| OLD | NEW |