Skip to content

Commit

Permalink
Fix recursive time tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
sveneld authored and bfeaver committed Nov 24, 2023
1 parent 03fc5d8 commit d0961f9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,12 @@ public function timing(string $key, float $value, float $sampleRate = 1.0, array
* starts the timing for a key
*
* @param string $key
* @param array $tags
*/
public function startTiming(string $key): void
public function startTiming(string $key, array $tags = []): void
{
$this->timings[$key] = gettimeofday(true);
$timingKey = $key . md5(json_encode($tags));
$this->timings[$timingKey] = gettimeofday(true);
}

/**
Expand All @@ -143,11 +145,12 @@ public function startTiming(string $key): void
public function endTiming(string $key, float $sampleRate = 1.0, array $tags = []): ?float
{
$end = gettimeofday(true);
$timingKey = $key . md5(json_encode($tags));

if (isset($this->timings[$key])) {
$timing = ($end - $this->timings[$key]) * 1000;
if (isset($this->timings[$timingKey])) {
$timing = ($end - $this->timings[$timingKey]) * 1000;
$this->timing($key, $timing, $sampleRate, $tags);
unset($this->timings[$key]);
unset($this->timings[$timingKey]);

return $timing;
}
Expand Down Expand Up @@ -207,7 +210,7 @@ public function memory(string $key, int $memory = null, float $sampleRate = 1.0,
*/
public function time(string $key, Closure $block, float $sampleRate = 1.0, array $tags = [])
{
$this->startTiming($key);
$this->startTiming($key, $tags);
try {
return $block();
} finally {
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,30 @@ public function testSetWithTags()
$message = $this->connection->getLastMessage();
$this->assertEquals('test.barfoo:666|s|#tag:value,tag2:value2', $message);
}

public function testTimeClosureRecursive()
{
$evald = $this->client->time(
'foo',
function () {
return $this->client->time(
'foo',
function () {
return 'foobar';
},
1.0,
['run' => 2]
);
},
1.0,
['run' => 1]
);

$this->assertEquals('foobar', $evald);

$messages = $this->connection->getMessages();
$this->assertEquals(2, count($messages));
$this->assertMatchesRegularExpression('/test\.foo\:[\d\.]*\|ms\|#run:2/', $messages[0]);
$this->assertMatchesRegularExpression('/test\.foo\:[\d\.]*\|ms\|#run:1/', $messages[1]);
}
}
5 changes: 5 additions & 0 deletions tests/unit/ConnectionMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function getLastMessage()
}
}

public function getMessages(): array
{
return $this->messages;
}

public function sendMessages(array $messages): void
{
$this->messages[] = join("\n", $messages);
Expand Down

0 comments on commit d0961f9

Please sign in to comment.