Benchmark.php 3.0 KB

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