API
5 years ago
Access
5 years ago
Application
5 years ago
Archive
5 years ago
ArchiveProcessor
5 years ago
Archiver
5 years ago
AssetManager
5 years ago
Auth
5 years ago
Category
5 years ago
CliMulti
5 years ago
Columns
5 years ago
Composer
5 years ago
Concurrency
5 years ago
Config
5 years ago
Container
5 years ago
CronArchive
5 years ago
DataAccess
5 years ago
DataFiles
5 years ago
DataTable
5 years ago
Db
5 years ago
DeviceDetector
5 years ago
Email
5 years ago
Exception
5 years ago
Http
5 years ago
Intl
5 years ago
Mail
5 years ago
Measurable
5 years ago
Menu
5 years ago
Metrics
5 years ago
Notification
5 years ago
Period
5 years ago
Plugin
5 years ago
ProfessionalServices
5 years ago
Report
5 years ago
ReportRenderer
5 years ago
Scheduler
5 years ago
Segment
5 years ago
Session
5 years ago
Settings
5 years ago
Tracker
5 years ago
Translation
5 years ago
UpdateCheck
5 years ago
Updater
5 years ago
Updates
5 years ago
Validators
5 years ago
View
5 years ago
ViewDataTable
5 years ago
Visualization
5 years ago
Widget
5 years ago
.htaccess
6 years ago
Access.php
5 years ago
Archive.php
5 years ago
ArchiveProcessor.php
5 years ago
AssetManager.php
5 years ago
Auth.php
5 years ago
AuthResult.php
5 years ago
BaseFactory.php
5 years ago
Cache.php
5 years ago
CacheId.php
5 years ago
CliMulti.php
5 years ago
Common.php
5 years ago
Config.php
5 years ago
Console.php
5 years ago
Context.php
5 years ago
Cookie.php
5 years ago
CronArchive.php
5 years ago
DataArray.php
5 years ago
DataTable.php
5 years ago
Date.php
5 years ago
Db.php
5 years ago
DbHelper.php
5 years ago
Development.php
5 years ago
ErrorHandler.php
5 years ago
EventDispatcher.php
5 years ago
ExceptionHandler.php
5 years ago
FileIntegrity.php
5 years ago
Filechecks.php
5 years ago
Filesystem.php
5 years ago
FrontController.php
5 years ago
Http.php
5 years ago
IP.php
5 years ago
Log.php
5 years ago
LogDeleter.php
5 years ago
Mail.php
5 years ago
Metrics.php
5 years ago
NoAccessException.php
5 years ago
Nonce.php
5 years ago
Notification.php
5 years ago
NumberFormatter.php
5 years ago
Option.php
5 years ago
Period.php
5 years ago
Piwik.php
5 years ago
Plugin.php
5 years ago
Profiler.php
5 years ago
ProxyHeaders.php
5 years ago
ProxyHttp.php
5 years ago
QuickForm2.php
5 years ago
RankingQuery.php
5 years ago
ReportRenderer.php
5 years ago
Segment.php
5 years ago
Sequence.php
5 years ago
Session.php
5 years ago
SettingsPiwik.php
5 years ago
SettingsServer.php
5 years ago
Singleton.php
5 years ago
Site.php
5 years ago
TCPDF.php
5 years ago
Theme.php
5 years ago
Timer.php
5 years ago
Tracker.php
5 years ago
Twig.php
5 years ago
Unzip.php
5 years ago
UpdateCheck.php
5 years ago
Updater.php
5 years ago
UpdaterErrorException.php
5 years ago
Updates.php
5 years ago
Url.php
5 years ago
UrlHelper.php
5 years ago
Version.php
5 years ago
View.php
5 years ago
bootstrap.php
5 years ago
dispatch.php
5 years ago
testMinimumPhpVersion.php
5 years ago
Timer.php
132 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik; |
| 10 | |
| 11 | use Piwik\Metrics\Formatter; |
| 12 | |
| 13 | /** |
| 14 | * |
| 15 | */ |
| 16 | class Timer |
| 17 | { |
| 18 | private $timerStart; |
| 19 | private $memoryStart; |
| 20 | private $formatter; |
| 21 | private $timerEnd; |
| 22 | |
| 23 | /** |
| 24 | * @return \Piwik\Timer |
| 25 | */ |
| 26 | public function __construct() |
| 27 | { |
| 28 | $this->formatter = new Formatter(); |
| 29 | |
| 30 | $this->init(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @return void |
| 35 | */ |
| 36 | public function init() |
| 37 | { |
| 38 | $this->timerStart = $this->getMicrotime(); |
| 39 | $this->memoryStart = $this->getMemoryUsage(); |
| 40 | } |
| 41 | |
| 42 | public function finish() |
| 43 | { |
| 44 | $this->timerEnd = $this->getMicrotime(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param int $decimals |
| 49 | * @return string |
| 50 | */ |
| 51 | public function getTime($decimals = 3) |
| 52 | { |
| 53 | return number_format($this->getTimerEnd() - $this->timerStart, $decimals, '.', ''); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param int $decimals |
| 58 | * @return string |
| 59 | */ |
| 60 | public function getTimeMs($decimals = 3) |
| 61 | { |
| 62 | return number_format(1000 * ($this->getTimerEnd() - $this->timerStart), $decimals, '.', ''); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @return string |
| 67 | */ |
| 68 | public function getMemoryLeak() |
| 69 | { |
| 70 | return "Memory delta: " . $this->getMemoryLeakValue(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @return string |
| 75 | */ |
| 76 | public function getMemoryLeakValue() |
| 77 | { |
| 78 | return $this->formatter->getPrettySizeFromBytes($this->getMemoryUsage() - $this->memoryStart); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @return string |
| 83 | */ |
| 84 | public function getPeakMemoryValue() |
| 85 | { |
| 86 | return $this->formatter->getPrettySizeFromBytes($this->getPeakMemoryUsage()); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @return string |
| 91 | */ |
| 92 | public function __toString() |
| 93 | { |
| 94 | return "Time elapsed: " . $this->getTime() . "s"; |
| 95 | } |
| 96 | |
| 97 | private function getTimerEnd() |
| 98 | { |
| 99 | return $this->timerEnd ?: $this->getMicrotime(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @return float |
| 104 | */ |
| 105 | private function getMicrotime() |
| 106 | { |
| 107 | list($micro_seconds, $seconds) = explode(" ", microtime()); |
| 108 | return ((float)$micro_seconds + (float)$seconds); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns current memory usage, if available |
| 113 | * |
| 114 | * @return int |
| 115 | */ |
| 116 | private function getMemoryUsage() |
| 117 | { |
| 118 | if (function_exists('memory_get_usage')) { |
| 119 | return memory_get_usage(); |
| 120 | } |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | public function getPeakMemoryUsage() |
| 125 | { |
| 126 | if (function_exists('memory_get_peak_usage')) { |
| 127 | return memory_get_peak_usage(); |
| 128 | } |
| 129 | return 0; |
| 130 | } |
| 131 | } |
| 132 |