Skip to content

Instantly share code, notes, and snippets.

@sousk
Created February 17, 2010 08:13
Show Gist options
  • Save sousk/306425 to your computer and use it in GitHub Desktop.
Save sousk/306425 to your computer and use it in GitHub Desktop.
a simple MySQL Profiler wrapper for Propel/PHP. stop while writing. i uploaded this for later.
<?php
/**
* A propel wrapper for mysql profiling tool
* see http://dev.mysql.com/tech-resources/articles/mysql_51_diagnostic_tools.html
*/
class PropelMysqlProfiler
{
protected $con;
public $default_keys = array(
'query_id',
'seq','state','duration','cpu_user','cpu_system',
'context_voluntary','context_involuntary','block_ops_in','block_ops_out',
'messages_sent','messages_received','page_faults_major','page_faults_minor',
'swaps','source_function','source_file','source_line'
);
public function __construct($con=null)
{
if ($con) {
$this->con = $con;
}
}
public function start()
{
$rs = $this->execute("set profiling=1");
return true;
}
public function getProfiles($keys=null)
{
$profs = array();
$rs = $this->execute("select * from information_schema.profiling");
while($rs->next()) {
$profs[] = $rs->getRow();
}
return $profs;
}
private function execute($sql)
{
if (is_null($this->con)) {
$this->con = Propel::getConnection();
}
return $this->con->executeQuery($sql, ResultSet::FETCHMODE_ASSOC);
}
}
/**
* #test PropelMysqlProfiler
* <code>
* #ok($pr = new PropelMysqlProfiler, "got profiler");
* #ok($pr->start(), "started");
* #ok(VmPeer::doCount(new Criteria), "execute sql");
* #ok($profs = $pr->getProfiles(), "got profile sets");
* foreach ($profs as $prof) {
* // #ok($msg = ArrayUtil::liner($prof), $msg);
* var_dump(implode("','", array_keys($prof)));
* }
* </code>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment