1 // Copyright (c) 2012 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.
7 #include "base/basictypes.h"
8 #include "base/json/json_writer.h"
9 #include "base/process/process_handle.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/tracked_objects.h"
12 #include "base/values.h"
13 #include "chrome/browser/task_profiler/task_profiler_data_serializer.h"
14 #include "content/public/common/process_type.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 void ExpectSerialization(
20 const tracked_objects::ProcessDataPhaseSnapshot
& process_data_phase
,
21 base::ProcessId process_id
,
23 const std::string
& expected_json
) {
24 base::DictionaryValue serialized_value
;
25 task_profiler::TaskProfilerDataSerializer::ToValue(
26 process_data_phase
, process_id
, process_type
, &serialized_value
);
28 std::string serialized_json
;
29 base::JSONWriter::Write(&serialized_value
, &serialized_json
);
31 EXPECT_EQ(expected_json
, serialized_json
);
34 } // anonymous namespace
36 // Tests the JSON serialization format for profiled process data.
37 TEST(TaskProfilerDataSerializerTest
, SerializeProcessDataToJson
) {
40 tracked_objects::ProcessDataPhaseSnapshot process_data_phase
;
41 int process_type
= content::PROCESS_TYPE_BROWSER
;
42 ExpectSerialization(process_data_phase
, 239, process_type
,
49 "\"process_type\":\"Browser\""
55 tracked_objects::ProcessDataPhaseSnapshot process_data_phase
;
57 tracked_objects::BirthOnThreadSnapshot parent
;
58 parent
.location
.file_name
= "path/to/foo.cc";
59 parent
.location
.function_name
= "WhizBang";
60 parent
.location
.line_number
= 101;
61 parent
.thread_name
= "CrBrowserMain";
63 tracked_objects::BirthOnThreadSnapshot child
;
64 child
.location
.file_name
= "path/to/bar.cc";
65 child
.location
.function_name
= "FizzBoom";
66 child
.location
.line_number
= 433;
67 child
.thread_name
= "Chrome_IOThread";
71 process_data_phase
.tasks
.push_back(tracked_objects::TaskSnapshot());
72 process_data_phase
.tasks
.back().birth
= parent
;
73 process_data_phase
.tasks
.back().death_data
.count
= 37;
74 process_data_phase
.tasks
.back().death_data
.run_duration_max
= 5;
75 process_data_phase
.tasks
.back().death_data
.run_duration_sample
= 3;
76 process_data_phase
.tasks
.back().death_data
.run_duration_sum
= 17;
77 process_data_phase
.tasks
.back().death_data
.queue_duration_max
= 53;
78 process_data_phase
.tasks
.back().death_data
.queue_duration_sample
= 13;
79 process_data_phase
.tasks
.back().death_data
.queue_duration_sum
= 79;
80 process_data_phase
.tasks
.back().death_thread_name
=
81 "WorkerPool/-1340960768";
83 // Add a second snapshot.
84 process_data_phase
.tasks
.push_back(tracked_objects::TaskSnapshot());
85 process_data_phase
.tasks
.back().birth
= child
;
86 process_data_phase
.tasks
.back().death_data
.count
= 41;
87 process_data_phase
.tasks
.back().death_data
.run_duration_max
= 205;
88 process_data_phase
.tasks
.back().death_data
.run_duration_sample
= 203;
89 process_data_phase
.tasks
.back().death_data
.run_duration_sum
= 2017;
90 process_data_phase
.tasks
.back().death_data
.queue_duration_max
= 2053;
91 process_data_phase
.tasks
.back().death_data
.queue_duration_sample
= 2013;
92 process_data_phase
.tasks
.back().death_data
.queue_duration_sum
= 2079;
93 process_data_phase
.tasks
.back().death_thread_name
= "PAC thread #3";
95 // Add a parent-child pair.
96 process_data_phase
.descendants
.push_back(
97 tracked_objects::ParentChildPairSnapshot());
98 process_data_phase
.descendants
.back().parent
= parent
;
99 process_data_phase
.descendants
.back().child
= child
;
101 int process_type
= content::PROCESS_TYPE_RENDERER
;
102 ExpectSerialization(process_data_phase
, 239, process_type
,
106 "\"child_location\":{"
107 "\"file_name\":\"path/to/bar.cc\","
108 "\"function_name\":\"FizzBoom\","
109 "\"line_number\":433"
111 "\"child_thread\":\"Chrome_IOThread\","
112 "\"parent_location\":{"
113 "\"file_name\":\"path/to/foo.cc\","
114 "\"function_name\":\"WhizBang\","
115 "\"line_number\":101"
117 "\"parent_thread\":\"CrBrowserMain\""
121 "\"birth_location\":{"
122 "\"file_name\":\"path/to/foo.cc\","
123 "\"function_name\":\"WhizBang\","
124 "\"line_number\":101"
126 "\"birth_thread\":\"CrBrowserMain\","
130 "\"queue_ms_max\":53,"
131 "\"queue_ms_sample\":13,"
134 "\"run_ms_sample\":3"
136 "\"death_thread\":\"WorkerPool/-1340960768\""
138 "\"birth_location\":{"
139 "\"file_name\":\"path/to/bar.cc\","
140 "\"function_name\":\"FizzBoom\","
141 "\"line_number\":433"
143 "\"birth_thread\":\"Chrome_IOThread\","
147 "\"queue_ms_max\":2053,"
148 "\"queue_ms_sample\":2013,"
150 "\"run_ms_max\":205,"
151 "\"run_ms_sample\":203"
153 "\"death_thread\":\"PAC thread #3\""
155 "\"process_id\":239,"
156 "\"process_type\":\"Tab\""