Skip to content

Commit

Permalink
Add debug log which print amount of used memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
czolnowski committed Dec 3, 2014
1 parent c6a737b commit dd2d931
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/ArchiveProcessor/PluginsArchiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Piwik\DataAccess\ArchiveWriter;
use Piwik\DataTable\Manager;
use Piwik\Metrics;
use Piwik\Piwik;
use Piwik\Plugin\Archiver;
use Piwik\Log;

Expand Down Expand Up @@ -101,6 +102,7 @@ public function callAggregateAllPlugins($visits, $visitsConverted)
}

if ($this->shouldProcessReportsForPlugin($pluginName)) {
$memoryUsageBeforePluginArchiving = memory_get_usage(true);
if ($this->isSingleSiteDayArchive) {
Log::debug("PluginsArchiver::%s: Archiving day reports for plugin '%s'.", __FUNCTION__, $pluginName);

Expand All @@ -110,6 +112,14 @@ public function callAggregateAllPlugins($visits, $visitsConverted)

$archiver->aggregateMultipleReports();
}

$memoryUsageInArchiving = memory_get_usage(true) - $memoryUsageBeforePluginArchiving;
Log::debug("PluginsArchiver::%s: Used %s memory while archiving %s reports for plugin '%s'.",
__FUNCTION__,
Piwik::bytesToSize($memoryUsageInArchiving),
$this->isSingleSiteDayArchive ? 'day' : 'period',
$pluginName
);
} else {
Log::debug("PluginsArchiver::%s: Not archiving reports for plugin '%s'.", __FUNCTION__, $pluginName);
}
Expand Down
33 changes: 33 additions & 0 deletions core/Piwik.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,4 +765,37 @@ public static function doAsSuperUser($function)

return $result;
}

/**
* Convert bytes to human readable format
*
* @param int $bytes Size in bytes to convert
* @param int $precision Precision value, default 2.
* @return string
*/
public static function bytesToSize($bytes, $precision = 2)
{
$kilobyte = 1024;
$megabyte = $kilobyte * 1024;
$gigabyte = $megabyte * 1024;
$terabyte = $gigabyte * 1024;

if (($bytes >= 0) && ($bytes < $kilobyte)) {
return $bytes . ' B';

} elseif (($bytes >= $kilobyte) && ($bytes < $megabyte)) {
return round($bytes / $kilobyte, $precision) . ' KB';

} elseif (($bytes >= $megabyte) && ($bytes < $gigabyte)) {
return round($bytes / $megabyte, $precision) . ' MB';

} elseif (($bytes >= $gigabyte) && ($bytes < $terabyte)) {
return round($bytes / $gigabyte, $precision) . ' GB';

} elseif ($bytes >= $terabyte) {
return round($bytes / $terabyte, $precision) . ' TB';
} else {
return $bytes . ' B';
}
}
}

0 comments on commit dd2d931

Please sign in to comment.