Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) set memory baseline on MemoryCollector #559

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/DebugBar/DataCollector/MemoryCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class MemoryCollector extends DataCollector implements Renderable
{
protected $realUsage = false;

protected $memoryRealStart = 0;

protected $memoryStart = 0;

protected $peakUsage = 0;

/**
Expand All @@ -41,14 +45,25 @@ public function setRealUsage($realUsage)
$this->realUsage = $realUsage;
}

/**
* Reset memory baseline, to measure multiple requests in a long running process
*
* @return void
*/
public function resetMemoryBaseline()
{
$this->memoryStart = memory_get_usage(false);
$this->memoryRealStart = memory_get_usage(true);
}

/**
* Returns the peak memory usage
*
* @return integer
*/
public function getPeakUsage()
{
return $this->peakUsage;
return $this->peakUsage - ($this->realUsage ? $this->memoryRealStart : $this->memoryStart);
}

/**
Expand All @@ -66,8 +81,8 @@ public function collect()
{
$this->updatePeakUsage();
return array(
'peak_usage' => $this->peakUsage,
'peak_usage_str' => $this->getDataFormatter()->formatBytes($this->peakUsage, 0)
'peak_usage' => $this->getPeakUsage(),
'peak_usage_str' => $this->getDataFormatter()->formatBytes($this->getPeakUsage(), 0)
);
}

Expand Down