Skip to content

Commit 6dfdb97

Browse files
committed
feature symfony#10198 [Stopwatch] Allow getting duration of events without calling stop() (jochenvdv)
This PR was merged into the 2.5-dev branch. Discussion ---------- [Stopwatch] Allow getting duration of events without calling stop() | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | [symfony#10175](symfony#10175) | License | MIT | Doc PR | [symfony#3539](symfony/symfony-docs#3539) Commits ------- 2efe461 Allow retrieving unstopped stopwatch events d3d097d Include running periods in duration
2 parents bcb5239 + 2efe461 commit 6dfdb97

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

src/Symfony/Component/Stopwatch/Stopwatch.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ public function lap($name)
125125
return end($this->activeSections)->stopEvent($name)->start();
126126
}
127127

128+
/**
129+
* Returns a specific event by name
130+
*
131+
* @param string $name The event name
132+
*
133+
* @return StopwatchEvent A StopwatchEvent instance
134+
*/
135+
public function getEvent($name)
136+
{
137+
return end($this->activeSections)->getEvent($name);
138+
}
139+
128140
/**
129141
* Gets all events for a given section.
130142
*
@@ -293,6 +305,24 @@ public function lap($name)
293305
return $this->stopEvent($name)->start();
294306
}
295307

308+
/**
309+
* Returns a specific event by name
310+
*
311+
* @param string $name The event name
312+
*
313+
* @return StopwatchEvent The event
314+
*
315+
* @throws \LogicException When the event is not known
316+
*/
317+
public function getEvent($name)
318+
{
319+
if (!isset($this->events[$name])) {
320+
throw new \LogicException(sprintf('Event "%s" is not known.', $name));
321+
}
322+
323+
return $this->events[$name];
324+
}
325+
296326
/**
297327
* Returns the events from this section.
298328
*

src/Symfony/Component/Stopwatch/StopwatchEvent.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,17 @@ public function getEndTime()
171171
*/
172172
public function getDuration()
173173
{
174+
$periods = $this->periods;
175+
$stopped = count($periods);
176+
$left = count($this->started) - $stopped;
177+
178+
for ($i = 0; $i < $left; $i++) {
179+
$index = $stopped + $i;
180+
$periods[] = new StopwatchPeriod($this->started[$index], $this->getNow());
181+
}
182+
174183
$total = 0;
175-
foreach ($this->periods as $period) {
184+
foreach ($periods as $period) {
176185
$total += $period->getDuration();
177186
}
178187

src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ public function testDuration()
8282
$this->assertEquals(200, $event->getDuration(), null, self::DELTA);
8383
}
8484

85+
public function testDurationBeforeStop()
86+
{
87+
$event = new StopwatchEvent(microtime(true) * 1000);
88+
$event->start();
89+
usleep(200000);
90+
$this->assertEquals(200, $event->getDuration(), null, self::DELTA);
91+
92+
$event = new StopwatchEvent(microtime(true) * 1000);
93+
$event->start();
94+
usleep(100000);
95+
$event->stop();
96+
$event->start();
97+
usleep(100000);
98+
$this->assertEquals(100, $event->getDuration(), null, self::DELTA);
99+
}
100+
85101
/**
86102
* @expectedException \LogicException
87103
*/

src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function testStart()
2929

3030
$this->assertInstanceof('Symfony\Component\Stopwatch\StopwatchEvent', $event);
3131
$this->assertEquals('cat', $event->getCategory());
32+
$this->assertSame($event, $stopwatch->getEvent('foo'));
3233
}
3334

3435
public function testIsStarted()
@@ -92,6 +93,15 @@ public function testLap()
9293
$this->assertEquals(200, $event->getDuration(), null, self::DELTA);
9394
}
9495

96+
/**
97+
* @expectedException \LogicException
98+
*/
99+
public function testUnknownEvent()
100+
{
101+
$stopwatch = new Stopwatch();
102+
$stopwatch->getEvent('foo');
103+
}
104+
95105
/**
96106
* @expectedException \LogicException
97107
*/

0 commit comments

Comments
 (0)