ts3phpframework
Loading...
Searching...
No Matches
Timer.php
Go to the documentation of this file.
1<?php
2
4
11class Timer
12{
18 protected bool $running = false;
19
25 protected float $started = 0;
26
32 protected string $name;
33
39 protected array $data = [];
40
46 public function __construct(string $name)
47 {
48 $this->name = $name;
49
50 $this->data["runtime"] = 0;
51 $this->data["realmem"] = 0;
52 $this->data["emalloc"] = 0;
53
54 $this->start();
55 }
56
62 public function start(): void
63 {
64 if ($this->isRunning()) {
65 return;
66 }
67
68 $this->data["realmem_start"] = memory_get_usage(true);
69 $this->data["emalloc_start"] = memory_get_usage();
70
71 $this->started = microtime(true);
72 $this->running = true;
73 }
74
80 public function stop(): void
81 {
82 if (!$this->isRunning()) {
83 return;
84 }
85
86 $this->data["runtime"] += microtime(true) - $this->started;
87 $this->data["realmem"] += memory_get_usage(true) - $this->data["realmem_start"];
88 $this->data["emalloc"] += memory_get_usage() - $this->data["emalloc_start"];
89
90 $this->started = 0;
91 $this->running = false;
92 }
93
99 public function getRuntime(): float
100 {
101 if ($this->isRunning()) {
102 $this->stop();
103 $this->start();
104 }
105
106 return $this->data["runtime"];
107 }
108
115 public function getMemUsage(bool $realmem = false): int
116 {
117 if ($this->isRunning()) {
118 $this->stop();
119 $this->start();
120 }
121
122 return ($realmem !== false) ? $this->data["realmem"] : $this->data["emalloc"];
123 }
124
130 public function isRunning(): bool
131 {
132 return $this->running;
133 }
134}
Helper class providing profiler timers.
Definition Timer.php:12