Home | History | Annotate | Download | only in measurements
      1 # Copyright (c) 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 sys
      7 
      8 from telemetry.page import page_measurement
      9 from telemetry.page import page_set
     10 
     11 
     12 def CreatePageSetFromPath(path):
     13   assert os.path.exists(path)
     14 
     15   page_set_dict = {'pages': []}
     16 
     17   def _AddPage(path):
     18     if not path.endswith('.html'):
     19       return
     20     if '../' in open(path, 'r').read():
     21       # If the page looks like it references its parent dir, include it.
     22       page_set_dict['serving_dirs'] = [os.path.dirname(os.path.dirname(path))]
     23     page_set_dict['pages'].append({'url':
     24                                    'file://' + path.replace('\\', '/')})
     25 
     26   def _AddDir(dir_path, skipped):
     27     for candidate_path in os.listdir(dir_path):
     28       if candidate_path == 'resources':
     29         continue
     30       candidate_path = os.path.join(dir_path, candidate_path)
     31       if candidate_path.startswith(tuple([os.path.join(path, s)
     32                                           for s in skipped])):
     33         continue
     34       if os.path.isdir(candidate_path):
     35         _AddDir(candidate_path, skipped)
     36       else:
     37         _AddPage(candidate_path)
     38 
     39   if os.path.isdir(path):
     40     skipped = []
     41     skipped_file = os.path.join(path, 'Skipped')
     42     if os.path.exists(skipped_file):
     43       for line in open(skipped_file, 'r').readlines():
     44         line = line.strip()
     45         if line and not line.startswith('#'):
     46           skipped.append(line.replace('/', os.sep))
     47     _AddDir(path, skipped)
     48   else:
     49     _AddPage(path)
     50   return page_set.PageSet.FromDict(page_set_dict, os.getcwd() + os.sep)
     51 
     52 
     53 class BlinkPerfMeasurement(page_measurement.PageMeasurement):
     54   """Tuns a blink performance test and reports the results."""
     55   def __init__(self):
     56     super(BlinkPerfMeasurement, self).__init__('')
     57     with open(os.path.join(os.path.dirname(__file__),
     58                            'blink_perf.js'), 'r') as f:
     59       self._blink_perf_js = f.read()
     60 
     61   def CreatePageSet(self, args, options):
     62     if len(args) < 2:
     63       print 'Must specify a file or directory to run.'
     64       sys.exit(1)
     65 
     66     page_set_arg = args[1]
     67 
     68     if not os.path.exists(page_set_arg):
     69       print '%s does not exist.' % page_set_arg
     70       sys.exit(1)
     71 
     72     return CreatePageSetFromPath(page_set_arg)
     73 
     74   @property
     75   def results_are_the_same_on_every_page(self):
     76     return False
     77 
     78   def WillNavigateToPage(self, page, tab):
     79     page.script_to_evaluate_on_commit = self._blink_perf_js
     80 
     81   def CustomizeBrowserOptions(self, options):
     82     options.AppendExtraBrowserArgs([
     83         '--js-flags=--expose_gc',
     84         '--enable-experimental-web-platform-features'
     85     ])
     86 
     87   def MeasurePage(self, page, tab, results):
     88     tab.WaitForJavaScriptExpression('testRunner.isDone', 600)
     89 
     90     log = tab.EvaluateJavaScript('document.getElementById("log").innerHTML')
     91 
     92     for line in log.splitlines():
     93       if not line.startswith('values '):
     94         continue
     95       parts = line.split()
     96       values = [float(v.replace(',', '')) for v in parts[1:-1]]
     97       units = parts[-1]
     98       metric = page.display_name.split('.')[0].replace('/', '_')
     99       results.Add(metric, units, values)
    100       break
    101 
    102     print log
    103