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

Side by Side Diff: third_party/WebKit/Source/platform/TimerPerfTest.cpp

Issue 2319053004: [Reland] Make canceling Timers fast. (Closed)
Patch Set: Add missing file 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);
39 for (int i = 0; i < numIterations; i++) {
40 timers[i] = base::MakeUnique<Timer<TimerPerfTest>>(this, &TimerPerfTest: :nopTask);
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 printf("TimerBase::startOneShot cost (us/call) %f (total %ld us)\n", (postEn d - postStart).InMicroseconds() / static_cast<double>(numIterations), (postEnd - postStart).InMicroseconds());
57 printf("Time to run %d trivial tasks (us) %ld\n", numIterations, (m_runEnd - m_runStart).InMicroseconds());
58 }
59
60 TEST_F(TimerPerfTest, PostThenCancelTenThousandTimers)
61 {
62 const int numIterations = 10000;
63 std::vector<std::unique_ptr<Timer<TimerPerfTest>>> timers(numIterations);
64 for (int i = 0; i < numIterations; i++) {
65 timers[i] = base::MakeUnique<Timer<TimerPerfTest>>(this, &TimerPerfTest: :nopTask);
66 }
67
68 Timer<TimerPerfTest> measureRunStart(this, &TimerPerfTest::recordStartRunTim e);
69 Timer<TimerPerfTest> measureRunEnd(this, &TimerPerfTest::recordEndRunTime);
70
71 measureRunStart.startOneShot(0.0, BLINK_FROM_HERE);
72 base::ThreadTicks postStart = base::ThreadTicks::Now();
73 for (int i = 0; i < numIterations; i++) {
74 timers[i]->startOneShot(0.0, BLINK_FROM_HERE);
75 }
76 base::ThreadTicks postEnd = base::ThreadTicks::Now();
77 measureRunEnd.startOneShot(0.0, BLINK_FROM_HERE);
78
79 base::ThreadTicks cancelStart = base::ThreadTicks::Now();
80 for (int i = 0; i < numIterations; i++) {
81 timers[i]->stop();
82 }
83 base::ThreadTicks cancelEnd = base::ThreadTicks::Now();
84
85 testing::enterRunLoop();
86
87 printf("TimerBase::startOneShot cost (us/call) %f (total %ld us)\n", (postEn d - postStart).InMicroseconds() / static_cast<double>(numIterations), (postEnd - postStart).InMicroseconds());
88 printf("TimerBase::stop cost (us/call) %f (total %ld us)\n", (cancelEnd - ca ncelStart).InMicroseconds() / static_cast<double>(numIterations), (cancelEnd - c ancelStart).InMicroseconds());
89 printf("Time to run %d canceled tasks (us) %ld\n", numIterations, (m_runEnd - m_runStart).InMicroseconds());
90 }
91
92 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698