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
,
47 "\"process_type\":\"Browser\""
53 tracked_objects::ProcessDataPhaseSnapshot process_data_phase
;
55 tracked_objects::BirthOnThreadSnapshot parent
;
56 parent
.location
.file_name
= "path/to/foo.cc";
57 parent
.location
.function_name
= "WhizBang";
58 parent
.location
.line_number
= 101;
59 parent
.thread_name
= "CrBrowserMain";
61 tracked_objects::BirthOnThreadSnapshot child
;
62 child
.location
.file_name
= "path/to/bar.cc";
63 child
.location
.function_name
= "FizzBoom";
64 child
.location
.line_number
= 433;
65 child
.thread_name
= "Chrome_IOThread";
69 process_data_phase
.tasks
.push_back(tracked_objects::TaskSnapshot());
70 process_data_phase
.tasks
.back().birth
= parent
;
71 process_data_phase
.tasks
.back().death_data
.count
= 37;
72 process_data_phase
.tasks
.back().death_data
.run_duration_max
= 5;
73 process_data_phase
.tasks
.back().death_data
.run_duration_sample
= 3;
74 process_data_phase
.tasks
.back().death_data
.run_duration_sum
= 17;
75 process_data_phase
.tasks
.back().death_data
.queue_duration_max
= 53;
76 process_data_phase
.tasks
.back().death_data
.queue_duration_sample
= 13;
77 process_data_phase
.tasks
.back().death_data
.queue_duration_sum
= 79;
78 process_data_phase
.tasks
.back().death_thread_name
=
79 "WorkerPool/-1340960768";
81 // Add a second snapshot.
82 process_data_phase
.tasks
.push_back(tracked_objects::TaskSnapshot());
83 process_data_phase
.tasks
.back().birth
= child
;
84 process_data_phase
.tasks
.back().death_data
.count
= 41;
85 process_data_phase
.tasks
.back().death_data
.run_duration_max
= 205;
86 process_data_phase
.tasks
.back().death_data
.run_duration_sample
= 203;
87 process_data_phase
.tasks
.back().death_data
.run_duration_sum
= 2017;
88 process_data_phase
.tasks
.back().death_data
.queue_duration_max
= 2053;
89 process_data_phase
.tasks
.back().death_data
.queue_duration_sample
= 2013;
90 process_data_phase
.tasks
.back().death_data
.queue_duration_sum
= 2079;
91 process_data_phase
.tasks
.back().death_thread_name
= "PAC thread #3";
93 int process_type
= content::PROCESS_TYPE_RENDERER
;
94 ExpectSerialization(process_data_phase
, 239, process_type
,
97 "\"birth_location\":{"
98 "\"file_name\":\"path/to/foo.cc\","
99 "\"function_name\":\"WhizBang\","
100 "\"line_number\":101"
102 "\"birth_thread\":\"CrBrowserMain\","
106 "\"queue_ms_max\":53,"
107 "\"queue_ms_sample\":13,"
110 "\"run_ms_sample\":3"
112 "\"death_thread\":\"WorkerPool/-1340960768\""
114 "\"birth_location\":{"
115 "\"file_name\":\"path/to/bar.cc\","
116 "\"function_name\":\"FizzBoom\","
117 "\"line_number\":433"
119 "\"birth_thread\":\"Chrome_IOThread\","
123 "\"queue_ms_max\":2053,"
124 "\"queue_ms_sample\":2013,"
126 "\"run_ms_max\":205,"
127 "\"run_ms_sample\":203"
129 "\"death_thread\":\"PAC thread #3\""
131 "\"process_id\":239,"
132 "\"process_type\":\"Tab\""