PageRenderTime 119ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/Library/Data/Analysis.php

http://phpmemcacheadmin.googlecode.com/
PHP | 355 lines | 216 code | 30 blank | 109 comment | 51 complexity | d0697259754bb381f8490bd66105f20e MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2010 Cyrille Mahieux
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and limitations
  14. * under the License.
  15. *
  16. * ><)))°> ><)))°> ><)))°> ><)))°> ><)))°> ><)))°> ><)))°> ><)))°> ><)))°>
  17. *
  18. * Analysis of memcached command response
  19. *
  20. * @author c.mahieux@of2m.fr
  21. * @since 20/03/2010
  22. */
  23. class Library_Data_Analysis
  24. {
  25. /**
  26. * Merge two arrays of stats from Command_XX::stats()
  27. *
  28. * @param Array $array Statistic from Command_XX::stats()
  29. * @param Array $stats Statistic from Command_XX::stats()
  30. *
  31. * @return Array
  32. */
  33. public static function merge($array, $stats)
  34. {
  35. # Checking input
  36. if(!is_array($array))
  37. {
  38. return $stats;
  39. }
  40. elseif(!is_array($stats))
  41. {
  42. return $array;
  43. }
  44. # Merging Stats
  45. foreach($stats as $key => $value)
  46. {
  47. if(isset($array[$key]) && ($key != 'version') && ($key != 'uptime'))
  48. {
  49. $array[$key] += $value;
  50. }
  51. else
  52. {
  53. $array[$key] = $value;
  54. }
  55. }
  56. return $array;
  57. }
  58. /**
  59. * Diff two arrays of stats from Command_XX::stats()
  60. *
  61. * @param Array $array Statistic from Command_XX::stats()
  62. * @param Array $stats Statistic from Command_XX::stats()
  63. *
  64. * @return Array
  65. */
  66. public static function diff($array, $stats)
  67. {
  68. # Checking input
  69. if(!is_array($array))
  70. {
  71. return $stats;
  72. }
  73. elseif(!is_array($stats))
  74. {
  75. return $array;
  76. }
  77. # Diff for each key
  78. foreach($stats as $key => $value)
  79. {
  80. if(isset($array[$key]))
  81. {
  82. $stats[$key] = $value - $array[$key];
  83. }
  84. }
  85. return $stats;
  86. }
  87. /**
  88. * Analyse and return memcache stats command
  89. *
  90. * @param Array $stats Statistic from Command_XX::stats()
  91. *
  92. * @return Array
  93. */
  94. public static function stats($stats)
  95. {
  96. if(!is_array($stats) || (count($stats) == 0))
  97. {
  98. return false;
  99. }
  100. # Command set()
  101. $stats['set_rate'] = ($stats['cmd_set'] == 0) ? '0.0' : sprintf('%.1f', $stats['cmd_set'] / $stats['uptime'], 1);
  102. # Command get()
  103. $stats['get_hits_percent'] = ($stats['cmd_get'] == 0) ? ' - ' : sprintf('%.1f', $stats['get_hits'] / $stats['cmd_get'] * 100, 1);
  104. $stats['get_misses_percent'] = ($stats['cmd_get'] == 0) ? ' - ' : sprintf('%.1f', $stats['get_misses'] / $stats['cmd_get'] * 100, 1);
  105. $stats['get_rate'] = ($stats['cmd_get'] == 0) ? '0.0' : sprintf('%.1f', $stats['cmd_get'] / $stats['uptime'], 1);
  106. # Command delete(), version > 1.2.X
  107. if(isset($stats['delete_hits'], $stats['delete_misses']))
  108. {
  109. $stats['cmd_delete'] = $stats['delete_hits'] + $stats['delete_misses'];
  110. $stats['delete_hits_percent'] = ($stats['cmd_delete'] == 0) ? ' - ' : sprintf('%.1f', $stats['delete_hits'] / $stats['cmd_delete'] * 100, 1);
  111. $stats['delete_misses_percent'] = ($stats['cmd_delete'] == 0) ? ' - ' : sprintf('%.1f', $stats['delete_misses'] / $stats['cmd_delete'] * 100, 1);
  112. }
  113. else
  114. {
  115. $stats['cmd_delete'] = 0;
  116. $stats['delete_hits_percent'] = ' - ';
  117. $stats['delete_misses_percent'] = ' - ';
  118. }
  119. $stats['delete_rate'] = ($stats['cmd_delete'] == 0) ? '0.0' : sprintf('%.1f', $stats['cmd_delete'] / $stats['uptime'], 1);
  120. # Command cas(), version > 1.2.X
  121. if(isset($stats['cas_hits'], $stats['cas_misses'], $stats['cas_badval']))
  122. {
  123. $stats['cmd_cas'] = $stats['cas_hits'] + $stats['cas_misses'] + $stats['cas_badval'];
  124. $stats['cas_hits_percent'] = ($stats['cmd_cas'] == 0) ? ' - ' : sprintf('%.1f', $stats['cas_hits'] / $stats['cmd_cas'] * 100, 1);
  125. $stats['cas_misses_percent'] = ($stats['cmd_cas'] == 0) ? ' - ' : sprintf('%.1f', $stats['cas_misses'] / $stats['cmd_cas'] * 100, 1);
  126. $stats['cas_badval_percent'] = ($stats['cmd_cas'] == 0) ? ' - ' : sprintf('%.1f', $stats['cas_badval'] / $stats['cmd_cas'] * 100, 1);
  127. }
  128. else
  129. {
  130. $stats['cmd_cas'] = 0;
  131. $stats['cas_hits_percent'] = ' - ';
  132. $stats['cas_misses_percent'] = ' - ';
  133. $stats['cas_badval_percent'] = ' - ';
  134. }
  135. $stats['cas_rate'] = ($stats['cmd_cas'] == 0) ? '0.0' : sprintf('%.1f', $stats['cmd_cas'] / $stats['uptime'], 1);
  136. # Command increment(), version > 1.2.X
  137. if(isset($stats['incr_hits'], $stats['incr_misses']))
  138. {
  139. $stats['cmd_incr'] = $stats['incr_hits'] + $stats['incr_misses'];
  140. $stats['incr_hits_percent'] = ($stats['cmd_incr'] == 0) ? ' - ' : sprintf('%.1f', $stats['incr_hits'] / $stats['cmd_incr'] * 100, 1);
  141. $stats['incr_misses_percent'] = ($stats['cmd_incr'] == 0) ? ' - ' : sprintf('%.1f', $stats['incr_misses'] / $stats['cmd_incr'] * 100, 1);
  142. }
  143. else
  144. {
  145. $stats['cmd_incr'] = 0;
  146. $stats['incr_hits_percent'] = ' - ';
  147. $stats['incr_misses_percent'] = ' - ';
  148. }
  149. $stats['incr_rate'] = ($stats['cmd_incr'] == 0) ? '0.0' : sprintf('%.1f', $stats['cmd_incr'] / $stats['uptime'], 1);
  150. # Command decrement(), version > 1.2.X
  151. if(isset($stats['decr_hits'], $stats['decr_misses']))
  152. {
  153. $stats['cmd_decr'] = $stats['decr_hits'] + $stats['decr_misses'];
  154. $stats['decr_hits_percent'] = ($stats['cmd_decr'] == 0) ? ' - ' : sprintf('%.1f', $stats['decr_hits'] / $stats['cmd_decr'] * 100, 1);
  155. $stats['decr_misses_percent'] = ($stats['cmd_decr'] == 0) ? ' - ' : sprintf('%.1f', $stats['decr_misses'] / $stats['cmd_decr'] * 100, 1);
  156. }
  157. else
  158. {
  159. $stats['cmd_decr'] = 0;
  160. $stats['decr_hits_percent'] = ' - ';
  161. $stats['decr_misses_percent'] = ' - ';
  162. }
  163. $stats['decr_rate'] = ($stats['cmd_decr'] == 0) ? '0.0' : sprintf('%.1f', $stats['cmd_decr'] / $stats['uptime'], 1);
  164. # Command decrement(), version > 1.4.7
  165. if(isset($stats['touch_hits'], $stats['touch_misses']))
  166. {
  167. $stats['cmd_touch'] = $stats['touch_hits'] + $stats['touch_misses'];
  168. $stats['touch_hits_percent'] = ($stats['cmd_touch'] == 0) ? ' - ' : sprintf('%.1f', $stats['touch_hits'] / $stats['cmd_touch'] * 100, 1);
  169. $stats['touch_misses_percent'] = ($stats['cmd_touch'] == 0) ? ' - ' : sprintf('%.1f', $stats['touch_misses'] / $stats['cmd_touch'] * 100, 1);
  170. }
  171. else
  172. {
  173. $stats['cmd_touch'] = 0;
  174. $stats['touch_hits_percent'] = ' - ';
  175. $stats['touch_misses_percent'] = ' - ';
  176. }
  177. $stats['touch_rate'] = ($stats['cmd_touch'] == 0) ? '0.0' : sprintf('%.1f', $stats['cmd_touch'] / $stats['uptime'], 1);
  178. # Total hit & miss
  179. #$stats['cmd_total'] = $stats['cmd_get'] + $stats['cmd_set'] + $stats['cmd_delete'] + $stats['cmd_cas'] + $stats['cmd_incr'] + $stats['cmd_decr'];
  180. #$stats['hit_percent'] = ($stats['cmd_get'] == 0) ? '0.0' : sprintf('%.1f', ($stats['get_hits']) / ($stats['get_hits'] + $stats['get_misses']) * 100, 1);
  181. #$stats['miss_percent'] = ($stats['cmd_get'] == 0) ? '0.0' : sprintf('%.1f', ($stats['get_misses']) / ($stats['get_hits'] + $stats['get_misses']) * 100, 1);
  182. # Command flush_all
  183. if(isset($stats['cmd_flush']))
  184. {
  185. $stats['flush_rate'] = ($stats['cmd_flush'] == 0) ? '0.0' : sprintf('%.1f', $stats['cmd_flush'] / $stats['uptime'], 1);
  186. }
  187. else
  188. {
  189. $stats['flush_rate'] = '0.0';
  190. }
  191. # Cache size
  192. $stats['bytes_percent'] = ($stats['limit_maxbytes'] == 0) ? '0.0' : sprintf('%.1f', $stats['bytes'] / $stats['limit_maxbytes'] * 100, 1);
  193. # Request rate
  194. $stats['request_rate'] = sprintf('%.1f', ($stats['cmd_get'] + $stats['cmd_set'] + $stats['cmd_delete'] + $stats['cmd_cas'] + $stats['cmd_incr'] + $stats['cmd_decr']) / $stats['uptime'], 1);
  195. $stats['hit_rate'] = sprintf('%.1f', ($stats['get_hits']) / $stats['uptime'], 1);
  196. $stats['miss_rate'] = sprintf('%.1f', ($stats['get_misses']) / $stats['uptime'], 1);
  197. # Eviction & reclaimed rate
  198. $stats['eviction_rate'] = ($stats['evictions'] == 0) ? '0.0' : sprintf('%.1f', $stats['evictions'] / $stats['uptime'], 1);
  199. $stats['reclaimed_rate'] = (!isset($stats['reclaimed']) || ($stats['reclaimed'] == 0)) ? '0.0' : sprintf('%.1f', $stats['reclaimed'] / $stats['uptime'], 1);
  200. return $stats;
  201. }
  202. /**
  203. * Analyse and return memcache slabs command
  204. *
  205. * @param Array $slabs Statistic from Command_XX::slabs()
  206. *
  207. * @return Array
  208. */
  209. public static function slabs($slabs)
  210. {
  211. # Initializing Used Slabs
  212. $slabs['used_slabs'] = 0;
  213. $slabs['total_wasted'] = 0;
  214. # Request Rate par Slabs
  215. foreach($slabs as $id => $slab)
  216. {
  217. # Check if it's a Slab
  218. if(is_numeric($id))
  219. {
  220. # Check if Slab is used
  221. if($slab['used_chunks'] > 0)
  222. {
  223. $slabs['used_slabs']++;
  224. }
  225. $slabs[$id]['request_rate'] = sprintf('%.1f', ($slab['get_hits'] + $slab['cmd_set'] + $slab['delete_hits'] + $slab['cas_hits'] + $slab['cas_badval'] + $slab['incr_hits'] + $slab['decr_hits']) / $slabs['uptime'], 1);
  226. $slabs[$id]['mem_wasted'] = (($slab['total_chunks'] * $slab['chunk_size']) < $slab['mem_requested']) ?(($slab['total_chunks'] - $slab['used_chunks']) * $slab['chunk_size']):(($slab['total_chunks'] * $slab['chunk_size']) - $slab['mem_requested']);
  227. $slabs['total_wasted'] += $slabs[$id]['mem_wasted'];
  228. }
  229. }
  230. # Cheking server total malloced > 0
  231. if(!isset($slabs['total_malloced']))
  232. {
  233. $slabs['total_malloced'] = 0;
  234. }
  235. return $slabs;
  236. }
  237. /**
  238. * Calculate Uptime
  239. *
  240. * @param Integer $uptime Uptime timestamp
  241. *
  242. * @return String
  243. */
  244. public static function uptime($uptime)
  245. {
  246. if($uptime > 0)
  247. {
  248. $days = floor($uptime/60/60/24);
  249. $hours = $uptime/60/60%24;
  250. $mins = $uptime/60%60;
  251. if(($days + $hours + $mins) == 0)
  252. {
  253. return ' less than 1 min';
  254. }
  255. return $days . ' days ' . $hours . ' hrs ' . $mins . ' min';
  256. }
  257. return ' - ';
  258. }
  259. /**
  260. * Resize a byte value
  261. *
  262. * @param Integer $value Value to resize
  263. *
  264. * @return String
  265. */
  266. public static function byteResize($value)
  267. {
  268. # Unit list
  269. $units = array('', 'K', 'M', 'G', 'T');
  270. # Resizing
  271. foreach($units as $unit)
  272. {
  273. if($value < 1024)
  274. {
  275. break;
  276. }
  277. $value /= 1024;
  278. }
  279. return sprintf('%.1f %s', $value, $unit);
  280. }
  281. /**
  282. * Resize a value
  283. *
  284. * @param Integer $value Value to resize
  285. *
  286. * @return String
  287. */
  288. public static function valueResize($value)
  289. {
  290. # Unit list
  291. $units = array('', 'K', 'M', 'G', 'T');
  292. # Resizing
  293. foreach($units as $unit)
  294. {
  295. if($value < 1000)
  296. {
  297. break;
  298. }
  299. $value /= 1000;
  300. }
  301. return sprintf('%.1f%s', $value, $unit);
  302. }
  303. /**
  304. * Resize a hit value
  305. *
  306. * @param Integer $value Hit value to resize
  307. *
  308. * @return String
  309. */
  310. public static function hitResize($value)
  311. {
  312. # Unit list
  313. $units = array('', 'K', 'M', 'G', 'T');
  314. # Resizing
  315. foreach($units as $unit)
  316. {
  317. if($value < 10000000)
  318. {
  319. break;
  320. }
  321. $value /= 1000;
  322. }
  323. return sprintf('%.0f%s', $value, $unit);
  324. }
  325. }