Benchmark.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace ZN\Benchmarking;
  3. class __USE_STATIC_ACCESS__Benchmark implements BenchmarkInterface
  4. {
  5. //----------------------------------------------------------------------------------------------------
  6. //
  7. // Yazar : Ozan UYKUN <ozanbote@windowslive.com> | <ozanbote@gmail.com>
  8. // Site : www.zntr.net
  9. // Lisans : The MIT License
  10. // Telif Hakkı: Copyright (c) 2012-2016, zntr.net
  11. //
  12. //----------------------------------------------------------------------------------------------------
  13. //----------------------------------------------------------------------------------------------------
  14. // Protected Test
  15. //----------------------------------------------------------------------------------------------------
  16. //
  17. // Test isimleri bilgisi
  18. //
  19. // @var array
  20. //
  21. //----------------------------------------------------------------------------------------------------
  22. protected $tests = [];
  23. //----------------------------------------------------------------------------------------------------
  24. // Protected Memtests
  25. //----------------------------------------------------------------------------------------------------
  26. //
  27. // Bellek test isimleri bilgisi
  28. //
  29. // @var array
  30. //
  31. //----------------------------------------------------------------------------------------------------
  32. protected $memtests = [];
  33. //----------------------------------------------------------------------------------------------------
  34. // Protected Memtests
  35. //----------------------------------------------------------------------------------------------------
  36. //
  37. // Bellek test isimleri bilgisi
  38. //
  39. // @var array
  40. //
  41. //----------------------------------------------------------------------------------------------------
  42. protected $usedtests = [];
  43. //----------------------------------------------------------------------------------------------------
  44. // Protected Test Count
  45. //----------------------------------------------------------------------------------------------------
  46. //
  47. // Test sayısı bilgisi
  48. //
  49. // @var numeric
  50. //
  51. //----------------------------------------------------------------------------------------------------
  52. protected $testCount = 0;
  53. //----------------------------------------------------------------------------------------------------
  54. // Call Method
  55. //----------------------------------------------------------------------------------------------------
  56. //
  57. // __call()
  58. //
  59. //----------------------------------------------------------------------------------------------------
  60. use \CallUndefinedMethodTrait;
  61. //----------------------------------------------------------------------------------------------------
  62. // Error Control
  63. //----------------------------------------------------------------------------------------------------
  64. //
  65. // $error
  66. // $success
  67. //
  68. // error()
  69. // success()
  70. //
  71. //----------------------------------------------------------------------------------------------------
  72. use \ErrorControlTrait;
  73. //----------------------------------------------------------------------------------------------------
  74. // Test Methods Başlangıç
  75. //----------------------------------------------------------------------------------------------------
  76. //----------------------------------------------------------------------------------------------------
  77. // Test Start
  78. //----------------------------------------------------------------------------------------------------
  79. //
  80. // @param string $test
  81. // @return void
  82. //
  83. //----------------------------------------------------------------------------------------------------
  84. public function start($test = '')
  85. {
  86. if( ! is_string($test))
  87. {
  88. return \Errors::set('Error', 'stringParameter', 'test');
  89. }
  90. // Kaç test kullanıldığını hesaplamak için
  91. // test count değişkeni birer birer artırılıyor.
  92. $this->testCount++;
  93. // Yöntem içinden tanımlanan kodlardan kaynaklı
  94. // fazlalık hesaplanıyor.
  95. $legancy = ( $this->testCount === 1 )
  96. ? $legancy = 136
  97. : 48;
  98. $test = $test."_start";
  99. // Mikrotime yöntemi başlatılıyor.
  100. $this->tests[$test] = microtime();
  101. // Mikrotime yöntemi başlatılıyor.
  102. $this->usedtests[$test] = get_required_files();
  103. // Bu satıra kadar olan bellek miktarı hesaplanıyor.
  104. $this->memtests[$test] = memory_get_usage() + $legancy;
  105. }
  106. //----------------------------------------------------------------------------------------------------
  107. // Test End
  108. //----------------------------------------------------------------------------------------------------
  109. //
  110. // @param string $test
  111. // @return void
  112. //
  113. //----------------------------------------------------------------------------------------------------
  114. public function end($test = '')
  115. {
  116. if( ! is_string($test) )
  117. {
  118. return \Errors::set('Error', 'stringParameter', 'test');
  119. }
  120. $test = $test."_end";
  121. $this->memtests[$test] = memory_get_usage();
  122. $this->usedtests[$test] = get_required_files();
  123. $this->tests[$test] = microtime();
  124. }
  125. //----------------------------------------------------------------------------------------------------
  126. // Test Methods Bitiş
  127. //----------------------------------------------------------------------------------------------------
  128. //----------------------------------------------------------------------------------------------------
  129. // Result Methods Başlangıç
  130. //----------------------------------------------------------------------------------------------------
  131. //----------------------------------------------------------------------------------------------------
  132. // Elapsed Time
  133. //----------------------------------------------------------------------------------------------------
  134. //
  135. // @param string $result
  136. // @param numeric $decimal
  137. // @return string
  138. //
  139. //----------------------------------------------------------------------------------------------------
  140. public function elapsedTime($result = '', $decimal = 4)
  141. {
  142. if( ! is_string($result) )
  143. {
  144. return \Errors::set('Error', 'stringParameter', 'result');
  145. }
  146. if( ! is_numeric($decimal) )
  147. {
  148. $decimal = 4;
  149. }
  150. $resend = $result."_end";
  151. $restart = $result."_start";
  152. if( isset($this->tests[$resend]) && isset($this->tests[$restart]) )
  153. {
  154. return round(($this->tests[$resend] - $this->tests[$restart]), $decimal);
  155. }
  156. else
  157. {
  158. return false;
  159. }
  160. }
  161. //----------------------------------------------------------------------------------------------------
  162. // Used Files
  163. //----------------------------------------------------------------------------------------------------
  164. //
  165. // @param string $result
  166. // @return numeric
  167. //
  168. //----------------------------------------------------------------------------------------------------
  169. public function usedFiles($result = '')
  170. {
  171. if( ! is_string($result) )
  172. {
  173. return \Errors::set('Error', 'stringParameter', 'result');
  174. }
  175. if( empty($result) )
  176. {
  177. return get_required_files();
  178. }
  179. $resend = $result."_end";
  180. $restart = $result."_start";
  181. if( isset($this->usedtests[$resend]) && isset($this->usedtests[$restart]) )
  182. {
  183. return array_diff($this->usedtests[$resend], $this->usedtests[$restart]);
  184. }
  185. }
  186. //----------------------------------------------------------------------------------------------------
  187. // Used File Count
  188. //----------------------------------------------------------------------------------------------------
  189. //
  190. // @param string $result
  191. // @return numeric
  192. //
  193. //----------------------------------------------------------------------------------------------------
  194. public function usedFileCount($result = '')
  195. {
  196. if( ! is_string($result) )
  197. {
  198. return \Errors::set('Error', 'stringParameter', 'result');
  199. }
  200. if( empty($result) )
  201. {
  202. return get_required_files();
  203. }
  204. $resend = $result."_end";
  205. $restart = $result."_start";
  206. if( isset($this->usedtests[$resend]) && isset($this->usedtests[$restart]) )
  207. {
  208. return count($this->usedtests[$resend]) - count($this->usedtests[$restart]);
  209. }
  210. }
  211. //----------------------------------------------------------------------------------------------------
  212. // Calculated Memory
  213. //----------------------------------------------------------------------------------------------------
  214. //
  215. // @param string $result
  216. // @return string
  217. //
  218. //----------------------------------------------------------------------------------------------------
  219. public function calculatedMemory($result = '')
  220. {
  221. if( ! is_string($result) )
  222. {
  223. return \Errors::set('Error', 'stringParameter', 'result');
  224. }
  225. $resend = $result."_end";
  226. $restart = $result."_start";
  227. if( isset($this->memtests[$resend]) && isset($this->memtests[$restart]) )
  228. {
  229. $calc = $this->memtests[$resend] - $this->memtests[$restart];
  230. return $calc;
  231. }
  232. else
  233. {
  234. return false;
  235. }
  236. }
  237. //----------------------------------------------------------------------------------------------------
  238. // Result Methods Bitiş
  239. //----------------------------------------------------------------------------------------------------
  240. //----------------------------------------------------------------------------------------------------
  241. // Memory Methods Başlangıç
  242. //----------------------------------------------------------------------------------------------------
  243. //----------------------------------------------------------------------------------------------------
  244. // Memory Usage
  245. //----------------------------------------------------------------------------------------------------
  246. //
  247. // @param bool $realMemory
  248. // @return string
  249. //
  250. //----------------------------------------------------------------------------------------------------
  251. public function memoryUsage($realMemory = false)
  252. {
  253. if( ! is_bool($realMemory) )
  254. {
  255. $realMemory = false;
  256. }
  257. return memory_get_usage($realMemory);
  258. }
  259. //----------------------------------------------------------------------------------------------------
  260. // Max Memory Usage
  261. //----------------------------------------------------------------------------------------------------
  262. //
  263. // @param bool $realMemory
  264. // @return string
  265. //
  266. //----------------------------------------------------------------------------------------------------
  267. public function maxMemoryUsage($realMemory = false)
  268. {
  269. if( ! is_bool($realMemory) )
  270. {
  271. $realMemory = false;
  272. }
  273. return memory_get_peak_usage($realMemory);
  274. }
  275. //----------------------------------------------------------------------------------------------------
  276. // Memory Methods Bitiş
  277. //----------------------------------------------------------------------------------------------------
  278. }