[Chromoting] Add app_remoting/internal to .gitignore
[chromium-blink-merge.git] / tools / profile_chrome / chrome_startup_controller.py
blob8852cfe622ce9da5fd1426651d484ed416fc14d1
1 # Copyright 2015 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 import os
6 import re
7 import time
9 from pylib import flag_changer
10 from pylib.perf import cache_control
12 from profile_chrome import controllers
14 class ChromeStartupTracingController(controllers.BaseController):
15 def __init__(self, device, package_info, cold, url):
16 self._device = device
17 self._package_info = package_info
18 self._cold = cold
19 self._url = url
20 self._trace_file = None
21 self._trace_finish_re = re.compile(r' Completed startup tracing to (.*)')
22 self._device.old_interface.StartMonitoringLogcat(clear=False)
24 def __repr__(self):
25 return 'Browser Startup Trace'
27 def _SetupTracing(self):
28 # TODO(lizeb): Figure out how to clean up the command-line file when
29 # _TearDownTracing() is not executed in StopTracing().
30 changer = flag_changer.FlagChanger(
31 self._device, self._package_info.cmdline_file)
32 changer.AddFlags(['--trace-startup'])
33 self._device.old_interface.CloseApplication(self._package_info.package)
34 if self._cold:
35 self._device.old_interface.EnableAdbRoot()
36 cache_control.CacheControl(self._device).DropRamCaches()
37 self._device.old_interface.StartActivity(
38 package=self._package_info.package,
39 activity=self._package_info.activity,
40 data=self._url,
41 extras={'create_new_tab' : True})
43 def _TearDownTracing(self):
44 changer = flag_changer.FlagChanger(
45 self._device, self._package_info.cmdline_file)
46 changer.RemoveFlags(['--trace-startup'])
48 def StartTracing(self, interval):
49 self._SetupTracing()
50 self._device.old_interface.SyncLogCat()
52 def StopTracing(self):
53 try:
54 self._trace_file = self._device.old_interface.WaitForLogMatch(
55 self._trace_finish_re, None, timeout=10).group(1)
56 finally:
57 self._TearDownTracing()
59 def PullTrace(self):
60 # Wait a bit for the browser to finish writing the trace file.
61 time.sleep(3)
62 trace_file = self._trace_file.replace('/storage/emulated/0/', '/sdcard/')
63 host_file = os.path.join(os.path.curdir, os.path.basename(trace_file))
64 self._device.PullFile(trace_file, host_file)
65 return host_file