1#!/usr/bin/python2 2# 3# Copyright 2010 Google Inc. All Rights Reserved. 4"""Script to run ChromeOS benchmarks 5 6Inputs: 7 chromeos_root 8 board 9 [chromeos/cpu/<benchname>| 10 chromeos/browser/[pagecycler|sunspider]| 11 chromeos/startup] 12 hostname/IP of Chromeos machine 13 14 chromeos/cpu/<benchname> 15 - Read run script rules from bench.mk perflab-bin, copy benchmark to 16 host, run 17 and return results. 18 19 chromeos/startup 20 - Re-image host with image in perflab-bin 21 - Call run_tests to run startup test, gather results. 22 - Restore host back to what it was. 23 24 chromeos/browser/* 25 - Call build_chromebrowser to build image with new browser 26 - Copy image to perflab-bin 27 28""" 29 30from __future__ import print_function 31 32__author__ = 'bjanakiraman@google.com (Bhaskar Janakiraman)' 33 34import argparse 35import os 36import re 37import sys 38 39import image_chromeos 40import run_tests 41from cros_utils import command_executer 42from cros_utils import logger 43 44# pylint: disable=anomalous-backslash-in-string 45 46KNOWN_BENCHMARKS = [ 47 'chromeos/startup', 'chromeos/browser/pagecycler', 48 'chromeos/browser/sunspider', 'chromeos/browser/v8bench', 49 'chromeos/cpu/bikjmp' 50] 51 52name_map = { 53 'pagecycler': 'Page', 54 'sunspider': 'SunSpider', 55 'v8bench': 'V8Bench', 56 'startup': 'BootPerfServer' 57} 58 59# Run command template 60 61# Common initializations 62cmd_executer = command_executer.GetCommandExecuter() 63 64 65def Usage(parser, message): 66 print('ERROR: %s' % message) 67 parser.print_help() 68 sys.exit(0) 69 70 71def RunBrowserBenchmark(chromeos_root, board, bench, machine): 72 """Run browser benchmarks. 73 74 Args: 75 chromeos_root: ChromeOS src dir 76 board: Board being tested 77 bench: Name of benchmark (chromeos/browser/*) 78 machine: name of chromeos machine 79 """ 80 benchname = re.split('/', bench)[2] 81 benchname = name_map[benchname] 82 ret = run_tests.RunRemoteTests(chromeos_root, machine, board, benchname) 83 return ret 84 85 86def RunStartupBenchmark(chromeos_root, board, machine): 87 """Run browser benchmarks. 88 89 Args: 90 chromeos_root: ChromeOS src dir 91 board: Board being tested 92 machine: name of chromeos machine 93 """ 94 benchname = 'startup' 95 benchname = name_map[benchname] 96 ret = run_tests.RunRemoteTests(chromeos_root, machine, board, benchname) 97 return ret 98 99 100def RunCpuBenchmark(chromeos_root, bench, workdir, machine): 101 """Run CPU benchmark. 102 103 Args: 104 chromeos_root: ChromeOS src dir 105 bench: Name of benchmark 106 workdir: directory containing benchmark directory 107 machine: name of chromeos machine 108 109 Returns: 110 status: 0 on success 111 """ 112 113 benchname = re.split('/', bench)[2] 114 benchdir = '%s/%s' % (workdir, benchname) 115 116 # Delete any existing run directories on machine. 117 # Since this has exclusive access to the machine, 118 # we do not worry about duplicates. 119 args = 'rm -rf /tmp/%s' % benchname 120 retv = cmd_executer.CrosRunCommand(args, 121 chromeos_root=chromeos_root, 122 machine=machine) 123 if retv: 124 return retv 125 126 # Copy benchmark directory. 127 retv = cmd_executer.CopyFiles(benchdir, 128 '/tmp/' + benchname, 129 chromeos_root=chromeos_root, 130 dest_machine=machine, 131 dest_cros=True) 132 if retv: 133 return retv 134 135 # Parse bench.mk to extract run flags. 136 137 benchmk_file = open('%s/bench.mk' % benchdir, 'r') 138 for line in benchmk_file: 139 line.rstrip() 140 if re.match('^run_cmd', line): 141 line = re.sub('^run_cmd.*\${PERFLAB_PATH}', './out', line) 142 line = re.sub('\${PERFLAB_INPUT}', './data', line) 143 run_cmd = line 144 break 145 146 # Execute on remote machine 147 # Capture output and process it. 148 sshargs = "'cd /tmp/%s;" % benchname 149 sshargs += "time -p %s'" % run_cmd 150 cmd_executer.CrosRunCommand(sshargs, 151 chromeos_root=chromeos_root, 152 machine=machine) 153 154 return retv 155 156 157def Main(argv): 158 """Build ChromeOS.""" 159 # Common initializations 160 161 parser = argparse.ArgumentParser() 162 parser.add_argument('-c', 163 '--chromeos_root', 164 dest='chromeos_root', 165 help='Target directory for ChromeOS installation.') 166 parser.add_argument('-m', 167 '--machine', 168 dest='machine', 169 help='The chromeos host machine.') 170 parser.add_argument('--workdir', 171 dest='workdir', 172 default='./perflab-bin', 173 help='Work directory for perflab outputs.') 174 parser.add_argument('--board', 175 dest='board', 176 help='ChromeOS target board, e.g. x86-generic') 177 parser.add_argumen('args', margs='+', help='Benchmarks to run.') 178 179 options = parser.parse_args(argv[1:]) 180 181 # validate args 182 for arg in options.args: 183 if arg not in KNOWN_BENCHMARKS: 184 logger.GetLogger().LogFatal('Bad benchmark %s specified' % arg) 185 186 if options.chromeos_root is None: 187 Usage(parser, '--chromeos_root must be set') 188 189 if options.board is None: 190 Usage(parser, '--board must be set') 191 192 if options.machine is None: 193 Usage(parser, '--machine must be set') 194 195 found_err = 0 196 retv = 0 197 for arg in options.args: 198 # CPU benchmarks 199 comps = re.split('/', arg) 200 if re.match('chromeos/cpu', arg): 201 benchname = comps[2] 202 print('RUNNING %s' % benchname) 203 retv = RunCpuBenchmark(options.chromeos_root, arg, options.workdir, 204 options.machine) 205 if not found_err: 206 found_err = retv 207 elif re.match('chromeos/startup', arg): 208 benchname = comps[1] 209 image_args = [ 210 os.path.dirname(os.path.abspath(__file__)) + '/image_chromeos.py', 211 '--chromeos_root=' + options.chromeos_root, 212 '--remote=' + options.machine, '--image=' + options.workdir + '/' + 213 benchname + '/chromiumos_image.bin' 214 ] 215 logger.GetLogger().LogOutput('Reimaging machine %s' % options.machine) 216 image_chromeos.Main(image_args) 217 218 logger.GetLogger().LogOutput('Running %s' % arg) 219 retv = RunStartupBenchmark(options.chromeos_root, options.board, 220 options.machine) 221 if not found_err: 222 found_err = retv 223 elif re.match('chromeos/browser', arg): 224 benchname = comps[2] 225 image_args = [ 226 os.path.dirname(os.path.abspath(__file__)) + '/image_chromeos.py', 227 '--chromeos_root=' + options.chromeos_root, 228 '--remote=' + options.machine, '--image=' + options.workdir + '/' + 229 benchname + '/chromiumos_image.bin' 230 ] 231 logger.GetLogger().LogOutput('Reimaging machine %s' % options.machine) 232 image_chromeos.Main(image_args) 233 234 logger.GetLogger().LogOutput('Running %s' % arg) 235 retv = RunBrowserBenchmark(options.chromeos_root, options.board, arg, 236 options.machine) 237 if not found_err: 238 found_err = retv 239 240 return found_err 241 242 243if __name__ == '__main__': 244 retval = Main(sys.argv) 245 sys.exit(retval) 246