Home | History | Annotate | Download | only in measurements
      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   options = {'page_repeat': 2}
     14 
     15   def __init__(self):
     16     super(LoadingProfile, self).__init__(discard_first_result=True)
     17 
     18   @property
     19   def results_are_the_same_on_every_page(self):
     20     return False
     21 
     22   def CustomizeBrowserOptions(self, options):
     23     if not perf_profiler.PerfProfiler.is_supported(browser_type='any'):
     24       raise Exception('This measurement is not supported on this platform')
     25 
     26     perf_profiler.PerfProfiler.CustomizeBrowserOptions(
     27         browser_type='any', options=options)
     28 
     29   def WillNavigateToPage(self, page, tab):
     30     tab.browser.StartProfiling(perf_profiler.PerfProfiler.name(),
     31                                os.path.join(tempfile.mkdtemp(),
     32                                             page.file_safe_name))
     33 
     34   def MeasurePage(self, page, tab, results):
     35     # In current telemetry tests, all tests wait for DocumentComplete state,
     36     # but we need to wait for the load event.
     37     tab.WaitForJavaScriptExpression('performance.timing.loadEventStart', 300)
     38 
     39     profile_files = tab.browser.StopProfiling()
     40 
     41     loading.LoadingMetric().AddResults(tab, results)
     42 
     43     profile_file = None
     44     for profile_file in profile_files:
     45       if 'renderer' in profile_file:
     46         break
     47 
     48     for function, period in perf_profiler.PerfProfiler.GetTopSamples(
     49         profile_file, 10).iteritems():
     50       results.Add(function.replace('.', '_'), 'period', period)
     51