Home | History | Annotate | Download | only in SunSpider
      1 #!/usr/bin/perl -w
      2 
      3 # Copyright (C) 2007 Apple Inc. All rights reserved.
      4 # Copyright (C) 2007 Eric Seidel <eric (at] 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 $ubench = 0;
     40 my $v8suite = 0;
     41 my $suite = "";
     42 my $parseOnly = 0;
     43 my $jsShellPath;
     44 my $jsShellArgs = "";
     45 my $setBaseline = 0;
     46 my $testsPattern;
     47 my $testRuns = 10;
     48 
     49 my $programName = basename($0);
     50 my $usage = <<EOF;
     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-0.9.1
     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
     65 EOF
     66 
     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,
     74            'suite=s' => \$suite,
     75            'ubench' => \$ubench,
     76            'v8-suite' => \$v8suite,
     77            'parse-only' => \$parseOnly,
     78            'tests=s' => \$testsPattern,
     79            'help' => \$showHelp);
     80 
     81 
     82 $suite = "ubench" if ($ubench);
     83 $suite = "v8-v4" if ($v8suite);
     84 $suite = "parse-only" if ($parseOnly);
     85 $suite = "sunspider-0.9.1" if (!$suite);
     86 
     87 my $resultDirectory = "${suite}-results";
     88 
     89 my $suitePath = $suite;
     90 $suitePath = "tests/" . $suitePath unless ($suite =~ /\//);
     91 
     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";
     97 }
     98 
     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";
    110             last;
    111         }
    112     }
    113     die "Failed to find L2 Cache Miss Profile for --shark-cache\n"  unless ($sharkCacheProfileIndex);
    114 }
    115 
    116 if (!$jsShellPath || $showHelp) {
    117    print STDERR $usage;
    118    exit 1;
    119 }
    120 
    121 sub dumpToFile($$)
    122 {
    123     my ($contents, $path) = @_;
    124     open FILE, ">", $path or die "Failed to open $path";
    125     print FILE $contents;
    126     close FILE;
    127 }
    128 
    129 my @tests = ();
    130 my @categories = ();
    131 my %uniqueCategories = ();
    132 
    133 sub loadTestsList()
    134 {
    135     open TESTLIST, "<", "${suitePath}/LIST" or die "Can't find ${suitePath}/LIST";
    136     while (<TESTLIST>) {
    137         chomp;
    138         next unless !$testsPattern || /$testsPattern/;
    139         
    140         push @tests, $_;
    141         my $category = $_;
    142         $category =~ s/-.*//;
    143         if (!$uniqueCategories{$category}) {
    144             push @categories, $category;
    145             $uniqueCategories{$category} = $category;
    146         }
    147     }
    148     close TESTLIST;
    149 }
    150 
    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";
    154 
    155 sub writePrefixFile()
    156 {
    157     my $prefix = "var suitePath = " . '"' . $suitePath . '"' . ";\n";
    158     $prefix .= "var tests = [ " . join(", ", map { '"' . $_ . '"' } @tests) . " ];\n";
    159     $prefix .= "var categories = [ " . join(", ", map { '"' . $_ . '"' } @categories) . " ];\n";
    160 
    161     mkdir "$resultDirectory";
    162     dumpToFile($prefix, $prefixFile);
    163 }
    164 
    165 sub runTestsOnce($)
    166 {
    167     my ($useShark) = @_;
    168     my $shellArgs = $jsShellArgs . " -f $prefixFile -f resources/sunspider-standalone-driver.js 2> " . File::Spec->devnull();
    169     my $output;
    170     if ($useShark) {
    171         my $intervalArg = $useShark == 20 ? "-I 20u" : "";
    172         my $cacheArg = $runSharkCache ? "-c $sharkCacheProfileIndex" : "";
    173         $output = `shark $intervalArg $cacheArg -i -1-q "$jsShellPath" $shellArgs`;
    174     } else {
    175         $output = `"$jsShellPath" $shellArgs | grep -v break`;
    176     }
    177     return $output;
    178 }
    179 
    180 sub newestFile($$)
    181 {
    182     my ($dir, $pattern) = @_;
    183 
    184     my $newestAge;
    185     my $newestFile = "";
    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) {
    191                 $newestFile = $file;
    192                 $newestAge = $age;
    193             }
    194         }
    195     }
    196     closedir DIR;
    197 
    198     return "$dir/$newestFile";
    199 }
    200 
    201 loadTestsList();
    202 if ($testsPattern) {
    203     print STDERR "Found " . scalar(@tests) . " tests matching '" . $testsPattern . "'\n";
    204 } else {
    205     print STDERR "Found " . scalar(@tests) . " tests\n";
    206 }
    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";
    209 writePrefixFile();
    210 
    211 runTestsOnce(0);
    212 print "Discarded first run.\n";
    213 
    214 my $result;
    215 my $count = 0;
    216 my @results = ();
    217 my $total = 0;
    218 print "[";
    219 while ($count++ < $testRuns) {
    220     $result = runTestsOnce($runShark);
    221     $result =~ s/\r\n/\n/g;
    222     chomp $result;
    223     push @results, $result;
    224     print $result;
    225     print ",\n" unless ($count == $testRuns);
    226 }
    227 print "]\n";
    228 
    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;
    232 
    233 system("$jsShellPath", "-f", $prefixFile, "-f", $resultsFile, "-f", "resources/sunspider-analyze-results.js");
    234 
    235 print("\nResults are located at $resultsFile\n");
    236 
    237 if ($runShark) {
    238     my $newestMShark = newestFile(".", qr/\.mshark$/);
    239     if ($newestMShark) {
    240         my $profileFile = "$resultDirectory/sunspider-profile-$timeString.mshark";
    241         rename $newestMShark, $profileFile or die;
    242         exec "/usr/bin/open", $profileFile;
    243     }
    244 }
    245