1#!/usr/bin/perl -w 2 3# Copyright (C) 2007 Apple Inc. All rights reserved. 4# Copyright (C) 2007 Eric Seidel <eric@webkit.org> 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: 9# 1. Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# 2. Redistributions in binary form must reproduce the above copyright 12# notice, this list of conditions and the following disclaimer in the 13# documentation and/or other materials provided with the distribution. 14# 15# THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 16# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 19# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 27use strict; 28use Getopt::Long; 29use File::Basename; 30use File::Spec; 31use Cwd; 32use POSIX qw(strftime); 33use Time::HiRes qw(gettimeofday tv_interval); 34 35my $showHelp = 0; 36my $runShark = 0; 37my $runShark20 = 0; 38my $runSharkCache = 0; 39my $ubench = 0; 40my $v8suite = 0; 41my $suite = ""; 42my $parseOnly = 0; 43my $jsShellPath; 44my $jsShellArgs = ""; 45my $setBaseline = 0; 46my $testsPattern; 47my $testRuns = 10; 48 49my $programName = basename($0); 50my $usage = <<EOF; 51Usage: $programName --shell=[path] [options] 52 --help Show this help message 53 --set-baseline Set baseline for future comparisons 54 --shell Path to JavaScript shell 55 --args Arguments to pass to JavaScript shell 56 --runs Number of times to run tests (default: $testRuns) 57 --tests Only run tests matching provided pattern 58 --shark Sample execution time with the Mac OS X "Shark" performance testing tool (implies --runs=1) 59 --shark20 Like --shark, but with a 20 microsecond sampling interval 60 --shark-cache Like --shark, but performs a L2 cache-miss sample instead of time sample 61 --suite Select a specific benchmark suite. The default is sunspider-0.9.1 62 --ubench Use microbenchmark suite instead of regular tests. Same as --suite=ubench 63 --v8-suite Use the V8 benchmark suite. Same as --suite=v8-v4 64 --parse-only Use the parse-only benchmark suite. Same as --suite=parse-only 65EOF 66 67GetOptions('runs=i' => \$testRuns, 68 'set-baseline' => \$setBaseline, 69 'shell=s' => \$jsShellPath, 70 'args=s' => \$jsShellArgs, 71 'shark' => \$runShark, 72 'shark20' => \$runShark20, 73 'shark-cache' => \$runSharkCache, 74 'suite=s' => \$suite, 75 'ubench' => \$ubench, 76 'v8-suite' => \$v8suite, 77 'parse-only' => \$parseOnly, 78 'tests=s' => \$testsPattern, 79 'help' => \$showHelp); 80 81 82$suite = "ubench" if ($ubench); 83$suite = "v8-v4" if ($v8suite); 84$suite = "parse-only" if ($parseOnly); 85$suite = "sunspider-0.9.1" if (!$suite); 86 87my $resultDirectory = "${suite}-results"; 88 89my $suitePath = $suite; 90$suitePath = "tests/" . $suitePath unless ($suite =~ /\//); 91 92$runShark = 1 if $runSharkCache; 93$runShark = 20 if $runShark20; 94$testRuns = 1 if $runShark; 95if ($runShark && ! -x "/usr/bin/shark") { 96 die "Please install CHUD tools from http://developer.apple.com/tools/download/\n"; 97} 98 99my $sharkCacheProfileIndex = 0; 100if ($runSharkCache) { 101 my $sharkProfileList = `shark -l 2>&1`; 102 for my $profile (split(/\n/, $sharkProfileList)) { 103 $profile =~ /(\d+) - (.+)/; 104 next unless (defined $1); 105 my $profileIndex = $1; 106 my $profileName = $2; 107 if ($profileName =~ /L2 Cache/) { 108 $sharkCacheProfileIndex = $profileIndex; 109 print "Using Shark L2 Cache Miss Profile: " . $profile . "\n"; 110 last; 111 } 112 } 113 die "Failed to find L2 Cache Miss Profile for --shark-cache\n" unless ($sharkCacheProfileIndex); 114} 115 116if (!$jsShellPath || $showHelp) { 117 print STDERR $usage; 118 exit 1; 119} 120 121sub dumpToFile($$) 122{ 123 my ($contents, $path) = @_; 124 open FILE, ">", $path or die "Failed to open $path"; 125 print FILE $contents; 126 close FILE; 127} 128 129my @tests = (); 130my @categories = (); 131my %uniqueCategories = (); 132 133sub loadTestsList() 134{ 135 open TESTLIST, "<", "${suitePath}/LIST" or die "Can't find ${suitePath}/LIST"; 136 while (<TESTLIST>) { 137 chomp; 138 next unless !$testsPattern || /$testsPattern/; 139 140 push @tests, $_; 141 my $category = $_; 142 $category =~ s/-.*//; 143 if (!$uniqueCategories{$category}) { 144 push @categories, $category; 145 $uniqueCategories{$category} = $category; 146 } 147 } 148 close TESTLIST; 149} 150 151my $timeString = strftime "%Y-%m-%d-%H.%M.%S", localtime $^T; 152my $prefixFile = "$resultDirectory/sunspider-test-prefix.js"; 153my $resultsFile = "$resultDirectory/sunspider-results-$timeString.js"; 154 155sub writePrefixFile() 156{ 157 my $prefix = "var suitePath = " . '"' . $suitePath . '"' . ";\n"; 158 $prefix .= "var tests = [ " . join(", ", map { '"' . $_ . '"' } @tests) . " ];\n"; 159 $prefix .= "var categories = [ " . join(", ", map { '"' . $_ . '"' } @categories) . " ];\n"; 160 161 mkdir "$resultDirectory"; 162 dumpToFile($prefix, $prefixFile); 163} 164 165sub runTestsOnce($) 166{ 167 my ($useShark) = @_; 168 my $shellArgs = $jsShellArgs . " -f $prefixFile -f resources/sunspider-standalone-driver.js 2> " . File::Spec->devnull(); 169 my $output; 170 if ($useShark) { 171 my $intervalArg = $useShark == 20 ? "-I 20u" : ""; 172 my $cacheArg = $runSharkCache ? "-c $sharkCacheProfileIndex" : ""; 173 $output = `shark $intervalArg $cacheArg -i -1-q "$jsShellPath" $shellArgs`; 174 } else { 175 $output = `"$jsShellPath" $shellArgs | grep -v break`; 176 } 177 return $output; 178} 179 180sub newestFile($$) 181{ 182 my ($dir, $pattern) = @_; 183 184 my $newestAge; 185 my $newestFile = ""; 186 opendir DIR, $dir or die; 187 for my $file (readdir DIR) { 188 if ($file =~ $pattern) { 189 my $age = -M "$dir/$file"; 190 if (!defined $newestAge || $age < $newestAge) { 191 $newestFile = $file; 192 $newestAge = $age; 193 } 194 } 195 } 196 closedir DIR; 197 198 return "$dir/$newestFile"; 199} 200 201loadTestsList(); 202if ($testsPattern) { 203 print STDERR "Found " . scalar(@tests) . " tests matching '" . $testsPattern . "'\n"; 204} else { 205 print STDERR "Found " . scalar(@tests) . " tests\n"; 206} 207die "No tests to run" unless scalar(@tests); 208print STDERR "Running SunSpider once for warmup, then " . ($runShark ? "under Shark" : "$testRuns time" . ($testRuns == 1 ? "" : "s")) . "\n"; 209writePrefixFile(); 210 211runTestsOnce(0); 212print "Discarded first run.\n"; 213 214my $result; 215my $count = 0; 216my @results = (); 217my $total = 0; 218print "["; 219while ($count++ < $testRuns) { 220 $result = runTestsOnce($runShark); 221 $result =~ s/\r\n/\n/g; 222 chomp $result; 223 push @results, $result; 224 print $result; 225 print ",\n" unless ($count == $testRuns); 226} 227print "]\n"; 228 229my $output = "var output = [\n" . join(",\n", @results) . "\n];\n"; 230dumpToFile($output, $resultsFile); 231dumpToFile(File::Spec->rel2abs($resultsFile), "$resultDirectory/baseline-filename.txt") if $setBaseline; 232 233system("$jsShellPath", "-f", $prefixFile, "-f", $resultsFile, "-f", "resources/sunspider-analyze-results.js"); 234 235print("\nResults are located at $resultsFile\n"); 236 237if ($runShark) { 238 my $newestMShark = newestFile(".", qr/\.mshark$/); 239 if ($newestMShark) { 240 my $profileFile = "$resultDirectory/sunspider-profile-$timeString.mshark"; 241 rename $newestMShark, $profileFile or die; 242 exec "/usr/bin/open", $profileFile; 243 } 244} 245