简体   繁体   English

如何正确使用Laravel 4 Profiler

[英]How to use Laravel 4 profiler correctly

I'm trying to improve the performance of some code in my site and found this profiler: https://github.com/loic-sharma/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. 我似乎看不到工具栏中的计时器“ testLogging”。

Have I misunderstood a concept here? 我在这里误解了一个概念吗?

How can I time specific functions in my code and display the results? 如何在代码中计时特定功能并显示结果?

Thanks 谢谢

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. 要在Laravel 4中正确使用profiler(loic-sharma / profiler),例如,它不需要您创建对象的实例。

$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: Laravel具有称为Facades( http://laravel.com/docs/facades )的这些漂亮的东西,探查器实现了它们,因此您可以像这样调用探查器和日志:

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. 这不需要您回显$ profiler,所有输出将自动显示在浏览器的探查器栏中。

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. 请注意, ::现在在Profiler之后通常意味着您正在使用外观,重要的是要了解外观和$ profiler是完全不同的实体。

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. 首先,您必须使用composer安装该软件包,确保在“ require”中的composer.json中添加了composer update之后,您已经运行了composer update。-您已经完成了此操作。
  2. Next add 'Profiler\\ProfilerServiceProvider', to the list of service providers in app/config/app.php 接下来,将'Profiler\\ProfilerServiceProvider',添加到app / config / app.php中的服务提供商列表中
  3. Next add 'Profiler' => 'Profiler\\Facades\\Profiler', to the list of class aliases in app/config/app.php 接下来,将'Profiler' => 'Profiler\\Facades\\Profiler',到app / config / app.php中的类别名列表中
  4. Then in the console run php artisan config:publish loic-sharma/profiler 然后在控制台中运行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; 为了阐明您做错了什么,您使用new Profiler\\Logger\\Logger;创建了新的new Profiler\\Logger\\Logger;实例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; 如果您已经设置好外观,则探查器栏将已经显示(回显)到浏览器,因此当您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. 您现在在浏览器中有两个探查器,导致打开关闭问题,并且当您不echo $profiler ,该条仍然显示,因为它不是您创建的,因此无法正确显示输出。

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 从app / config / app.php中的服务提供者列表中删除'Profiler\\ProfilerServiceProvider',
  2. remove 'Profiler' => 'Profiler\\Facades\\Profiler', from the list of class aliases in app/config/app.php 从app / config / app.php中的类别名列表中删除'Profiler' => 'Profiler\\Facades\\Profiler',
  3. Then in the console run php artisan dump-autoload 然后在控制台中运行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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM