PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/code/ryzom/tools/server/www/webtt/plugins/debug_kit/vendors/shells/benchmark.php

https://bitbucket.org/mattraykowski/ryzomcore_demoshard
PHP | 167 lines | 81 code | 17 blank | 69 comment | 7 complexity | 8d07758e9ca23d4cca4e23be339fb5e8 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Benchmark Shell.
  4. *
  5. * Provides basic benchmarking of application requests
  6. * functionally similar to Apache AB
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  11. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://cakephp.org
  18. * @package debug_kit
  19. * @subpackage debug_kit.vendors.shells
  20. * @since DebugKit 1.0
  21. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  22. **/
  23. /**
  24. * Benchmark Shell Class
  25. *
  26. * @package cake
  27. * @subpackage cake.debug_kit.vendors.shells
  28. * @todo Print/export time detail information
  29. * @todo Export/graphing of data to .dot format for graphviz visualization
  30. * @todo Make calculated results round to leading significant digit position of std dev.
  31. */
  32. class BenchmarkShell extends Shell {
  33. /**
  34. * Main execution of shell
  35. *
  36. * @return void
  37. * @access public
  38. */
  39. function main() {
  40. if (empty($this->args) || count($this->args) > 1) {
  41. return $this->help();
  42. }
  43. $url = $this->args[0];
  44. $defaults = array('t' => 100, 'n' => 10);
  45. $options = array_merge($defaults, $this->params);
  46. $times = array();
  47. $this->out(String::insert(__d('debug_kit', '-> Testing :url', true), compact('url')));
  48. $this->out("");
  49. for ($i = 0; $i < $options['n']; $i++) {
  50. if (floor($options['t'] - array_sum($times)) <= 0 || $options['n'] <= 1) {
  51. break;
  52. }
  53. $start = microtime(true);
  54. file_get_contents($url);
  55. $stop = microtime(true);
  56. $times[] = $stop - $start;
  57. }
  58. $this->_results($times);
  59. }
  60. /**
  61. * Prints calculated results
  62. *
  63. * @param array $times Array of time values
  64. * @return void
  65. * @access protected
  66. */
  67. function _results($times) {
  68. $duration = array_sum($times);
  69. $requests = count($times);
  70. $this->out(String::insert(__d('debug_kit', 'Total Requests made: :requests', true), compact('requests')));
  71. $this->out(String::insert(__d('debug_kit', 'Total Time elapsed: :duration (seconds)', true), compact('duration')));
  72. $this->out("");
  73. $this->out(String::insert(__d('debug_kit', 'Requests/Second: :rps req/sec', true), array(
  74. 'rps' => round($requests / $duration, 3)
  75. )));
  76. $this->out(String::insert(__d('debug_kit', 'Average request time: :average-time seconds', true), array(
  77. 'average-time' => round($duration / $requests, 3)
  78. )));
  79. $this->out(String::insert(__d('debug_kit', 'Standard deviation of average request time: :std-dev', true), array(
  80. 'std-dev' => round($this->_deviation($times, true), 3)
  81. )));
  82. $this->out(String::insert(__d('debug_kit', 'Longest/shortest request: :longest sec/:shortest sec', true), array(
  83. 'longest' => round(max($times), 3),
  84. 'shortest' => round(min($times), 3)
  85. )));
  86. $this->out("");
  87. }
  88. /**
  89. * One-pass, numerically stable calculation of population variance.
  90. *
  91. * Donald E. Knuth (1998).
  92. * The Art of Computer Programming, volume 2: Seminumerical Algorithms, 3rd edn.,
  93. * p. 232. Boston: Addison-Wesley.
  94. *
  95. * @param array $times Array of values
  96. * @param boolean $sample If true, calculates an unbiased estimate of the population
  97. * variance from a finite sample.
  98. * @return float Variance
  99. * @access protected
  100. */
  101. function _variance($times, $sample = true) {
  102. $n = $mean = $M2 = 0;
  103. foreach($times as $time){
  104. $n += 1;
  105. $delta = $time - $mean;
  106. $mean = $mean + $delta/$n;
  107. $M2 = $M2 + $delta*($time - $mean);
  108. }
  109. if ($sample) $n -= 1;
  110. return $M2/$n;
  111. }
  112. /**
  113. * Calculate the standard deviation.
  114. *
  115. * @param array $times Array of values
  116. * @return float Standard deviation
  117. * @access protected
  118. */
  119. function _deviation($times, $sample = true) {
  120. return sqrt($this->_variance($times, $sample));
  121. }
  122. /**
  123. * Help for Benchmark shell
  124. *
  125. * @return void
  126. * @access public
  127. */
  128. function help() {
  129. $this->out(__d('debug_kit', "DebugKit Benchmark Shell", true));
  130. $this->out("");
  131. $this->out(__d('debug_kit', "\tAllows you to obtain some rough benchmarking statistics \n\tabout a fully qualified URL.", true));
  132. $this->out("");
  133. $this->out(__d('debug_kit', "\tUse:", true));
  134. $this->out(__d('debug_kit', "\t\tcake benchmark [-n iterations] [-t timeout] url", true));
  135. $this->out("");
  136. $this->out(__d('debug_kit', "\tParams:", true));
  137. $this->out(__d('debug_kit', "\t\t-n Number of iterations to perform. Defaults to 10. \n\t\t Must be an integer.", true));
  138. $this->out(__d('debug_kit', "\t\t-t Maximum total time for all iterations, in seconds. \n\t\t Defaults to 100. Must be an integer.", true));
  139. $this->out("");
  140. $this->out(__d('debug_kit', "\tIf a single iteration takes more than the \n\ttimeout specified, only one request will be made.", true));
  141. $this->out("");
  142. $this->out(__d('debug_kit', "\tExample Use:", true));
  143. $this->out(__d('debug_kit', "\t\tcake benchmark -n 10 -t 100 http://localhost/testsite", true));
  144. $this->out("");
  145. $this->out(__d('debug_kit', "\tNote that this benchmark does not include browser render time", true));
  146. $this->out("");
  147. $this->hr();
  148. $this->out("");
  149. }
  150. }