Skip to content

Commit

Permalink
Merge pull request #8 from Slickdeals/fix-timing-rebased
Browse files Browse the repository at this point in the history
Fix recursive time tracking
  • Loading branch information
bfeaver authored Nov 24, 2023
2 parents 03fc5d8 + f789d00 commit 361e84f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
uses: actions/checkout@v1

- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v1 #https://github.com/shivammathur/setup-php
uses: shivammathur/setup-php@v2 #https://github.com/shivammathur/setup-php
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring
Expand Down
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 361e84f

Please sign in to comment.