summaryrefslogtreecommitdiffstats
path: root/SunSpider/sunspider
blob: dbe26cd9afa7221d094c85c1467ad7015044412a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/perl -w

# Copyright (C) 2007 Apple Inc. All rights reserved.
# Copyright (C) 2007 Eric Seidel <eric@webkit.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 

use strict;
use Getopt::Long;
use File::Basename;
use File::Spec;
use Cwd;
use POSIX qw(strftime);
use Time::HiRes qw(gettimeofday tv_interval);

my $showHelp = 0;
my $runShark = 0;
my $runShark20 = 0;
my $runSharkCache = 0;
my $ubench = 0;
my $v8suite = 0;
my $suite = "";
my $parseOnly = 0;
my $jsShellPath;
my $jsShellArgs = "";
my $setBaseline = 0;
my $testsPattern;
my $testRuns = 10;

my $programName = basename($0);
my $usage = <<EOF;
Usage: $programName --shell=[path] [options]
  --help            Show this help message
  --set-baseline    Set baseline for future comparisons
  --shell           Path to JavaScript shell
  --args            Arguments to pass to JavaScript shell
  --runs            Number of times to run tests (default: $testRuns)
  --tests           Only run tests matching provided pattern
  --shark           Sample execution time with the Mac OS X "Shark" performance testing tool (implies --runs=1)
  --shark20         Like --shark, but with a 20 microsecond sampling interval
  --shark-cache     Like --shark, but performs a L2 cache-miss sample instead of time sample
  --suite           Select a specific benchmark suite. The default is sunspider-0.9.1
  --ubench          Use microbenchmark suite instead of regular tests. Same as --suite=ubench
  --v8-suite        Use the V8 benchmark suite. Same as --suite=v8-v4
  --parse-only      Use the parse-only benchmark suite. Same as --suite=parse-only
EOF

GetOptions('runs=i' => \$testRuns,
           'set-baseline' => \$setBaseline,
           'shell=s' => \$jsShellPath,
           'args=s' => \$jsShellArgs,
           'shark' => \$runShark,
           'shark20' => \$runShark20,
           'shark-cache' => \$runSharkCache,
           'suite=s' => \$suite,
           'ubench' => \$ubench,
           'v8-suite' => \$v8suite,
           'parse-only' => \$parseOnly,
           'tests=s' => \$testsPattern,
           'help' => \$showHelp);


$suite = "ubench" if ($ubench);
$suite = "v8-v4" if ($v8suite);
$suite = "parse-only" if ($parseOnly);
$suite = "sunspider-0.9.1" if (!$suite);

my $resultDirectory = "${suite}-results";

