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
;
14 using base::trace_event::CategoryFilter
;
15 using base::trace_event::TraceLog
;
16 using base::trace_event::TraceOptions
;
17 using base::trace_event::TraceResultBuffer
;
21 class V8SamplingProfilerTest
: public RenderViewTest
{
23 void SetUp() override
{
24 RenderViewTest::SetUp();
25 sampling_profiler_
.reset(new V8SamplingProfiler(true));
26 trace_buffer_
.SetOutputCallback(json_output_
.GetCallback());
29 void TearDown() override
{
30 sampling_profiler_
.reset();
31 RenderViewTest::TearDown();
34 void KickV8() { ExecuteJavaScript("1"); }
36 void SyncFlush(TraceLog
* trace_log
) {
37 base::WaitableEvent
flush_complete_event(false, false);
39 base::Bind(&V8SamplingProfilerTest::OnTraceDataCollected
,
40 base::Unretained(static_cast<V8SamplingProfilerTest
*>(this)),
41 base::Unretained(&flush_complete_event
)));
42 base::RunLoop().RunUntilIdle();
43 flush_complete_event
.Wait();
46 void OnTraceDataCollected(
47 base::WaitableEvent
* flush_complete_event
,
48 const scoped_refptr
<base::RefCountedString
>& events_str
,
49 bool has_more_events
) {
50 base::AutoLock
lock(lock_
);
51 json_output_
.json_output
.clear();
52 trace_buffer_
.Start();
53 trace_buffer_
.AddFragment(events_str
->data());
54 trace_buffer_
.Finish();
56 scoped_ptr
<Value
> root
;
57 root
.reset(base::JSONReader::Read(
58 json_output_
.json_output
,
59 base::JSON_PARSE_RFC
| base::JSON_DETACHABLE_CHILDREN
));
62 LOG(ERROR
) << json_output_
.json_output
;
65 ListValue
* root_list
= NULL
;
66 ASSERT_TRUE(root
.get());
67 ASSERT_TRUE(root
->GetAsList(&root_list
));
69 // Move items into our aggregate collection
70 while (root_list
->GetSize()) {
71 scoped_ptr
<Value
> item
;
72 root_list
->Remove(0, &item
);
73 trace_parsed_
.Append(item
.release());
77 flush_complete_event
->Signal();
80 scoped_ptr
<V8SamplingProfiler
> sampling_profiler_
;
83 ListValue trace_parsed_
;
84 TraceResultBuffer trace_buffer_
;
85 TraceResultBuffer::SimpleOutput json_output_
;
88 TEST_F(V8SamplingProfilerTest
, V8SamplingEventFired
) {
89 scoped_ptr
<V8SamplingProfiler
> sampling_profiler(
90 new V8SamplingProfiler(true));
91 sampling_profiler
->EnableSamplingEventForTesting();
92 TraceLog::GetInstance()->SetEnabled(
93 CategoryFilter(TRACE_DISABLED_BY_DEFAULT("v8.cpu_profile")),
94 TraceLog::RECORDING_MODE
, TraceOptions());
95 sampling_profiler
->WaitSamplingEventForTesting();
96 TraceLog::GetInstance()->SetDisabled();
99 TEST_F(V8SamplingProfilerTest
, V8SamplingJitCodeEventsCollected
) {
100 TraceLog
* trace_log
= TraceLog::GetInstance();
101 trace_log
->SetEnabled(
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
))
114 if (!dict
->GetString("cat", &value
) ||
115 value
!= TRACE_DISABLED_BY_DEFAULT("v8.cpu_profile"))
117 if (!dict
->GetString("name", &value
) || value
!= "JitCodeAdded")
119 ++jit_code_added_events_count
;
121 CHECK_LT(0, jit_code_added_events_count
);
122 base::RunLoop().RunUntilIdle();
125 } // namespace content