| OLD | NEW | 
|---|
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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/json/json_reader.h" | 5 #include "base/json/json_reader.h" | 
| 6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" | 
| 7 #include "base/trace_event/trace_event.h" | 7 #include "base/trace_event/trace_event.h" | 
| 8 #include "content/public/test/render_view_test.h" | 8 #include "content/public/test/render_view_test.h" | 
| 9 #include "content/renderer/devtools/v8_sampling_profiler.h" | 9 #include "content/renderer/devtools/v8_sampling_profiler.h" | 
| 10 | 10 | 
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 70     while (root_list->GetSize()) { | 70     while (root_list->GetSize()) { | 
| 71       scoped_ptr<Value> item; | 71       scoped_ptr<Value> item; | 
| 72       root_list->Remove(0, &item); | 72       root_list->Remove(0, &item); | 
| 73       trace_parsed_.Append(item.release()); | 73       trace_parsed_.Append(item.release()); | 
| 74     } | 74     } | 
| 75 | 75 | 
| 76     if (!has_more_events) | 76     if (!has_more_events) | 
| 77       flush_complete_event->Signal(); | 77       flush_complete_event->Signal(); | 
| 78   } | 78   } | 
| 79 | 79 | 
|  | 80   void CollectTrace() { | 
|  | 81     TraceLog* trace_log = TraceLog::GetInstance(); | 
|  | 82     sampling_profiler_->EnableSamplingEventForTesting(); | 
|  | 83     trace_log->SetEnabled( | 
|  | 84         CategoryFilter(TRACE_DISABLED_BY_DEFAULT("v8.cpu_profile")), | 
|  | 85         TraceLog::RECORDING_MODE, TraceOptions()); | 
|  | 86     base::RunLoop().RunUntilIdle(); | 
|  | 87     KickV8();  // Make a call to V8 so it can invoke interrupt request | 
|  | 88                // callbacks. | 
|  | 89     base::RunLoop().RunUntilIdle(); | 
|  | 90     sampling_profiler_->WaitSamplingEventForTesting(); | 
|  | 91     trace_log->SetDisabled(); | 
|  | 92     SyncFlush(trace_log); | 
|  | 93   } | 
|  | 94 | 
|  | 95   int CountEvents(const std::string& name) const { | 
|  | 96     size_t trace_parsed_count = trace_parsed_.GetSize(); | 
|  | 97     int events_count = 0; | 
|  | 98     for (size_t i = 0; i < trace_parsed_count; i++) { | 
|  | 99       const DictionaryValue* dict; | 
|  | 100       if (!trace_parsed_.GetDictionary(i, &dict)) | 
|  | 101         continue; | 
|  | 102       std::string value; | 
|  | 103       if (!dict->GetString("cat", &value) || | 
|  | 104           value != TRACE_DISABLED_BY_DEFAULT("v8.cpu_profile")) | 
|  | 105         continue; | 
|  | 106       if (!dict->GetString("name", &value) || value != name) | 
|  | 107         continue; | 
|  | 108       ++events_count; | 
|  | 109     } | 
|  | 110     return events_count; | 
|  | 111   } | 
|  | 112 | 
| 80   scoped_ptr<V8SamplingProfiler> sampling_profiler_; | 113   scoped_ptr<V8SamplingProfiler> sampling_profiler_; | 
| 81   base::Lock lock_; | 114   base::Lock lock_; | 
| 82 | 115 | 
| 83   ListValue trace_parsed_; | 116   ListValue trace_parsed_; | 
| 84   TraceResultBuffer trace_buffer_; | 117   TraceResultBuffer trace_buffer_; | 
| 85   TraceResultBuffer::SimpleOutput json_output_; | 118   TraceResultBuffer::SimpleOutput json_output_; | 
| 86 }; | 119 }; | 
| 87 | 120 | 
| 88 TEST_F(V8SamplingProfilerTest, V8SamplingEventFired) { | 121 TEST_F(V8SamplingProfilerTest, V8SamplingEventFired) { | 
| 89   scoped_ptr<V8SamplingProfiler> sampling_profiler( | 122   sampling_profiler_->EnableSamplingEventForTesting(); | 
| 90       new V8SamplingProfiler(true)); |  | 
| 91   sampling_profiler->EnableSamplingEventForTesting(); |  | 
| 92   TraceLog::GetInstance()->SetEnabled( | 123   TraceLog::GetInstance()->SetEnabled( | 
| 93       CategoryFilter(TRACE_DISABLED_BY_DEFAULT("v8.cpu_profile")), | 124       CategoryFilter(TRACE_DISABLED_BY_DEFAULT("v8.cpu_profile")), | 
| 94       TraceLog::RECORDING_MODE, TraceOptions()); | 125       TraceLog::RECORDING_MODE, TraceOptions()); | 
| 95   sampling_profiler->WaitSamplingEventForTesting(); | 126   base::RunLoop().RunUntilIdle(); | 
|  | 127   sampling_profiler_->WaitSamplingEventForTesting(); | 
| 96   TraceLog::GetInstance()->SetDisabled(); | 128   TraceLog::GetInstance()->SetDisabled(); | 
| 97 } | 129 } | 
| 98 | 130 | 
| 99 TEST_F(V8SamplingProfilerTest, V8SamplingJitCodeEventsCollected) { | 131 TEST_F(V8SamplingProfilerTest, V8SamplingJitCodeEventsCollected) { | 
| 100   TraceLog* trace_log = TraceLog::GetInstance(); | 132   CollectTrace(); | 
| 101   trace_log->SetEnabled( | 133   int jit_code_added_events_count = CountEvents("JitCodeAdded"); | 
| 102       CategoryFilter(TRACE_DISABLED_BY_DEFAULT("v8.cpu_profile")), |  | 
| 103       TraceLog::RECORDING_MODE, TraceOptions()); |  | 
| 104   KickV8();  // Make a call to V8 so it can invoke interrupt request callbacks. |  | 
| 105   trace_log->SetDisabled(); |  | 
| 106   SyncFlush(trace_log); |  | 
| 107   size_t trace_parsed_count = trace_parsed_.GetSize(); |  | 
| 108   int jit_code_added_events_count = 0; |  | 
| 109   for (size_t i = 0; i < trace_parsed_count; i++) { |  | 
| 110     const DictionaryValue* dict; |  | 
| 111     if (!trace_parsed_.GetDictionary(i, &dict)) |  | 
| 112       continue; |  | 
| 113     std::string value; |  | 
| 114     if (!dict->GetString("cat", &value) || |  | 
| 115         value != TRACE_DISABLED_BY_DEFAULT("v8.cpu_profile")) |  | 
| 116       continue; |  | 
| 117     if (!dict->GetString("name", &value) || value != "JitCodeAdded") |  | 
| 118       continue; |  | 
| 119     ++jit_code_added_events_count; |  | 
| 120   } |  | 
| 121   CHECK_LT(0, jit_code_added_events_count); | 134   CHECK_LT(0, jit_code_added_events_count); | 
| 122   base::RunLoop().RunUntilIdle(); | 135   base::RunLoop().RunUntilIdle(); | 
| 123 } | 136 } | 
| 124 | 137 | 
|  | 138 #if defined(OS_WIN) || defined(OS_ANDROID) | 
|  | 139 #define MAYBE(x) DISABLED_##x | 
|  | 140 #else | 
|  | 141 #define MAYBE(x) x | 
|  | 142 #endif | 
|  | 143 | 
|  | 144 // TODO(alph): Implement on Windows and Android | 
|  | 145 TEST_F(V8SamplingProfilerTest, MAYBE(V8SamplingSamplesCollected)) { | 
|  | 146   CollectTrace(); | 
|  | 147   int sample_events_count = CountEvents("V8Sample"); | 
|  | 148   CHECK_LT(0, sample_events_count); | 
|  | 149   base::RunLoop().RunUntilIdle(); | 
|  | 150 } | 
|  | 151 | 
| 125 }  // namespace content | 152 }  // namespace content | 
| OLD | NEW | 
|---|