PageRenderTime 36ms CodeModel.GetById 17ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/build/profile_pageloader.pl

http://github.com/zpao/v8monkey
Perl | 211 lines | 111 code | 33 blank | 67 comment | 19 complexity | 45ae52f6ec14294cdbb56148936faf80 MD5 | raw file
  1#!/usr/bin/perl
  2# ***** BEGIN LICENSE BLOCK *****
  3# Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4#
  5# The contents of this file are subject to the Mozilla Public License Version
  6# 1.1 (the "License"); you may not use this file except in compliance with
  7# the License. You may obtain a copy of the License at
  8# http://www.mozilla.org/MPL/
  9#
 10# Software distributed under the License is distributed on an "AS IS" basis,
 11# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 12# for the specific language governing rights and limitations under the
 13# License.
 14#
 15# The Original Code is the Mozilla Browser code.
 16#
 17# The Initial Developer of the Original Code is
 18# Netscape Communications Corporation.
 19# Portions created by the Initial Developer are Copyright (C) 2002
 20# the Initial Developer. All Rights Reserved.
 21#
 22# Contributor(s):
 23#  Chris Mcafee <mcafee@netscape.com>
 24#  Brian Ryner <bryner@brianryner.com>
 25#
 26# Alternatively, the contents of this file may be used under the terms of
 27# either the GNU General Public License Version 2 or later (the "GPL"), or
 28# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 29# in which case the provisions of the GPL or the LGPL are applicable instead
 30# of those above. If you wish to allow use of your version of this file only
 31# under the terms of either the GPL or the LGPL, and not to allow others to
 32# use your version of this file under the terms of the MPL, indicate your
 33# decision by deleting the provisions above and replace them with the notice
 34# and other provisions required by the GPL or the LGPL. If you do not delete
 35# the provisions above, a recipient may use your version of this file under
 36# the terms of any one of the MPL, the GPL or the LGPL.
 37#
 38# ***** END LICENSE BLOCK *****
 39
 40use Cwd;
 41use File::Find ();
 42
 43use POSIX qw(sys_wait_h);
 44
 45sub kill_process {
 46    my ($target_pid) = @_;
 47    my $start_time = time;
 48
 49    # Try to kill and wait 10 seconds, then try a kill -9
 50    my $sig;
 51    for $sig ('TERM', 'KILL') {
 52        print "kill $sig $target_pid\n";
 53        kill $sig => $target_pid;
 54        my $interval_start = time;
 55        while (time - $interval_start < 10) {
 56            # the following will work with 'cygwin' perl on win32, but not
 57            # with 'MSWin32' (ActiveState) perl
 58            my $pid = waitpid($target_pid, POSIX::WNOHANG());
 59            if (($pid == $target_pid and POSIX::WIFEXITED($?)) or $pid == -1) {
 60                my $secs = time - $start_time;
 61                $secs = $secs == 1 ? '1 second' : "$secs seconds";
 62                print "Process killed. Took $secs to die.\n";
 63                return;
 64            }
 65            sleep 1;
 66        }
 67    }
 68    die "Unable to kill process: $target_pid";
 69}
 70
 71# Stripped down version of fork_and_log().
 72sub system_fork_and_log {
 73    # Fork a sub process and log the output.
 74    my ($cmd) = @_;
 75
 76    my $pid = fork; # Fork off a child process.
 77
 78    unless ($pid) { # child
 79        exec { $cmd->[0] } @$cmd;
 80        die "Could not exec()";
 81    }
 82    return $pid;
 83}
 84
 85
 86sub wait_for_pid {
 87    # Wait for a process to exit or kill it if it takes too long.
 88    my ($pid, $timeout_secs) = @_;
 89    my ($exit_value, $signal_num, $dumped_core, $timed_out) = (0,0,0,0);
 90    my $sig_name;
 91    my $loop_count;
 92
 93    die ("Invalid timeout value passed to wait_for_pid()\n")
 94        if ($timeout_secs <= 0);
 95
 96    eval {
 97        $loop_count = 0;
 98        while (++$loop_count < $timeout_secs) {
 99            my $wait_pid = waitpid($pid, POSIX::WNOHANG());
100            # the following will work with 'cygwin' perl on win32, but not 
101            # with 'MSWin32' (ActiveState) perl
102            last if ($wait_pid == $pid and POSIX::WIFEXITED($?)) or $wait_pid == -1;
103            sleep 1;
104        }
105
106        $exit_value = $? >> 8;
107        $signal_num = $? >> 127;
108        $dumped_core = $? & 128;
109        if ($loop_count >= $timeout_secs) {
110            die "timeout";
111        }
112        return "done";
113    };
114
115    if ($@) {
116        if ($@ =~ /timeout/) {
117            kill_process($pid);
118            $timed_out = 1;
119        } else { # Died for some other reason.
120            die; # Propagate the error up.
121        }
122    }
123#    $sig_name = $signal_num ? signal_name($signal_num) : '';
124#
125#    return { timed_out=>$timed_out,
126#             exit_value=>$exit_value,
127#             sig_name=>$sig_name,
128#             dumped_core=>$dumped_core };
129}
130
131# System version of run_cmd().
132sub run_system_cmd {
133    my ($cmd, $timeout_secs) = @_;
134
135#    print_log "cmd = $cmd\n";
136    my $pid = system_fork_and_log($cmd);
137    my $result = wait_for_pid($pid, $timeout_secs);
138
139    return $result;
140}
141
142#
143# Given profile directory, find pref file hidden in salt directory.
144# profile $Settings::MozProfileName must exist before calling this sub.
145#
146sub find_pref_file {
147    my $profile_dir = shift;
148
149    # default to *nix
150    my $pref_file = "prefs.js";
151
152    unless (-e $profile_dir) {
153        return; # empty list
154    }
155
156    my $found = undef;
157    my $sub = sub {$pref_file = $File::Find::name, $found++ if $pref_file eq $_};
158    File::Find::find($sub, $profile_dir);
159    unless ($found) {
160        return; # empty list
161    }
162
163    return $pref_file;
164}
165
166my $topdir = cwd();
167
168chdir $ENV{OBJDIR};
169my $app_name = `grep "MOZ_APP_NAME	" config/autoconf.mk | sed "s/.*= //"`;
170chomp($app_name);
171
172# On mac, the app directory is the product name with the first
173# letter capitalized
174
175my $toolkit = `grep "MOZ_WIDGET_TOOLKIT        " config/autoconf.mk  |sed "s/.*= //"`;
176chomp($toolkit);
177
178if ($toolkit =~ /(mac|cocoa)/) {
179    my $app_dir = uc(substr($app_name, 0, 1)).substr($app_name, 1);
180    chdir "dist/$app_dir.app/Contents/MacOS";
181} else {
182    chdir "dist/bin";
183}
184
185my $bin_suffix = "";
186if ($toolkit =~ /(windows|os2)/) {
187    $bin_suffix = ".exe";
188}
189
190my $old_home = $ENV{HOME};
191$ENV{HOME} = cwd();
192
193# Create a profile to test with.
194run_system_cmd(["./".$app_name.$bin_suffix, "-createProfile", "testprofile"], 45);
195
196my $pref_file = find_pref_file(".mozilla/".$app_name);
197open PREFS, ">>$pref_file";
198# Add allow_scripts_to_close_windows; this lets us cleanly exit.
199print PREFS "user_pref(\"dom.allow_scripts_to_close_windows\", true);\n";
200# Suppress the default browser dialog since it keeps the test from starting.
201print PREFS "user_pref(\"browser.shell.checkDefaultBrowser\", false);\n";
202close PREFS;
203
204# Run the pageload test.
205run_system_cmd(["./".$app_name.$bin_suffix, $ENV{PAGELOAD_URL}."/loader.pl?maxcyc=2&delay=500&nocache=0&timeout=30000&auto=1"], 240);
206
207# Start up again; this will gather data for reading global history and
208# reading the fastload file.
209run_system_cmd(["./".$app_name.$bin_suffix, "file://$topdir/build/profile_pageloader.html"], 45);
210
211chdir $topdir;