Codebench.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Codebench — A benchmarking module.
  4. *
  5. * @package koseven/Codebench
  6. * @category Base
  7. *
  8. * @copyright (c) 2007-2016 Kohana Team
  9. * @copyright (c) since 2016 Koseven Team
  10. * @license https://koseven.dev/LICENSE
  11. */
  12. abstract class KO7_Codebench {
  13. /**
  14. * @var string Some optional explanatory comments about the benchmark file.
  15. * HTML allowed. URLs will be converted to links automatically.
  16. */
  17. public $description = '';
  18. /**
  19. * @var integer How many times to execute each method per subject.
  20. */
  21. public $loops = 1000;
  22. /**
  23. * @var array The subjects to supply iteratively to your benchmark methods.
  24. */
  25. public $subjects = [];
  26. /**
  27. * @var array Grade letters with their maximum scores. Used to color the graphs.
  28. */
  29. public $grades = [
  30. 125 => 'A',
  31. 150 => 'B',
  32. 200 => 'C',
  33. 300 => 'D',
  34. 500 => 'E',
  35. 'default' => 'F',
  36. ];
  37. /**
  38. * Constructor.
  39. *
  40. * @return void
  41. */
  42. public function __construct()
  43. {
  44. // Set the maximum execution time
  45. set_time_limit(KO7::$config->load('codebench')->max_execution_time);
  46. }
  47. /**
  48. * Runs Codebench on the extending class.
  49. *
  50. * @return array benchmark output
  51. */
  52. public function run()
  53. {
  54. // Array of all methods to loop over
  55. $methods = array_filter(get_class_methods($this), [$this, '_method_filter']);
  56. // Make sure the benchmark runs at least once,
  57. // also if no subject data has been provided.
  58. if (empty($this->subjects))
  59. {
  60. $this->subjects = ['NULL' => NULL];
  61. }
  62. // Initialize benchmark output
  63. $codebench = [
  64. 'class' => get_class($this),
  65. 'description' => $this->description,
  66. 'loops' => [
  67. 'base' => (int) $this->loops,
  68. 'total' => (int) $this->loops * count($this->subjects) * count($methods),
  69. ],
  70. 'subjects' => $this->subjects,
  71. 'benchmarks' => [],
  72. ];
  73. // Benchmark each method
  74. foreach ($methods as $method)
  75. {
  76. // Initialize benchmark output for this method
  77. $codebench['benchmarks'][$method] = ['time' => 0, 'memory' => 0];
  78. // Using Reflection because simply calling $this->$method($subject) in the loop below
  79. // results in buggy benchmark times correlating to the length of the method name.
  80. $reflection = new ReflectionMethod(get_class($this), $method);
  81. // Benchmark each subject on each method
  82. foreach ($this->subjects as $subject_key => $subject)
  83. {
  84. // Prerun each method/subject combo before the actual benchmark loop.
  85. // This way relatively expensive initial processes won't be benchmarked, e.g. autoloading.
  86. // At the same time we capture the return here so we don't have to do that in the loop anymore.
  87. $return = $reflection->invoke($this, $subject);
  88. // Start the timer for one subject
  89. $token = Profiler::start('codebench', $method.$subject_key);
  90. // The heavy work
  91. for ($i = 0; $i < $this->loops; ++$i)
  92. {
  93. $reflection->invoke($this, $subject);
  94. }
  95. // Stop and read the timer
  96. $benchmark = Profiler::total($token);
  97. // Benchmark output specific to the current method and subject
  98. $codebench['benchmarks'][$method]['subjects'][$subject_key] = [
  99. 'return' => $return,
  100. 'time' => $benchmark[0],
  101. 'memory' => $benchmark[1],
  102. ];
  103. // Update method totals
  104. $codebench['benchmarks'][$method]['time'] += $benchmark[0];
  105. $codebench['benchmarks'][$method]['memory'] += $benchmark[1];
  106. }
  107. }
  108. // Initialize the fastest and slowest benchmarks for both methods and subjects, time and memory,
  109. // these values will be overwritten using min() and max() later on.
  110. // The 999999999 values look like a hack, I know, but they work,
  111. // unless your method runs for more than 31 years or consumes over 1GB of memory.
  112. $fastest_method = $fastest_subject = ['time' => 999999999, 'memory' => 999999999];
  113. $slowest_method = $slowest_subject = ['time' => 0, 'memory' => 0];
  114. // Find the fastest and slowest benchmarks, needed for the percentage calculations
  115. foreach ($methods as $method)
  116. {
  117. // Update the fastest and slowest method benchmarks
  118. $fastest_method['time'] = min($fastest_method['time'], $codebench['benchmarks'][$method]['time']);
  119. $fastest_method['memory'] = min($fastest_method['memory'], $codebench['benchmarks'][$method]['memory']);
  120. $slowest_method['time'] = max($slowest_method['time'], $codebench['benchmarks'][$method]['time']);
  121. $slowest_method['memory'] = max($slowest_method['memory'], $codebench['benchmarks'][$method]['memory']);
  122. foreach ($this->subjects as $subject_key => $subject)
  123. {
  124. // Update the fastest and slowest subject benchmarks
  125. $fastest_subject['time'] = min($fastest_subject['time'], $codebench['benchmarks'][$method]['subjects'][$subject_key]['time']);
  126. $fastest_subject['memory'] = min($fastest_subject['memory'], $codebench['benchmarks'][$method]['subjects'][$subject_key]['memory']);
  127. $slowest_subject['time'] = max($slowest_subject['time'], $codebench['benchmarks'][$method]['subjects'][$subject_key]['time']);
  128. $slowest_subject['memory'] = max($slowest_subject['memory'], $codebench['benchmarks'][$method]['subjects'][$subject_key]['memory']);
  129. }
  130. }
  131. // Percentage calculations for methods
  132. foreach ($codebench['benchmarks'] as & $method)
  133. {
  134. // Calculate percentage difference relative to fastest and slowest methods
  135. $method['percent']['fastest']['time'] = (empty($fastest_method['time'])) ? 0 : ($method['time'] / $fastest_method['time'] * 100);
  136. $method['percent']['fastest']['memory'] = (empty($fastest_method['memory'])) ? 0 : ($method['memory'] / $fastest_method['memory'] * 100);
  137. $method['percent']['slowest']['time'] = (empty($slowest_method['time'])) ? 0 : ($method['time'] / $slowest_method['time'] * 100);
  138. $method['percent']['slowest']['memory'] = (empty($slowest_method['memory'])) ? 0 : ($method['memory'] / $slowest_method['memory'] * 100);
  139. // Assign a grade for time and memory to each method
  140. $method['grade']['time'] = $this->_grade($method['percent']['fastest']['time']);
  141. $method['grade']['memory'] = $this->_grade($method['percent']['fastest']['memory']);
  142. // Percentage calculations for subjects
  143. foreach ($method['subjects'] as & $subject)
  144. {
  145. // Calculate percentage difference relative to fastest and slowest subjects for this method
  146. $subject['percent']['fastest']['time'] = (empty($fastest_subject['time'])) ? 0 : ($subject['time'] / $fastest_subject['time'] * 100);
  147. $subject['percent']['fastest']['memory'] = (empty($fastest_subject['memory'])) ? 0 : ($subject['memory'] / $fastest_subject['memory'] * 100);
  148. $subject['percent']['slowest']['time'] = (empty($slowest_subject['time'])) ? 0 : ($subject['time'] / $slowest_subject['time'] * 100);
  149. $subject['percent']['slowest']['memory'] = (empty($slowest_subject['memory'])) ? 0 : ($subject['memory'] / $slowest_subject['memory'] * 100);
  150. // Assign a grade letter for time and memory to each subject
  151. $subject['grade']['time'] = $this->_grade($subject['percent']['fastest']['time']);
  152. $subject['grade']['memory'] = $this->_grade($subject['percent']['fastest']['memory']);
  153. }
  154. }
  155. return $codebench;
  156. }
  157. /**
  158. * Callback for array_filter().
  159. * Filters out all methods not to benchmark.
  160. *
  161. * @param string method name
  162. * @return boolean
  163. */
  164. protected function _method_filter($method)
  165. {
  166. // Only benchmark methods with the "bench" prefix
  167. return (substr($method, 0, 5) === 'bench');
  168. }
  169. /**
  170. * Returns the applicable grade letter for a score.
  171. *
  172. * @param integer|double score
  173. * @return string grade letter
  174. */
  175. protected function _grade($score)
  176. {
  177. foreach ($this->grades as $max => $grade)
  178. {
  179. if ($max === 'default')
  180. continue;
  181. if ($score <= $max)
  182. return $grade;
  183. }
  184. return $this->grades['default'];
  185. }
  186. }