Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / renderer / devtools / v8_sampling_profiler_browsertest.cc
blobdb3c8d5f7ae5bb9d933e3a4f345b272e1359b104
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
3 // found in the LICENSE file.
5 #include "base/json/json_reader.h"
6 #include "base/run_loop.h"
7 #include "base/trace_event/trace_event.h"
8 #include "content/public/test/render_view_test.h"
9 #include "content/renderer/devtools/v8_sampling_profiler.h"
11 using base::DictionaryValue;
12 using base::ListValue;
13 using base::Value;
14 using base::trace_event::TraceConfig;
15 using base::trace_event::TraceLog;
16 using base::trace_event::TraceResultBuffer;
18 namespace content {
20 class V8SamplingProfilerTest : public RenderViewTest {
21 public:
22 void SetUp() override {
23 RenderViewTest::SetUp();
24 sampling_profiler_.reset(new V8SamplingProfiler(true));
25 trace_buffer_.SetOutputCallback(json_output_.GetCallback());
28 void TearDown() override {
29 sampling_profiler_.reset();
30 RenderViewTest::TearDown();
33 void KickV8() { ExecuteJavaScriptForTests("1"); }
35 void SyncFlush(TraceLog* trace_log) {
36 base::WaitableEvent flush_complete_event(false, false);
37 trace_log->Flush(
38 base::Bind(&V8SamplingProfilerTest::OnTraceDataCollected,
39 base::Unretained(static_cast<V8SamplingProfilerTest*>(this)),
40 base::Unretained(&flush_complete_event)));
41 base::RunLoop().RunUntilIdle();
42 flush_complete_event.Wait();
45 void OnTraceDataCollected(
46 base::WaitableEvent* flush_complete_event,
47 const scoped_refptr<base::RefCountedString>& events_str,
48 bool has_more_events) {
49 base::AutoLock lock(lock_);
50 json_output_.json_output.clear();
51 trace_buffer_.Start();
52 trace_buffer_.AddFragment(events_str->data());
53 trace_buffer_.Finish();
55 scoped_ptr<Value> root;
56 root.reset(base::JSONReader::DeprecatedRead(
57 json_output_.json_output,
58 base::JSON_PARSE_RFC | base::JSON_DETACHABLE_CHILDREN));
60 if (!root.get()) {
61 LOG(ERROR) << json_output_.json_output;
64 ListValue* root_list = NULL;
65 ASSERT_TRUE(root.get());
66 ASSERT_TRUE(root->GetAsList(&root_list));
68 // Move items into our aggregate collection
69 while (root_list->GetSize()) {
70 scoped_ptr<Value> item;
71 root_list->Remove(0, &item);
72 trace_parsed_.Append(item.release());
75 if (!has_more_events)
76 flush_complete_event->Signal();
79 void CollectTrace(int code_added_events, int sample_events) {
80 TraceLog* trace_log = TraceLog::GetInstance();
81 sampling_profiler_->EnableSamplingEventForTesting(code_added_events,
82 sample_events);
83 trace_log->SetEnabled(
84 TraceConfig(TRACE_DISABLED_BY_DEFAULT("v8.cpu_profile"), ""),
85 TraceLog::RECORDING_MODE);
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);
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;
110 return events_count;
113 scoped_ptr<V8SamplingProfiler> sampling_profiler_;
114 base::Lock lock_;
116 ListValue trace_parsed_;
117 TraceResultBuffer trace_buffer_;
118 TraceResultBuffer::SimpleOutput json_output_;
121 TEST_F(V8SamplingProfilerTest, V8SamplingEventFired) {
122 sampling_profiler_->EnableSamplingEventForTesting(0, 0);
123 TraceLog::GetInstance()->SetEnabled(
124 TraceConfig(TRACE_DISABLED_BY_DEFAULT("v8.cpu_profile"), ""),
125 TraceLog::RECORDING_MODE);
126 base::RunLoop().RunUntilIdle();
127 sampling_profiler_->WaitSamplingEventForTesting();
128 TraceLog::GetInstance()->SetDisabled();
129 base::RunLoop().RunUntilIdle();
132 TEST_F(V8SamplingProfilerTest, V8SamplingJitCodeEventsCollected) {
133 CollectTrace(1, 0);
134 int jit_code_added_events_count = CountEvents("JitCodeAdded");
135 CHECK_LT(0, jit_code_added_events_count);
136 base::RunLoop().RunUntilIdle();
139 TEST_F(V8SamplingProfilerTest, V8SamplingSamplesCollected) {
140 CollectTrace(0, 1);
141 int sample_events_count = CountEvents("V8Sample");
142 CHECK_LT(0, sample_events_count);
143 base::RunLoop().RunUntilIdle();
146 } // namespace content