PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Kint/parsers/custom/microtime.php

https://bitbucket.org/openfisma-ondemand/openfisma
PHP | 54 lines | 32 code | 9 blank | 13 comment | 4 complexity | 11c74194a0f7afa6fc7a0ec544dbc50c MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, GPL-3.0, Apache-2.0, EPL-1.0
  1. <?php
  2. class Kint_Parsers_Microtime extends kintParser
  3. {
  4. private static $_times = array();
  5. private static $_laps = array();
  6. protected function _parse( & $variable )
  7. {
  8. if ( !is_string( $variable ) || !preg_match( '[0\.[0-9]{8} [0-9]{10}]', $variable ) ) {
  9. return false;
  10. }
  11. list( $usec, $sec ) = explode( " ", microtime() );
  12. $time = (float)$usec + (float)$sec;
  13. $size = memory_get_usage( true );
  14. $this->value = @date( 'Y-m-d H:i:s', $sec ) . '.' . substr( $usec, 2, 4 );
  15. $numberOfCalls = count( self::$_times );
  16. if ( $numberOfCalls > 0 ) { # meh, faster than count($times) > 1
  17. $lap = $time - end( self::$_times );
  18. self::$_laps[] = $lap;
  19. $this->value .= "\n<strong>SINCE LAST CALL:</strong> " . round( $lap, 4 ) . 's.';
  20. if ( $numberOfCalls > 1 ) {
  21. $this->value .= "\n<strong>SINCE START:</strong> " . round( $time - self::$_times[0], 4 ) . 's.';
  22. $this->value .= "\n<strong>AVERAGE DURATION:</strong> "
  23. . round( array_sum( self::$_laps ) / $numberOfCalls, 4 ) . 's.';
  24. }
  25. }
  26. $unit = array( 'B', 'KB', 'MB', 'GB', 'TB' );
  27. $this->value .= "\n<strong>MEMORY USAGE:</strong> " . $size . " bytes ("
  28. . round( $size / pow( 1024, ( $i = floor( log( $size, 1024 ) ) ) ), 3 ) . ' ' . $unit[$i] . ")";
  29. self::$_times[] = $time;
  30. $this->type = 'Stats';
  31. }
  32. /*
  33. function test() {
  34. $x = '';
  35. d( microtime() );
  36. for ( $i = 0; $i < 10; $i++ ) {
  37. $x .= str_repeat( 'x', mt_rand( 128 * 1024, 5 * 1024 * 1024 ) );
  38. usleep( mt_rand( 0, 1000 * 1000 ) );
  39. d( microtime() );
  40. }
  41. unset( $x );
  42. dd( microtime() );
  43. }
  44. */
  45. }