diff --git a/src/DebugBar/DataCollector/TimeDataCollector.php b/src/DebugBar/DataCollector/TimeDataCollector.php index 5794ccd7..16a1fac0 100644 --- a/src/DebugBar/DataCollector/TimeDataCollector.php +++ b/src/DebugBar/DataCollector/TimeDataCollector.php @@ -38,6 +38,11 @@ class TimeDataCollector extends DataCollector implements Renderable */ protected $measures = array(); + /** + * @var bool + */ + protected $memoryMeasure = false; + /** * @param float $requestStartTime */ @@ -53,6 +58,14 @@ public function __construct($requestStartTime = null) $this->requestStartTime = (float)$requestStartTime; } + /** + * Starts memory measuring + */ + public function showMemoryUsage() + { + $this->memoryMeasure = true; + } + /** * Starts a measure * @@ -66,6 +79,7 @@ public function startMeasure($name, $label = null, $collector = null) $this->startedMeasures[$name] = array( 'label' => $label ?: $name, 'start' => $start, + 'memory' => $this->memoryMeasure ? memory_get_usage(false) : null, 'collector' => $collector ); } @@ -94,6 +108,9 @@ public function stopMeasure($name, $params = array()) if (!$this->hasStartedMeasure($name)) { throw new DebugBarException("Failed stopping measure '$name' because it hasn't been started"); } + if (! is_null($this->startedMeasures[$name]['memory'])) { + $params['memoryUsage'] = memory_get_usage(false) - $this->startedMeasures[$name]['memory']; + } $this->addMeasure( $this->startedMeasures[$name]['label'], $this->startedMeasures[$name]['start'], @@ -115,6 +132,10 @@ public function stopMeasure($name, $params = array()) */ public function addMeasure($label, $start, $end, $params = array(), $collector = null) { + if (isset($params['memoryUsage'])) { + $memory = $params['memoryUsage']; + unset($params['memoryUsage']); + } $this->measures[] = array( 'label' => $label, 'start' => $start, @@ -123,6 +144,8 @@ public function addMeasure($label, $start, $end, $params = array(), $collector = 'relative_end' => $end - $this->requestEndTime, 'duration' => $end - $start, 'duration_str' => $this->getDataFormatter()->formatDuration($end - $start), + 'memory' => $memory ?? 0, + 'memory_str' => $this->getDataFormatter()->formatBytes($memory ?? 0), 'params' => $params, 'collector' => $collector ); diff --git a/src/DebugBar/Resources/widgets.js b/src/DebugBar/Resources/widgets.js index 56187190..c30f086b 100644 --- a/src/DebugBar/Resources/widgets.js +++ b/src/DebugBar/Resources/widgets.js @@ -438,6 +438,19 @@ if (typeof(PhpDebugBar) == 'undefined') { return (seconds).toFixed(2) + 's'; }; + // ported from php DataFormatter + var formatBytes = function formatBytes(size) { + if (size === 0 || size === null) { + return '0B'; + } + + var sign = size < 0 ? '-' : '', + size = Math.abs(size), + base = Math.log(size) / Math.log(1024), + suffixes = ['B', 'KB', 'MB', 'GB', 'TB']; + return sign + (Math.round(Math.pow(1024, base - Math.floor(base)) * 100) / 100) + suffixes[Math.floor(base)]; + } + this.$el.empty(); if (data.measures) { var aggregate = {}; @@ -446,10 +459,11 @@ if (typeof(PhpDebugBar) == 'undefined') { var measure = data.measures[i]; if(!aggregate[measure.label]) - aggregate[measure.label] = { count: 0, duration: 0 }; + aggregate[measure.label] = { count: 0, duration: 0, memory : 0 }; aggregate[measure.label]['count'] += 1; aggregate[measure.label]['duration'] += measure.duration; + aggregate[measure.label]['memory'] += (measure.memory || 0); var m = $('
').addClass(csscls('measure')), li = $('
  • '), @@ -460,7 +474,8 @@ if (typeof(PhpDebugBar) == 'undefined') { left: left + "%", width: width + "%" })); - m.append($('').addClass(csscls('label')).text(measure.label + " (" + measure.duration_str + ")")); + m.append($('').addClass(csscls('label')) + .text(measure.label + " (" + measure.duration_str +(measure.memory ? ' - ' + measure.memory_str: '') + ")")); if (measure.collector) { $('').addClass(csscls('collector')).text(measure.collector).appendTo(m); @@ -506,7 +521,7 @@ if (typeof(PhpDebugBar) == 'undefined') { aggregateTable.append('' + aggregate.data.count + ' x ' + aggregate.label + ' (' + width + '%)' + '
    ' + '' + - '' + formatDuration(aggregate.data.duration) + '' + + '' + formatDuration(aggregate.data.duration) + (aggregate.data.memory ? ' - ' + formatBytes(aggregate.data.memory) : '') + '' + '
    '); });