1: <?php
2: namespace Yep\Stopwatch;
3:
4: class Event {
5: protected $name;
6: protected $group;
7:
8: /** @var int|float */
9: protected $start_time = 0;
10:
11: /** @var int|float */
12: protected $stop_time = 0;
13:
14: /** @var int */
15: protected $memory_usage = 0;
16: protected $started_laps = [];
17:
18: /** @var Lap[] */
19: protected $stopped_laps = [];
20:
21: /**
22: * Event constructor
23: *
24: * @param string $name
25: * @param null|string $group
26: */
27: public function __construct($name, $group = null) {
28: $this->name = $name;
29: $this->group = $group;
30: }
31:
32: /**
33: * Returns the event name
34: *
35: * @return string
36: */
37: public function getName() {
38: return $this->name;
39: }
40:
41: /**
42: * Returns the event group
43: *
44: * @return null|string
45: */
46: public function getGroup() {
47: return $this->group;
48: }
49:
50: /**
51: * Starts new lap in the event
52: *
53: * @return Event
54: */
55: public function startLap() {
56: $start_time = microtime(true);
57:
58: if ($this->start_time === 0) {
59: $this->start_time = $start_time;
60: }
61:
62: $this->started_laps[] = $start_time;
63:
64: return $this;
65: }
66:
67: /**
68: * Stops latest lap in the event
69: *
70: * @return Event
71: */
72: public function stopLap() {
73: if (empty($this->started_laps)) {
74: throw new StopwatchLapNotStartedException('You must call startLap() before stopLap()');
75: }
76:
77: $this->memory_usage = memory_get_usage(true);
78: $this->stop_time = microtime(true);
79:
80: $this->stopped_laps[] = new Lap(array_pop($this->started_laps), $this->stop_time, $this->memory_usage);
81:
82: return $this;
83: }
84:
85: /**
86: * Stops started laps
87: */
88: public function stopStartedLaps() {
89: while (!empty($this->started_laps)) {
90: $this->stopLap();
91: }
92: }
93:
94: /**
95: * Returns true if some started lap exists, or false if not
96: *
97: * @return bool
98: */
99: public function hasStartedLaps() {
100: return !empty($this->started_laps);
101: }
102:
103: /**
104: * Returns the start time of the first lap
105: *
106: * @return int|float Time in seconds
107: */
108: public function getStartTime() {
109: return $this->start_time;
110: }
111:
112: /**
113: * Returns the stop time of the last stopped lap
114: *
115: * @return int|float Time in seconds
116: */
117: public function getStopTime() {
118: return $this->stop_time;
119: }
120:
121: /**
122: * Returns the duration of all laps durations
123: *
124: * @return float|int Time in seconds
125: */
126: public function getDuration() {
127: return $this->stop_time - $this->start_time;
128: }
129:
130: /**
131: * Returns the memory usage
132: *
133: * @return int Memory in bytes
134: */
135: public function getMemoryUsage() {
136: return $this->memory_usage;
137: }
138:
139: /**
140: * Returns the stopped laps
141: *
142: * @return Lap[]
143: */
144: public function getStoppedLaps() {
145: return $this->stopped_laps;
146: }
147:
148: /**
149: * Returns the lowest duration of all laps
150: *
151: * @return float|int Time in seconds
152: */
153: public function getMinDuration() {
154: if (empty($this->stopped_laps)) {
155: return 0;
156: }
157:
158: $duration = 0;
159:
160: foreach ($this->stopped_laps as $lap) {
161: $lap_duration = $lap->getDuration();
162:
163: if ($duration === 0 || $lap_duration < $duration) {
164: $duration = $lap_duration;
165: }
166: }
167:
168: return $duration;
169: }
170:
171: /**
172: * Returns the highest duration of all laps
173: *
174: * @return float|int Time in seconds
175: */
176: public function getMaxDuration() {
177: if (empty($this->stopped_laps)) {
178: return 0;
179: }
180:
181: $duration = 0;
182:
183: foreach ($this->stopped_laps as $lap) {
184: if (($lap_duration = $lap->getDuration()) > $duration) {
185: $duration = $lap_duration;
186: }
187: }
188:
189: return $duration;
190: }
191:
192: /**
193: * Returns the average duration of all laps
194: *
195: * @return float|int Time in seconds
196: */
197: public function getAverageDuration() {
198: if (empty($this->stopped_laps)) {
199: return 0;
200: }
201:
202: $duration = 0;
203: $count = 0;
204:
205: foreach ($this->stopped_laps as $lap) {
206: $duration += $lap->getDuration();
207: $count++;
208: }
209:
210: return $duration / $count;
211: }
212: }
213: