Blame view

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