OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/process/process_handle.h" | 9 #include "base/process/process_handle.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/tracked_objects.h" | 11 #include "base/tracked_objects.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/task_profiler/task_profiler_data_serializer.h" | 13 #include "chrome/browser/task_profiler/task_profiler_data_serializer.h" |
14 #include "content/public/common/process_type.h" | 14 #include "content/public/common/process_type.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 std::string GetProcessIdString() { | |
20 return base::IntToString(base::GetCurrentProcId()); | |
21 } | |
22 | |
23 void ExpectSerialization( | 19 void ExpectSerialization( |
24 const tracked_objects::ProcessDataSnapshot& process_data, | 20 const tracked_objects::ProcessDataPhaseSnapshot& process_data_phase, |
| 21 base::ProcessId process_id, |
25 int process_type, | 22 int process_type, |
26 const std::string& expected_json) { | 23 const std::string& expected_json) { |
27 base::DictionaryValue serialized_value; | 24 base::DictionaryValue serialized_value; |
28 task_profiler::TaskProfilerDataSerializer::ToValue( | 25 task_profiler::TaskProfilerDataSerializer::ToValue( |
29 process_data, process_type, &serialized_value); | 26 process_data_phase, process_id, process_type, &serialized_value); |
30 | 27 |
31 std::string serialized_json; | 28 std::string serialized_json; |
32 base::JSONWriter::Write(&serialized_value, &serialized_json); | 29 base::JSONWriter::Write(&serialized_value, &serialized_json); |
33 | 30 |
34 EXPECT_EQ(expected_json, serialized_json); | 31 EXPECT_EQ(expected_json, serialized_json); |
35 } | 32 } |
36 | 33 |
37 } // anonymous namespace | 34 } // anonymous namespace |
38 | 35 |
39 // Tests the JSON serialization format for profiled process data. | 36 // Tests the JSON serialization format for profiled process data. |
40 TEST(TaskProfilerDataSerializerTest, SerializeProcessDataToJson) { | 37 TEST(TaskProfilerDataSerializerTest, SerializeProcessDataToJson) { |
41 { | 38 { |
42 // Empty data. | 39 // Empty data. |
43 tracked_objects::ProcessDataSnapshot process_data; | 40 tracked_objects::ProcessDataPhaseSnapshot process_data_phase; |
44 int process_type = content::PROCESS_TYPE_BROWSER; | 41 int process_type = content::PROCESS_TYPE_BROWSER; |
45 ExpectSerialization(process_data, process_type, | 42 ExpectSerialization(process_data_phase, 239, process_type, |
46 "{" | 43 "{" |
47 "\"descendants\":[" | 44 "\"descendants\":[" |
48 "]," | 45 "]," |
49 "\"list\":[" | 46 "\"list\":[" |
50 "]," | 47 "]," |
51 "\"process_id\":" + GetProcessIdString() + "," | 48 "\"process_id\":239," |
52 "\"process_type\":\"Browser\"" | 49 "\"process_type\":\"Browser\"" |
53 "}"); | 50 "}"); |
54 } | 51 } |
55 | 52 |
56 { | 53 { |
57 // Non-empty data. | 54 // Non-empty data. |
58 tracked_objects::ProcessDataSnapshot process_data; | 55 tracked_objects::ProcessDataPhaseSnapshot process_data_phase; |
59 | 56 |
60 tracked_objects::BirthOnThreadSnapshot parent; | 57 tracked_objects::BirthOnThreadSnapshot parent; |
61 parent.location.file_name = "path/to/foo.cc"; | 58 parent.location.file_name = "path/to/foo.cc"; |
62 parent.location.function_name = "WhizBang"; | 59 parent.location.function_name = "WhizBang"; |
63 parent.location.line_number = 101; | 60 parent.location.line_number = 101; |
64 parent.thread_name = "CrBrowserMain"; | 61 parent.thread_name = "CrBrowserMain"; |
65 | 62 |
66 tracked_objects::BirthOnThreadSnapshot child; | 63 tracked_objects::BirthOnThreadSnapshot child; |
67 child.location.file_name = "path/to/bar.cc"; | 64 child.location.file_name = "path/to/bar.cc"; |
68 child.location.function_name = "FizzBoom"; | 65 child.location.function_name = "FizzBoom"; |
69 child.location.line_number = 433; | 66 child.location.line_number = 433; |
70 child.thread_name = "Chrome_IOThread"; | 67 child.thread_name = "Chrome_IOThread"; |
71 | 68 |
72 | 69 |
73 // Add a snapshot. | 70 // Add a snapshot. |
74 process_data.tasks.push_back(tracked_objects::TaskSnapshot()); | 71 process_data_phase.tasks.push_back(tracked_objects::TaskSnapshot()); |
75 process_data.tasks.back().birth = parent; | 72 process_data_phase.tasks.back().birth = parent; |
76 process_data.tasks.back().death_data.count = 37; | 73 process_data_phase.tasks.back().death_data.count = 37; |
77 process_data.tasks.back().death_data.run_duration_max = 5; | 74 process_data_phase.tasks.back().death_data.run_duration_max = 5; |
78 process_data.tasks.back().death_data.run_duration_sample = 3; | 75 process_data_phase.tasks.back().death_data.run_duration_sample = 3; |
79 process_data.tasks.back().death_data.run_duration_sum = 17; | 76 process_data_phase.tasks.back().death_data.run_duration_sum = 17; |
80 process_data.tasks.back().death_data.queue_duration_max = 53; | 77 process_data_phase.tasks.back().death_data.queue_duration_max = 53; |
81 process_data.tasks.back().death_data.queue_duration_sample = 13; | 78 process_data_phase.tasks.back().death_data.queue_duration_sample = 13; |
82 process_data.tasks.back().death_data.queue_duration_sum = 79; | 79 process_data_phase.tasks.back().death_data.queue_duration_sum = 79; |
83 process_data.tasks.back().death_thread_name = "WorkerPool/-1340960768"; | 80 process_data_phase.tasks.back().death_thread_name = |
| 81 "WorkerPool/-1340960768"; |
84 | 82 |
85 // Add a second snapshot. | 83 // Add a second snapshot. |
86 process_data.tasks.push_back(tracked_objects::TaskSnapshot()); | 84 process_data_phase.tasks.push_back(tracked_objects::TaskSnapshot()); |
87 process_data.tasks.back().birth = child; | 85 process_data_phase.tasks.back().birth = child; |
88 process_data.tasks.back().death_data.count = 41; | 86 process_data_phase.tasks.back().death_data.count = 41; |
89 process_data.tasks.back().death_data.run_duration_max = 205; | 87 process_data_phase.tasks.back().death_data.run_duration_max = 205; |
90 process_data.tasks.back().death_data.run_duration_sample = 203; | 88 process_data_phase.tasks.back().death_data.run_duration_sample = 203; |
91 process_data.tasks.back().death_data.run_duration_sum = 2017; | 89 process_data_phase.tasks.back().death_data.run_duration_sum = 2017; |
92 process_data.tasks.back().death_data.queue_duration_max = 2053; | 90 process_data_phase.tasks.back().death_data.queue_duration_max = 2053; |
93 process_data.tasks.back().death_data.queue_duration_sample = 2013; | 91 process_data_phase.tasks.back().death_data.queue_duration_sample = 2013; |
94 process_data.tasks.back().death_data.queue_duration_sum = 2079; | 92 process_data_phase.tasks.back().death_data.queue_duration_sum = 2079; |
95 process_data.tasks.back().death_thread_name = "PAC thread #3"; | 93 process_data_phase.tasks.back().death_thread_name = "PAC thread #3"; |
96 | 94 |
97 // Add a parent-child pair. | 95 // Add a parent-child pair. |
98 process_data.descendants.push_back( | 96 process_data_phase.descendants.push_back( |
99 tracked_objects::ParentChildPairSnapshot()); | 97 tracked_objects::ParentChildPairSnapshot()); |
100 process_data.descendants.back().parent = parent; | 98 process_data_phase.descendants.back().parent = parent; |
101 process_data.descendants.back().child = child; | 99 process_data_phase.descendants.back().child = child; |
102 | 100 |
103 int process_type = content::PROCESS_TYPE_RENDERER; | 101 int process_type = content::PROCESS_TYPE_RENDERER; |
104 ExpectSerialization(process_data, process_type, | 102 ExpectSerialization(process_data_phase, 239, process_type, |
105 "{" | 103 "{" |
106 "\"descendants\":[" | 104 "\"descendants\":[" |
107 "{" | 105 "{" |
108 "\"child_location\":{" | 106 "\"child_location\":{" |
109 "\"file_name\":\"path/to/bar.cc\"," | 107 "\"file_name\":\"path/to/bar.cc\"," |
110 "\"function_name\":\"FizzBoom\"," | 108 "\"function_name\":\"FizzBoom\"," |
111 "\"line_number\":433" | 109 "\"line_number\":433" |
112 "}," | 110 "}," |
113 "\"child_thread\":\"Chrome_IOThread\"," | 111 "\"child_thread\":\"Chrome_IOThread\"," |
114 "\"parent_location\":{" | 112 "\"parent_location\":{" |
115 "\"file_name\":\"path/to/foo.cc\"," | 113 "\"file_name\":\"path/to/foo.cc\"," |
116 "\"function_name\":\"WhizBang\"," | 114 "\"function_name\":\"WhizBang\"," |
117 "\"line_number\":101" | 115 "\"line_number\":101" |
118 "}," | 116 "}," |
119 "\"parent_thread\":\"CrBrowserMain\"" | 117 "\"parent_thread\":\"CrBrowserMain\"" |
120 "}" | 118 "}" |
121 "]," | 119 "]," |
122 "\"list\":[{" | 120 "\"list\":[{" |
123 "\"birth_location\":{" | 121 "\"birth_location\":{" |
124 "\"file_name\":\"path/to/foo.cc\"," | 122 "\"file_name\":\"path/to/foo.cc\"," |
125 "\"function_name\":\"WhizBang\"," | 123 "\"function_name\":\"WhizBang\"," |
126 "\"line_number\":101" | 124 "\"line_number\":101" |
127 "}," | 125 "}," |
128 "\"birth_thread\":\"CrBrowserMain\"," | 126 "\"birth_thread\":\"CrBrowserMain\"," |
129 "\"death_data\":{" | 127 "\"death_data\":{" |
130 "\"count\":37," | 128 "\"count\":37," |
131 "\"queue_ms\":79," | 129 "\"queue_ms\":79," |
132 "\"queue_ms_max\":53," | 130 "\"queue_ms_max\":53," |
133 "\"queue_ms_sample\":13," | 131 "\"queue_ms_sample\":13," |
134 "\"run_ms\":17," | 132 "\"run_ms\":17," |
135 "\"run_ms_max\":5," | 133 "\"run_ms_max\":5," |
136 "\"run_ms_sample\":3" | 134 "\"run_ms_sample\":3" |
137 "}," | 135 "}," |
138 "\"death_thread\":\"WorkerPool/-1340960768\"" | 136 "\"death_thread\":\"WorkerPool/-1340960768\"" |
139 "},{" | 137 "},{" |
140 "\"birth_location\":{" | 138 "\"birth_location\":{" |
141 "\"file_name\":\"path/to/bar.cc\"," | 139 "\"file_name\":\"path/to/bar.cc\"," |
142 "\"function_name\":\"FizzBoom\"," | 140 "\"function_name\":\"FizzBoom\"," |
143 "\"line_number\":433" | 141 "\"line_number\":433" |
144 "}," | 142 "}," |
145 "\"birth_thread\":\"Chrome_IOThread\"," | 143 "\"birth_thread\":\"Chrome_IOThread\"," |
146 "\"death_data\":{" | 144 "\"death_data\":{" |
147 "\"count\":41," | 145 "\"count\":41," |
148 "\"queue_ms\":2079," | 146 "\"queue_ms\":2079," |
149 "\"queue_ms_max\":2053," | 147 "\"queue_ms_max\":2053," |
150 "\"queue_ms_sample\":2013," | 148 "\"queue_ms_sample\":2013," |
151 "\"run_ms\":2017," | 149 "\"run_ms\":2017," |
152 "\"run_ms_max\":205," | 150 "\"run_ms_max\":205," |
153 "\"run_ms_sample\":203" | 151 "\"run_ms_sample\":203" |
154 "}," | 152 "}," |
155 "\"death_thread\":\"PAC thread #3\"" | 153 "\"death_thread\":\"PAC thread #3\"" |
156 "}]," | 154 "}]," |
157 "\"process_id\":" + GetProcessIdString() + "," | 155 "\"process_id\":239," |
158 "\"process_type\":\"Tab\"" | 156 "\"process_type\":\"Tab\"" |
159 "}"); | 157 "}"); |
160 } | 158 } |
161 } | 159 } |
OLD | NEW |