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_util.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 std::string
GetProcessIdString() {
20 return base::IntToString(base::GetCurrentProcId());
23 void ExpectSerialization(
24 const tracked_objects::ProcessDataSnapshot
& process_data
,
26 const std::string
& expected_json
) {
27 base::DictionaryValue serialized_value
;
28 task_profiler::TaskProfilerDataSerializer::ToValue(
29 process_data
, process_type
, &serialized_value
);
31 std::string serialized_json
;
32 base::JSONWriter::Write(&serialized_value
, &serialized_json
);
34 EXPECT_EQ(expected_json
, serialized_json
);
37 } // anonymous namespace
39 // Tests the JSON serialization format for profiled process data.
40 TEST(TaskProfilerDataSerializerTest
, SerializeProcessDataToJson
) {
43 tracked_objects::ProcessDataSnapshot process_data
;
44 int process_type
= content::PROCESS_TYPE_BROWSER
;
45 ExpectSerialization(process_data
, process_type
,
51 "\"process_id\":" + GetProcessIdString() + ","
52 "\"process_type\":\"Browser\""
58 tracked_objects::ProcessDataSnapshot process_data
;
60 tracked_objects::BirthOnThreadSnapshot parent
;
61 parent
.location
.file_name
= "path/to/foo.cc";
62 parent
.location
.function_name
= "WhizBang";
63 parent
.location
.line_number
= 101;
64 parent
.thread_name
= "CrBrowserMain";
66 tracked_objects::BirthOnThreadSnapshot child
;
67 child
.location
.file_name
= "path/to/bar.cc";
68 child
.location
.function_name
= "FizzBoom";
69 child
.location
.line_number
= 433;
70 child
.thread_name
= "Chrome_IOThread";
74 process_data
.tasks
.push_back(tracked_objects::TaskSnapshot());
75 process_data
.tasks
.back().birth
= parent
;
76 process_data
.tasks
.back().death_data
.count
= 37;
77 process_data
.tasks
.back().death_data
.run_duration_max
= 5;
78 process_data
.tasks
.back().death_data
.run_duration_sample
= 3;
79 process_data
.tasks
.back().death_data
.run_duration_sum
= 17;
80 process_data
.tasks
.back().death_data
.queue_duration_max
= 53;
81 process_data
.tasks
.back().death_data
.queue_duration_sample
= 13;
82 process_data
.tasks
.back().death_data
.queue_duration_sum
= 79;
83 process_data
.tasks
.back().death_thread_name
= "WorkerPool/-1340960768";
85 // Add a second snapshot.
86 process_data
.tasks
.push_back(tracked_objects::TaskSnapshot());
87 process_data
.tasks
.back().birth
= child
;
88 process_data
.tasks
.back().death_data
.count
= 41;
89 process_data
.tasks
.back().death_data
.run_duration_max
= 205;
90 process_data
.tasks
.back().death_data
.run_duration_sample
= 203;
91 process_data
.tasks
.back().death_data
.run_duration_sum
= 2017;
92 process_data
.tasks
.back().death_data
.queue_duration_max
= 2053;
93 process_data
.tasks
.back().death_data
.queue_duration_sample
= 2013;
94 process_data
.tasks
.back().death_data
.queue_duration_sum
= 2079;
95 process_data
.tasks
.back().death_thread_name
= "PAC thread #3";
97 // Add a parent-child pair.
98 process_data
.descendants
.push_back(
99 tracked_objects::ParentChildPairSnapshot());
100 process_data
.descendants
.back().parent
= parent
;
101 process_data
.descendants
.back().child
= child
;
103 int process_type
= content::PROCESS_TYPE_RENDERER
;
104 ExpectSerialization(process_data
, process_type
,
108 "\"child_location\":{"
109 "\"file_name\":\"path/to/bar.cc\","
110 "\"function_name\":\"FizzBoom\","
111 "\"line_number\":433"
113 "\"child_thread\":\"Chrome_IOThread\","
114 "\"parent_location\":{"
115 "\"file_name\":\"path/to/foo.cc\","
116 "\"function_name\":\"WhizBang\","
117 "\"line_number\":101"
119 "\"parent_thread\":\"CrBrowserMain\""
123 "\"birth_location\":{"
124 "\"file_name\":\"path/to/foo.cc\","
125 "\"function_name\":\"WhizBang\","
126 "\"line_number\":101"
128 "\"birth_thread\":\"CrBrowserMain\","
132 "\"queue_ms_max\":53,"
133 "\"queue_ms_sample\":13,"
136 "\"run_ms_sample\":3"
138 "\"death_thread\":\"WorkerPool/-1340960768\""
140 "\"birth_location\":{"
141 "\"file_name\":\"path/to/bar.cc\","
142 "\"function_name\":\"FizzBoom\","
143 "\"line_number\":433"
145 "\"birth_thread\":\"Chrome_IOThread\","
149 "\"queue_ms_max\":2053,"
150 "\"queue_ms_sample\":2013,"
152 "\"run_ms_max\":205,"
153 "\"run_ms_sample\":203"
155 "\"death_thread\":\"PAC thread #3\""
157 "\"process_id\":" + GetProcessIdString() + ","
158 "\"process_type\":\"Tab\""