ProfilingPanel.php 2.32 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12
<?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;
13
use yii\debug\models\search\Profile;
Qiang Xue committed
14 15

/**
16 17
 * Debugger panel that collects and displays performance profiling info.
 *
Qiang Xue committed
18 19 20 21 22
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class ProfilingPanel extends Panel
{
23 24 25 26 27
	/**
	 * @var array current request profile timings
	 */
	private $_models;

28 29 30
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
31 32 33 34 35
	public function getName()
	{
		return 'Profiling';
	}

36 37 38
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
39 40
	public function getSummary()
	{
41
		return Yii::$app->view->render('panels/profile/summary', [
42 43 44 45
			'memory' => sprintf('%.1f MB', $this->data['memory'] / 1048576),
			'time' => number_format($this->data['time'] * 1000) . ' ms',
			'panel' => $this
		]);
Qiang Xue committed
46 47
	}

48 49 50
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
51 52
	public function getDetail()
	{
53
		$searchModel = new Profile();
54
		$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams(), $this->getModels());
55

56
		return Yii::$app->view->render('panels/profile/detail', [
57 58 59 60 61 62 63 64
			'panel' => $this,
			'dataProvider' => $dataProvider,
			'searchModel' => $searchModel,
			'memory' => sprintf('%.1f MB', $this->data['memory'] / 1048576),
			'time' => number_format($this->data['time'] * 1000) . ' ms',
		]);
	}

65 66 67
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
68 69 70 71
	public function save()
	{
		$target = $this->module->logTarget;
		$messages = $target->filterMessages($target->messages, Logger::LEVEL_PROFILE);
Alexander Makarov committed
72
		return [
Qiang Xue committed
73 74
			'memory' => memory_get_peak_usage(),
			'time' => microtime(true) - YII_BEGIN_TIME,
Qiang Xue committed
75
			'messages' => $messages,
Alexander Makarov committed
76
		];
Qiang Xue committed
77
	}
78 79

	/**
80
	 * Returns array of profiling models that can be used in a data provider.
81 82 83 84 85 86
	 * @return array models
	 */
	protected function getModels()
	{
		if ($this->_models === null) {
			$this->_models = [];
87
			$timings = Yii::$app->getLog()->calculateTimings($this->data['messages']);
88

Tobias Munk committed
89
			foreach ($timings as $seq => $profileTiming) {
90
				$this->_models[] = 	[
91 92 93 94
					'duration' => $profileTiming['duration'] * 1000, // in milliseconds
					'category' => $profileTiming['category'],
					'info' => $profileTiming['info'],
					'level' => $profileTiming['level'],
Mark committed
95
					'timestamp' => $profileTiming['timestamp'] * 1000, //in milliseconds
96
					'seq' => $seq,
97 98 99 100 101
				];
			}
		}
		return $this->_models;
	}
Qiang Xue committed
102
}