1 # Copyright 2013 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. 4 5 import os 6 import tempfile 7 8 from metrics import loading 9 from telemetry.core.platform.profiler import perf_profiler 10 from telemetry.page import page_measurement 11 12 class LoadingProfile(page_measurement.PageMeasurement): 13 def __init__(self): 14 super(LoadingProfile, self).__init__(discard_first_result=True) 15 16 @property 17 def results_are_the_same_on_every_page(self): 18 return False 19 20 def AddCommandLineOptions(self, parser): 21 # In order to change the default of an option, we must remove and re-add it. 22 page_repeat_option = parser.get_option('--page-repeat') 23 page_repeat_option.default = 2 24 parser.remove_option('--page-repeat') 25 parser.add_option(page_repeat_option) 26 27 def CustomizeBrowserOptions(self, options): 28 if not perf_profiler.PerfProfiler.is_supported(browser_type='any'): 29 raise Exception('This measurement is not supported on this platform') 30 31 perf_profiler.PerfProfiler.CustomizeBrowserOptions( 32 browser_type='any', options=options) 33 34 def WillNavigateToPage(self, page, tab): 35 tab.browser.StartProfiling(perf_profiler.PerfProfiler.name(), 36 os.path.join(tempfile.mkdtemp(), 37 page.file_safe_name)) 38 39 def MeasurePage(self, page, tab, results): 40 # In current telemetry tests, all tests wait for DocumentComplete state, 41 # but we need to wait for the load event. 42 tab.WaitForJavaScriptExpression('performance.timing.loadEventStart', 300) 43 44 profile_files = tab.browser.StopProfiling() 45 46 loading.LoadingMetric().AddResults(tab, results) 47 48 profile_file = None 49 for profile_file in profile_files: 50 if 'renderer' in profile_file: 51 break 52 53 for function, period in perf_profiler.PerfProfiler.GetTopSamples( 54 profile_file, 10).iteritems(): 55 results.Add(function.replace('.', '_'), 'period', period) 56