You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jubianchi 47bb40d42e Fix collector template 5 years ago
Console/Helper Inject correct output into Table 5 years ago
DataCollector Improve data collector 5 years ago
DependencyInjection Prefix keys with hoathis 5 years ago
Resources Fix collector template 5 years ago
Tests/Units Fix tests 5 years ago
Twig Clean everything and add tests 5 years ago
.atoum.php Clean everything and add tests 5 years ago
.bootstrap.atoum.php Clean everything and add tests 5 years ago
.gitignore Clean everything and add tests 5 years ago
.travis.yml Clean build script 5 years ago
HoathisBenchBundle.php Rename bundle from BenchBundle to HoathisBenchBundle 5 years ago
README.md Format, fix typos and fix links. 5 years ago
composer.json Fix bundle name 5 years ago

README.md

Hoa

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

Hoathis\BenchBundle Build Status

Installation

Add these lines to your require-dev section:

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

Then install dependencies:

$ composer update hoathis/bench-bundle

And add BenchBundle to your AppKernel:

//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.

<?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.

You can create several marks by simply assigning them a unique name and nest them as you want:

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:

  • 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.

Twig helper

BenchBundle also adds a Twig helper to use marks inside your templates:

<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.

Console helper

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

<?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.

The results will be render on the command’s output when you call the summarizemethod:

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