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;
49 my $programName = basename($0);
51 Usage: $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-1.0
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
67 GetOptions('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,
76 'v8-suite' => \$v8suite,
77 'parse-only' => \$parseOnly,
78 'tests=s' => \$testsPattern,
79 'help' => \$showHelp);
82 $suite = "ubench" if ($ubench);
83 $suite = "v8-v4" if ($v8suite);
84 $suite = "parse-only" if ($parseOnly);
85 $suite = "sunspider-1.0" if (!$suite);
87 my $resultDirectory = "${suite}-results";
89 my $suitePath = $suite;
90 $suitePath = "tests/" . $suitePath unless ($suite =~ /\//);
92 $runShark = 1 if $runSharkCache;
93 $runShark = 20 if $runShark20;
94 $testRuns = 1 if $runShark;
95 if ($runShark && ! -x "/usr/bin/shark") {
96 die "Please install CHUD tools from http://developer.apple.com/tools/download/\n";
99 my $sharkCacheProfileIndex = 0;
100 if ($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";
113 die "Failed to find L2 Cache Miss Profile for --shark-cache\n" unless ($sharkCacheProfileIndex);
116 if (!$jsShellPath || $showHelp) {
123 my ($contents, $path) = @_;
124 open FILE, ">", $path or die "Failed to open $path";
125 print FILE $contents;
131 my %uniqueCategories = ();
135 open TESTLIST, "<", "${suitePath}/LIST" or die "Can't find ${suitePath}/LIST";
138 next unless !$testsPattern || /$testsPattern/;
142 $category =~ s/-.*//;
143 if (!$uniqueCategories{$category}) {
144 push @categories, $category;
145 $uniqueCategories{$category} = $category;
151 my $timeString = strftime "%Y-%m-%d-%H.%M.%S", localtime $^T;
152 my $prefixFile = "$resultDirectory/sunspider-test-prefix.js";
153 my $resultsFile = "$resultDirectory/sunspider-results-$timeString.js";
155 sub writePrefixFile()
157 my $prefix = "var suitePath = " . '"' . $suitePath . '"' . ";\n";
158 $prefix .= "var tests = [ " . join(", ", map { '"' . $_ . '"' } @tests) . " ];\n";
159 $prefix .= "var categories = [ " . join(", ", map { '"' . $_ . '"' } @categories) . " ];\n";
161 mkdir "$resultDirectory";
162 dumpToFile($prefix, $prefixFile);
168 my $shellArgs = $jsShellArgs . " -f $prefixFile -f resources/sunspider-standalone-driver.js 2> " . File::Spec->devnull();
171 my $intervalArg = $useShark == 20 ? "-I 20u" : "";
172 my $cacheArg = $runSharkCache ? "-c $sharkCacheProfileIndex" : "";
173 $output = `shark $intervalArg $cacheArg -i -1-q "$jsShellPath" $shellArgs`;
175 $output = `"$jsShellPath" $shellArgs | grep -v break`;
182 my ($dir, $pattern) = @_;
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) {
198 return "$dir/$newestFile";
203 print STDERR "Found " . scalar(@tests) . " tests matching '" . $testsPattern . "'\n";
205 print STDERR "Found " . scalar(@tests) . " tests\n";
207 die "No tests to run" unless scalar(@tests);
208 print STDERR "Running SunSpider once for warmup, then " . ($runShark ? "under Shark" : "$testRuns time" . ($testRuns == 1 ? "" : "s")) . "\n";
212 print "Discarded first run.\n";
219 while ($count++ < $testRuns) {
220 $result = runTestsOnce($runShark);
221 $result =~ s/\r\n/\n/g;
223 push @results, $result;
225 print ",\n" unless ($count == $testRuns);
229 my $output = "var output = [\n" . join(",\n", @results) . "\n];\n";
230 dumpToFile($output, $resultsFile);
231 dumpToFile(File::Spec->rel2abs($resultsFile), "$resultDirectory/baseline-filename.txt") if $setBaseline;
233 system("$jsShellPath", "-f", $prefixFile, "-f", $resultsFile, "-f", "resources/sunspider-analyze-results.js");
235 print("\nResults are located at $resultsFile\n");
238 my $newestMShark = newestFile(".", qr/\.mshark$/);
240 my $profileFile = "$resultDirectory/sunspider-profile-$timeString.mshark";
241 rename $newestMShark, $profileFile or die;
242 exec "/usr/bin/open", $profileFile;