Finished up Profiler and test coverage
This commit is contained in:
parent
9e65b2cfc2
commit
173136ddce
2 changed files with 57 additions and 81 deletions
|
@ -122,44 +122,18 @@ class Profiler
|
||||||
*/
|
*/
|
||||||
public static function query($query, $input_parameters = false, $results = false, $duration = false, $explain = false)
|
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 = [
|
$log = [
|
||||||
'query' => $query,
|
'query' => $query,
|
||||||
'parameters' => $input_parameters,
|
'parameters' => $input_parameters,
|
||||||
'results' => $results,
|
'results' => $results,
|
||||||
'execution_time' => $duration,
|
'execution_time' => $duration,
|
||||||
'explain' => $explain,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
|
if ($explain)
|
||||||
|
{
|
||||||
|
$log['explain'] = $explain;
|
||||||
|
}
|
||||||
|
|
||||||
self::log($log, false, 'database');
|
self::log($log, false, 'database');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,60 +2,62 @@
|
||||||
|
|
||||||
class ProfilerTest extends PHPUnit_Framework_TestCase
|
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();
|
file_put_contents('/tmp/pickles.php', '<?php
|
||||||
}
|
$config = [
|
||||||
|
"environments" => [
|
||||||
|
"local" => "127.0.0.1",
|
||||||
|
"production" => "123.456.789.0",
|
||||||
|
],
|
||||||
|
"pickles" => [
|
||||||
|
"profiler" => true,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
');
|
||||||
|
|
||||||
public function testEnabled()
|
new Pickles\Config('/tmp/pickles.php');
|
||||||
{
|
|
||||||
$config = Pickles\Config::getInstance();
|
|
||||||
$config->data['pickles']['profiler'] = true;
|
|
||||||
|
|
||||||
$this->assertTrue(Pickles\Profiler::enabled());
|
Pickles\Profiler::log('i am a string');
|
||||||
}
|
|
||||||
|
|
||||||
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(['foo' => 'bar']);
|
||||||
Pickles\Profiler::log(new Pickles\Object);
|
Pickles\Profiler::log($this, 'testProfiler');
|
||||||
Pickles\Profiler::log('string');
|
Pickles\Profiler::timer('swatch');
|
||||||
Pickles\Profiler::log(3.14, 'method', true);
|
Pickles\Profiler::query('SELECT', ['foo' => 'bar'], ['results'], 1, 'EXPLAIN');
|
||||||
Pickles\Profiler::log('timer', 'timer-one');
|
Pickles\Profiler::timer('swatch');
|
||||||
}
|
Pickles\Profiler::query('SELECT', ['foo' => 'bar'], ['results'], 1);
|
||||||
|
|
||||||
public function testLogQuery()
|
$report = Pickles\Profiler::report();
|
||||||
{
|
|
||||||
$explain = [
|
|
||||||
[
|
|
||||||
'key' => '',
|
|
||||||
'possible_keys' => '',
|
|
||||||
'type' => '',
|
|
||||||
'rows' => '',
|
|
||||||
'Extra' => '',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
Pickles\Profiler::query('SELECT * FROM table;');
|
$this->assertEquals(7, count($report));
|
||||||
Pickles\Profiler::query('SELECT * FROM table WHERE column = ?;', ['foo']);
|
$this->assertEquals(7, count($report['logs']));
|
||||||
Pickles\Profiler::query('SELECT * FROM table;', false, $explain);
|
$this->assertEquals(5, count($report['logs'][0]));
|
||||||
}
|
$this->assertEquals('string', $report['logs'][0]['type']);
|
||||||
|
$this->assertEquals('i am a string', $report['logs'][0]['details']);
|
||||||
public function testTimer()
|
$this->assertEquals('array', $report['logs'][1]['type']);
|
||||||
{
|
$this->assertEquals(['foo' => 'bar'], $report['logs'][1]['details']);
|
||||||
Pickles\Profiler::timer('timer-two');
|
$this->assertEquals('object', $report['logs'][2]['type']);
|
||||||
Pickles\Profiler::timer('timer-two');
|
$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']));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue