8000 feature #37676 [Stopwatch] Add name property to the stopwatchEvent (A… · symfony/symfony@e29d230 · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e29d230

Browse files
committed
feature #37676 [Stopwatch] Add name property to the stopwatchEvent (AhmedRaafat14)
This PR was merged into the 5.2-dev branch. Discussion ---------- [Stopwatch] Add name property to the stopwatchEvent | Q | A | ------------- | --- | Branch? | master for features | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #37627 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Commits ------- f2b64ec Add name property to the stopwatchEvent
2 parents e7617da + f2b64ec commit e29d230

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

src/Symfony/Component/Stopwatch/Section.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function setId(string $id)
113113
public function startEvent(string $name, ?string $category)
114114
{
115115
if (!isset($this->events[$name])) {
116-
$this->events[$name] = new StopwatchEvent($this->origin ?: microtime(true) * 1000, $category, $this->morePrecision);
116+
$this->events[$name] = new StopwatchEvent($this->origin ?: microtime(true) * 1000, $category, $this->morePrecision, $name);
117117
}
118118

119119
return $this->events[$name]->start();

src/Symfony/Component/Stopwatch/StopwatchEvent.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,25 @@ class StopwatchEvent
4343
*/
4444
private $started = [];
4545

46+
/**
47+
* @var string
48+
*/
49+
private $name;
50+
4651
/**
4752
* @param float $origin The origin time in milliseconds
4853
* @param string|null $category The event category or null to use the default
4954
* @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision
55+
* @param string|null $name The event name or null to define the name as default
5056
*
5157
* @throws \InvalidArgumentException When the raw time is not valid
5258
*/
53-
public function __construct(float $origin, string $category = null, bool $morePrecision = false)
59+
public function __construct(float $origin, string $category = null, bool $morePrecision = false, string $name = null)
5460
{
5561
$this->origin = $this->formatTime($origin);
5662
$this->category = \is_string($category) ? $category : 'default';
5763
$this->morePrecision = $morePrecision;
64+
$this->name = $name ?? 'default';
5865
}
5966

6067
/**
@@ -236,8 +243,16 @@ private function formatTime(float $time): float
236243
return round($time, 1);
237244
}
238245

246+
/**
247+
* Gets the event name.
248+
*/
249+
public function getName(): string
250+
{
251+
return $this->name;
252+
}
253+
239254
public function __toString(): string
240255
{
241-
return sprintf('%s: %.2F MiB - %d ms', $this->getCategory(), $this->getMemory() / 1024 / 1024, $this->getDuration());
256+
return sprintf('%s/%s: %.2F MiB - %d ms', $this->getCategory(), $this->getName(), $this->getMemory() / 1024 / 1024, $this->getDuration());
242257
}
243258
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,24 @@ public function testStartTimeWhenStartedLater()
193193
public function testHumanRepresentation()
194194
{
195195
$event = new StopwatchEvent(microtime(true) * 1000);
196-
$this->assertEquals('default: 0.00 MiB - 0 ms', (string) $event);
196+
$this->assertEquals('default/default: 0.00 MiB - 0 ms', (string) $event);
197197
$event->start();
198198
$event->stop();
199199
$this->assertEquals(1, preg_match('/default: [0-9\.]+ MiB - [0-9]+ ms/', (string) $event));
200200

201201
$event = new StopwatchEvent(microtime(true) * 1000, 'foo');
202-
$this->assertEquals('foo: 0.00 MiB - 0 ms', (string) $event);
202+
$this->assertEquals('foo/default: 0.00 MiB - 0 ms', (string) $event);
203+
204+
$event = new StopwatchEvent(microtime(true) * 1000, 'foo', false, 'name');
205+
$this->assertEquals('foo/name: 0.00 MiB - 0 ms', (string) $event);
206+
}
207+
208+
public function testGetName()
209+
{
210+
$event = new StopwatchEvent(microtime(true) * 1000);
211+
$this->assertEquals('default', $event->getName());
212+
213+
$event = new StopwatchEvent(microtime(true) * 1000, 'cat', false, 'name');
214+
$this->assertEquals('name', $event->getName());
203215
}
204216
}

0 commit comments

Comments
 (0)
0