Skip to content

Commit aafb756

Browse files
authored
(feat) memory measure option (php-debugbar#558)
1 parent 47abaea commit aafb756

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/DebugBar/DataCollector/TimeDataCollector.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class TimeDataCollector extends DataCollector implements Renderable
3838
*/
3939
protected $measures = array();
4040

41+
/**
42+
* @var bool
43+
*/
44+
protected $memoryMeasure = false;
45+
4146
/**
4247
* @param float $requestStartTime
4348
*/
@@ -53,6 +58,14 @@ public function __construct($requestStartTime = null)
5358
$this->requestStartTime = (float)$requestStartTime;
5459
}
5560

61+
/**
62+
* Starts memory measuring
63+
*/
64+
public function showMemoryUsage()
65+
{
66+
$this->memoryMeasure = true;
67+
}
68+
5669
/**
5770
* Starts a measure
5871
*
@@ -66,6 +79,7 @@ public function startMeasure($name, $label = null, $collector = null)
6679
$this->startedMeasures[$name] = array(
6780
'label' => $label ?: $name,
6881
'start' => $start,
82+
'memory' => $this->memoryMeasure ? memory_get_usage(false) : null,
6983
'collector' => $collector
7084
);
7185
}
@@ -94,6 +108,9 @@ public function stopMeasure($name, $params = array())
94108
if (!$this->hasStartedMeasure($name)) {
95109
throw new DebugBarException("Failed stopping measure '$name' because it hasn't been started");
96110
}
111+
if (! is_null($this->startedMeasures[$name]['memory'])) {
112+
$params['memoryUsage'] = memory_get_usage(false) - $this->startedMeasures[$name]['memory'];
113+
}
97114
$this->addMeasure(
98115
$this->startedMeasures[$name]['label'],
99116
$this->startedMeasures[$name]['start'],
@@ -115,6 +132,11 @@ public function stopMeasure($name, $params = array())
115132
*/
116133
public function addMeasure($label, $start, $end, $params = array(), $collector = null)
117134
{
135+
if (isset($params['memoryUsage'])) {
136+
$memory = $this->memoryMeasure ? $params['memoryUsage'] : 0;
137+
unset($params['memoryUsage']);
138+
}
139+
118140
$this->measures[] = array(
119141
'label' => $label,
120142
'start' => $start,
@@ -123,6 +145,8 @@ public function addMeasure($label, $start, $end, $params = array(), $collector =
123145
'relative_end' => $end - $this->requestEndTime,
124146
'duration' => $end - $start,
125147
'duration_str' => $this->getDataFormatter()->formatDuration($end - $start),
148+
'memory' => $memory ?? 0,
149+
'memory_str' => $this->getDataFormatter()->formatBytes($memory ?? 0),
126150
'params' => $params,
127151
'collector' => $collector
128152
);

src/DebugBar/Resources/widgets.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,19 @@ if (typeof(PhpDebugBar) == 'undefined') {
438438
return (seconds).toFixed(2) + 's';
439439
};
440440

441+
// ported from php DataFormatter
442+
var formatBytes = function formatBytes(size) {
443+
if (size === 0 || size === null) {
444+
return '0B';
445+
}
446+
447+
var sign = size < 0 ? '-' : '',
448+
size = Math.abs(size),
449+
base = Math.log(size) / Math.log(1024),
450+
suffixes = ['B', 'KB', 'MB', 'GB', 'TB'];
451+
return sign + (Math.round(Math.pow(1024, base - Math.floor(base)) * 100) / 100) + suffixes[Math.floor(base)];
452+
}
453+
441454
this.$el.empty();
442455
if (data.measures) {
443456
var aggregate = {};
@@ -446,10 +459,11 @@ if (typeof(PhpDebugBar) == 'undefined') {
446459
var measure = data.measures[i];
447460

448461
if(!aggregate[measure.label])
449-
aggregate[measure.label] = { count: 0, duration: 0 };
462+
aggregate[measure.label] = { count: 0, duration: 0, memory : 0 };
450463

451464
aggregate[measure.label]['count'] += 1;
452465
aggregate[measure.label]['duration'] += measure.duration;
466+
aggregate[measure.label]['memory'] += (measure.memory || 0);
453467

454468
var m = $('<div />').addClass(csscls('measure')),
455469
li = $('<li />'),
@@ -460,7 +474,8 @@ if (typeof(PhpDebugBar) == 'undefined') {
460474
left: left + "%",
461475
width: width + "%"
462476
}));
463-
m.append($('<span />').addClass(csscls('label')).text(measure.label + " (" + measure.duration_str + ")"));
477+
m.append($('<span />').addClass(csscls('label'))
478+
.text(measure.label + " (" + measure.duration_str +(measure.memory ? '/' + measure.memory_str: '') + ")"));
464479

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

0 commit comments

Comments
 (0)