Benchmark.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * CodeIgniter Benchmark Class
  18. *
  19. * This class enables you to mark points and calculate the time difference
  20. * between them. Memory consumption can also be displayed.
  21. *
  22. * @package CodeIgniter
  23. * @subpackage Libraries
  24. * @category Libraries
  25. * @author ExpressionEngine Dev Team
  26. * @link http://codeigniter.com/user_guide/libraries/benchmark.html
  27. */
  28. class CI_Benchmark {
  29. /**
  30. * List of all benchmark markers and when they were added
  31. *
  32. * @var array
  33. */
  34. var $marker = array();
  35. // --------------------------------------------------------------------
  36. /**
  37. * Set a benchmark marker
  38. *
  39. * Multiple calls to this function can be made so that several
  40. * execution points can be timed
  41. *
  42. * @access public
  43. * @param string $name name of the marker
  44. * @return void
  45. */
  46. function mark($name)
  47. {
  48. $this->marker[$name] = microtime();
  49. }
  50. // --------------------------------------------------------------------
  51. /**
  52. * Calculates the time difference between two marked points.
  53. *
  54. * If the first parameter is empty this function instead returns the
  55. * {elapsed_time} pseudo-variable. This permits the full system
  56. * execution time to be shown in a template. The output class will
  57. * swap the real value for this variable.
  58. *
  59. * @access public
  60. * @param string a particular marked point
  61. * @param string a particular marked point
  62. * @param integer the number of decimal places
  63. * @return mixed
  64. */
  65. function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
  66. {
  67. if ($point1 == '')
  68. {
  69. return '{elapsed_time}';
  70. }
  71. if ( ! isset($this->marker[$point1]))
  72. {
  73. return '';
  74. }
  75. if ( ! isset($this->marker[$point2]))
  76. {
  77. $this->marker[$point2] = microtime();
  78. }
  79. list($sm, $ss) = explode(' ', $this->marker[$point1]);
  80. list($em, $es) = explode(' ', $this->marker[$point2]);
  81. return number_format(($em + $es) - ($sm + $ss), $decimals);
  82. }
  83. // --------------------------------------------------------------------
  84. /**
  85. * Memory Usage
  86. *
  87. * This function returns the {memory_usage} pseudo-variable.
  88. * This permits it to be put it anywhere in a template
  89. * without the memory being calculated until the end.
  90. * The output class will swap the real value for this variable.
  91. *
  92. * @access public
  93. * @return string
  94. */
  95. function memory_usage()
  96. {
  97. return '{memory_usage}';
  98. }
  99. }
  100. // END CI_Benchmark class
  101. /* End of file Benchmark.php */
  102. /* Location: ./system/core/Benchmark.php */