3 # Copyright (C) 2007 Apple Inc. All rights reserved.
4 # Copyright (C) 2007 Eric Seidel <eric@webkit.org>
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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.
32 use POSIX qw(strftime);
33 use Time::HiRes qw(gettimeofday tv_interval);
38 my $runSharkCache = 0;
39 my $runInstruments = 0;
50 my $programName = basename($0);
52 Usage: $programName --shell=[path] [options]
53 --help Show this help message
54 --set-baseline Set baseline for future comparisons
55 --shell Path to JavaScript shell
56 --args Arguments to pass to JavaScript shell
57 --runs Number of times to run tests (default: $testRuns)
58 --tests Only run tests matching provided pattern
59 --shark Sample execution time with the Mac OS X "Shark" performance testing tool (implies --runs=1)
60 --shark20 Like --shark, but with a 20 microsecond sampling interval
61 --shark-cache Like --shark, but performs a L2 cache-miss sample instead of time sample
62 --instruments Sample execution time with the Mac OS X "Instruments" tool (Time Profile) (implies --runs=1)
63 --suite Select a specific benchmark suite. The default is sunspider-1.0
64 --ubench Use microbenchmark suite instead of regular tests. Same as --suite=ubench
65 --v8-suite Use the V8 benchmark suite. Same as --suite=v8-v4
66 --parse-only Use the parse-only benchmark suite. Same as --suite=parse-only
69 GetOptions('runs=i' => \$testRuns,
70 'set-baseline' => \$setBaseline,
71 'shell=s' => \$jsShellPath,
72 'args=s' => \$jsShellArgs,
73 'shark' => \$runShark,
74 'shark20' => \$runShark20,
75 'shark-cache' => \$runSharkCache,
76 'instruments' => \$runInstruments,
79 'v8-suite' => \$v8suite,
80 'parse-only' => \$parseOnly,
81 'tests=s' => \$testsPattern,
82 'help' => \$showHelp);
85 $suite = "ubench" if ($ubench);
86 $suite = "v8-v4" if ($v8suite);
87 $suite = "parse-only" if ($parseOnly);
88 $suite = "sunspider-1.0" if (!$suite);
90 my $resultDirectory = "${suite}-results";
92 my $suitePath = $suite;
93 $suitePath = "tests/" . $suitePath unless ($suite =~ /\//);
95 $runShark = 1 if $runSharkCache;
96 $runShark = 20 if $runShark20;
97 $testRuns = 1 if $runShark || $runInstruments;
98 if ($runShark && ! -x "/usr/bin/shark") {
99 die "Please install CHUD tools from http://developer.apple.com/tools/download/\n";
102 my $sharkCacheProfileIndex = 0;
103 if ($runSharkCache) {
104 my $sharkProfileList = `shark -l 2>&1`;
105 for my $profile (split(/\n/, $sharkProfileList)) {
106 $profile =~ /(\d+) - (.+)/;
107 next unless (defined $1);
108 my $profileIndex = $1;
109 my $profileName = $2;
110 if ($profileName =~ /L2 Cache/) {
111 $sharkCacheProfileIndex = $profileIndex;
112 print "Using Shark L2 Cache Miss Profile: " . $profile . "\n";
116 die "Failed to find L2 Cache Miss Profile for --shark-cache\n" unless ($sharkCacheProfileIndex);
119 if (!$jsShellPath || $showHelp) {
126 my ($contents, $path) = @_;
127 open FILE, ">", $path or die "Failed to open $path";
128 print FILE $contents;
134 my %uniqueCategories = ();
138 open TESTLIST, "<", "${suitePath}/LIST" or die "Can't find ${suitePath}/LIST";
141 next unless !$testsPattern || /$testsPattern/;
145 $category =~ s/-.*//;
146 if (!$uniqueCategories{$category}) {
147 push @categories, $category;
148 $uniqueCategories{$category} = $category;
154 my $timeString = strftime "%Y-%m-%d-%H.%M.%S", localtime $^T;
155 my $prefixFile = "$resultDirectory/sunspider-test-prefix.js";
156 my $resultsFile = "$resultDirectory/sunspider-results-$timeString.js";
158 sub writePrefixFile()
160 my $prefix = "var suitePath = " . '"' . $suitePath . '"' . ";\n";
161 $prefix .= "var tests = [ " . join(", ", map { '"' . $_ . '"' } @tests) . " ];\n";
162 $prefix .= "var categories = [ " . join(", ", map { '"' . $_ . '"' } @categories) . " ];\n";
164 mkdir "$resultDirectory";
165 dumpToFile($prefix, $prefixFile);
170 my ($useShark, $useInstruments) = @_;
171 my $shellArgs = $jsShellArgs . " -f $prefixFile -f resources/sunspider-standalone-driver.js 2> " . File::Spec->devnull();
174 my $intervalArg = $useShark == 20 ? "-I 20u" : "";
175 my $cacheArg = $runSharkCache ? "-c $sharkCacheProfileIndex" : "";
176 $output = `shark $intervalArg $cacheArg -i -1-q "$jsShellPath" $shellArgs`;
177 } elsif ($useInstruments) {
178 $output = `instruments -t "resources/TimeProfile20us.tracetemplate" "$jsShellPath" $shellArgs`;
180 $output = `"$jsShellPath" $shellArgs | grep -v break`;
187 my ($dir, $pattern) = @_;
191 opendir DIR, $dir or die;
192 for my $file (readdir DIR) {
193 if ($file =~ $pattern) {
194 my $age = -M "$dir/$file";
195 if (!defined $newestAge || $age < $newestAge) {
203 return "$dir/$newestFile";
208 print STDERR "Found " . scalar(@tests) . " tests matching '" . $testsPattern . "'\n";
210 print STDERR "Found " . scalar(@tests) . " tests\n";
212 die "No tests to run" unless scalar(@tests);
213 print STDERR "Running SunSpider once for warmup, then " . ($runShark ? "under Shark" : ($runInstruments ? "under Instruments" : "$testRuns time" . ($testRuns == 1 ? "" : "s"))) . "\n";
217 print "Discarded first run.\n";
224 while ($count++ < $testRuns) {
225 $result = runTestsOnce($runShark, $runInstruments);
226 $result =~ s/\r\n/\n/g;
228 push @results, $result;
230 print ",\n" unless ($count == $testRuns);
234 my $output = "var output = [\n" . join(",\n", @results) . "\n];\n";
235 dumpToFile($output, $resultsFile);
236 dumpToFile(File::Spec->rel2abs($resultsFile), "$resultDirectory/baseline-filename.txt") if $setBaseline;
238 system("$jsShellPath", "-f", $prefixFile, "-f", $resultsFile, "-f", "resources/sunspider-analyze-results.js");
240 print("\nResults are located at $resultsFile\n");
243 my $newestMShark = newestFile(".", qr/\.mshark$/);
245 my $profileFile = "$resultDirectory/sunspider-profile-$timeString.mshark";
246 rename $newestMShark, $profileFile or die;
247 exec "/usr/bin/open", $profileFile;
251 if ($runInstruments) {
252 my $newestTrace = newestFile(".", qr/\.trace$/);
254 my $profileFile = "$resultDirectory/sunspider-profile-$timeString.trace";
255 rename $newestTrace, $profileFile or die;
256 exec "/usr/bin/open", $profileFile;