Skip to content

Commit

Permalink
(feat) memory measure option
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Sep 15, 2023
1 parent 1bee671 commit 27ac0c4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
23 changes: 23 additions & 0 deletions src/DebugBar/DataCollector/TimeDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class TimeDataCollector extends DataCollector implements Renderable
*/
protected $measures = array();

/**
* @var bool
*/
protected $memoryMeasure = false;

/**
* @param float $requestStartTime
*/
Expand All @@ -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
*
Expand All @@ -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
);
}
Expand Down Expand Up @@ -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'],
Expand All @@ -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,
Expand All @@ -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
);
Expand Down
21 changes: 18 additions & 3 deletions src/DebugBar/Resources/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand All @@ -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 = $('<div />').addClass(csscls('measure')),
li = $('<li />'),
Expand All @@ -460,7 +474,8 @@ if (typeof(PhpDebugBar) == 'undefined') {
left: left + "%",
width: width + "%"
}));
m.append($('<span />').addClass(csscls('label')).text(measure.label + " (" + measure.duration_str + ")"));
m.append($('<span />').addClass(csscls('label'))
.text(measure.label + " (" + measure.duration_str +(measure.memory ? ' - ' + measure.memory_str: '') + ")"));

if (measure.collector) {
$('<span />').addClass(csscls('collector')).text(measure.collector).appendTo(m);
Expand Down Expand Up @@ -506,7 +521,7 @@ if (typeof(PhpDebugBar) == 'undefined') {
aggregateTable.append('<tr><td class="' + csscls('name') + '">' + aggregate.data.count + ' x ' + aggregate.label + ' (' + width + '%)</td><td class="' + csscls('value') + '">' +
'<div class="' + csscls('measure') +'">' +
'<span class="' + csscls('value') + '" style="width:' + width + '%"></span>' +
'<span class="' + csscls('label') + '">' + formatDuration(aggregate.data.duration) + '</span>' +
'<span class="' + csscls('label') + '">' + formatDuration(aggregate.data.duration) + (aggregate.data.memory ? ' - ' + formatBytes(aggregate.data.memory) : '') + '</span>' +
'</div></td></tr>');
});

Expand Down

0 comments on commit 27ac0c4

Please sign in to comment.