Browse Source

Add Profiler support report message with turn on PROFILER_DETAILS (if use to show queries). Modified tests MongoStatementTest, MySQLiStatementTest. Adding new tests to ProfilerTest.

master
Alexander Demidov 12 years ago
parent
commit
a16a995ed3
  1. 3
      tests/model/MongoStatementTest.php
  2. 2
      tests/model/MySQLiStatementTest.php
  3. 184
      tests/util/profiler/ProfilerTest.php
  4. 22
      util/profiler/Profiler.php

3
tests/model/MongoStatementTest.php

@ -207,7 +207,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase
public function testFetch()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', true);
Config::set('PROFILER_DETAILS', false);
$this->assertFalse($this->stmt->fetch());
$this->setDriverGetConnectionMethod()->setRequestForFetch();
@ -216,6 +216,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase
$result = $this->stmt->fetch();
$this->assertEquals('prev', $result->next);
$this->assertFalse($this->stmt->fetch());
$this->assertContains('Queries not counted.', Profiler::getInstance()->getCli());
}
/**

2
tests/model/MySQLiStatementTest.php

@ -220,6 +220,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase
*/
public function testClose()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', false);
$this->assertAttributeEquals(null, 'result', $this->stmt);
$this->setDriverGetConnectionMethod();
@ -227,6 +228,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase
$this->assertAttributeNotEquals(null, 'result', $this->stmt);
$this->stmt->close();
$this->assertAttributeEquals(null, 'result', $this->stmt);
$this->assertContains('Queries not counted.', Profiler::getInstance()->getCli());
}
/**

184
tests/util/profiler/ProfilerTest.php

@ -72,6 +72,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase
public function testStartEndNoCommandProfiler()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', true);
$profiler = Profiler::getInstance();
$profiler->start();
$result = $profiler->end('<body></body>');
@ -90,6 +91,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase
public function testStartEndWithCommandProfiler()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', true);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
@ -105,14 +107,146 @@ class ProfilerTest extends PHPUnit_Framework_TestCase
/**
* @runInSeparateProcess
*/
public function testStartEndWithCommandProfilerWithNonDetails()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', false);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$profiler->start();
$result = $profiler->end('<body></body>');
$this->assertNotEquals('<body></body>', $result);
$this->assertContains('Queries not counted.', $result);
}
/**
* @runInSeparateProcess
*/
public function testStartEndWithCommandProfilerWithNonDetailsButExistingQueries()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', false);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$cmdProfiler = $profiler->profilerCommand('command', 'type');
$profiler->start();
$result = $profiler->end('<body></body>');
$this->assertNotEquals('<body></body>', $result);
$this->assertStringEndsNotWith(']<br/></div></body>', $result);
$this->assertContains('Queries: 1', $result);
}
/**
* @runInSeparateProcess
*/
public function testStartEndWithCommandProfilerWithDetailsButNonQueries()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', true);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$profiler->start();
$result = $profiler->end('<body></body>');
$this->assertNotEquals('<body></body>', $result);
$this->assertStringEndsWith(']<br/></div></body>', $result);
$this->assertContains('Queries: 0', $result);
}
/**
* @runInSeparateProcess
*/
public function testGetJSON()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', true);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$cmdProfiler = $profiler->profilerCommand('command', 'type');
$fire_php_mock = $this->getMockBuilder('FirePHP')
->disableOriginalConstructor()
->setMethods(array('__construct', 'fb'))
->getMock();
$fire_php_mock->expects($this->exactly(2))
->method('fb')
->with($this->anything(), $this->logicalAnd($this->logicalXor($this->anything(), $this->stringContains('Queries not'))), $this->logicalXor($this->anything(), $this->stringContains('Queries: 0'))); // Expected "Queries: 1"
$reflection_property = new ReflectionProperty('FirePHP', 'instance');
$reflection_property->setAccessible(true);
$reflection_property->setValue($fire_php_mock);
$this->assertNull($profiler->getJson());
}
/**
* @runInSeparateProcess
*/
public function testGetJSONWithNonDetails()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', false);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$fire_php_mock = $this->getMockBuilder('FirePHP')
->disableOriginalConstructor()
->setMethods(array('__construct', 'fb'))
->getMock();
$fire_php_mock->expects($this->exactly(2))
->method('fb')
->with($this->anything(), $this->logicalAnd($this->logicalXor($this->anything(), $this->stringContains('Queries: 1'))), $this->logicalXor($this->anything(), $this->stringContains('Queries: 0'))); // expected "Queries not counted"
$reflection_property = new ReflectionProperty('FirePHP', 'instance');
$reflection_property->setAccessible(true);
$reflection_property->setValue($fire_php_mock);
$this->assertNull($profiler->getJson());
}
/**
* @runInSeparateProcess
*/
public function testGetJSONWithNonDetailsButExistingQueries()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', false);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$cmdProfiler = $profiler->profilerCommand('command', 'type');
$this->assertNull($profiler->getJson());
$fire_php_mock = $this->getMockBuilder('FirePHP')
->disableOriginalConstructor()
->setMethods(array('__construct', 'fb'))
->getMock();
$fire_php_mock->expects($this->exactly(2))
->method('fb')
->with($this->anything(), $this->logicalAnd($this->logicalXor($this->anything(), $this->stringContains('Queries not counted'))), $this->logicalXor($this->anything(), $this->stringContains('Queries: 0'))); // expected "Queries: 1"
$reflection_property = new ReflectionProperty('FirePHP', 'instance');
$reflection_property->setAccessible(true);
$reflection_property->setValue($fire_php_mock);
$this->assertNull($profiler->getJson());
}
/**
* @runInSeparateProcess
*/
public function testGetJSONWithDetailsButNonQueries()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', true);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$fire_php_mock = $this->getMockBuilder('FirePHP')
->disableOriginalConstructor()
->setMethods(array('__construct', 'fb'))
->getMock();
$fire_php_mock->expects($this->exactly(2))
->method('fb')
->with($this->anything(), $this->logicalAnd($this->logicalXor($this->anything(), $this->stringContains('Queries not counted'))), $this->logicalXor($this->anything(), $this->stringContains('Queries: 1'))); // expected "Queries: 0"
$reflection_property = new ReflectionProperty('FirePHP', 'instance');
$reflection_property->setAccessible(true);
$reflection_property->setValue($fire_php_mock);
$this->assertNull($profiler->getJson());
}
/**
@ -121,12 +255,60 @@ class ProfilerTest extends PHPUnit_Framework_TestCase
public function testGetCLI()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', true);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$cmdProfiler = $profiler->profilerCommand('command', 'type');
$result = $profiler->getCli();
$this->assertNotNull($result);
$this->assertContains('Queries: 1', Profiler::getInstance()->getCli());
}
/**
* @runInSeparateProcess
*/
public function testGetCLIWithNonDetails()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', false);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$result = $profiler->getCli();
$this->assertNotNull($result);
$this->assertContains('Queries not counted', Profiler::getInstance()->getCli());
}
/**
* @runInSeparateProcess
*/
public function testGetCLIWithNonDetailsButExistingQueries()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', false);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$cmdProfiler = $profiler->profilerCommand('command', 'type');
$result = $profiler->getCli();
$this->assertNotNull($result);
$this->assertContains('Queries: 1', Profiler::getInstance()->getCli());
}
/**
* @runInSeparateProcess
*/
public function testGetCLIWithDetailsButNonQueries()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', true);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$result = $profiler->getCli();
$this->assertNotNull($result);
$this->assertContains('Queries: 0', Profiler::getInstance()->getCli());
}
public function tearDown()

