Finished up Profiler and test coverage

This commit is contained in:
Josh Sherman 2014-10-02 06:42:16 -04:00
parent 9e65b2cfc2
commit 173136ddce
2 changed files with 57 additions and 81 deletions

View file

@ -122,44 +122,18 @@ class Profiler
*/
public static function query($query, $input_parameters = false, $results = false, $duration = false, $explain = false)
{
$log = [];
/*
if ($input_parameters != 'false' && is_array($input_parameters))
{
foreach ($input_parameters as $key => $value)
{
$log .= $key . ' => ' . $value;
$query = str_replace($key, $key, $query);
}
}
$log = $query . ' ' . $log;
if (is_array($explain))
{
foreach ($explain as $table)
{
$log .= 'Possible Keys => ' . ($table['possible_keys'] == '' ? 'NONE' : $table['possible_keys'])
. 'Key => ' . ($table['key'] == '' ? 'NONE' : $table['key'])
. 'Type => ' . $table['type']
. 'Rows => ' . $table['rows']
. ($table['Extra'] != '' ? 'Extra => ' . $table['Extra'] : '');
}
}
$log .= 'query_time: ' . $duration;
*/
$log = [
'query' => $query,
'parameters' => $input_parameters,
'results' => $results,
'execution_time' => $duration,
'explain' => $explain,
];
if ($explain)
{
$log['explain'] = $explain;
}
self::log($log, false, 'database');
}

View file

@ -2,60 +2,62 @@
class ProfilerTest extends PHPUnit_Framework_TestCase
{
public function testReport()
public function testProfiler()
{
$this->expectOutputRegex('//');
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['SERVER_NAME'] = '127.0.0.1';
Pickles\Profiler::report();
}
public function testEnabled()
{
$config = Pickles\Config::getInstance();
$config->data['pickles']['profiler'] = true;
$this->assertTrue(Pickles\Profiler::enabled());
}
public function testEnabledType()
{
$config = Pickles\Config::getInstance();
$config->data['pickles']['profiler'] = 'timers';
$this->assertTrue(Pickles\Profiler::enabled('timers'));
}
public function testLogAndTimer()
{
Pickles\Profiler::log('timer', 'timer-one');
Pickles\Profiler::log(['foo' => 'bar']);
Pickles\Profiler::log(new Pickles\Object);
Pickles\Profiler::log('string');
Pickles\Profiler::log(3.14, 'method', true);
Pickles\Profiler::log('timer', 'timer-one');
}
public function testLogQuery()
{
$explain = [
[
'key' => '',
'possible_keys' => '',
'type' => '',
'rows' => '',
'Extra' => '',
file_put_contents('/tmp/pickles.php', '<?php
$config = [
"environments" => [
"local" => "127.0.0.1",
"production" => "123.456.789.0",
],
"pickles" => [
"profiler" => true,
],
];
');
Pickles\Profiler::query('SELECT * FROM table;');
Pickles\Profiler::query('SELECT * FROM table WHERE column = ?;', ['foo']);
Pickles\Profiler::query('SELECT * FROM table;', false, $explain);
}
new Pickles\Config('/tmp/pickles.php');
public function testTimer()
{
Pickles\Profiler::timer('timer-two');
Pickles\Profiler::timer('timer-two');
Pickles\Profiler::log('i am a string');
Pickles\Profiler::log(['foo' => 'bar']);
Pickles\Profiler::log($this, 'testProfiler');
Pickles\Profiler::timer('swatch');
Pickles\Profiler::query('SELECT', ['foo' => 'bar'], ['results'], 1, 'EXPLAIN');
Pickles\Profiler::timer('swatch');
Pickles\Profiler::query('SELECT', ['foo' => 'bar'], ['results'], 1);
$report = Pickles\Profiler::report();
$this->assertEquals(7, count($report));
$this->assertEquals(7, count($report['logs']));
$this->assertEquals(5, count($report['logs'][0]));
$this->assertEquals('string', $report['logs'][0]['type']);
$this->assertEquals('i am a string', $report['logs'][0]['details']);
$this->assertEquals('array', $report['logs'][1]['type']);
$this->assertEquals(['foo' => 'bar'], $report['logs'][1]['details']);
$this->assertEquals('object', $report['logs'][2]['type']);
$this->assertEquals(['class' => 'ProfilerTest', 'method' => 'testProfiler()'], $report['logs'][2]['details']);
$this->assertEquals('timer', $report['logs'][3]['type']);
$this->assertEquals('swatch', $report['logs'][3]['details']['name']);
$this->assertEquals('start', $report['logs'][3]['details']['action']);
$this->assertEquals('database', $report['logs'][4]['type']);
$this->assertEquals('SELECT', $report['logs'][4]['details']['query']);
$this->assertEquals(['foo' => 'bar'], $report['logs'][4]['details']['parameters']);
$this->assertEquals(['results'], $report['logs'][4]['details']['results']);
$this->assertEquals(1, $report['logs'][4]['details']['execution_time']);
$this->assertEquals('EXPLAIN', $report['logs'][4]['details']['explain']);
$this->assertEquals('timer', $report['logs'][5]['type']);
$this->assertEquals('swatch', $report['logs'][5]['details']['name']);
$this->assertEquals('stop', $report['logs'][5]['details']['action']);
$this->assertEquals('database', $report['logs'][6]['type']);
$this->assertEquals('SELECT', $report['logs'][6]['details']['query']);
$this->assertEquals(['foo' => 'bar'], $report['logs'][6]['details']['parameters']);
$this->assertEquals(['results'], $report['logs'][6]['details']['results']);
$this->assertEquals(1, $report['logs'][6]['details']['execution_time']);
$this->assertFalse(isset($report['logs'][6]['details']['explain']));
}
}