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"
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"
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
) {
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;
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()) {
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
84 size_t write_byte
= static_cast<size_t>(io_counters
.WriteTransferCount
86 size_t other_byte
= static_cast<size_t>(io_counters
.OtherTransferCount
88 size_t total_byte
= static_cast<size_t>((io_counters
.ReadTransferCount
+
89 io_counters
.WriteTransferCount
+
90 io_counters
.OtherTransferCount
)
93 if (*it
== browser_pid
) {
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
;
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
);
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;
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;
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()) {
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
;
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
;
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
;
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
;
314 std::string
trace_name(test_name
);
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)
356 "vm_size_f_b" + trace_name
,
357 browser_virtual_size
,
359 false /* not important */);
363 "vm_rss_f_b" + trace_name
,
364 browser_working_set_size
,
366 false /* not important */);
370 "vm_size_f_r" + trace_name
,
371 renderer_virtual_size
,
373 false /* not important */);
377 "vm_rss_f_r" + trace_name
,
378 renderer_working_set_size
,
380 false /* not important */);
384 "vm_size_f_t" + trace_name
,
387 false /* not important */);
391 "vm_rss_f_t" + trace_name
,
392 total_working_set_size
,
394 false /* not important */);
401 "proc_" + trace_name
,
402 chrome_processes
.size(),
404 false /* not important */);
409 } // namespace perf_test