function CollectHARs($tests, $outDir) { $count = 0; $total = count($tests); $index = 0; foreach ($tests as $id) { $index++; logMessage("[{$index}/{$total}] Collecting HAR ({$id})..."); $testPath = './' . GetTestPath($id); $har = GenerateHAR($id, $testPath, ['bodies' => 1, 'run' => 'median', 'cached' => 0]); if (isset($har) && strlen($har) > 10) { $harFile = "{$outDir}/{$id}.har"; file_put_contents($harFile, $har); $size = filesize($harFile); logMessage("[{$index}/{$total}] Compressing {$harFile} ({$size} bytes)"); exec("gzip -7 \"{$harFile}\""); $harFile .= '.gz'; if (is_file($harFile)) { $size = filesize($harFile); logMessage("[{$index}/{$total}] Validating {$harFile} ({$size} bytes)"); exec("gzip -t \"{$harFile}\"", $out, $return); if (!$return) { $count++; } else { logMessage("Invalid gzip file: {$return}"); @unlink($harFile); } } } } logMessage("Collected {$count} of {$total} HAR files to {$outDir}"); return $count; }
} $options['cached'] = $cached; if (isset($_REQUEST['php'])) { $options['php'] = $_REQUEST['php']; } if (isset($_REQUEST['pretty'])) { $options['pretty'] = $_REQUEST['pretty']; } if (isset($_REQUEST['run'])) { $options['run'] = $_REQUEST['run']; } $filename = ''; if (@strlen($url)) { $parts = parse_url($url); $filename = $parts['host']; } if (!strlen($filename)) { $filename = "pagetest"; } $filename .= ".{$id}.har"; header("Content-disposition: attachment; filename={$filename}"); header('Content-type: application/json'); // see if we need to wrap it in a JSONP callback if (isset($_REQUEST['callback']) && strlen($_REQUEST['callback'])) { echo "{$_REQUEST['callback']}("; } $json = GenerateHAR($id, $testPath, $options); echo $json; if (isset($_REQUEST['callback']) && strlen($_REQUEST['callback'])) { echo ");"; }
function ProcessTest($id) { global $tempDir; global $name; global $count; global $total; $ok = false; $testPath = './' . GetTestPath($id); $restored = false; if (!is_dir($testPath)) { // try restoring the test several times in case there are network issues $attempt = 0; do { $attempt++; har_log("{$id} - restoring test ({$attempt})"); RestoreTest($id); if (is_dir($testPath)) { $restored = true; } else { sleep(1); } } while (!$restored && $attempt < 120); } if (is_dir($testPath)) { har_log("{$id} - generating HAR"); $har = GenerateHAR($id, $testPath, ['bodies' => 1, 'run' => 'median', 'cached' => 0]); if (isset($har) && strlen($har)) { gz_file_put_contents("{$tempDir}/{$id}.har", $har); unset($har); $file = "{$tempDir}/{$id}.har.gz"; if (is_file($file)) { $file = realpath($file); $remoteFile = "{$name}/{$id}.har.gz"; $bucket = 'httparchive'; har_log("{$id} - Uploading to {$remoteFile}"); if (gsUpload($file, $bucket, $remoteFile)) { $ok = true; } else { har_log("{$id} - error uploading HAR"); } unlink($file); } else { har_log("{$id} - error saving HAR"); } } else { har_log("{$id} - error generating HAR"); } // clean up the test if we restored it if ($restored) { delTree($testPath, true); } } else { har_log("{$id} - error restoring test"); } return $ok; }