No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

105 líneas
2.8KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\debug\panels;
  8. use Yii;
  9. use yii\debug\Panel;
  10. use yii\log\Logger;
  11. use yii\debug\models\search\Profile;
  12. /**
  13. * Debugger panel that collects and displays performance profiling info.
  14. *
  15. * @author Qiang Xue <qiang.xue@gmail.com>
  16. * @since 2.0
  17. */
  18. class ProfilingPanel extends Panel
  19. {
  20. /**
  21. * @var array current request profile timings
  22. */
  23. private $_models;
  24. /**
  25. * @inheritdoc
  26. */
  27. public function getName()
  28. {
  29. return 'Profiling';
  30. }
  31. /**
  32. * @inheritdoc
  33. */
  34. public function getSummary()
  35. {
  36. return Yii::$app->view->render('panels/profile/summary', [
  37. 'memory' => sprintf('%.3f MB', $this->data['memory'] / 1048576),
  38. 'time' => number_format($this->data['time'] * 1000) . ' ms',
  39. 'panel' => $this
  40. ]);
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function getDetail()
  46. {
  47. $searchModel = new Profile();
  48. $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams(), $this->getModels());
  49. return Yii::$app->view->render('panels/profile/detail', [
  50. 'panel' => $this,
  51. 'dataProvider' => $dataProvider,
  52. 'searchModel' => $searchModel,
  53. 'memory' => sprintf('%.3f MB', $this->data['memory'] / 1048576),
  54. 'time' => number_format($this->data['time'] * 1000) . ' ms',
  55. ]);
  56. }
  57. /**
  58. * @inheritdoc
  59. */
  60. public function save()
  61. {
  62. $target = $this->module->logTarget;
  63. $messages = $target->filterMessages($target->messages, Logger::LEVEL_PROFILE);
  64. return [
  65. 'memory' => memory_get_peak_usage(),
  66. 'time' => microtime(true) - YII_BEGIN_TIME,
  67. 'messages' => $messages,
  68. ];
  69. }
  70. /**
  71. * Returns array of profiling models that can be used in a data provider.
  72. * @return array models
  73. */
  74. protected function getModels()
  75. {
  76. if ($this->_models === null) {
  77. $this->_models = [];
  78. $timings = Yii::getLogger()->calculateTimings(isset($this->data['messages']) ? $this->data['messages'] : []);
  79. foreach ($timings as $seq => $profileTiming) {
  80. $this->_models[] = [
  81. 'duration' => $profileTiming['duration'] * 1000, // in milliseconds
  82. 'category' => $profileTiming['category'],
  83. 'info' => $profileTiming['info'],
  84. 'level' => $profileTiming['level'],
  85. 'timestamp' => $profileTiming['timestamp'] * 1000, //in milliseconds
  86. 'seq' => $seq,
  87. ];
  88. }
  89. }
  90. return $this->_models;
  91. }
  92. }