| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/memory/ptr_util.h" |
| 6 #include "base/memory/ref_counted_memory.h" | 7 #include "base/memory/ref_counted_memory.h" |
| 7 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/threading/thread.h" |
| 8 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
| 11 #include "base/trace_event/trace_event_argument.h" |
| 9 #include "perf_test_helpers.h" | 12 #include "perf_test_helpers.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 14 |
| 12 namespace tracing { | 15 namespace tracing { |
| 13 namespace { | 16 namespace { |
| 14 | 17 |
| 18 using base::Bind; |
| 19 using base::Thread; |
| 20 using base::Unretained; |
| 21 using base::WaitableEvent; |
| 15 using base::trace_event::TraceConfig; | 22 using base::trace_event::TraceConfig; |
| 16 using base::trace_event::TraceLog; | 23 using base::trace_event::TraceLog; |
| 17 using base::trace_event::TraceRecordMode; | 24 using base::trace_event::TraceRecordMode; |
| 25 using base::trace_event::TracedValue; |
| 18 | 26 |
| 19 const int kNumRuns = 100; | 27 const int kNumRuns = 10; |
| 20 | 28 |
| 21 class TraceEventPerfTest : public ::testing::Test { | 29 class TraceEventPerfTest : public ::testing::Test { |
| 22 public: | 30 public: |
| 23 void BeginTrace() { | 31 void BeginTrace() { |
| 24 TraceConfig config("*", ""); | 32 TraceConfig config("*", ""); |
| 25 config.SetTraceRecordMode(TraceRecordMode::RECORD_CONTINUOUSLY); | 33 config.SetTraceRecordMode(TraceRecordMode::RECORD_CONTINUOUSLY); |
| 26 TraceLog::GetInstance()->SetEnabled(config, TraceLog::RECORDING_MODE); | 34 TraceLog::GetInstance()->SetEnabled(config, TraceLog::RECORDING_MODE); |
| 27 } | 35 } |
| 28 | 36 |
| 29 void EndTraceAndFlush() { | 37 void EndTraceAndFlush() { |
| 30 ScopedStopwatch stopwatch("flush"); | 38 ScopedStopwatch stopwatch("flush"); |
| 31 base::RunLoop run_loop; | 39 base::RunLoop run_loop; |
| 32 TraceLog::GetInstance()->SetDisabled(); | 40 TraceLog::GetInstance()->SetDisabled(); |
| 33 TraceLog::GetInstance()->Flush( | 41 TraceLog::GetInstance()->Flush( |
| 34 Bind(&OnTraceDataCollected, run_loop.QuitClosure())); | 42 Bind(&OnTraceDataCollected, run_loop.QuitClosure())); |
| 35 run_loop.Run(); | 43 run_loop.Run(); |
| 36 } | 44 } |
| 37 | 45 |
| 38 static void OnTraceDataCollected( | 46 static void OnTraceDataCollected( |
| 39 base::Closure quit_closure, | 47 base::Closure quit_closure, |
| 40 const scoped_refptr<base::RefCountedString>& events_str, | 48 const scoped_refptr<base::RefCountedString>& events_str, |
| 41 bool has_more_events) { | 49 bool has_more_events) { |
| 42 | 50 |
| 43 if (!has_more_events) | 51 if (!has_more_events) |
| 44 quit_closure.Run(); | 52 quit_closure.Run(); |
| 45 } | 53 } |
| 54 |
| 55 std::unique_ptr<TracedValue> MakeTracedValue(int counter) { |
| 56 auto value = base::MakeUnique<TracedValue>(); |
| 57 value->SetInteger("counter", counter); |
| 58 value->BeginDictionary("test_dict"); |
| 59 value->BeginArray("nodes"); |
| 60 for (int i = 0; i < 10; i++) { |
| 61 value->BeginDictionary(); |
| 62 value->SetInteger("id", i); |
| 63 value->SetBoolean("valid", true); |
| 64 value->SetString("value", "foo"); |
| 65 value->EndDictionary(); |
| 66 } |
| 67 value->EndArray(); |
| 68 value->SetInteger("count", 10); |
| 69 value->EndDictionary(); |
| 70 return value; |
| 71 } |
| 72 |
| 73 static void SubmitTraceEventsAndSignal(WaitableEvent* complete_event) { |
| 74 for (int i = 0; i < 10000; i++) { |
| 75 TRACE_EVENT0("test_category", "some call"); |
| 76 } |
| 77 complete_event->Signal(); |
| 78 } |
| 79 |
| 80 private: |
| 81 base::MessageLoop _message_loop; |
| 46 }; | 82 }; |
| 47 | 83 |
| 48 TEST_F(TraceEventPerfTest, Submit_10000_TRACE_EVENT0) { | 84 TEST_F(TraceEventPerfTest, Submit_10000_TRACE_EVENT0) { |
| 49 BeginTrace(); | 85 BeginTrace(); |
| 50 IterableStopwatch stopwatch("events"); | 86 IterableStopwatch stopwatch("events"); |
| 51 for (int lap = 0; lap < kNumRuns; lap++) { | 87 for (int lap = 0; lap < kNumRuns; lap++) { |
| 52 for (int i = 0; i < 10000; i++) { | 88 for (int i = 0; i < 10000; i++) { |
| 53 TRACE_EVENT0("test_category", "TRACE_EVENT0 call"); | 89 TRACE_EVENT0("test_category", "TRACE_EVENT0 call"); |
| 54 } | 90 } |
| 55 stopwatch.NextLap(); | 91 stopwatch.NextLap(); |
| 56 } | 92 } |
| 57 EndTraceAndFlush(); | 93 EndTraceAndFlush(); |
| 58 } | 94 } |
| 59 | 95 |
| 96 TEST_F(TraceEventPerfTest, Long_TRACE_EVENT0) { |
| 97 BeginTrace(); |
| 98 IterableStopwatch stopwatch("long_event"); |
| 99 for (int lap = 0; lap < kNumRuns; lap++) { |
| 100 TRACE_EVENT0("test_category", "Outer event"); |
| 101 for (int i = 0; i < 10000; i++) { |
| 102 TRACE_EVENT0("test_category", "TRACE_EVENT0 call"); |
| 103 } |
| 104 stopwatch.NextLap(); |
| 105 } |
| 106 EndTraceAndFlush(); |
| 107 } |
| 108 |
| 109 TEST_F(TraceEventPerfTest, Create_10000_TracedValue) { |
| 110 std::unique_ptr<TracedValue> value; |
| 111 { |
| 112 ScopedStopwatch value_sw("create_traced_values"); |
| 113 for (int i = 0; i < 10000; i++) { |
| 114 value = MakeTracedValue(i); |
| 115 } |
| 116 } |
| 117 } |
| 118 |
| 119 TEST_F(TraceEventPerfTest, Submit_10000_TRACE_EVENT_with_TracedValue) { |
| 120 BeginTrace(); |
| 121 // Time reported by this timer includes TracedValue creation as well. |
| 122 IterableStopwatch trace_sw("events_with_value"); |
| 123 for (int lap = 0; lap < kNumRuns; lap++) { |
| 124 for (int i = 0; i < 10000; i++) { |
| 125 TRACE_EVENT_INSTANT1("test_category", "event_with_value", |
| 126 TRACE_EVENT_SCOPE_THREAD, "value", MakeTracedValue(i)); |
| 127 } |
| 128 trace_sw.NextLap(); |
| 129 } |
| 130 EndTraceAndFlush(); |
| 131 } |
| 132 |
| 133 TEST_F(TraceEventPerfTest, Submit_10000_TRACE_EVENT0_multithreaded) { |
| 134 BeginTrace(); |
| 135 const int kNumThreads = 4; |
| 136 |
| 137 std::vector<std::unique_ptr<Thread>> threads; |
| 138 std::vector<std::unique_ptr<WaitableEvent>> complete_events; |
| 139 |
| 140 for (int i = 0; i < kNumThreads; i++) { |
| 141 Thread* thread = new Thread(std::string("thread_%d") + std::to_string(i)); |
| 142 WaitableEvent* complete_event = |
| 143 new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 144 WaitableEvent::InitialState::NOT_SIGNALED); |
| 145 threads.push_back(base::WrapUnique(thread)); |
| 146 complete_events.push_back(base::WrapUnique(complete_event)); |
| 147 thread->Start(); |
| 148 } |
| 149 |
| 150 { |
| 151 ScopedStopwatch stopwatch("events_over_multiple_threads"); |
| 152 for (int i = 0; i < kNumThreads; i++) { |
| 153 threads[i]->task_runner()->PostTask( |
| 154 FROM_HERE, |
| 155 base::Bind(&SubmitTraceEventsAndSignal, complete_events[i].get())); |
| 156 } |
| 157 for (int i = 0; i < kNumThreads; i++) { |
| 158 complete_events[i]->Wait(); |
| 159 } |
| 160 } |
| 161 |
| 162 EndTraceAndFlush(); |
| 163 for (int i = 0; i < kNumThreads; i++) { |
| 164 threads[i]->Stop(); |
| 165 } |
| 166 } |
| 167 |
| 60 } // namespace | 168 } // namespace |
| 61 } // namespace tracing | 169 } // namespace tracing |
| OLD | NEW |