123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php namespace Laravel\Profiling;
- use Laravel\View;
- use Laravel\File;
- use Laravel\Event;
- use Laravel\Config;
- use Laravel\Request;
- use Laravel\Database;
- class Profiler {
-
- protected static $data = array('queries' => array(), 'logs' => array(), 'timers' => array());
-
-
- public static function render($response)
- {
-
-
-
- if ( ! Request::ajax())
- {
- static::$data['memory'] = get_file_size(memory_get_usage(true));
- static::$data['memory_peak'] = get_file_size(memory_get_peak_usage(true));
- static::$data['time'] = number_format((microtime(true) - LARAVEL_START) * 1000, 2);
- foreach ( static::$data['timers'] as &$timer)
- {
- $timer['running_time'] = number_format((microtime(true) - $timer['start'] ) * 1000, 2);
- }
- return render('path: '.__DIR__.'/template'.BLADE_EXT, static::$data);
- }
- }
-
- public static function time( $func, $name = 'default_func_timer' )
- {
-
- $start = microtime(true);
- $func();
- $end = microtime(true);
-
- if (isset(static::$data['timers'][$name]))
- {
- $name = $name.uniqid();
- }
-
-
- static::$data['timers'][$name]['start'] = $start;
- static::$data['timers'][$name]['end'] = $end;
- static::$data['timers'][$name]['time'] = number_format(($end - $start) * 1000, 2);
- }
-
- public static function tick($name = 'default_timer', $callback = null)
- {
- $name = trim($name);
- if (empty($name)) $name = 'default_timer';
-
- if (isset(static::$data['timers'][$name]))
- {
- $current_timer = static::$data['timers'][$name];
- $ticks = count($current_timer['ticks']);
-
- $new_tick = array();
- $mt = microtime(true);
- $new_tick['raw_time'] = $mt - $current_timer['start'];
- $new_tick['time'] = number_format(($mt - $current_timer['start']) * 1000, 2);
-
- if ($ticks > 0)
- {
- $last_tick = $current_timer['ticks'][$ticks- 1]['raw_time'];
- $new_tick['diff'] = number_format(($new_tick['raw_time'] - $last_tick) * 1000, 2);
- }
- else
- {
- $new_tick['diff'] = $new_tick['time'];
- }
-
- static::$data['timers'][$name]['ticks'][] = $new_tick;
- }
- else
- {
-
- static::$data['timers'][$name]['start'] = microtime(true);
- static::$data['timers'][$name]['ticks'] = array();
- }
-
- if ( ! is_null($callback) and is_callable($callback))
- {
-
- call_user_func_array($callback, array(
- static::$data['timers'][$name]
- ));
- }
- }
-
- public static function log($type, $message)
- {
- static::$data['logs'][] = array($type, $message);
- }
-
- public static function query($sql, $bindings, $time)
- {
- foreach ($bindings as $binding)
- {
- $binding = Database::connection()->pdo->quote($binding);
- $sql = preg_replace('/\?/', $binding, $sql, 1);
- }
- static::$data['queries'][] = array($sql, $time);
- }
-
- public static function attach()
- {
-
-
-
- Event::listen('laravel.log', function($type, $message)
- {
- Profiler::log($type, $message);
- });
- Event::listen('laravel.query', function($sql, $bindings, $time)
- {
- Profiler::query($sql, $bindings, $time);
- });
-
-
-
- Event::listen('laravel.done', function($response)
- {
- echo Profiler::render($response);
- });
- }
- }
|