性能分析器(Profiler)

Phalcon\Profiler 一个性能分析组件,可以实现多层嵌套:

  1. <?php
  2.  
  3. $profiler = new Phalcon\Profiler();
  4.  
  5. $profiler->startProfile('one');
  6.  
  7. $profile = $profiler->getCurrentProfile(); // 'one'
  8. $profile = $profiler->getLastProfile(); // 'one'
  9.  
  10. $profiler->startProfile('two');
  11.  
  12. $profile = $profiler->getCurrentProfile(); // 'two'
  13. $profile = $profiler->getLastProfile(); // 'two'
  14.  
  15. $profiler->stopProfile('two');
  16.  
  17. $num = count($profiler->getProfiles()); // 1
  18. $profile = $profiler->getCurrentProfile(); // 'one'
  19.  
  20. $profile = $profiler->getLastProfile(); // 'two'
  21.  
  22. $profiler->stopProfile('one');
  23. $num = count($profiler->getProfiles()); // 2

DB 性能分析器(DB Profiler)

Phalcon\Db\Profiler 组件,它被用于分析数据库的操作性能以便诊断性能问题,并发现瓶颈:
  1. <?php
  2.  
  3. use Phalcon\DB\Profiler as DbProfiler;
  4.  
  5. $profiler = new DbProfiler();
  6.  
  7. // 设置性能分析组件
  8. $connection->setProfile($profiler);
  9.  
  10. $sql = "SELECT buyer_name, quantity, product_name "
  11. . "FROM buyers "
  12. . "LEFT JOIN products ON buyers.pid = products.id";
  13.  
  14. // 执行SQL
  15. $connection->query($sql);
  16.  
  17. // 获取最后一个分析结果
  18. $profile = $profiler->getLastProfile();
  19.  
  20. echo "SQL Statement: ", $profile->getSQLStatement(), "\n";
  21. echo "Start Time: ", $profile->getInitialTime(), "\n";
  22. echo "Final Time: ", $profile->getFinalTime(), "\n";
  23. echo "Total Elapsed Time: ", $profile->getTotalElapsedSeconds(), "\n";
  24. echo "Total Usage Memory: ", $profile->getTotalUsageMemory(), "\n";

或者通过自己监听事件来实现性能分析:

  1. <?php
  2.  
  3. use Phalcon\Events\Manager as EventsManager;
  4. use Phalcon\Db\Profiler as DbProfiler;
  5.  
  6. $eventsManager = new EventsManager();
  7.  
  8. $profiler = new DbProfiler();
  9.  
  10. // 监听所有数据库的事件
  11. $eventsManager->attach('db', function ($event, $connection) use ($profiler) {
  12. if ($event->getType() == 'beforeQuery') {
  13. // 操作前启动分析
  14. $profiler->startProfile('db', ['sqlStatement' => $connection->getSQLStatement()]);
  15. }
  16. if ($event->getType() == 'afterQuery') {
  17. // 操作后停止分析
  18. $profiler->stopProfile();
  19. }
  20. });
  21.  
  22. // 设置事件管理器
  23. $connection->setEventsManager($eventsManager);
  24.  
  25. $sql = "SELECT buyer_name, quantity, product_name "
  26. . "FROM buyers "
  27. . "LEFT JOIN products ON buyers.pid = products.id";
  28.  
  29. // 执行SQL
  30. $connection->query($sql);
  31.  
  32. // 获取最后一个分析结果
  33. $profile = $profiler->getLastProfile();
  34.  
  35. echo "SQL Statement: ", $profile->getSQLStatement(), "\n";
  36. echo "Start Time: ", $profile->getInitialTime(), "\n";
  37. echo "Final Time: ", $profile->getFinalTime(), "\n";
  38. echo "Total Elapsed Time: ", $profile->getTotalElapsedSeconds(), "\n";
  39. echo "Total Usage Memory: ", $profile->getTotalUsageMemory(), "\n";

你也可以基于 Phalcon\ProfilerPhalcon\Db\Profiler 建立你自己的分析器类。

原文: http://www.myleftstudio.com/reference/profiler.html