OLD | NEW |
(Empty) | |
| 1 |
| 2 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 // Use of this source code is governed by a BSD-style license that can be |
| 4 // found in the LICENSE file. |
| 5 |
| 6 #include "platform/Timer.h" |
| 7 |
| 8 #include "platform/testing/UnitTestHelpers.h" |
| 9 #include "public/platform/Platform.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "wtf/PtrUtil.h" |
| 12 #include "wtf/Vector.h" |
| 13 |
| 14 namespace blink { |
| 15 |
| 16 class TimerPerfTest : public ::testing::Test { |
| 17 public: |
| 18 void nopTask(TimerBase*) |
| 19 { |
| 20 } |
| 21 |
| 22 void recordStartRunTime(TimerBase*) |
| 23 { |
| 24 m_runStart = base::ThreadTicks::Now(); |
| 25 } |
| 26 |
| 27 void recordEndRunTime(TimerBase*) |
| 28 { |
| 29 m_runEnd = base::ThreadTicks::Now(); |
| 30 base::MessageLoop::current()->QuitNow(); |
| 31 } |
| 32 |
| 33 base::ThreadTicks m_runStart; |
| 34 base::ThreadTicks m_runEnd; |
| 35 }; |
| 36 |
| 37 TEST_F(TimerPerfTest, PostAndRunTimers) |
| 38 { |
| 39 const int numIterations = 10000; |
| 40 Vector<std::unique_ptr<Timer<TimerPerfTest>>> timers(numIterations); |
| 41 for (int i = 0; i < numIterations; i++) { |
| 42 timers[i].reset(new Timer<TimerPerfTest>(this, &TimerPerfTest::nopTask))
; |
| 43 } |
| 44 |
| 45 Timer<TimerPerfTest> measureRunStart(this, &TimerPerfTest::recordStartRunTim
e); |
| 46 Timer<TimerPerfTest> measureRunEnd(this, &TimerPerfTest::recordEndRunTime); |
| 47 |
| 48 measureRunStart.startOneShot(0.0, BLINK_FROM_HERE); |
| 49 base::ThreadTicks postStart = base::ThreadTicks::Now(); |
| 50 for (int i = 0; i < numIterations; i++) { |
| 51 timers[i]->startOneShot(0.0, BLINK_FROM_HERE); |
| 52 } |
| 53 base::ThreadTicks postEnd = base::ThreadTicks::Now(); |
| 54 measureRunEnd.startOneShot(0.0, BLINK_FROM_HERE); |
| 55 |
| 56 testing::enterRunLoop(); |
| 57 |
| 58 double postingTime = (postEnd - postStart).InMicroseconds(); |
| 59 double postingTimeUsPerCall = postingTime / static_cast<double>(numIteration
s); |
| 60 LOG(INFO) << "TimerBase::startOneShot cost (us/call) " << postingTimeUsPerCa
ll << " (total " << postingTime << " us)"; |
| 61 LOG(INFO) << "Time to run " << numIterations << " trivial tasks (us) " << (m
_runEnd - m_runStart).InMicroseconds(); |
| 62 } |
| 63 |
| 64 TEST_F(TimerPerfTest, PostThenCancelTenThousandTimers) |
| 65 { |
| 66 const int numIterations = 10000; |
| 67 Vector<std::unique_ptr<Timer<TimerPerfTest>>> timers(numIterations); |
| 68 for (int i = 0; i < numIterations; i++) { |
| 69 timers[i].reset(new Timer<TimerPerfTest>(this, &TimerPerfTest::nopTask))
; |
| 70 } |
| 71 |
| 72 Timer<TimerPerfTest> measureRunStart(this, &TimerPerfTest::recordStartRunTim
e); |
| 73 Timer<TimerPerfTest> measureRunEnd(this, &TimerPerfTest::recordEndRunTime); |
| 74 |
| 75 measureRunStart.startOneShot(0.0, BLINK_FROM_HERE); |
| 76 base::ThreadTicks postStart = base::ThreadTicks::Now(); |
| 77 for (int i = 0; i < numIterations; i++) { |
| 78 timers[i]->startOneShot(0.0, BLINK_FROM_HERE); |
| 79 } |
| 80 base::ThreadTicks postEnd = base::ThreadTicks::Now(); |
| 81 measureRunEnd.startOneShot(0.0, BLINK_FROM_HERE); |
| 82 |
| 83 base::ThreadTicks cancelStart = base::ThreadTicks::Now(); |
| 84 for (int i = 0; i < numIterations; i++) { |
| 85 timers[i]->stop(); |
| 86 } |
| 87 base::ThreadTicks cancelEnd = base::ThreadTicks::Now(); |
| 88 |
| 89 testing::enterRunLoop(); |
| 90 |
| 91 double postingTime = (postEnd - postStart).InMicroseconds(); |
| 92 double postingTimeUsPerCall = postingTime / static_cast<double>(numIteration
s); |
| 93 LOG(INFO) << "TimerBase::startOneShot cost (us/call) " << postingTimeUsPerCa
ll << " (total " << postingTime << " us)"; |
| 94 |
| 95 double cancelTime = (cancelEnd - cancelStart).InMicroseconds(); |
| 96 double cancelTimeUsPerCall = cancelTime / static_cast<double>(numIterations)
; |
| 97 LOG(INFO) << "TimerBase::stop cost (us/call) " << cancelTimeUsPerCall << " (
total " << cancelTime << " us)"; |
| 98 LOG(INFO) << "Time to run " << numIterations << " canceled tasks (us) " << (
m_runEnd - m_runStart).InMicroseconds(); |
| 99 } |
| 100 |
| 101 } // namespace blink |
OLD | NEW |