forked from catchpoint/WebPageTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
breakdown.inc
107 lines (97 loc) · 3.15 KB
/
breakdown.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
require_once('object_detail.inc');
require_once('contentColors.inc');
if(extension_loaded('newrelic')) {
newrelic_add_custom_tracer('getBreakdown');
newrelic_add_custom_tracer('getBreakdownCombined');
}
/**
* Aggregate the individual requests by mime type
*
* @param mixed $requests
*/
function getBreakdown($id, $testPath, $run, $cached, &$requests)
{
$cache = array();
$cache_file = "$testPath/breakdown.json";
if (is_file($cache_file)) {
$cache = json_decode(file_get_contents($cache_file), true);
}
if (array_key_exists("run_$run", $cache)) {
if (array_key_exists("cached_$cached", $cache["run_$run"])) {
$breakdown = $cache["run_$run"]["cached_$cached"];
}
} else {
$cache["run_$run"] = array("cached_$cached" => array());
}
if (!isset($breakdown)) {
// get all of the requests
$secure = false;
$haveLocations = false;
$requests = getRequests($id, $testPath, $run, $cached, $secure, $haveLocations, false);
$breakdown = array();
$mime_colors = MimeColors();
foreach ($mime_colors as $contentType => $color) {
$entry = array(
'color' => $color,
'bytes' => 0,
'requests' => 0
);
$breakdown["$contentType"] = $entry;
}
foreach($requests as $request) {
$contentType = ContentType($request['contentType']);
$object = strtolower($request['url']);
if( strlen($contentType) && (stripos($object, 'favicon.ico') === FALSE) ) {
$breakdown[$contentType]['bytes'] += $request['bytesIn'];
$breakdown[$contentType]['requests']++;
}
}
$cache["run_$run"]["cached_$cached"] = $breakdown;
file_put_contents($cache_file, json_encode($cache));
}
return $breakdown;
}
/**
* Get a combined result that has both first and repeat view data
*
* @param mixed $id
* @param mixed $run
*/
function getBreakdownCombined($id, $testPath, $run)
{
$rCombined;
$combined = getBreakdown($id, $testPath, $run, 0, $rCombined);
$rRv;
$rv = getBreakdown($id, $testPath, $run, 1, $rRv);
// combine the two data sets
foreach( $combined as $type => $data )
{
unset($rvData);
$rvData = $rv[$type];
if( !count($rvData) )
{
$rvData['bytes'] = 0;
$rvData['requests'] = 0;
}
$combined[$type]['rvBytes'] = $rvData['bytes'];
$combined[$type]['rvRequests'] = $rvData['requests'];
}
// grab any that had data in the repeat view but not first view
foreach( $rv as $type => $data )
{
unset($fv);
$fv = $combined[$type];
if( !count($fv) )
{
$fv = array();
$fv['bytes'] = 0;
$fv['requests'] = 0;
$fv['rvBytes'] = $data['bytes'];
$fv['rvRequests'] = $data['requests'];
$combined[$type] = $fv;
}
}
return $combined;
}
?>