<?php
set_time_limit(0);
//error_reporting(0);

// global options
$video = true;
$private = false;
$docComplete = true;
$runs = 5;
$iterations = 1;
$fvonly = true;
$options = 'shard=0&discard=1&keepua=1';
$server = 'http://www.webpagetest.org/';
$key='';  // insert API key here

// list of permutations to test
$permutations = array();
$permutations['Original'] = array('location' => 'Dulles:Chrome.DSL', 'options' => 'script=addHeader%09X-Jpeg-Mode%3A%200%0AsetDns%09*%0972.66.115.11%0Anavigate%09%25URL%25');
$permutations['Optimized'] = array('location' => 'Dulles:Chrome.DSL', 'options' => 'script=addHeader%09X-Jpeg-Mode%3A%203%0AsetDns%09*%0972.66.115.11%0Anavigate%09%25URL%25');
$permutations['Progressive'] = array('location' => 'Dulles:Chrome.DSL', 'options' => 'script=addHeader%09X-Jpeg-Mode%3A%201%0AsetDns%09*%0972.66.115.11%0Anavigate%09%25URL%25');
$permutations['Truncated'] = array('location' => 'Dulles:Chrome.DSL', 'options' => 'script=addHeader%09X-Jpeg-Mode%3A%202%0AsetDns%09*%0972.66.115.11%0Anavigate%09%25URL%25');
$maxBandwidth = 3000;
$maxVariancePct = 20;

$metrics = array('TTFB', 'render', 'docTime', 'fullyLoaded', 'SpeedIndex', 'SpeedIndexDT', 'bytesInDoc', 'requestsDoc', 'domContentLoadedEventStart', 'visualComplete');

/**
* shared function to load the results file
* 
* @param mixed $results
*/
function LoadResults(&$results)
{
    $ret = false;

    if( is_file('./results.json') ) {
        $results = json_decode(file_get_contents('./results.json'), true);
        if (is_array($results) && count($results)) {
            $ret = true;
        }
    }
    
    return $ret;
}  

/**
* shared function to write out the results file
*/
function StoreResults(&$results) {
    file_put_contents('./results.json', json_encode($results));
}

if (function_exists('curl_init')) {
    $CURL_CONTEXT = curl_init();
    if ($CURL_CONTEXT !== false) {
        curl_setopt($CURL_CONTEXT, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($CURL_CONTEXT, CURLOPT_FAILONERROR, true);
        curl_setopt($CURL_CONTEXT, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($CURL_CONTEXT, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($CURL_CONTEXT, CURLOPT_DNS_CACHE_TIMEOUT, 30);
        curl_setopt($CURL_CONTEXT, CURLOPT_MAXREDIRS, 10);
        curl_setopt($CURL_CONTEXT, CURLOPT_TIMEOUT, 30);
    }
} else
    $CURL_CONTEXT = false;

function http_fetch($url) {
    $ret = null;
    global $CURL_CONTEXT;
    if ($CURL_CONTEXT !== false) {
        curl_setopt($CURL_CONTEXT, CURLOPT_URL, $url);
        $ret = curl_exec($CURL_CONTEXT);
    } else {
        $context = stream_context_create(array('http' => array('header'=>'Connection: close', 'timeout' => 600)));
        $ret = file_get_contents($url, false, $context);
    }
    return $ret;
}

?>
