Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hoaproject/contributions-symfony-benchbundle

The Hoa\Bench Symfony2 bundle.
https://github.com/hoaproject/contributions-symfony-benchbundle

Last synced: 3 months ago
JSON representation

The Hoa\Bench Symfony2 bundle.

Awesome Lists containing this project

README

        

![Hoa](http://static.hoa-project.net/Image/Hoa_small.png)

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](https://travis-ci.org/jubianchi/HoathisBenchBundle.png?branch=master)](https://travis-ci.org/jubianchi/HoathisBenchBundle)

* [Installation](#installation)
* [How to use](#how-to-use)
* [Bench service](#bench-service)
* [Twig helper](#twig-helper)
* [Console helper](#console-helper)

## Installation

Add these lines to your `require-dev` section:

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

Then install dependencies:

```sh
$ composer update hoathis/bench-bundle
```

And add `BenchBundle` to your `AppKernel`:

```php
//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
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:

```php
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](http://hoa-project.net/Literature/Hack/Bench.html#Manipulate_marks).

### Twig helper

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

```html


    {% benchstart 'usersLoop' %}
    {% for user in users %}

  • {{ user.username }}

    {% benchstart 'messagesCount' %}

    {% if user.messages|length %}
    No new mesages
    {% else %}
    {{ user.messages|length }} new message(s)
    {% endif %}

    {% benchpause 'messagesCount' %}



  • {% endfor %}
    {% benchstop 'messagesCount' %}
    {% benchstop 'usersLoop' %}

      ```

      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
      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
      `summarize`method:

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