Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(795)

Unified Diff: third_party/WebKit/Source/platform/TimerPerfTest.cpp

Issue 2319053004: [Reland] Make canceling Timers fast. (Closed)
Patch Set: Getting printf to work with size_t cross platform is too hard, use C++ io streams instead Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..f767bb35c8f3bdb3f766a31719118f76ebe1b017
--- /dev/null
+++ b/third_party/WebKit/Source/platform/TimerPerfTest.cpp
@@ -0,0 +1,103 @@
+// 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);
haraken 2016/09/09 02:34:07 Use Vector.
alex clarke (OOO till 29th) 2016/09/09 09:40:59 Done.
+ for (int i = 0; i < numIterations; i++) {
+ timers[i] = base::MakeUnique<Timer<TimerPerfTest>>(this, &TimerPerfTest::nopTask);
haraken 2016/09/09 02:34:07 Use wrapUnique.
alex clarke (OOO till 29th) 2016/09/09 09:40:59 Used reset in the end.
+ }
+
+ 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();
+
+ std::cout << "TimerBase::startOneShot cost (us/call) ";
haraken 2016/09/09 02:34:07 Use LOG.
alex clarke (OOO till 29th) 2016/09/09 09:40:59 Done.
+ std::cout << (postEnd - postStart).InMicroseconds() / static_cast<double>(numIterations);
+ std::cout << " (total " << (postEnd - postStart).InMicroseconds() << " us)" << std::endl;
+
+ std::cout << "Time to run " << numIterations << " trivial tasks (us) ";
+ std::cout << (m_runEnd - m_runStart).InMicroseconds() << std::endl;
+}
+
+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();
+
+ std::cout << "TimerBase::startOneShot cost (us/call) ";
+ std::cout << (postEnd - postStart).InMicroseconds() / static_cast<double>(numIterations);
+ std::cout << " (total " << (postEnd - postStart).InMicroseconds() << " us)" << std::endl;
+
+ std::cout << "TimerBase::stop cost (us/call) ";
+ std::cout << (cancelEnd - cancelStart).InMicroseconds() / static_cast<double>(numIterations);
+ std::cout << " (total " << (cancelEnd - cancelStart).InMicroseconds() << " us)" << std::endl;
+
+ std::cout << "Time to run " << numIterations << " canceled tasks (us) ";
+ std::cout << (m_runEnd - m_runStart).InMicroseconds() << std::endl;
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698