PageRenderTime 24ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/bulktest/stats.php

http://webpagetest.googlecode.com/
PHP | 256 lines | 250 code | 4 blank | 2 comment | 9 complexity | 8638e11d6fcb1ece9415e1d1befb6720 MD5 | raw file
Possible License(s): AGPL-1.0, Apache-2.0, GPL-3.0, LGPL-3.0, MIT, BSD-3-Clause, ISC, LGPL-2.1
  1. <?php
  2. include './settings.inc';
  3. $results = array();
  4. $loc = array();
  5. $loc['EC2_East_Chromium:Webkit.DSL'] = 'Webkit';
  6. $loc['EC2_East_Chromium:Clone.DSL'] = 'Clone Invalidate';
  7. $loc['EC2_East_Chromium:Invalidate.DSL'] = 'Invalidate';
  8. $loc['EC2_East_Chromium:iframes.DSL'] = 'Preload iframes';
  9. $metrics = array('ttfb', 'startRender', 'docComplete', 'fullyLoaded', 'speedIndex', 'bytes', 'requests');
  10. // Load the existing results
  11. if (LoadResults($results)) {
  12. // split them up by URL and location
  13. $data = array();
  14. $stats = array();
  15. foreach($results as &$result) {
  16. $url = $result['url'];
  17. $location = $loc[$result['location']];
  18. if( !array_key_exists($url, $data) ) {
  19. $data[$url] = array();
  20. }
  21. $data[$url][$location] = array();
  22. $data[$url][$location]['id'] = $result['id'];
  23. $data[$url][$location]['result'] = $result['result'];
  24. if( $result['result'] == 0 || $result['result'] == 99999 ) {
  25. if (!array_key_exists($location, $stats)) {
  26. $stats[$location] = array();
  27. foreach ($metrics as $metric) {
  28. $stats[$location][$metric] = array();
  29. }
  30. }
  31. foreach ($metrics as $metric) {
  32. $data[$url][$location][$metric] = $result[$metric];
  33. $stats[$location][$metric][] = $result[$metric];
  34. }
  35. }
  36. }
  37. ksort($data);
  38. foreach ($metrics as $metric) {
  39. $file = fopen("./$metric.csv", 'w+');
  40. if ($file) {
  41. fwrite($file, 'URL,');
  42. $metricData = array();
  43. $first = true;
  44. foreach($loc as $label) {
  45. fwrite($file, "$label,");
  46. if (!$first)
  47. fwrite($file, "$label Delta,");
  48. $metricData[$label] = array();
  49. $first = false;
  50. }
  51. fwrite($file, "Test Comparison\r\n");
  52. foreach($data as $url => &$urlData) {
  53. fwrite($file, "$url,");
  54. $compare = "\"http://www.webpagetest.org/video/compare.php?thumbSize=200&ival=100&end=doc&tests=";
  55. $first = true;
  56. $baseline = null;
  57. foreach($loc as $label) {
  58. $value = '';
  59. if (array_key_exists($label, $urlData) && array_key_exists($metric, $urlData[$label]))
  60. $value = $urlData[$label][$metric];
  61. fwrite($file, "$value,");
  62. if ($first)
  63. $baseline = $value;
  64. else {
  65. $delta = '';
  66. if (strlen($value) && strlen($baseline))
  67. $delta = $value - $baseline;
  68. fwrite($file, "$delta,");
  69. }
  70. if (strlen($value))
  71. $metricData[$label][] = $value;
  72. if (array_key_exists($label, $urlData) && array_key_exists('id', $urlData[$label]))
  73. $compare .= $urlData[$label]['id'] . '-l:' . urlencode($label) . ',';
  74. $first = false;
  75. }
  76. $compare .= '"';
  77. fwrite($file, $compare);
  78. fwrite($file, "\r\n");
  79. }
  80. fclose($file);
  81. $summary = fopen("./{$metric}_Summary.csv", 'w+');
  82. if ($summary) {
  83. fwrite($summary, ',');
  84. $first = true;
  85. foreach($loc as $label) {
  86. sort($metricData[$label]);
  87. fwrite($summary, "$label,");
  88. if (!$first)
  89. fwrite($summary, "$label Delta,");
  90. $first = false;
  91. }
  92. fwrite($summary, "\r\n");
  93. fwrite($summary, 'Average,');
  94. $first = true;
  95. $baseline = null;
  96. $testCount = 0;
  97. foreach($loc as $label) {
  98. $value = '';
  99. if (array_key_exists($label, $metricData))
  100. $value = Avg($metricData[$label]);
  101. fwrite($summary, "$value,");
  102. if ($first) {
  103. $testCount = count($metricData[$label]);
  104. $baseline = $value;
  105. } else {
  106. $delta = '';
  107. if (strlen($value) && strlen($baseline)) {
  108. $delta = $value - $baseline;
  109. if ($baseline) {
  110. $deltaPct = number_format(($delta / $baseline) * 100, 2);
  111. $delta .= " ($deltaPct%)";
  112. }
  113. }
  114. fwrite($summary, "$delta,");
  115. }
  116. $first = false;
  117. }
  118. fwrite($summary, "\r\n");
  119. $percentiles = array(25,50,75,95,99);
  120. foreach($percentiles as $percentile) {
  121. fwrite($summary, "{$percentile}th Percentile,");
  122. $first = true;
  123. $baseline = null;
  124. foreach($loc as $label) {
  125. $value = '';
  126. if (array_key_exists($label, $metricData))
  127. $value = Percentile($metricData[$label], $percentile);
  128. fwrite($summary, "$value,");
  129. if ($first)
  130. $baseline = $value;
  131. else {
  132. $delta = '';
  133. if (strlen($value) && strlen($baseline)) {
  134. $delta = $value - $baseline;
  135. if ($baseline) {
  136. $deltaPct = number_format(($delta / $baseline) * 100, 2);
  137. $delta .= " ($deltaPct%)";
  138. }
  139. }
  140. fwrite($summary, "$delta,");
  141. }
  142. $first = false;
  143. }
  144. fwrite($summary, "\r\n");
  145. }
  146. fwrite($summary, "\r\n");
  147. fwrite($summary, "Test Count,$testCount\r\n");
  148. fclose($summary);
  149. }
  150. }
  151. }
  152. }
  153. function Avg(&$data) {
  154. $avg = '';
  155. $count = count($data);
  156. if ($count) {
  157. $total = 0;
  158. foreach($data as $value) {
  159. $total += $value;
  160. }
  161. $avg = round($total / $count);
  162. }
  163. return $avg;
  164. }
  165. function Percentile(&$data, $percentile) {
  166. $val = '';
  167. $count = count($data);
  168. if ($count) {
  169. $pos = min($count - 1, max(0,floor((($count - 1) * $percentile) / 100)));
  170. $val = $data[$pos];
  171. }
  172. return $val;
  173. }
  174. /**
  175. * Dump the data of a particular type to a tab-delimited report
  176. *
  177. * @param mixed $data
  178. * @param mixed $type
  179. */
  180. function ReportData(&$data, $type)
  181. {
  182. global $locations;
  183. $file = fopen("./report_$type.txt", 'wb');
  184. if($file)
  185. {
  186. fwrite($file, "URL\t");
  187. foreach($locations as $location)
  188. fwrite($file, "$location $type avg\t");
  189. foreach($locations as $location)
  190. fwrite($file, "$location $type stddev\t");
  191. foreach($locations as $location)
  192. fwrite($file, "$location $type stddev/avg\t");
  193. fwrite($file, "\r\n");
  194. foreach($data as $urlhash => &$urlentry)
  195. {
  196. fwrite($file, "{$urlentry['url']}\t");
  197. foreach($locations as $location){
  198. $value = $urlentry['data'][$location]["{$type}_avg"];
  199. fwrite($file, "$value\t");
  200. }
  201. foreach($locations as $location){
  202. $value = $urlentry['data'][$location]["{$type}_stddev"];
  203. fwrite($file, "$value\t");
  204. }
  205. foreach($locations as $location){
  206. $value = $urlentry['data'][$location]["{$type}_ratio"];
  207. fwrite($file, "$value\t");
  208. }
  209. fwrite($file, "\r\n");
  210. }
  211. fclose($file);
  212. }
  213. }
  214. /**
  215. * Calculate the average and standard deviation for the supplied data set
  216. *
  217. * @param mixed $data
  218. * @param mixed $avg
  219. * @param mixed $stddev
  220. * @param mixed $ratio
  221. */
  222. function CalculateStats(&$data, &$avg, &$stddev, &$ratio)
  223. {
  224. $avg = 0;
  225. $stddev = 0;
  226. $ratio = 0;
  227. // pass 1 - average
  228. $total = 0;
  229. $count = 0;
  230. foreach($data as $value){
  231. $total += $value;
  232. $count++;
  233. }
  234. if( $count ){
  235. $avg = $total / $count;
  236. // pass 2 - stddev
  237. $total = 0;
  238. foreach($data as $value){
  239. $total += pow($value - $avg, 2);
  240. }
  241. $stddev = sqrt($total / $count);
  242. if ($avg)
  243. $ratio = $stddev / $avg;
  244. }
  245. }
  246. ?>