profiler.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Fuel\Core;
  3. import('phpquickprofiler/console', 'vendor');
  4. import('phpquickprofiler/phpquickprofiler', 'vendor');
  5. use \Console;
  6. use \PhpQuickProfiler;
  7. class Profiler
  8. {
  9. protected static $profiler = null;
  10. protected static $query = null;
  11. public static function init()
  12. {
  13. if ( ! \Fuel::$is_cli and ! \Input::is_ajax() and ! static::$profiler)
  14. {
  15. static::$profiler = new PhpQuickProfiler(FUEL_START_TIME);
  16. static::$profiler->queries = array();
  17. static::$profiler->queryCount = 0;
  18. static::mark(__METHOD__.' Start');
  19. \Fuel::$profiling = true;
  20. }
  21. }
  22. public static function mark($label)
  23. {
  24. static::$profiler and Console::logSpeed($label);
  25. }
  26. public static function mark_memory($var = false, $name = 'PHP')
  27. {
  28. static::$profiler and Console::logMemory($var, $name);
  29. }
  30. public static function console($text)
  31. {
  32. static::$profiler and Console::log($text);
  33. }
  34. public static function output()
  35. {
  36. return static::$profiler ? static::$profiler->display(static::$profiler) : '';
  37. }
  38. public static function start($dbname, $sql)
  39. {
  40. if (static::$profiler)
  41. {
  42. static::$query = array(
  43. 'sql' => \Security::htmlentities($sql),
  44. 'time' => static::$profiler->getMicroTime(),
  45. );
  46. return true;
  47. }
  48. }
  49. public static function stop($text)
  50. {
  51. if (static::$profiler)
  52. {
  53. static::$query['time'] = (static::$profiler->getMicroTime() - static::$query['time']) *1000;
  54. array_push(static::$profiler->queries, static::$query);
  55. static::$profiler->queryCount++;
  56. }
  57. }
  58. public static function delete($text)
  59. {
  60. static::$query = null;
  61. }
  62. public static function app_total()
  63. {
  64. return array(
  65. microtime(true) - FUEL_START_TIME,
  66. memory_get_peak_usage() - FUEL_START_MEM
  67. );
  68. }
  69. }