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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "platform/Timer.h"
6
7 #include "base/memory/ptr_util.h"
8 #include "platform/testing/UnitTestHelpers.h"
9 #include "public/platform/Platform.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace blink {
13
14 class TimerPerfTest : public ::testing::Test {
15 public:
16 void nopTask(TimerBase*)
17 {
18 }
19
20 void recordStartRunTime(TimerBase*)
21 {
22 m_runStart = base::ThreadTicks::Now();
23 }
24
25 void recordEndRunTime(TimerBase*)
26 {
27 m_runEnd = base::ThreadTicks::Now();
28 base::MessageLoop::current()->QuitNow();
29 }
30
31 base::ThreadTicks m_runStart;
32 base::ThreadTicks m_runEnd;
33 };
34
35 TEST_F(TimerPerfTest, PostAndRunTimers)
36 {
37 const int numIterations = 10000;
38 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.
39 for (int i = 0; i < numIterations; i++) {
40 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.
41 }
42
43 Timer<TimerPerfTest> measureRunStart(this, &TimerPerfTest::recordStartRunTim e);
44 Timer<TimerPerfTest> measureRunEnd(this, &TimerPerfTest::recordEndRunTime);
45
46 measureRunStart.startOneShot(0.0, BLINK_FROM_HERE);
47 base::ThreadTicks postStart = base::ThreadTicks::Now();
48 for (int i = 0; i < numIterations; i++) {
49 timers[i]->startOneShot(0.0, BLINK_FROM_HERE);
50 }
51 base::ThreadTicks postEnd = base::ThreadTicks::Now();
52 measureRunEnd.startOneShot(0.0, BLINK_FROM_HERE);
53
54 testing::enterRunLoop();
55
56 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.
57 std::cout << (postEnd - postStart).InMicroseconds() / static_cast<double>(nu mIterations);
58 std::cout << " (total " << (postEnd - postStart).InMicroseconds() << " us)" << std::endl;
59
60 std::cout << "Time to run " << numIterations << " trivial tasks (us) ";
61 std::cout << (m_runEnd - m_runStart).InMicroseconds() << std::endl;
62 }
63
64 TEST_F(TimerPerfTest, PostThenCancelTenThousandTimers)
65 {
66 const int numIterations = 10000;
67 std::vector<std::unique_ptr<Timer<TimerPerfTest>>> timers(numIterations);
68 for (int i = 0; i < numIterations; i++) {
69 timers[i] = base::MakeUnique<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 std::cout << "TimerBase::startOneShot cost (us/call) ";
92 std::cout << (postEnd - postStart).InMicroseconds() / static_cast<double>(nu mIterations);
93 std::cout << " (total " << (postEnd - postStart).InMicroseconds() << " us)" << std::endl;
94
95 std::cout << "TimerBase::stop cost (us/call) ";
96 std::cout << (cancelEnd - cancelStart).InMicroseconds() / static_cast<double >(numIterations);
97 std::cout << " (total " << (cancelEnd - cancelStart).InMicroseconds() << " u s)" << std::endl;
98
99 std::cout << "Time to run " << numIterations << " canceled tasks (us) ";
100 std::cout << (m_runEnd - m_runStart).InMicroseconds() << std::endl;
101 }
102
103 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698