Question

I'm trying to improve the performance of some code in my site and found this profiler: https://github.com/loic-sharma/profiler

I've followed the guidance on the site and included the following in one of the sites controllers:

 public function getTest() {

    $logger = new Profiler\Logger\Logger;
    $profiler = new Profiler\Profiler($logger);
    $profiler->startTimer('testLogging');




    $data = Article::select(array(
        'articles.id',
        'articles.article_date',
        'articles.image_link',
        'articles.headline',
        'articles.category'
    ))  ->get()
        ->toArray();
    var_dump($data);

    $profiler->endTimer('testLogging');
    Log::info('Hello World!');
    echo $profiler;

In the browser I get the expected results and can see the profiler bar at the bottom.

I have one problem: in this basic test the profiler bar, when clicked doesn't stay open so I'm unable to view the logs etc. I'm not sure why or how to go about fixing. THe pane opens then closes again immediately.

If I remove the final echo it works correctly.

I can't though seem to see the timer 'testLogging' in the toolbar.

Have I misunderstood a concept here?

How can I time specific functions in my code and display the results?

Thanks

Was it helpful?

Solution

To use the profiler(loic-sharma/profiler) correctly in Laravel 4 it does not require you the create an instance of the objects for example.

$logger = new Profiler\Logger\Logger;
$profiler = new Profiler\Profiler($logger);

Laravel has these beautiful things called Facades (http://laravel.com/docs/facades), the profiler implements them so you can call the Profiler and Log like this:

public function getTest() {

    Profiler::startTimer('testLogging');

    $data = Article::select(array(
        'articles.id',
        'articles.article_date',
        'articles.image_link',
        'articles.headline',
        'articles.category'
    ))  ->get()
        ->toArray();
    var_dump($data);

    Profiler::endTimer('testLogging');
    Log::info('Hello World!');
}

This does not require you to echo the $profiler, all output will be displayed in the profiler bar in the browser automatically.

Notice the :: now after Profiler this usually means you are using the facade, it is important you understand that the facade and the $profiler are completely different entities.

If you have not yet installed the facades and or service provider do the following:

  1. First you have to install the package with composer, make sure you have run composer update after adding it in your composer.json in "require".- you have already done this.
  2. Next add 'Profiler\ProfilerServiceProvider', to the list of service providers in app/config/app.php
  3. Next add 'Profiler' => 'Profiler\Facades\Profiler', to the list of class aliases in app/config/app.php
  4. Then in the console run php artisan config:publish loic-sharma/profiler

After you have complete that the amended code above should work perfectly.

Just to clarify what you did wrong, you created a new Instance of the profiler with new Profiler\Logger\Logger; if you already had the facades set up the profiler bar would be displayed (echoed) to the browser already so when you echo $profiler; you now have two profilers in your browser causing the open close issue, and when you don't echo $profiler the bar is still displayed because it not the one you created thus not showing your output correctly.

If you still want to use your own instance of the profiler:

  1. remove 'Profiler\ProfilerServiceProvider', from the list of service providers in app/config/app.php
  2. remove 'Profiler' => 'Profiler\Facades\Profiler', from the list of class aliases in app/config/app.php
  3. Then in the console run php artisan dump-autoload

Then this will work:

public function getTest() {

    $logger = new Profiler\Logger\Logger;
    $profiler = new Profiler\Profiler($logger);
    $profiler->startTimer('testLogging');

    $data = Article::select(array(
        'articles.id',
        'articles.article_date',
        'articles.image_link',
        'articles.headline',
        'articles.category'
    ))  ->get()
        ->toArray();
    $logger->debug(var_dump($data));

    $profiler->endTimer('testLogging');
    $logger->info('Hello World!');
    echo $profiler;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top