OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 |
| 25 // TraceEventSyntheticDelayClock implementation. |
| 26 virtual base::TimeTicks Now() OVERRIDE { |
| 27 AdvanceTime(base::TimeDelta::FromMilliseconds(kShortDurationMs / 10)); |
| 28 return now_; |
| 29 } |
| 30 |
| 31 TraceEventSyntheticDelay* ConfigureDelay(const char* name) { |
| 32 TraceEventSyntheticDelay* delay = TraceEventSyntheticDelay::Lookup(name); |
| 33 delay->SetClock(this); |
| 34 delay->SetTargetDuration( |
| 35 base::TimeDelta::FromMilliseconds(kTargetDurationMs)); |
| 36 return delay; |
| 37 } |
| 38 |
| 39 void AdvanceTime(base::TimeDelta delta) { now_ += delta; } |
| 40 |
| 41 int TestFunction() { |
| 42 base::TimeTicks start = Now(); |
| 43 { TRACE_EVENT_SYNTHETIC_DELAY("test.Delay"); } |
| 44 return (Now() - start).InMilliseconds(); |
| 45 } |
| 46 |
| 47 int AsyncTestFunctionActivate() { |
| 48 base::TimeTicks start = Now(); |
| 49 { TRACE_EVENT_SYNTHETIC_DELAY_ACTIVATE("test.AsyncDelay"); } |
| 50 return (Now() - start).InMilliseconds(); |
| 51 } |
| 52 |
| 53 int AsyncTestFunctionApply() { |
| 54 base::TimeTicks start = Now(); |
| 55 { TRACE_EVENT_SYNTHETIC_DELAY_APPLY("test.AsyncDelay"); } |
| 56 return (Now() - start).InMilliseconds(); |
| 57 } |
| 58 |
| 59 private: |
| 60 base::TimeTicks now_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(TraceEventSyntheticDelayTest); |
| 63 }; |
| 64 |
| 65 TEST_F(TraceEventSyntheticDelayTest, StaticDelay) { |
| 66 TraceEventSyntheticDelay* delay = ConfigureDelay("test.Delay"); |
| 67 delay->SetMode(TraceEventSyntheticDelay::STATIC); |
| 68 EXPECT_GE(TestFunction(), kTargetDurationMs); |
| 69 } |
| 70 |
| 71 TEST_F(TraceEventSyntheticDelayTest, OneShotDelay) { |
| 72 TraceEventSyntheticDelay* delay = ConfigureDelay("test.Delay"); |
| 73 delay->SetMode(TraceEventSyntheticDelay::ONE_SHOT); |
| 74 EXPECT_GE(TestFunction(), kTargetDurationMs); |
| 75 EXPECT_LT(TestFunction(), kShortDurationMs); |
| 76 |
| 77 delay->SetTargetDuration( |
| 78 base::TimeDelta::FromMilliseconds(kTargetDurationMs)); |
| 79 EXPECT_GE(TestFunction(), kTargetDurationMs); |
| 80 } |
| 81 |
| 82 TEST_F(TraceEventSyntheticDelayTest, AlternatingDelay) { |
| 83 TraceEventSyntheticDelay* delay = ConfigureDelay("test.Delay"); |
| 84 delay->SetMode(TraceEventSyntheticDelay::ALTERNATING); |
| 85 EXPECT_GE(TestFunction(), kTargetDurationMs); |
| 86 EXPECT_LT(TestFunction(), kShortDurationMs); |
| 87 EXPECT_GE(TestFunction(), kTargetDurationMs); |
| 88 EXPECT_LT(TestFunction(), kShortDurationMs); |
| 89 } |
| 90 |
| 91 TEST_F(TraceEventSyntheticDelayTest, AsyncDelay) { |
| 92 ConfigureDelay("test.AsyncDelay"); |
| 93 EXPECT_LT(AsyncTestFunctionActivate(), kShortDurationMs); |
| 94 EXPECT_GE(AsyncTestFunctionApply(), kTargetDurationMs / 2); |
| 95 } |
| 96 |
| 97 TEST_F(TraceEventSyntheticDelayTest, AsyncDelayExceeded) { |
| 98 ConfigureDelay("test.AsyncDelay"); |
| 99 EXPECT_LT(AsyncTestFunctionActivate(), kShortDurationMs); |
| 100 AdvanceTime(base::TimeDelta::FromMilliseconds(kTargetDurationMs)); |
| 101 EXPECT_LT(AsyncTestFunctionApply(), kShortDurationMs); |
| 102 } |
| 103 |
| 104 TEST_F(TraceEventSyntheticDelayTest, AsyncDelayNoActivation) { |
| 105 ConfigureDelay("test.AsyncDelay"); |
| 106 EXPECT_LT(AsyncTestFunctionApply(), kShortDurationMs); |
| 107 } |
| 108 |
| 109 TEST_F(TraceEventSyntheticDelayTest, AsyncDelayMultipleActivations) { |
| 110 ConfigureDelay("test.AsyncDelay"); |
| 111 EXPECT_LT(AsyncTestFunctionActivate(), kShortDurationMs); |
| 112 AdvanceTime(base::TimeDelta::FromMilliseconds(kTargetDurationMs)); |
| 113 EXPECT_LT(AsyncTestFunctionActivate(), kShortDurationMs); |
| 114 EXPECT_LT(AsyncTestFunctionApply(), kShortDurationMs); |
| 115 } |
| 116 |
| 117 } // namespace debug |
| 118 } // namespace base |
OLD | NEW |