Skip to content

Commit

Permalink
Add name property to the stopwatchEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed Raafat committed Aug 11, 2020
1 parent 12de0e8 commit 0d427e5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Section.php
Expand Up @@ -113,7 +113,7 @@ public function setId(string $id)
public function startEvent(string $name, ?string $category)
{
if (!isset($this->events[$name])) {
$this->events[$name] = new StopwatchEvent($this->origin ?: microtime(true) * 1000, $category, $this->morePrecision);
$this->events[$name] = new StopwatchEvent($this->origin ?: microtime(true) * 1000, $category, $this->morePrecision, $name);
}

return $this->events[$name]->start();
Expand Down
19 changes: 17 additions & 2 deletions StopwatchEvent.php
Expand Up @@ -43,18 +43,25 @@ class StopwatchEvent
*/
private $started = [];

/**
* @var string
*/
private $name;

/**
* @param float $origin The origin time in milliseconds
* @param string|null $category The event category or null to use the default
* @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision
* @param string|null $name The event name or null to define the name as default
*
* @throws \InvalidArgumentException When the raw time is not valid
*/
public function __construct(float $origin, string $category = null, bool $morePrecision = false)
public function __construct(float $origin, string $category = null, bool $morePrecision = false, string $name = null)
{
$this->origin = $this->formatTime($origin);
$this->category = \is_string($category) ? $category : 'default';
$this->morePrecision = $morePrecision;
$this->name = $name ?? 'default';
}

/**
Expand Down Expand Up @@ -236,8 +243,16 @@ private function formatTime(float $time): float
return round($time, 1);
}

/**
* Gets the event name.
*/
public function getName(): string
{
return $this->name;
}

public function __toString(): string
{
return sprintf('%s: %.2F MiB - %d ms', $this->getCategory(), $this->getMemory() / 1024 / 1024, $this->getDuration());
return sprintf('%s/%s: %.2F MiB - %d ms', $this->getCategory(), $this->getName(), $this->getMemory() / 1024 / 1024, $this->getDuration());
}
}
16 changes: 14 additions & 2 deletions Tests/StopwatchEventTest.php
Expand Up @@ -193,12 +193,24 @@ public function testStartTimeWhenStartedLater()
public function testHumanRepresentation()
{
$event = new StopwatchEvent(microtime(true) * 1000);
$this->assertEquals('default: 0.00 MiB - 0 ms', (string) $event);
$this->assertEquals('default/default: 0.00 MiB - 0 ms', (string) $event);
$event->start();
$event->stop();
$this->assertEquals(1, preg_match('/default: [0-9\.]+ MiB - [0-9]+ ms/', (string) $event));

$event = new StopwatchEvent(microtime(true) * 1000, 'foo');
$this->assertEquals('foo: 0.00 MiB - 0 ms', (string) $event);
$this->assertEquals('foo/default: 0.00 MiB - 0 ms', (string) $event);

$event = new StopwatchEvent(microtime(true) * 1000, 'foo', false, 'name');
$this->assertEquals('foo/name: 0.00 MiB - 0 ms', (string) $event);
}

public function testGetName()
{
$event = new StopwatchEvent(microtime(true) * 1000);
$this->assertEquals('default', $event->getName());

$event = new StopwatchEvent(microtime(true) * 1000, 'cat', false, 'name');
$this->assertEquals('name', $event->getName());
}
}

0 comments on commit 0d427e5

Please sign in to comment.