22
util/profiler/Profiler.php

@ -80,8 +80,12 @@ class Profiler
$queriesTime += $query->getElapsed();
}
$html = '<div style="clear:both; font:12px monospace; margin: 5px; white-space: pre;">'
. 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.<br/>'
. 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]<br/>';
. 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.<br/>';
if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) {
$html .= 'Queries not counted. Turn PROFILER_DETAILS on true, if you want to profile queries.<br>';
} else {
$html .= 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]<br/>';
}
$html .= $temp;
$html .= '</div>';
return $html;
@ -99,7 +103,11 @@ class Profiler
$table[] = array($query->getType(), round($query->getElapsed() * 1000, 2), $query->getCommand());
$queriesTime += $query->getElapsed();
}
FB::table('Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]', $table);
if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) {
FB::table('Queries not counted. Turn PROFILER_DETAILS on true, if you want to profile queries.', $table);
} else {
FB::table('Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]', $table);
}
}
public function getCli()
@ -112,8 +120,12 @@ class Profiler
$queriesTime += $query->getElapsed();
}
$html = str_pad(PHP_EOL, 60, '-', STR_PAD_LEFT);
$html .= 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.' . PHP_EOL
. 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms] ' . PHP_EOL;
$html .= 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.' . PHP_EOL;
if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) {
$html .= 'Queries not counted. Turn PROFILER_DETAILS on true, if you want to profile queries.' . PHP_EOL;
} else {
$html .= 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms] ' . PHP_EOL;
}
$html .= $temp;
return $html;
}
Loading…
Cancel
Save