| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 import json |
| 5 import time | 6 import time |
| 6 | 7 |
| 7 from profile_chrome import chrome_startup_tracing_agent | 8 from profile_chrome import chrome_startup_tracing_agent |
| 8 from profile_chrome import chrome_tracing_agent | 9 from profile_chrome import chrome_tracing_agent |
| 9 from profile_chrome import ui | 10 from profile_chrome import ui |
| 10 from profile_chrome import util | 11 from profile_chrome import util |
| 11 from systrace import output_generator | |
| 12 from systrace import tracing_controller | 12 from systrace import tracing_controller |
| 13 | 13 |
| 14 | 14 |
| 15 def _GetResults(trace_results, controller, output, compress, write_json, | 15 def _GetResults(trace_results, controller, output, compress, write_json, |
| 16 interval): | 16 interval): |
| 17 ui.PrintMessage('Downloading...') | 17 ui.PrintMessage('Downloading...') |
| 18 | 18 |
| 19 # Wait for the trace file to get written. | 19 # Wait for the trace file to get written. |
| 20 time.sleep(1) | 20 time.sleep(1) |
| 21 | 21 |
| 22 for agent in controller.get_child_agents: | 22 for agent in controller.get_child_agents: |
| 23 if isinstance(agent, chrome_tracing_agent.ChromeTracingAgent): | 23 if isinstance(agent, chrome_tracing_agent.ChromeTracingAgent): |
| 24 time.sleep(interval / 4) | 24 time.sleep(interval / 4) |
| 25 | 25 |
| 26 # Ignore the systraceController because it will not contain any results, | 26 trace_results = controller.all_results |
| 27 # instead being in charge of collecting results. | |
| 28 trace_results = [x for x in controller.all_results if not (x.source_name == | |
| 29 'systraceController')] | |
| 30 | 27 |
| 31 if not trace_results: | 28 if not trace_results: |
| 32 ui.PrintMessage('No results') | 29 ui.PrintMessage('No results') |
| 33 return '' | 30 return '' |
| 34 | 31 |
| 35 result = None | 32 result = None |
| 36 trace_results = output_generator.MergeTraceResultsIfNeeded(trace_results) | 33 trace_name = 'profile_chrome' |
| 34 |
| 37 if not write_json: | 35 if not write_json: |
| 38 ui.PrintMessage('Writing trace HTML...') | 36 ui.PrintMessage('Writing trace HTML...') |
| 39 html_file = trace_results[0].source_name + '.html' | 37 html_file = trace_name + '.html' |
| 40 result = output_generator.GenerateHTMLOutput(trace_results, html_file) | 38 trace_results.Serialize(html_file, 'Profile Chrome') |
| 39 result = html_file |
| 41 ui.PrintMessage('\nWrote file://%s' % result) | 40 ui.PrintMessage('\nWrote file://%s' % result) |
| 42 elif compress and len(trace_results) == 1: | 41 elif compress: |
| 43 result = output or trace_results[0].source_name + '.gz' | 42 result = output or trace_name + '.gz' |
| 44 util.WriteDataToCompressedFile(trace_results[0].raw_data, result) | 43 util.WriteDataToCompressedFile(trace_results._raw_data, result) |
| 45 elif len(trace_results) > 1: | |
| 46 result = (output or 'chrome-combined-trace-%s.zip' % | |
| 47 util.GetTraceTimestamp()) | |
| 48 util.ArchiveData(trace_results, result) | |
| 49 elif output: | 44 elif output: |
| 50 result = output | 45 result = output |
| 51 with open(result, 'wb') as f: | 46 with open(result, 'wb') as f: |
| 52 f.write(trace_results[0].raw_data) | 47 json.dump(trace_results._raw_data, f, indent=4, |
| 48 separators=(',', ':')) |
| 53 else: | 49 else: |
| 54 result = trace_results[0].source_name | 50 result = trace_name + '.json' |
| 55 with open(result, 'wb') as f: | 51 with open(result, 'wb') as f: |
| 56 f.write(trace_results[0].raw_data) | 52 json.dump(trace_results._raw_data, f, indent=4, separators=(',', ':')) |
| 57 | 53 |
| 58 return result | 54 return result |
| 59 | 55 |
| 60 | 56 |
| 61 def CaptureProfile(options, interval, modules, output=None, | 57 def CaptureProfile(options, interval, modules, output=None, |
| 62 compress=False, write_json=False): | 58 compress=False, write_json=False): |
| 63 """Records a profiling trace saves the result to a file. | 59 """Records a profiling trace saves the result to a file. |
| 64 | 60 |
| 65 Args: | 61 Args: |
| 66 options: Command line options. | 62 options: Command line options. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 raw_input() | 95 raw_input() |
| 100 | 96 |
| 101 ui.PrintMessage('Stopping...') | 97 ui.PrintMessage('Stopping...') |
| 102 all_results = controller.StopTracing() | 98 all_results = controller.StopTracing() |
| 103 finally: | 99 finally: |
| 104 if interval: | 100 if interval: |
| 105 ui.PrintMessage('done') | 101 ui.PrintMessage('done') |
| 106 | 102 |
| 107 return _GetResults(all_results, controller, output, compress, write_json, | 103 return _GetResults(all_results, controller, output, compress, write_json, |
| 108 interval) | 104 interval) |
| OLD | NEW |