All pastes #780499 Raw Edit

Mine

public php v1 · immutable
·published 2007-11-18 16:26 UTC
rendered paste body
<?php/* How to use:Profiler::Start('site_table_name');... your code ...Profiler::Mark('Here there stuff');... more code ...Profiler::Mark('Other stuff');... code ends ...*/require_once 'performance/config.inc.php';require_once 'db/database_mysql.class.php';require_once 'Benchmark/Timer.php';/** * The static (singleton) profiler class is used to measure code path timing between two marked points. Use setup.sql to create * the database and an empty template table. Include this file in the very beginning of your site configuration, and do * Profiler::Start('<table>') which will create the per-site table if it does not exist. Use subsequent Profiler::Mark('<label>') * calls to designate the measure points. The profiler will collect the statistics of start, end and all specified points whichwill * be dumped to the database as soon as the execution finishes. * Note: you can disable the profiler by simply not issuing Profiler::Start(); if that isn't done, any other calls to Profiler:: * would be ignored by the class * Note: PEAR Benchmark package must be installed for this to work. */class Profiler{  private static $profiler;  private static $table_name;  private static $db;  private static $session;  private static $location_cache;  public static function Start($table_name)  {    self::$session = time();    self::$db = new Database_MySQL('localhost', 'root', null, 'performance');    self::$db->Query('CREATE TABLE IF NOT EXISTS ' . $table_name . ' LIKE template');    self::$table_name = 'performance.' . $table_name;    self::$profiler = new Benchmark_Timer();    self::$profiler->start();    self::$location_cache = array('Start' => array('file' => $_SERVER['REQUEST_URI'],  'line' => 'null'), 'Stop' =>  array('file' => null,  'line' => 'null'));    register_shutdown_function('profiler_shutdown_callback');  }  public static function Mark($label)  {    if (! self::$profiler) return;    $trace = debug_backtrace();    self::$location_cache[$label] = array('file' => $trace[0]['file'], 'line' => $trace[0]['line']);    self::$profiler->setMarker($label);  }  public static function Done()  {    if (! self::$profiler) return;    self::$profiler->stop();    $profile = self::$profiler->getProfiling();    $db = self::$db;    foreach ($profile as $position)      {        extract($position);        $file = self::$location_cache[$name]['file'];        $line = self::$location_cache[$name]['line'];        foreach(array('time', 'diff', 'total', 'name', 'file') as $varname) $$varname = $db->Quote($$varname);        $sql = 'INSERT INTO ' . self::$table_name . "(session, ip, label, time, diff, total, file, line) VALUES(" .                   self::$session . ',' . $db->Quote($_SERVER[REMOTE_ADDR]) . ", $name, $time, $diff, $total, $file, $line)";        $db->Query($sql);      }  }}function profiler_shutdown_callback(){    Profiler::Done();}