Bug 845676 - Remove most of the assertion annotations in the content/media mochitests...
[gecko.git] / build / profile_pageloader.pl
blobdd6be1c1b83cc3d2aa1c6c61849a2a466b96390c
1 #!/usr/bin/perl
2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 use Cwd;
7 use File::Find ();
9 use POSIX qw(sys_wait_h);
11 sub kill_process {
12 my ($target_pid) = @_;
13 my $start_time = time;
15 # Try to kill and wait 10 seconds, then try a kill -9
16 my $sig;
17 for $sig ('TERM', 'KILL') {
18 print "kill $sig $target_pid\n";
19 kill $sig => $target_pid;
20 my $interval_start = time;
21 while (time - $interval_start < 10) {
22 # the following will work with 'cygwin' perl on win32, but not
23 # with 'MSWin32' (ActiveState) perl
24 my $pid = waitpid($target_pid, POSIX::WNOHANG());
25 if (($pid == $target_pid and POSIX::WIFEXITED($?)) or $pid == -1) {
26 my $secs = time - $start_time;
27 $secs = $secs == 1 ? '1 second' : "$secs seconds";
28 print "Process killed. Took $secs to die.\n";
29 return;
31 sleep 1;
34 die "Unable to kill process: $target_pid";
37 # Stripped down version of fork_and_log().
38 sub system_fork_and_log {
39 # Fork a sub process and log the output.
40 my ($cmd) = @_;
42 my $pid = fork; # Fork off a child process.
44 unless ($pid) { # child
45 exec { $cmd->[0] } @$cmd;
46 die "Could not exec()";
48 return $pid;
52 sub wait_for_pid {
53 # Wait for a process to exit or kill it if it takes too long.
54 my ($pid, $timeout_secs) = @_;
55 my ($exit_value, $signal_num, $dumped_core, $timed_out) = (0,0,0,0);
56 my $sig_name;
57 my $loop_count;
59 die ("Invalid timeout value passed to wait_for_pid()\n")
60 if ($timeout_secs <= 0);
62 eval {
63 $loop_count = 0;
64 while (++$loop_count < $timeout_secs) {
65 my $wait_pid = waitpid($pid, POSIX::WNOHANG());
66 # the following will work with 'cygwin' perl on win32, but not
67 # with 'MSWin32' (ActiveState) perl
68 last if ($wait_pid == $pid and POSIX::WIFEXITED($?)) or $wait_pid == -1;
69 sleep 1;
72 $exit_value = $? >> 8;
73 $signal_num = $? >> 127;
74 $dumped_core = $? & 128;
75 if ($loop_count >= $timeout_secs) {
76 die "timeout";
78 return "done";
81 if ($@) {
82 if ($@ =~ /timeout/) {
83 kill_process($pid);
84 $timed_out = 1;
85 } else { # Died for some other reason.
86 die; # Propagate the error up.
89 # $sig_name = $signal_num ? signal_name($signal_num) : '';
91 # return { timed_out=>$timed_out,
92 # exit_value=>$exit_value,
93 # sig_name=>$sig_name,
94 # dumped_core=>$dumped_core };
97 # System version of run_cmd().
98 sub run_system_cmd {
99 my ($cmd, $timeout_secs) = @_;
101 # print_log "cmd = $cmd\n";
102 my $pid = system_fork_and_log($cmd);
103 my $result = wait_for_pid($pid, $timeout_secs);
105 return $result;
109 # Given profile directory, find pref file hidden in salt directory.
110 # profile $Settings::MozProfileName must exist before calling this sub.
112 sub find_pref_file {
113 my $profile_dir = shift;
115 # default to *nix
116 my $pref_file = "prefs.js";
118 unless (-e $profile_dir) {
119 return; # empty list
122 my $found = undef;
123 my $sub = sub {$pref_file = $File::Find::name, $found++ if $pref_file eq $_};
124 File::Find::find($sub, $profile_dir);
125 unless ($found) {
126 return; # empty list
129 return $pref_file;
132 my $topdir = cwd();
134 chdir $ENV{OBJDIR};
135 my $app_name = `grep "MOZ_APP_NAME " config/autoconf.mk | sed "s/.*= //"`;
136 chomp($app_name);
138 # On mac, the app directory is the product name with the first
139 # letter capitalized
141 my $toolkit = `grep "MOZ_WIDGET_TOOLKIT " config/autoconf.mk |sed "s/.*= //"`;
142 chomp($toolkit);
144 if ($toolkit =~ /(mac|cocoa)/) {
145 my $app_dir = uc(substr($app_name, 0, 1)).substr($app_name, 1);
146 chdir "dist/$app_dir.app/Contents/MacOS";
147 } else {
148 chdir "dist/bin";
151 my $bin_suffix = "";
152 if ($toolkit =~ /(windows|os2)/) {
153 $bin_suffix = ".exe";
156 my $old_home = $ENV{HOME};
157 $ENV{HOME} = cwd();
159 # Create a profile to test with.
160 run_system_cmd(["./".$app_name.$bin_suffix, "-createProfile", "testprofile"], 45);
162 my $pref_file = find_pref_file(".mozilla/".$app_name);
163 open PREFS, ">>$pref_file";
164 # Add allow_scripts_to_close_windows; this lets us cleanly exit.
165 print PREFS "user_pref(\"dom.allow_scripts_to_close_windows\", true);\n";
166 # Suppress the default browser dialog since it keeps the test from starting.
167 print PREFS "user_pref(\"browser.shell.checkDefaultBrowser\", false);\n";
168 close PREFS;
170 # Run the pageload test.
171 run_system_cmd(["./".$app_name.$bin_suffix, $ENV{PAGELOAD_URL}."/loader.pl?maxcyc=2&delay=500&nocache=0&timeout=30000&auto=1"], 240);
173 # Start up again; this will gather data for reading global history and
174 # reading the fastload file.
175 run_system_cmd(["./".$app_name.$bin_suffix, "file://$topdir/build/profile_pageloader.html"], 45);
177 chdir $topdir;