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