my $suitePath = $suite;
$suitePath = "tests/" . $suitePath unless ($suite =~ /\//);

$runShark = 1 if $runSharkCache;
$runShark = 20 if $runShark20;
$testRuns = 1 if $runShark;
if ($runShark && ! -x "/usr/bin/shark") {
    die "Please install CHUD tools from http://developer.apple.com/tools/download/\n";
}

my $sharkCacheProfileIndex = 0;
if ($runSharkCache) {
    my $sharkProfileList = `shark -l 2>&1`;
    for my $profile (split(/\n/, $sharkProfileList)) {
        $profile =~ /(\d+) - (.+)/;
        next  unless (defined $1);
        my $profileIndex = $1;
        my $profileName = $2;
        if ($profileName =~ /L2 Cache/) {
            $sharkCacheProfileIndex = $profileIndex;
            print "Using Shark L2 Cache Miss Profile: " . $profile . "\n";
            last;
        }
    }
    die "Failed to find L2 Cache Miss Profile for --shark-cache\n"  unless ($sharkCacheProfileIndex);
}

if (!$jsShellPath || $showHelp) {
   print STDERR $usage;
   exit 1;
}

sub dumpToFile($$)
{
    my ($contents, $path) = @_;
    open FILE, ">", $path or die "Failed to open $path";
    print FILE $contents;
    close FILE;
}

my @tests = ();
my @categories = ();
my %uniqueCategories = ();

sub loadTestsList()
{
    open TESTLIST, "<", "${suitePath}/LIST" or die "Can't find ${suitePath}/LIST";
    while (<TESTLIST>) {
        chomp;
        next unless !$testsPattern || /$testsPattern/;
        
        push @tests, $_;
        my $category = $_;
        $category =~ s/-.*//;
        if (!$uniqueCategories{$category}) {
            push @categories, $category;
            $uniqueCategories{$category} = $category;
        }
    }
    close TESTLIST;
}

my $timeString = strftime "%Y-%m-%d-%H.%M.%S", localtime $^T;
my $prefixFile = "$resultDirectory/sunspider-test-prefix.js";
my $resultsFile = "$resultDirectory/sunspider-results-$timeString.js";

sub writePrefixFile()
{
    my $prefix = "var suitePath = " . '"' . $suitePath . '"' . ";\n";
    $prefix .= "var tests = [ " . join(", ", map { '"' . $_ . '"' } @tests) . " ];\n";
    $prefix .= "var categories = [ " . join(", ", map { '"' . $_ . '"' } @categories) . " ];\n";

    mkdir "$resultDirectory";
    dumpToFile($prefix, $prefixFile);
}

sub runTestsOnce($)
{
    my ($useShark) = @_;
    my $shellArgs = $jsShellArgs . " -f $prefixFile -f resources/sunspider-standalone-driver.js 2> " . File::Spec->devnull();
    my $output;
    if ($useShark) {
        my $intervalArg = $useShark == 20 ? "-I 20u" : "";
        my $cacheArg = $runSharkCache ? "-c $sharkCacheProfileIndex" : "";
        $output = `shark $intervalArg $cacheArg -i -1-q "$jsShellPath" $shellArgs`;
    } else {
        $output = `"$jsShellPath" $shellArgs | grep -v break`;
    }
    return $output;
}

sub newestFile($$)
{
    my ($dir, $pattern) = @_;

    my $newestAge;
    my $newestFile = "";
    opendir DIR, $dir or die;
    for my $file (readdir DIR) {
        if ($file =~ $pattern) {
            my $age = -M "$dir/$file";
            if (!defined $newestAge || $age < $newestAge) {
                $newestFile = $file;
                $newestAge = $age;
            }
        }
    }
    closedir DIR;

    return "$dir/$newestFile";
}

loadTestsList();
if ($testsPattern) {
    print STDERR "Found " . scalar(@tests) . " tests matching '" . $testsPattern . "'\n";
} else {
    print STDERR "Found " . scalar(@tests) . " tests\n";
}
die "No tests to run"  unless scalar(@tests);
print STDERR "Running SunSpider once for warmup, then " . ($runShark ? "under Shark" : "$testRuns time" . ($testRuns == 1 ? "" : "s")) . "\n";
writePrefixFile();

runTestsOnce(0);
print "Discarded first run.\n";

my $result;
my $count = 0;
my @results = ();
my $total = 0;
print "[";
while ($count++ < $testRuns) {
    $result = runTestsOnce($runShark);
    $result =~ s/\r\n/\n/g;
    chomp $result;
    push @results, $result;
    print $result;
    print ",\n" unless ($count == $testRuns);
}
print "]\n";

my $output = "var output = [\n" . join(",\n", @results) . "\n];\n";
dumpToFile($output, $resultsFile);
dumpToFile(File::Spec->rel2abs($resultsFile), "$resultDirectory/baseline-filename.txt") if $setBaseline;

system("$jsShellPath", "-f", $prefixFile, "-f", $resultsFile, "-f", "resources/sunspider-analyze-results.js");

print("\nResults are located at $resultsFile\n");

if ($runShark) {
    my $newestMShark = newestFile(".", qr/\.mshark$/);
    if ($newestMShark) {
        my $profileFile = "$resultDirectory/sunspider-profile-$timeString.mshark";
        rename $newestMShark, $profileFile or die;
        exec "/usr/bin/open", $profileFile;
    }
}