Revert accidental change. andersca--
[webkit/qt.git] / SunSpider / sunspider
blob39af5b9cb1e52c3a08ec35be1423387f1e3b7181
1 #!/usr/bin/perl -w
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
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.
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.
27 use strict;
28 use Getopt::Long;
29 use File::Basename;
30 use File::Spec;
31 use Cwd;
32 use POSIX qw(strftime);
34 my $showHelp = 0;
35 my $runShark = 0;
36 my $runShark20 = 0;
37 my $runSharkCache = 0;
38 my $jsShellPath;
39 my $setBaseline = 0;
40 my $testsPattern;
41 my $testRuns = 5; # This number may be different from what ./sunspider defaults to (that's OK)
43 my $programName = basename($0);
44 my $usage = <<EOF;
45 Usage: $programName --shell=[path] [options]
46 --help Show this help message
47 --set-baseline Set baseline for future comparisons
48 --shell Path to JavaScript shell
49 --runs Number of times to run tests (default: $testRuns)
50 --tests Only run tests matching provided pattern
51 --shark Sample execution time with the Mac OS X "Shark" performance testing tool (implies --runs=1)
52 --shark20 Like --shark, but with a 20 microsecond sampling interval
53 --shark-cache Like --shark, but performs a L2 cache-miss sample instead of time sample
54 EOF
56 GetOptions('runs=i' => \$testRuns,
57 'set-baseline' => \$setBaseline,
58 'shell=s' => \$jsShellPath,
59 'shark' => \$runShark,
60 'shark20' => \$runShark20,
61 'shark-cache' => \$runSharkCache,
62 'tests=s' => \$testsPattern,
63 'help' => \$showHelp);
65 $runShark = 1 if $runSharkCache;
66 $runShark = 20 if $runShark20;
67 $testRuns = 1 if $runShark;
68 if ($runShark && ! -x "/usr/bin/shark") {
69 die "Please install CHUD tools from http://developer.apple.com/tools/download/\n";
72 my $sharkCacheProfileIndex = 0;
73 if ($runSharkCache) {
74 my $sharkProfileList = `shark -l 2>&1`;
75 for my $profile (split(/\n/, $sharkProfileList)) {
76 $profile =~ /(\d+) - (.+)/;
77 next unless (defined $1);
78 my $profileIndex = $1;
79 my $profileName = $2;
80 if ($profileName =~ /L2 Cache/) {
81 $sharkCacheProfileIndex = $profileIndex;
82 print "Using Shark L2 Cache Miss Profile: " . $profile . "\n";
83 last;
86 die "Failed to find L2 Cache Miss Profile for --shark-cache\n" unless ($sharkCacheProfileIndex);
89 if (!$jsShellPath || $showHelp) {
90 print STDERR $usage;
91 exit 1;
94 sub dumpToFile($$)
96 my ($contents, $path) = @_;
97 open FILE, ">", $path or die "Failed to open $path";
98 print FILE $contents;
99 close FILE;
102 my @tests = ();
103 my @categories = ();
104 my %uniqueCategories = ();
106 sub loadTestsList()
108 open TESTLIST, "<", "tests/LIST" or die;
109 while (<TESTLIST>) {
110 chomp;
111 next unless !$testsPattern || /$testsPattern/;
113 push @tests, $_;
114 my $category = $_;
115 $category =~ s/-.*//;
116 if (!$uniqueCategories{$category}) {
117 push @categories, $category;
118 $uniqueCategories{$category} = $category;
121 close TESTLIST;
124 my $timeString = strftime "%Y-%m-%d-%H.%M.%S", localtime $^T;
125 my $prefixFile = "tmp/sunspider-test-prefix.js";
126 my $resultsFile = "tmp/sunspider-results-$timeString.js";
128 sub writePrefixFile()
130 my $prefix = "var tests = [ " . join(", ", map { '"' . $_ . '"' } @tests) . " ];\n";
131 $prefix .= "var categories = [ " . join(", ", map { '"' . $_ . '"' } @categories) . " ];\n";
133 mkdir "tmp";
134 dumpToFile($prefix, $prefixFile);
137 sub runTestsOnce($)
139 my ($useShark) = @_;
140 my $shellArgs = "-f $prefixFile -f resources/sunspider-standalone-driver.js 2> /dev/null";
141 my $output;
142 if ($useShark) {
143 my $intervalArg = $useShark == 20 ? "-I 20u" : "";
144 my $cacheArg = $runSharkCache ? "-c $sharkCacheProfileIndex" : "";
145 $output = `shark $intervalArg $cacheArg -i -1-q "$jsShellPath" $shellArgs`;
146 } else {
147 $output = `"$jsShellPath" $shellArgs | grep -v break`;
149 return $output;
152 sub newestFile($$)
154 my ($dir, $pattern) = @_;
156 my $newestAge;
157 my $newestFile = "";
158 opendir DIR, $dir or die;
159 for my $file (readdir DIR) {
160 if ($file =~ $pattern) {
161 my $age = -M "$dir/$file";
162 if (!defined $newestAge || $age < $newestAge) {
163 $newestFile = $file;
164 $newestAge = $age;
168 closedir DIR;
170 return "$dir/$newestFile";
173 loadTestsList();
174 if ($testsPattern) {
175 print STDERR "Found " . scalar(@tests) . " tests matching '" . $testsPattern . "'\n";
176 } else {
177 print STDERR "Found " . scalar(@tests) . " tests\n";
179 die "No tests to run" unless scalar(@tests);
180 print STDERR "Running SunSpider once for warmup, then " . ($runShark ? "under Shark" : "$testRuns time" . ($testRuns == 1 ? "" : "s")) . "\n";
181 writePrefixFile();
183 runTestsOnce(0);
184 print "Discarded first run.\n";
186 my $result;
187 my $count = 0;
188 my @results = ();
189 my $total = 0;
190 print "[";
191 while ($count++ < $testRuns) {
192 $result = runTestsOnce($runShark);
193 $result =~ s/\r\n/\n/g;
194 chomp $result;
195 push @results, $result;
196 print $result;
197 print ",\n" unless ($count == $testRuns);
199 print "]\n";
201 my $output = "var output = [\n" . join(",\n", @results) . "\n];\n";
202 dumpToFile($output, $resultsFile);
203 dumpToFile(File::Spec->rel2abs($resultsFile), "tmp/baseline-filename.txt") if $setBaseline;
205 system("$jsShellPath", "-f", $prefixFile, "-f", $resultsFile, "-f", "resources/sunspider-analyze-results.js");
207 if ($runShark) {
208 my $newestMShark = newestFile(".", qr/\.mshark$/);
209 if ($newestMShark) {
210 my $profileFile = "tmp/sunspider-profile-$timeString.mshark";
211 rename $newestMShark, $profileFile or die;
212 exec "/usr/bin/open", $profileFile;