Tizen 2.1 base
[framework/web/webkit-efl.git] / PerformanceTests / SunSpider / sunspider
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
27 use strict;
28 use Getopt::Long;
29 use File::Basename;
30 use File::Spec;
31 use Cwd;
32 use POSIX qw(strftime);
33 use Time::HiRes qw(gettimeofday tv_interval);
34
35 my $showHelp = 0;
36 my $runShark = 0;
37 my $runShark20 = 0;
38 my $runSharkCache = 0;
39 my $runInstruments = 0;
40 my $ubench = 0;
41 my $v8suite = 0;
42 my $suite = "";
43 my $parseOnly = 0;
44 my $jsShellPath;
45 my $jsShellArgs = "";
46 my $setBaseline = 0;
47 my $testsPattern;
48 my $testRuns = 10;
49
50 my $programName = basename($0);
51 my $usage = <<EOF;
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
67 EOF
68
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,
77            'suite=s' => \$suite,
78            'ubench' => \$ubench,
79            'v8-suite' => \$v8suite,
80            'parse-only' => \$parseOnly,
81            'tests=s' => \$testsPattern,
82            'help' => \$showHelp);
83
84
85 $suite = "ubench" if ($ubench);
86 $suite = "v8-v4" if ($v8suite);
87 $suite = "parse-only" if ($parseOnly);
88 $suite = "sunspider-1.0" if (!$suite);
89
90 my $resultDirectory = "${suite}-results";
91
92 my $suitePath = $suite;
93 $suitePath = "tests/" . $suitePath unless ($suite =~ /\//);
94
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";
100 }
101
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";
113             last;
114         }
115     }
116     die "Failed to find L2 Cache Miss Profile for --shark-cache\n"  unless ($sharkCacheProfileIndex);
117 }
118
119 if (!$jsShellPath || $showHelp) {
120    print STDERR $usage;
121    exit 1;
122 }
123
124 sub dumpToFile($$)
125 {
126     my ($contents, $path) = @_;
127     open FILE, ">", $path or die "Failed to open $path";
128     print FILE $contents;
129     close FILE;
130 }
131
132 my @tests = ();
133 my @categories = ();
134 my %uniqueCategories = ();
135
136 sub loadTestsList()
137 {
138     open TESTLIST, "<", "${suitePath}/LIST" or die "Can't find ${suitePath}/LIST";
139     while (<TESTLIST>) {
140         chomp;
141         next unless !$testsPattern || /$testsPattern/;
142         
143         push @tests, $_;
144         my $category = $_;
145         $category =~ s/-.*//;
146         if (!$uniqueCategories{$category}) {
147             push @categories, $category;
148             $uniqueCategories{$category} = $category;
149         }
150     }
151     close TESTLIST;
152 }
153
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";
157
158 sub writePrefixFile()
159 {
160     my $prefix = "var suitePath = " . '"' . $suitePath . '"' . ";\n";
161     $prefix .= "var tests = [ " . join(", ", map { '"' . $_ . '"' } @tests) . " ];\n";
162     $prefix .= "var categories = [ " . join(", ", map { '"' . $_ . '"' } @categories) . " ];\n";
163
164     mkdir "$resultDirectory";
165     dumpToFile($prefix, $prefixFile);
166 }
167
168 sub runTestsOnce($$)
169 {
170     my ($useShark, $useInstruments) = @_;
171     my $shellArgs = $jsShellArgs . " -f $prefixFile -f resources/sunspider-standalone-driver.js 2> " . File::Spec->devnull();
172     my $output;
173     if ($useShark) {
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`;
179     } else {
180         $output = `"$jsShellPath" $shellArgs | grep -v break`;
181     }
182     return $output;
183 }
184
185 sub newestFile($$)
186 {
187     my ($dir, $pattern) = @_;
188
189     my $newestAge;
190     my $newestFile = "";
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) {
196                 $newestFile = $file;
197                 $newestAge = $age;
198             }
199         }
200     }
201     closedir DIR;
202
203     return "$dir/$newestFile";
204 }
205
206 loadTestsList();
207 if ($testsPattern) {
208     print STDERR "Found " . scalar(@tests) . " tests matching '" . $testsPattern . "'\n";
209 } else {
210     print STDERR "Found " . scalar(@tests) . " tests\n";
211 }
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";
214 writePrefixFile();
215
216 runTestsOnce(0, 0);
217 print "Discarded first run.\n";
218
219 my $result;
220 my $count = 0;
221 my @results = ();
222 my $total = 0;
223 print "[";
224 while ($count++ < $testRuns) {
225     $result = runTestsOnce($runShark, $runInstruments);
226     $result =~ s/\r\n/\n/g;
227     chomp $result;
228     push @results, $result;
229     print $result;
230     print ",\n" unless ($count == $testRuns);
231 }
232 print "]\n";
233
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;
237
238 system("$jsShellPath", "-f", $prefixFile, "-f", $resultsFile, "-f", "resources/sunspider-analyze-results.js");
239
240 print("\nResults are located at $resultsFile\n");
241
242 if ($runShark) {
243     my $newestMShark = newestFile(".", qr/\.mshark$/);
244     if ($newestMShark) {
245         my $profileFile = "$resultDirectory/sunspider-profile-$timeString.mshark";
246         rename $newestMShark, $profileFile or die;
247         exec "/usr/bin/open", $profileFile;
248     }
249 }
250
251 if ($runInstruments) {
252     my $newestTrace = newestFile(".", qr/\.trace$/);
253     if ($newestTrace) {
254         my $profileFile = "$resultDirectory/sunspider-profile-$timeString.trace";
255         rename $newestTrace, $profileFile or die;
256         exec "/usr/bin/open", $profileFile;
257     }
258 }