diff --git a/composer.json b/composer.json index e33a4d3..85877e5 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ "doctrine/orm": "~2.4", "erusev/parsedown": "~1.5", "phalcon/devtools": "1.3.*@dev", - "elasticsearch/elasticsearch": "~1.3" + "elasticsearch/elasticsearch": "~1.3", + "container-interop/container-interop": "^1.1" }, "suggest": { "phalcon/devtools": "This tools provide you useful scripts, helping to develop applications that use with Phalcon.", diff --git a/composer.lock b/composer.lock index a352478..ad0981c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "2885e296b1c755384b0d5f3c2b1841ef", + "hash": "d94e8ee441efc4045a37a005a3d22f1c", + "content-hash": "10dfa091ae7bf6897a82100048c7d116", "packages": [ { "name": "psr/log", @@ -46,6 +47,33 @@ } ], "packages-dev": [ + { + "name": "container-interop/container-interop", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/container-interop/container-interop.git", + "reference": "fc08354828f8fd3245f77a66b9e23a6bca48297e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/container-interop/container-interop/zipball/fc08354828f8fd3245f77a66b9e23a6bca48297e", + "reference": "fc08354828f8fd3245f77a66b9e23a6bca48297e", + "shasum": "" + }, + "type": "library", + "autoload": { + "psr-4": { + "Interop\\Container\\": "src/Interop/Container/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Promoting the interoperability of container objects (DIC, SL, etc.)", + "time": "2014-12-30 15:22:37" + }, { "name": "doctrine/annotations", "version": "v1.2.6", @@ -1034,7 +1062,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phalcon/phalcon-devtools/zipball/f7e231d866ae2bdb2d175a7907cb229bed52d187", + "url": "https://api.github.com/repos/phalcon/phalcon-devtools/zipball/aabf4b07df813b1d0011dab67b5746abdb560cdc", "reference": "f7e231d866ae2bdb2d175a7907cb229bed52d187", "shasum": "" }, @@ -2081,12 +2109,12 @@ "version": "v2.7.1", "source": { "type": "git", - "url": "https://github.com/symfony/Console.git", + "url": "https://github.com/symfony/console.git", "reference": "564398bc1f33faf92fc2ec86859983d30eb81806" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Console/zipball/564398bc1f33faf92fc2ec86859983d30eb81806", + "url": "https://api.github.com/repos/symfony/console/zipball/564398bc1f33faf92fc2ec86859983d30eb81806", "reference": "564398bc1f33faf92fc2ec86859983d30eb81806", "shasum": "" }, @@ -2138,12 +2166,12 @@ "version": "v2.7.1", "source": { "type": "git", - "url": "https://github.com/symfony/EventDispatcher.git", + "url": "https://github.com/symfony/event-dispatcher.git", "reference": "be3c5ff8d503c46768aeb78ce6333051aa6f26d9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/be3c5ff8d503c46768aeb78ce6333051aa6f26d9", + "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/be3c5ff8d503c46768aeb78ce6333051aa6f26d9", "reference": "be3c5ff8d503c46768aeb78ce6333051aa6f26d9", "shasum": "" }, diff --git a/public/demo.php b/public/demo.php index 8d9c729..aaf48e5 100644 --- a/public/demo.php +++ b/public/demo.php @@ -93,7 +93,7 @@ $profiler->stop($cache); $view = $profiler->start('PDO::exec', ['query' => 'DELETE FROM users WHERE email = "foo@bar.com"'], 'Database'); - usleep($wait(50)); + usleep($wait(100)); $profiler->stop($view); $cache = $profiler->start('Phalcon\Cache\Backend\Apc::exists', ['get' => 'app_data__lorem_ipsum'], 'Cache'); @@ -104,13 +104,12 @@ usleep($wait(20)); $profiler->stop($cache); - $view = $profiler->start('PDO::query', ['query' => 'SELECT userLoginEmail, userID, userLoginPassword FROM users LIMIT 2500;'], 'Database'); - usleep($wait(350)); - $profiler->stop($view); + for ($i=0; $i <= 3; $i++) { + $view = $profiler->start('PDO::query', ['query' => 'SELECT article.*, author.name FROM article JOIN author ON article.author_id = author.id WHERE article.id = ?;'], 'Database'); + usleep($wait(150)); + $profiler->stop($view); + } - $view = $profiler->start('PDO::query', ['query' => 'SELECT lorem, ipsum FROM foobar WHERE id = ? LIMIT 1;'], 'Database'); - usleep($wait(50)); - $profiler->stop($view); $cache = $profiler->start('Phalcon\Cache\Backend\Apc::exists', ['get' => 'app_data__lorem_ipsum'], 'Cache'); usleep($wait(20)); @@ -120,13 +119,12 @@ usleep($wait(20)); $profiler->stop($cache); - $view = $profiler->start('PDO::query', ['query' => 'SELECT lorem, ipsum FROM foobar WHERE id = ? LIMIT 1;'], 'Database'); - usleep($wait(60)); - $profiler->stop($view); + for ($i=0; $i <= 8; $i++) { + $query = $profiler->start('PDO::query', ['query' => 'SELECT lorem, ipsum FROM foobar WHERE id = ? LIMIT 1;'], 'Database'); + usleep($wait(rand(70, 100))); + $profiler->stop($query); + } - $view = $profiler->start('PDO::query', ['query' => 'SELECT lorem, ipsum FROM foobar WHERE id = ? LIMIT 1;'], 'Database'); - usleep($wait(70)); - $profiler->stop($view); $view = $profiler->start('View::render', ['data' => ['user' => ['name' => 'John Doe', 'age' => 26]], 'foobar' => 123], 'View'); usleep($wait(10)); diff --git a/src/Fabfuel/Prophiler/Adapter/Interop/Container/Container.php b/src/Fabfuel/Prophiler/Adapter/Interop/Container/Container.php new file mode 100644 index 0000000..e29e39c --- /dev/null +++ b/src/Fabfuel/Prophiler/Adapter/Interop/Container/Container.php @@ -0,0 +1,72 @@ + + * @created 18.10.15, 12:14 + */ +namespace Fabfuel\Prophiler\Adapter\Interop\Container; + +use Fabfuel\Prophiler\Adapter\AdapterAbstract; +use Fabfuel\Prophiler\ProfilerInterface; +use Interop\Container\ContainerInterface; + +class Container extends AdapterAbstract implements ContainerInterface +{ + /** + * @var ContainerInterface + */ + protected $container; + + /** + * Creates a new {@link \Fabfuel\Prophiler\Adapter\Interop\Container\Container}. + * + * @param ContainerInterface $container + * @param ProfilerInterface $profiler + */ + public function __construct(ContainerInterface $container, ProfilerInterface $profiler) + { + parent::__construct($profiler); + $this->container = $container; + } + + /** + * {@inheritdoc} + */ + public function get($id) + { + $entry = null; + $metadata = [ + 'id' => $id + ]; + + try { + $benchmark = $this->profiler->start('Container::get', $metadata, 'Container-Interop'); + $entry = $this->container->get($id); + } catch (\Exception $e) { + // exception needs to be catched and thrown after stopping the profiler + } + + $this->profiler->stop($benchmark); + + if (isset($e)) { + throw $e; + } + + return $entry; + } + + /** + * {@inheritdoc} + */ + public function has($id) + { + $metadata = [ + 'id' => $id + ]; + $benchmark = $this->profiler->start('Container::has', $metadata, 'Container-Interop'); + + $has = $this->container->has($id); + + $this->profiler->stop($benchmark); + return $has; + } +} diff --git a/src/Fabfuel/Prophiler/View/toolbar.phtml b/src/Fabfuel/Prophiler/View/toolbar.phtml index 204e3f6..727f988 100644 --- a/src/Fabfuel/Prophiler/View/toolbar.phtml +++ b/src/Fabfuel/Prophiler/View/toolbar.phtml @@ -23,13 +23,13 @@ use Fabfuel\Prophiler\Toolbar\Formatter\BenchmarkFormatter;
  • - Timeline + Timeline
  • - + @@ -44,7 +44,7 @@ use Fabfuel\Prophiler\Toolbar\Formatter\BenchmarkFormatter; = 1): ?>
  • - + Logs @@ -57,7 +57,7 @@ use Fabfuel\Prophiler\Toolbar\Formatter\BenchmarkFormatter;
  • - getIcon() ?> getTitle() ?> + getIcon() ?> getTitle() ?>
  • diff --git a/tests/Fabfuel/Prophiler/Adapter/Interop/Container/ContainerTest.php b/tests/Fabfuel/Prophiler/Adapter/Interop/Container/ContainerTest.php new file mode 100644 index 0000000..d97a448 --- /dev/null +++ b/tests/Fabfuel/Prophiler/Adapter/Interop/Container/ContainerTest.php @@ -0,0 +1,82 @@ + + * @created 23.10.15, 10:59 + */ +namespace Fabfuel\Prophiler\Adapter\Interop\Container; + +use \Fabfuel\Prophiler\Benchmark\Benchmark; + +class ContainerTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Interop\Container\ContainerInterface + */ + protected $container; + /** + * @var \Fabfuel\Prophiler\ProfilerInterface + */ + protected $profiler; + /** + * @var \Fabfuel\Prophiler\Adapter\Interop\Container\Container + */ + protected $adapter; + + /** + * @see PHPUnit_Framework_TestCase::setUp() + */ + protected function setUp() + { + parent::setUp(); + + $this->container = $this->getMock('Interop\Container\ContainerInterface'); + $this->profiler = $this->getMock('Fabfuel\Prophiler\ProfilerInterface'); + $this->adapter = new Container($this->container, $this->profiler); + } + + public function testHasCallTriggersProfiler() + { + $this->profiler->expects($this->once()) + ->method('start') + ->will($this->returnValue(new Benchmark('test'))); + $this->profiler->expects($this->once()) + ->method('stop') + ->will($this->returnValue(new Benchmark('test'))); + + $this->adapter->has('sample-id'); + } + + public function testGetCallTriggersProfiler() + { + $this->profiler->expects($this->once()) + ->method('start') + ->will($this->returnValue(new Benchmark('test'))); + $this->profiler->expects($this->once()) + ->method('stop') + ->will($this->returnValue(new Benchmark('test'))); + $this->container->expects($this->once()) + ->method('get') + ->will($this->returnValue(new \stdClass())); + + $instance = $this->adapter->get('sample-id'); + $this->assertInstanceOf('\stdClass', $instance); + } + + /** + * @expectedException \RuntimeException + */ + public function testGetCallThrowsExceptionAndStillTriggersProfiler() + { + $this->profiler->expects($this->once()) + ->method('start') + ->will($this->returnValue(new Benchmark('test'))); + $this->profiler->expects($this->once()) + ->method('stop') + ->will($this->returnValue(new Benchmark('test'))); + $this->container->expects($this->once()) + ->method('get') + ->will($this->throwException(new \RuntimeException())); + + $instance = $this->adapter->get('sample-id'); + } +}