2017 © Pedro Peláez
 

symfony-bundle bench-bundle

The Hoa\Bench Symfony 2 bundle.

image

hoathis/bench-bundle

The Hoa\Bench Symfony 2 bundle.

  • Sunday, September 21, 2014
  • by jubianchi
  • Repository
  • 11 Watchers
  • 2 Stars
  • 83 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 3 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Hoa, (*1)

Hoa is a modular, extensible and structured set of PHP libraries. Moreover, Hoa aims at being a bridge between industrial and research worlds., (*2)

Hoathis\BenchBundle Build Status

Installation

Add these lines to your require-dev section:, (*3)

{
    "require-dev": {
        "hoa/core": "*@dev",
        "hoa/bench": "*@dev",
        "hoathis/bench-bundle": "dev-master"
    }
}

Then install dependencies:, (*4)

$ composer update hoathis/bench-bundle

And add BenchBundle to your AppKernel:, (*5)

//app/AppKernel.php

class AppKernel extends Kernel
{
    …

    public function registerBundles()
    {
        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            …
            $bundles[] = new \Hoathis\Bundle\BenchBundle\BenchBundle();
        }

        return $bundles;
    }
}

How to use

Bench service

BenchBundle will automatically setup a bench service which you can use in your PHP code to benchmark parts of your application. Results will be aggregated and reported in the profile., (*6)

<?php

namespace Hoathis\BenchDemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DemoController extends Controller
{
    public function indexAction()
    {
        $this->container->get('bench')->renderView->start();
        $response = $this->render('HoaDemoBundle:Welcome:index.html.twig');
        $this->container->get('bench')->renderView->stop();

        return $response;
    }
}

In the previous example we created a mark named renderView in measuring the time taken to render the Twig template., (*7)

You can create several marks by simply assigning them a unique name and nest them as you want:, (*8)

public function indexAction()
{
    $this->container->get('bench')->fetchUsers->start();
    $users = …

    foreach($users as $user) {
        $this->container->get('bench')->fetchMessages->start();
        $user->messages = …
        $this->container->get('bench')->fetchMessages->pause();
    }

    $this->container->get('bench')->fetchMessages->stop(true);
    $this->container->get('bench')->fetchUsers->stop();

    $this->container->get('bench')->renderView->start();
    $response = $this->render('HoaDemoBundle:Users:index.html.twig', array('users' => $users));
    $this->container->get('bench')->renderView->stop();

    return $response;
}

As you can see in the previous example you have three methods to control mark state:, (*9)

  • Hoa\Bench\Mark::start(): to start or unpause a mark,
  • Hoa\Bench\Mark::pause($silent = false): to pause a mark,
  • Hoa\Bench\Mark::stop($silent = false): to stop a mark.

You can also get more informations from marks using their native API., (*10)

Twig helper

BenchBundle also adds a Twig helper to use marks inside your templates:, (*11)



< ul> {% benchstart 'usersLoop' %} {% for user in users %} <li> {{ user.username }} {% benchstart 'messagesCount' %} <span> {% if user.messages|length %} No new mesages {% else %} {{ user.messages|length }} new message(s) {% endif %} </span> {% benchpause 'messagesCount' %} <li> {% endfor %} {% benchstop 'messagesCount' %} {% benchstop 'usersLoop' %} < ul>

Results of those marks will also be displayed in the web profiler., (*12)

Console helper

Finally, BenchBundle will configure a bench.helper service which you can use in your console commands to access marks:, (*13)

<?php
namespace Hoa\DemoBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class TestCommand extends ContainerAwareCommand
{
    public function __construct($name = null)
    {
        parent::__construct($name ?: 'hoa:bench:demo');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $bench = $this->getContainer()->get('bench.helper');

        $bench->start('foo');
        …

        $bench->start('bar');
        …
        $bench->stop('bar');

        $bench->stop('foo');

        $bench->summarize($output);
    }
} 

The API is the same as the bench service except that with the helper, you pass marks' names as argument of the start/pause/stop methods., (*14)

The results will be render on the command's output when you call the summarizemethod:, (*15)

$ app/console hoa:bench:demo
# ...
+------+-----------------+-----------------+
| Mark | Time            | Percent         |
+------+-----------------+-----------------+
| foo  | 4.0034830570221 | 100             |
| bar  | 2.001620054245  | 49.996965785434 |
+------+-----------------+-----------------+

The Versions