Esempio n. 1
0
            ob_flush();
            flush();
        }
        echo rtrim($buffer, "\r\n}");
        gzclose($handle);
    }
} elseif (gz_is_file("{$testPath}/{$run}{$cachedText}_timeline.json")) {
    $ok = true;
    header("Content-disposition: attachment; filename=timeline.json");
    header("Content-type: application/json");
    gz_readfile_chunked("{$testPath}/{$run}{$cachedText}_timeline.json");
} elseif (gz_is_file("{$testPath}/{$run}{$cachedText}_devtools.json")) {
    require_once 'devtools.inc.php';
    $devTools = array();
    $startOffset = null;
    GetTimeline($testPath, $run, $cached, $devTools, $startOffset);
    if (isset($devTools) && is_array($devTools) && count($devTools)) {
        foreach ($devTools as $entry) {
            $events = GetEvents($entry);
            if (count($events)) {
                if (!$ok) {
                    $ok = true;
                    header("Content-disposition: attachment; filename=timeline.json");
                    header("Content-type: application/json");
                    echo "[\"WebPagetest\"";
                }
                foreach ($events as $event) {
                    echo ",\n";
                    echo json_encode($event);
                }
            }
Esempio n. 2
0
/**
* If we have a timeline, figure out what each thread was doing at each point in time.
* Basically CPU utilization from the timeline.
* 
* returns an array of threads with each thread being an array of slices (one for
* each time period).  Each slice is an array of events and the fraction of that
* slice that they consumed (with a total maximum of 1 for any slice).
*/
function DevToolsGetCPUSlices($testPath, $run, $cached)
{
    $count = 0;
    $slices = null;
    $devTools = array();
    $startOffset = null;
    $ver = 1;
    $cacheFile = "{$testPath}/{$run}.{$cached}.devToolsCPUSlices.{$ver}";
    if (gz_is_file($cacheFile)) {
        $slices = json_decode(gz_file_get_contents($cacheFile), true);
    }
    if (!isset($slices)) {
        GetTimeline($testPath, $run, $cached, $devTools, $startOffset);
        if (isset($devTools) && is_array($devTools) && count($devTools)) {
            // Do a first pass to get the start and end times as well as the number of threads
            $threads = array(0 => true);
            $startTime = 0;
            $endTime = 0;
            foreach ($devTools as &$entry) {
                if (isset($entry['method']) && $entry['method'] == 'Timeline.eventRecorded' && isset($entry['params']['record'])) {
                    $start = DevToolsEventTime($entry);
                    if ($start && (!$startTime || $start < $startTime)) {
                        $startTime = $start;
                    }
                    $end = DevToolsEventEndTime($entry);
                    if ($end && (!$endTime || $end > $endTime)) {
                        $endTime = $end;
                    }
                    $thread = isset($entry['params']['record']['thread']) ? $entry['params']['record']['thread'] : 0;
                    $threads[$thread] = true;
                }
            }
            // create time slice arrays for each thread
            $slices = array();
            foreach ($threads as $id => $bogus) {
                $slices[$id] = array();
            }
            // create 1ms time slices for the full time
            if ($endTime > $startTime) {
                $startTime = floor($startTime);
                $endTime = ceil($endTime);
                for ($i = $startTime; $i <= $endTime; $i++) {
                    $ms = intval($i - $startTime);
                    foreach ($threads as $id => $bogus) {
                        $slices[$id][$ms] = array();
                    }
                }
                // Go through each element and account for the time
                foreach ($devTools as &$entry) {
                    if (isset($entry['method']) && $entry['method'] == 'Timeline.eventRecorded' && isset($entry['params']['record'])) {
                        $count += DevToolsGetEventTimes($entry['params']['record'], $startTime, $slices);
                    }
                }
            }
        }
        if ($count) {
            // remove any threads that didn't have actual slices populated
            $emptyThreads = array();
            foreach ($slices as $thread => &$records) {
                $is_empty = true;
                foreach ($records as $ms => &$values) {
                    if (count($values)) {
                        $is_empty = false;
                        break;
                    }
                }
                if ($is_empty) {
                    $emptyThreads[] = $thread;
                }
            }
            if (count($emptyThreads)) {
                foreach ($emptyThreads as $thread) {
                    unset($slices[$thread]);
                }
            }
            gz_file_put_contents($cacheFile, json_encode($slices));
        } else {
            $slices = null;
        }
    }
    return $slices;
}