/**
  * @param int $run Run number
  * @param bool $cached False for first view, true for repeat view
  * @param int $tableColumns number of columns in the table
  * @return string The created markup
  */
 private function _createRunResultRows($run, $cached, $tableColumns)
 {
     $runResults = $this->testResults->getRunResult($run, $cached);
     if (!$runResults || !$runResults->isValid()) {
         return $this->_createErrorRow($run, $cached, $tableColumns);
     }
     $evenRow = false;
     $out = "";
     foreach ($runResults->getStepResults() as $stepResult) {
         $out .= $this->_createStepResultRow($stepResult, $evenRow);
         $evenRow = !$evenRow;
     }
     return $out;
 }
 private function getTestResultsFromPageData()
 {
     $run1 = array('result' => 0, 'TTFB' => 300, 'loadTime' => 6000);
     $run2 = array('result' => 404, 'TTFB' => 200, 'loadTime' => 3000);
     $run3 = array('result' => 0, 'TTFB' => 100, 'loadTime' => 2000);
     $run4 = array('result' => 99999, 'TTFB' => 500, 'loadTime' => 1000);
     $run5 = array('result' => 0, 'TTFB' => 400, 'loadTime' => 5000);
     $pageData = array(1 => array($run1), 2 => array($run2, $run4), 3 => array($run3, $run1), 4 => array($run4, $run5));
     foreach (array_keys($pageData) as $key) {
         $pageData[$key][0]["cached"] = 0;
         if (isset($pageData[$key][1])) {
             $pageData[$key][1]["cached"] = 1;
         }
     }
     $testinfo = array();
     return TestResults::fromPageData(TestInfo::fromValues("testId", "/test/path", $testinfo), $pageData);
 }
 public function testJsonResultArrayGeneration()
 {
     global $SINGLESTEP_JSON_RESULT_ARRAY;
     $this->tempDir = TestUtil::extractToTemp(__DIR__ . '/data/singlestepResults.zip');
     $testInfo = TestInfo::fromFiles($this->tempDir . '/singlestepResults');
     $imitatedPath = $this->imitatedResultPath($testInfo->getId());
     // we need to move the results to a directory structure that equal to the real one.
     // Then, we can go into the parent directory, so the relatece "testRoot" is the same as it would be in production
     // This is important, as during XML generation, some URLs contain the test path
     mkdir($this->tempDir . $imitatedPath, 0777, true);
     rename($this->tempDir . '/singlestepResults', $this->tempDir . $imitatedPath);
     $this->orgDir = getcwd();
     chdir($this->tempDir);
     $testRoot = "." . $imitatedPath;
     $testInfo = TestInfo::fromFiles($testRoot);
     $testResults = TestResults::fromFiles($testInfo);
     $jsonGenerator = new JsonResultGenerator($testInfo, "http://wpt-test-vm", new FileHandler());
     $resultArray = $jsonGenerator->resultDataArray($testResults, "loadTime");
     $this->assertThat($resultArray, new IsArraySubsetConstraint($SINGLESTEP_JSON_RESULT_ARRAY));
 }
 public function testCompleteXmlGeneration()
 {
     $this->tempDir = TestUtil::extractToTemp(__DIR__ . '/data/singlestepResults.zip');
     $testInfo = TestInfo::fromFiles($this->tempDir . '/singlestepResults');
     $imitatedPath = $this->imitatedResultPath($testInfo->getId());
     // we need to move the results to a directory structure that equal to the real one.
     // Then, we can go into the parent directory, so the relatece "testRoot" is the same as it would be in production
     // This is important, as during XML generation, some URLs contain the test path
     mkdir($this->tempDir . $imitatedPath, 0777, true);
     rename($this->tempDir . '/singlestepResults', $this->tempDir . $imitatedPath);
     $this->orgDir = getcwd();
     chdir($this->tempDir);
     $testRoot = "." . $imitatedPath;
     $testInfo = TestInfo::fromFiles($testRoot);
     $expectedXmlFile = __DIR__ . '/data/singlestepXmlResult.xml.gz';
     $testResults = TestResults::fromFiles($testInfo);
     $xmlGenerator = new XmlResultGenerator($testInfo, "http://wpt-test-vm", new FileHandler(), $this->allAdditionalInfo, true);
     $xmlGenerator->printAllResults($testResults, "loadTime", null);
     $this->assertThat(ob_get_contents(), IsCompatibleXMLConstraint::fromFile($expectedXmlFile));
 }
     }
     $test['url'] = $info['url'];
     if (isset($info['medianMetric'])) {
         $test_median_metric = $info['medianMetric'];
     }
 }
 $testInfo = parse_ini_file("./{$test['path']}/testinfo.ini", true);
 if ($testInfo !== FALSE) {
     if (array_key_exists('test', $testInfo) && array_key_exists('location', $testInfo['test'])) {
         $test['location'] = $testInfo['test']['location'];
     }
     if (isset($testInfo['test']) && isset($testInfo['test']['completeTime'])) {
         $test['done'] = true;
         $testInfoObject = TestInfo::fromFiles("./" . $test['path']);
         if (!array_key_exists('run', $test) || !$test['run']) {
             $testResults = TestResults::fromFiles($testInfoObject);
             $test['run'] = $testResults->getMedianRunNumber($test_median_metric, $test['cached']);
             $runResults = $testResults->getRunResult($test['run'], $test['cached']);
             $stepResult = $runResults->getStepResult($test['step']);
         } else {
             $stepResult = TestStepResult::fromFiles($testInfoObject, $test['run'], $test['cached'], $test['step']);
         }
         $test['stepResult'] = $stepResult;
         $test['aft'] = (int) $stepResult->getMetric('aft');
         $loadTime = $stepResult->getMetric('fullyLoaded');
         if (isset($loadTime) && (!isset($fastest) || $loadTime < $fastest)) {
             $fastest = $loadTime;
         }
         // figure out the real end time (in ms)
         if (isset($test['end'])) {
             $visualComplete = $stepResult->getMetric("visualComplte");
 /**
  * @param TestResults $testResults The test results to use for constructing the data array
  * @param string $medianMetric Metric to consider when selecting the median run
  * @return array An array containing all data about the test, in a form that can be encoded with JSON
  */
 public function resultDataArray($testResults, $medianMetric = "loadTime")
 {
     $testInfo = $this->testInfo->getInfoArray();
     $fvOnly = $this->testInfo->isFirstViewOnly();
     $cacheLabels = array('firstView', 'repeatView');
     // summary information
     $ret = array();
     $ret['id'] = $this->testInfo->getId();
     $url = $this->testInfo->getUrl();
     if (!$url) {
         $url = $testResults->getUrlFromRun();
     }
     $ret['url'] = $url;
     $urlGenerator = UrlGenerator::create(false, $this->urlStart, $this->testInfo->getId(), 0, 0);
     $ret['summary'] = $urlGenerator->resultSummary();
     $runs = $testResults->countRuns();
     if (isset($testInfo)) {
         if (array_key_exists('url', $testInfo) && strlen($testInfo['url'])) {
             $ret['testUrl'] = $testInfo['url'];
         }
         if (array_key_exists('location', $testInfo) && strlen($testInfo['location'])) {
             $locstring = $testInfo['location'];
             if (array_key_exists('browser', $testInfo) && strlen($testInfo['browser'])) {
                 $locstring .= ':' . $testInfo['browser'];
             }
             $ret['location'] = $locstring;
         }
         $testLocation = $this->testInfo->getTestLocation();
         if ($testLocation) {
             $ret['from'] = $testLocation;
         }
         if (array_key_exists('connectivity', $testInfo) && strlen($testInfo['connectivity'])) {
             $ret['connectivity'] = $testInfo['connectivity'];
         }
         if (array_key_exists('bwIn', $testInfo)) {
             $ret['bwDown'] = $testInfo['bwIn'];
         }
         if (array_key_exists('bwOut', $testInfo)) {
             $ret['bwUp'] = $testInfo['bwOut'];
         }
         if (array_key_exists('latency', $testInfo)) {
             $ret['latency'] = $testInfo['latency'];
         }
         if (array_key_exists('plr', $testInfo)) {
             $ret['plr'] = $testInfo['plr'];
         }
         if (array_key_exists('mobile', $testInfo)) {
             $ret['mobile'] = $testInfo['mobile'];
         }
         if (array_key_exists('label', $testInfo) && strlen($testInfo['label'])) {
             $ret['label'] = $testInfo['label'];
         }
         if (array_key_exists('completed', $testInfo)) {
             $ret['completed'] = $testInfo['completed'];
         }
         if (array_key_exists('tester', $testInfo) && strlen($testInfo['tester'])) {
             $ret['tester'] = $testInfo['tester'];
         }
         if (array_key_exists('testerDNS', $testInfo) && strlen($testInfo['testerDNS'])) {
             $ret['testerDNS'] = $testInfo['testerDNS'];
         }
         if (array_key_exists('runs', $testInfo) && $testInfo['runs']) {
             $runs = $testInfo['runs'];
         }
         if (array_key_exists('fvonly', $testInfo)) {
             $fvOnly = $testInfo['fvonly'] ? true : false;
         }
     }
     $cachedMax = 0;
     if (!$fvOnly) {
         $cachedMax = 1;
     }
     $ret['runs'] = $runs;
     $ret['fvonly'] = $fvOnly;
     $ret['successfulFVRuns'] = $testResults->countSuccessfulRuns(false);
     if (!$fvOnly) {
         $ret['successfulRVRuns'] = $testResults->countSuccessfulRuns(true);
     }
     // average
     $stats = array($testResults->getFirstViewAverage(), $testResults->getRepeatViewAverage());
     if (!$this->hasInfoFlag(self::WITHOUT_AVERAGE)) {
         $ret['average'] = array();
         for ($cached = 0; $cached <= $cachedMax; $cached++) {
             $label = $cacheLabels[$cached];
             $ret['average'][$label] = $stats[$cached];
         }
     }
     // standard deviation
     if (!$this->hasInfoFlag(self::WITHOUT_STDDEV)) {
         $ret['standardDeviation'] = array();
         for ($cached = 0; $cached <= $cachedMax; $cached++) {
             $label = $cacheLabels[$cached];
             $ret['standardDeviation'][$label] = array();
             foreach ($stats[$cached] as $key => $val) {
                 $ret['standardDeviation'][$label][$key] = $testResults->getStandardDeviation($key, $cached);
             }
         }
     }
     // median
     if (!$this->hasInfoFlag(self::WITHOUT_MEDIAN)) {
         $ret['median'] = array();
         for ($cached = 0; $cached <= $cachedMax; $cached++) {
             $label = $cacheLabels[$cached];
             $medianRun = $testResults->getMedianRunNumber($medianMetric, $cached == 1 ? true : false);
             if ($medianRun) {
                 $ret['median'][$label] = $this->medianRunDataArray($testResults->getRunResult($medianRun, $cached));
             }
         }
     }
     // runs
     if (!$this->hasInfoFlag(self::WITHOUT_RUNS)) {
         $ret['runs'] = array();
         for ($run = 1; $run <= $runs; $run++) {
             $ret['runs'][$run] = array();
             for ($cached = 0; $cached <= $cachedMax; $cached++) {
                 $label = $cacheLabels[$cached];
                 $ret['runs'][$run][$label] = $this->runDataArray($testResults->getRunResult($run, $cached));
             }
         }
     }
     return $ret;
 }
Exemple #7
0
$pagespeed = 0;
if (array_key_exists('pagespeed', $_REQUEST)) {
    $pagespeed = (int) $_REQUEST['pagespeed'];
}
if (isset($test['test']) && $test['test']['batch']) {
    BatchResult($id, $testPath);
} else {
    // see if the test is actually finished
    $status = GetTestStatus($id);
    if (isset($test['test']['completeTime'])) {
        $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' || isset($_SERVER['HTTP_SSL']) && $_SERVER['HTTP_SSL'] == 'On' ? 'https' : 'http';
        $host = $_SERVER['HTTP_HOST'];
        $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
        $path = substr($testPath, 1);
        $testInfo = TestInfo::fromValues($id, $testPath, $test);
        $testResults = TestResults::fromFiles($testInfo);
        $msLoad = microtime(true);
        // if we don't have an url, try to get it from the page results
        if (!strlen($url)) {
            $url = $testResults->getUrlFromRun();
        }
        $additionalInfo = array();
        if ($pagespeed) {
            $additionalInfo[] = XmlResultGenerator::INFO_PAGESPEED;
        }
        if (array_key_exists("requests", $_REQUEST) && $_REQUEST["requests"]) {
            $additionalInfo[] = XmlResultGenerator::INFO_MEDIAN_REQUESTS;
            if ($_REQUEST["requests"] != "median") {
                $additionalInfo[] = XmlResultGenerator::INFO_REQUESTS;
            }
        }
 /**
  * @param TestResults $testResults
  * @param string $median_metric
  * @param string $requestId
  * @param bool $medianFvOnly 
  */
 public function printAllResults($testResults, $median_metric, $requestId = null, $medianFvOnly = false)
 {
     $urlGenerator = UrlGenerator::create($this->friendlyUrls, $this->baseUrl, $this->testInfo->getId(), 0, 0);
     echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
     echo "<response>\n";
     echo "<statusCode>200</statusCode>\n";
     echo "<statusText>Ok</statusText>\n";
     if (!empty($requestId)) {
         echo "<requestId>{$requestId}</requestId>\n";
     }
     if (defined("VER_WEBPAGETEST")) {
         echo "<webPagetestVersion>" . VER_WEBPAGETEST . "</webPagetestVersion>";
     }
     echo "<data>\n";
     echo "<testId>" . $this->testInfo->getId() . "</testId>\n";
     echo "<summary>" . $urlGenerator->resultSummary() . "</summary>\n";
     $testInfo = $this->testInfo->getInfoArray();
     if ($testInfo) {
         if (@strlen($testInfo['url'])) {
             echo "<testUrl>" . xml_entities($testInfo['url']) . "</testUrl>\n";
         }
         if (@strlen($testInfo['location'])) {
             $locstring = $testInfo['location'];
             if (@strlen($testInfo['browser'])) {
                 $locstring .= ':' . $testInfo['browser'];
             }
             echo "<location>{$locstring}</location>\n";
         }
         $location = $this->testInfo->getTestLocation();
         if ($location) {
             echo "<from>" . xml_entities($location) . "</from>\n";
         }
         if (@strlen($testInfo['connectivity'])) {
             echo "<connectivity>{$testInfo['connectivity']}</connectivity>\n";
             echo "<bwDown>{$testInfo['bwIn']}</bwDown>\n";
             echo "<bwUp>{$testInfo['bwOut']}</bwUp>\n";
             echo "<latency>{$testInfo['latency']}</latency>\n";
             echo "<plr>{$testInfo['plr']}</plr>\n";
         }
         if (isset($testInfo['mobile'])) {
             echo "<mobile>" . xml_entities($testInfo['mobile']) . "</mobile>\n";
         }
         if (@strlen($testInfo['label'])) {
             echo "<label>" . xml_entities($testInfo['label']) . "</label>\n";
         }
         if (@strlen($testInfo['completed'])) {
             echo "<completed>" . gmdate("r", $testInfo['completed']) . "</completed>\n";
         }
         if (@strlen($testInfo['tester'])) {
             echo "<tester>" . xml_entities($testInfo['tester']) . "</tester>\n";
         }
         if (@strlen($testInfo['testerDNS'])) {
             echo "<testerDNS>" . xml_entities($testInfo['testerDNS']) . "</testerDNS>\n";
         }
     }
     // spit out the calculated averages
     $fv = $testResults->getFirstViewAverage();
     $rv = $testResults->getRepeatViewAverage();
     $runs = $testResults->countRuns();
     echo "<runs>{$runs}</runs>\n";
     echo "<successfulFVRuns>" . $testResults->countSuccessfulRuns(false) . "</successfulFVRuns>\n";
     if (isset($rv)) {
         echo "<successfulRVRuns>" . $testResults->countSuccessfulRuns(true) . "</successfulRVRuns>\n";
     }
     echo "<average>\n";
     echo "<firstView>\n";
     foreach ($fv as $key => $val) {
         $key = preg_replace('/[^a-zA-Z0-9\\.\\-_]/', '_', $key);
         echo "<{$key}>" . number_format($val, 0, '.', '') . "</{$key}>\n";
     }
     echo "</firstView>\n";
     if (isset($rv)) {
         echo "<repeatView>\n";
         foreach ($rv as $key => $val) {
             $key = preg_replace('/[^a-zA-Z0-9\\.\\-_]/', '_', $key);
             echo "<{$key}>" . number_format($val, 0, '.', '') . "</{$key}>\n";
         }
         echo "</repeatView>\n";
     }
     echo "</average>\n";
     echo "<standardDeviation>\n";
     echo "<firstView>\n";
     foreach ($fv as $key => $val) {
         $key = preg_replace('/[^a-zA-Z0-9\\.\\-_]/', '_', $key);
         echo "<{$key}>" . $testResults->getStandardDeviation($key, false) . "</{$key}>\n";
     }
     echo "</firstView>\n";
     if (isset($rv)) {
         echo "<repeatView>\n";
         foreach ($rv as $key => $val) {
             $key = preg_replace('/[^a-zA-Z0-9\\.\\-_]/', '_', $key);
             echo "<{$key}>" . $testResults->getStandardDeviation($key, true) . "</{$key}>\n";
         }
         echo "</repeatView>\n";
     }
     echo "</standardDeviation>\n";
     // output the median run data
     $medianMode = !empty($_REQUEST["medianRun"]) && $_REQUEST["medianRun"] == "fastest" ? "fastest" : "median";
     $fvMedian = $testResults->getMedianRunNumber($median_metric, false, $medianMode);
     if ($fvMedian) {
         echo "<median>\n";
         $this->printMedianRun($testResults->getRunResult($fvMedian, false));
         if (isset($rv)) {
             $rvMedian = $medianFvOnly ? $fvMedian : $testResults->getMedianRunNumber($median_metric, true, $medianMode);
             if ($rvMedian) {
                 $this->printMedianRun($testResults->getRunResult($rvMedian, true));
             }
         }
         echo "</median>\n";
     }
     // spit out the raw data for each run
     for ($i = 1; $i <= $runs; $i++) {
         echo "<run>\n";
         echo "<id>{$i}</id>\n";
         $this->printRun($testResults->getRunResult($i, false));
         $this->printRun($testResults->getRunResult($i, true));
         echo "</run>\n";
     }
     echo "</data>\n";
     echo "</response>\n";
 }