| Index: third_party/WebKit/Source/platform/TimerPerfTest.cpp
|
| diff --git a/third_party/WebKit/Source/platform/TimerPerfTest.cpp b/third_party/WebKit/Source/platform/TimerPerfTest.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..4e670dc2b1591174c532ed59acf47de7c4788251
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/platform/TimerPerfTest.cpp
|
| @@ -0,0 +1,92 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "platform/Timer.h"
|
| +
|
| +#include "base/memory/ptr_util.h"
|
| +#include "platform/testing/UnitTestHelpers.h"
|
| +#include "public/platform/Platform.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace blink {
|
| +
|
| +class TimerPerfTest : public ::testing::Test {
|
| +public:
|
| + void nopTask(TimerBase*)
|
| + {
|
| + }
|
| +
|
| + void recordStartRunTime(TimerBase*)
|
| + {
|
| + m_runStart = base::ThreadTicks::Now();
|
| + }
|
| +
|
| + void recordEndRunTime(TimerBase*)
|
| + {
|
| + m_runEnd = base::ThreadTicks::Now();
|
| + base::MessageLoop::current()->QuitNow();
|
| + }
|
| +
|
| + base::ThreadTicks m_runStart;
|
| + base::ThreadTicks m_runEnd;
|
| +};
|
| +
|
| +TEST_F(TimerPerfTest, PostAndRunTimers)
|
| +{
|
| + const int numIterations = 10000;
|
| + std::vector<std::unique_ptr<Timer<TimerPerfTest>>> timers(numIterations);
|
| + for (int i = 0; i < numIterations; i++) {
|
| + timers[i] = base::MakeUnique<Timer<TimerPerfTest>>(this, &TimerPerfTest::nopTask);
|
| + }
|
| +
|
| + Timer<TimerPerfTest> measureRunStart(this, &TimerPerfTest::recordStartRunTime);
|
| + Timer<TimerPerfTest> measureRunEnd(this, &TimerPerfTest::recordEndRunTime);
|
| +
|
| + measureRunStart.startOneShot(0.0, BLINK_FROM_HERE);
|
| + base::ThreadTicks postStart = base::ThreadTicks::Now();
|
| + for (int i = 0; i < numIterations; i++) {
|
| + timers[i]->startOneShot(0.0, BLINK_FROM_HERE);
|
| + }
|
| + base::ThreadTicks postEnd = base::ThreadTicks::Now();
|
| + measureRunEnd.startOneShot(0.0, BLINK_FROM_HERE);
|
| +
|
| + testing::enterRunLoop();
|
| +
|
| + printf("TimerBase::startOneShot cost (us/call) %f (total %ld us)\n", (postEnd - postStart).InMicroseconds() / static_cast<double>(numIterations), (postEnd - postStart).InMicroseconds());
|
| + printf("Time to run %d trivial tasks (us) %ld\n", numIterations, (m_runEnd - m_runStart).InMicroseconds());
|
| +}
|
| +
|
| +TEST_F(TimerPerfTest, PostThenCancelTenThousandTimers)
|
| +{
|
| + const int numIterations = 10000;
|
| + std::vector<std::unique_ptr<Timer<TimerPerfTest>>> timers(numIterations);
|
| + for (int i = 0; i < numIterations; i++) {
|
| + timers[i] = base::MakeUnique<Timer<TimerPerfTest>>(this, &TimerPerfTest::nopTask);
|
| + }
|
| +
|
| + Timer<TimerPerfTest> measureRunStart(this, &TimerPerfTest::recordStartRunTime);
|
| + Timer<TimerPerfTest> measureRunEnd(this, &TimerPerfTest::recordEndRunTime);
|
| +
|
| + measureRunStart.startOneShot(0.0, BLINK_FROM_HERE);
|
| + base::ThreadTicks postStart = base::ThreadTicks::Now();
|
| + for (int i = 0; i < numIterations; i++) {
|
| + timers[i]->startOneShot(0.0, BLINK_FROM_HERE);
|
| + }
|
| + base::ThreadTicks postEnd = base::ThreadTicks::Now();
|
| + measureRunEnd.startOneShot(0.0, BLINK_FROM_HERE);
|
| +
|
| + base::ThreadTicks cancelStart = base::ThreadTicks::Now();
|
| + for (int i = 0; i < numIterations; i++) {
|
| + timers[i]->stop();
|
| + }
|
| + base::ThreadTicks cancelEnd = base::ThreadTicks::Now();
|
| +
|
| + testing::enterRunLoop();
|
| +
|
| + printf("TimerBase::startOneShot cost (us/call) %f (total %ld us)\n", (postEnd - postStart).InMicroseconds() / static_cast<double>(numIterations), (postEnd - postStart).InMicroseconds());
|
| + printf("TimerBase::stop cost (us/call) %f (total %ld us)\n", (cancelEnd - cancelStart).InMicroseconds() / static_cast<double>(numIterations), (cancelEnd - cancelStart).InMicroseconds());
|
| + printf("Time to run %d canceled tasks (us) %ld\n", numIterations, (m_runEnd - m_runStart).InMicroseconds());
|
| +}
|
| +
|
| +} // namespace blink
|
|
|