Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / chrome / test / perf / perf_test.cc
blobf04ee2d71a766bf2c39a376f454f98ffbba07f97
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.
5 #include "chrome/test/perf/perf_test.h"
7 #include <stdio.h>
9 #include "base/logging.h"
10 #include "base/process/process.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/stringprintf.h"
13 #include "chrome/test/base/chrome_process_util.h"
14 #include "testing/perf/perf_test.h"
16 namespace perf_test {
18 void PrintIOPerfInfo(const std::string& test_name,
19 const ChromeProcessList& chrome_processes,
20 base::ProcessId browser_pid) {
21 PrintIOPerfInfo(stdout, test_name, chrome_processes, browser_pid);
24 void PrintIOPerfInfo(FILE* target,
25 const std::string& test_name,
26 const ChromeProcessList& chrome_processes,
27 base::ProcessId browser_pid) {
28 fprintf(target, "%s", IOPerfInfoToString(test_name, chrome_processes,
29 browser_pid).c_str());
32 std::string IOPerfInfoToString(const std::string& test_name,
33 const ChromeProcessList& chrome_processes,
34 base::ProcessId browser_pid) {
35 size_t read_op_b = 0;
36 size_t read_op_r = 0;
37 size_t write_op_b = 0;
38 size_t write_op_r = 0;
39 size_t other_op_b = 0;
40 size_t other_op_r = 0;
41 size_t total_op_b = 0;
42 size_t total_op_r = 0;
44 size_t read_byte_b = 0;
45 size_t read_byte_r = 0;
46 size_t write_byte_b = 0;
47 size_t write_byte_r = 0;
48 size_t other_byte_b = 0;
49 size_t other_byte_r = 0;
50 size_t total_byte_b = 0;
51 size_t total_byte_r = 0;
53 std::string output;
54 ChromeProcessList::const_iterator it;
55 for (it = chrome_processes.begin(); it != chrome_processes.end(); ++it) {
56 base::Process process = base::Process::Open(*it);
57 if (!process.IsValid()) {
58 NOTREACHED();
59 return output;
62 // TODO(sgk): if/when base::ProcessMetrics returns real stats on mac:
63 // scoped_ptr<base::ProcessMetrics> process_metrics(
64 // base::ProcessMetrics::CreateProcessMetrics(process.Handle()));
65 scoped_ptr<ChromeTestProcessMetrics> process_metrics(
66 ChromeTestProcessMetrics::CreateProcessMetrics(process.Handle()));
67 base::IoCounters io_counters;
68 memset(&io_counters, 0, sizeof(io_counters));
70 if (process_metrics.get()->GetIOCounters(&io_counters)) {
71 // Print out IO performance. We assume that the values can be
72 // converted to size_t (they're reported as ULONGLONG, 64-bit numbers).
73 std::string chrome_name = (*it == browser_pid) ? "_b" : "_r";
75 size_t read_op = static_cast<size_t>(io_counters.ReadOperationCount);
76 size_t write_op = static_cast<size_t>(io_counters.WriteOperationCount);
77 size_t other_op = static_cast<size_t>(io_counters.OtherOperationCount);
78 size_t total_op = static_cast<size_t>(io_counters.ReadOperationCount +
79 io_counters.WriteOperationCount +
80 io_counters.OtherOperationCount);
82 size_t read_byte = static_cast<size_t>(io_counters.ReadTransferCount
83 / 1024);
84 size_t write_byte = static_cast<size_t>(io_counters.WriteTransferCount
85 / 1024);
86 size_t other_byte = static_cast<size_t>(io_counters.OtherTransferCount
87 / 1024);
88 size_t total_byte = static_cast<size_t>((io_counters.ReadTransferCount +
89 io_counters.WriteTransferCount +
90 io_counters.OtherTransferCount)
91 / 1024);
93 if (*it == browser_pid) {
94 read_op_b = read_op;
95 write_op_b = write_op;
96 other_op_b = other_op;
97 total_op_b = total_op;
98 read_byte_b = read_byte;
99 write_byte_b = write_byte;
100 other_byte_b = other_byte;
101 total_byte_b = total_byte;
102 } else {
103 read_op_r += read_op;
104 write_op_r += write_op;
105 other_op_r += other_op;
106 total_op_r += total_op;
107 read_byte_r += read_byte;
108 write_byte_r += write_byte;
109 other_byte_r += other_byte;
110 total_byte_r += total_byte;
115 std::string t_name(test_name);
116 AppendResult(output,
117 "read_op_b",
118 std::string(),
119 "r_op_b" + t_name,
120 read_op_b,
121 "ops",
122 false);
123 AppendResult(output,
124 "write_op_b",
125 std::string(),
126 "w_op_b" + t_name,
127 write_op_b,
128 "ops",
129 false);
130 AppendResult(output,
131 "other_op_b",
132 std::string(),
133 "o_op_b" + t_name,
134 other_op_b,
135 "ops",
136 false);
137 AppendResult(output,
138 "total_op_b",
139 std::string(),
140 "IO_op_b" + t_name,
141 total_op_b,
142 "ops",
143 false);
145 AppendResult(output,
146 "read_byte_b",
147 std::string(),
148 "r_b" + t_name,
149 read_byte_b,
150 "kb",
151 false);
152 AppendResult(output,
153 "write_byte_b",
154 std::string(),
155 "w_b" + t_name,
156 write_byte_b,
157 "kb",
158 false);
159 AppendResult(output,
160 "other_byte_b",
161 std::string(),
162 "o_b" + t_name,
163 other_byte_b,
164 "kb",
165 false);
166 AppendResult(output,
167 "total_byte_b",
168 std::string(),
169 "IO_b" + t_name,
170 total_byte_b,
171 "kb",
172 false);
174 AppendResult(output,
175 "read_op_r",
176 std::string(),
177 "r_op_r" + t_name,
178 read_op_r,
179 "ops",
180 false);
181 AppendResult(output,
182 "write_op_r",
183 std::string(),
184 "w_op_r" + t_name,
185 write_op_r,
186 "ops",
187 false);
188 AppendResult(output,
189 "other_op_r",
190 std::string(),
191 "o_op_r" + t_name,
192 other_op_r,
193 "ops",
194 false);
195 AppendResult(output,
196 "total_op_r",
197 std::string(),
198 "IO_op_r" + t_name,
199 total_op_r,
200 "ops",
201 false);
203 AppendResult(output,
204 "read_byte_r",
205 std::string(),
206 "r_r" + t_name,
207 read_byte_r,
208 "kb",
209 false);
210 AppendResult(output,
211 "write_byte_r",
212 std::string(),
213 "w_r" + t_name,
214 write_byte_r,
215 "kb",
216 false);
217 AppendResult(output,
218 "other_byte_r",
219 std::string(),
220 "o_r" + t_name,
221 other_byte_r,
222 "kb",
223 false);
224 AppendResult(output,
225 "total_byte_r",
226 std::string(),
227 "IO_r" + t_name,
228 total_byte_r,
229 "kb",
230 false);
232 return output;
235 void PrintMemoryUsageInfo(const std::string& test_name,
236 const ChromeProcessList& chrome_processes,
237 base::ProcessId browser_pid) {
238 PrintMemoryUsageInfo(stdout, test_name, chrome_processes, browser_pid);
241 void PrintMemoryUsageInfo(FILE* target,
242 const std::string& test_name,
243 const ChromeProcessList& chrome_processes,
244 base::ProcessId browser_pid) {
245 fprintf(target, "%s", MemoryUsageInfoToString(test_name, chrome_processes,
246 browser_pid).c_str());
249 std::string MemoryUsageInfoToString(const std::string& test_name,
250 const ChromeProcessList& chrome_processes,
251 base::ProcessId browser_pid) {
252 size_t browser_virtual_size = 0;
253 size_t browser_working_set_size = 0;
254 size_t renderer_virtual_size = 0;
255 size_t renderer_working_set_size = 0;
256 size_t total_virtual_size = 0;
257 size_t total_working_set_size = 0;
258 #if defined(OS_WIN)
259 size_t browser_peak_virtual_size = 0;
260 size_t browser_peak_working_set_size = 0;
261 size_t renderer_total_peak_virtual_size = 0;
262 size_t renderer_total_peak_working_set_size = 0;
263 size_t renderer_single_peak_virtual_size = 0;
264 size_t renderer_single_peak_working_set_size = 0;
265 #endif
267 std::string output;
268 ChromeProcessList::const_iterator it;
269 for (it = chrome_processes.begin(); it != chrome_processes.end(); ++it) {
270 base::Process process = base::Process::Open(*it);
271 if (!process.IsValid()) {
272 NOTREACHED();
273 return output;
276 // TODO(sgk): if/when base::ProcessMetrics returns real stats on mac:
277 // scoped_ptr<base::ProcessMetrics> process_metrics(
278 // base::ProcessMetrics::CreateProcessMetrics(process.Handle()));
279 scoped_ptr<ChromeTestProcessMetrics> process_metrics(
280 ChromeTestProcessMetrics::CreateProcessMetrics(process.Handle()));
282 size_t current_virtual_size = process_metrics->GetPagefileUsage();
283 size_t current_working_set_size = process_metrics->GetWorkingSetSize();
285 if (*it == browser_pid) {
286 browser_virtual_size = current_virtual_size;
287 browser_working_set_size = current_working_set_size;
288 } else {
289 renderer_virtual_size += current_virtual_size;
290 renderer_working_set_size += current_working_set_size;
292 total_virtual_size += current_virtual_size;
293 total_working_set_size += current_working_set_size;
295 #if defined(OS_WIN)
296 size_t peak_virtual_size = process_metrics->GetPeakPagefileUsage();
297 size_t peak_working_set_size = process_metrics->GetPeakWorkingSetSize();
298 if (*it == browser_pid) {
299 browser_peak_virtual_size = peak_virtual_size;
300 browser_peak_working_set_size = peak_working_set_size;
301 } else {
302 if (peak_virtual_size > renderer_single_peak_virtual_size) {
303 renderer_single_peak_virtual_size = peak_virtual_size;
305 if (peak_working_set_size > renderer_single_peak_working_set_size) {
306 renderer_single_peak_working_set_size = peak_working_set_size;
308 renderer_total_peak_virtual_size += peak_virtual_size;
309 renderer_total_peak_working_set_size += peak_working_set_size;
311 #endif
314 std::string trace_name(test_name);
315 #if defined(OS_WIN)
316 AppendResult(output, "vm_peak_b", "", "vm_pk_b" + trace_name,
317 browser_peak_virtual_size, "bytes",
318 false /* not important */);
319 AppendResult(output, "ws_peak_b", "", "ws_pk_b" + trace_name,
320 browser_peak_working_set_size, "bytes",
321 false /* not important */);
322 AppendResult(output, "vm_peak_r", "", "vm_pk_r" + trace_name,
323 renderer_total_peak_virtual_size, "bytes",
324 false /* not important */);
325 AppendResult(output, "ws_peak_r", "", "ws_pk_r" + trace_name,
326 renderer_total_peak_working_set_size, "bytes",
327 false /* not important */);
328 AppendResult(output, "vm_single_peak_r", "", "vm_spk_r" + trace_name,
329 renderer_single_peak_virtual_size, "bytes",
330 false /* not important */);
331 AppendResult(output, "ws_single_peak_r", "", "ws_spk_r" + trace_name,
332 renderer_single_peak_working_set_size, "bytes",
333 false /* important */);
334 AppendResult(output, "vm_final_b", "", "vm_f_b" + trace_name,
335 browser_virtual_size, "bytes",
336 false /* not important */);
337 AppendResult(output, "ws_final_b", "", "ws_f_b" + trace_name,
338 browser_working_set_size, "bytes",
339 false /* not important */);
340 AppendResult(output, "vm_final_r", "", "vm_f_r" + trace_name,
341 renderer_virtual_size, "bytes",
342 false /* not important */);
343 AppendResult(output, "ws_final_r", "", "ws_f_r" + trace_name,
344 renderer_working_set_size, "bytes",
345 false /* not important */);
346 AppendResult(output, "vm_final_t", "", "vm_f_t" + trace_name,
347 total_virtual_size, "bytes",
348 false /* not important */);
349 AppendResult(output, "ws_final_t", "", "ws_f_t" + trace_name,
350 total_working_set_size, "bytes",
351 false /* not important */);
352 #elif defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_ANDROID)
353 AppendResult(output,
354 "vm_size_final_b",
355 std::string(),
356 "vm_size_f_b" + trace_name,
357 browser_virtual_size,
358 "bytes",
359 false /* not important */);
360 AppendResult(output,
361 "vm_rss_final_b",
362 std::string(),
363 "vm_rss_f_b" + trace_name,
364 browser_working_set_size,
365 "bytes",
366 false /* not important */);
367 AppendResult(output,
368 "vm_size_final_r",
369 std::string(),
370 "vm_size_f_r" + trace_name,
371 renderer_virtual_size,
372 "bytes",
373 false /* not important */);
374 AppendResult(output,
375 "vm_rss_final_r",
376 std::string(),
377 "vm_rss_f_r" + trace_name,
378 renderer_working_set_size,
379 "bytes",
380 false /* not important */);
381 AppendResult(output,
382 "vm_size_final_t",
383 std::string(),
384 "vm_size_f_t" + trace_name,
385 total_virtual_size,
386 "bytes",
387 false /* not important */);
388 AppendResult(output,
389 "vm_rss_final_t",
390 std::string(),
391 "vm_rss_f_t" + trace_name,
392 total_working_set_size,
393 "bytes",
394 false /* not important */);
395 #else
396 NOTIMPLEMENTED();
397 #endif
398 AppendResult(output,
399 "processes",
400 std::string(),
401 "proc_" + trace_name,
402 chrome_processes.size(),
403 "count",
404 false /* not important */);
406 return output;
409 } // namespace perf_test