Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix recursive time tracking #8

Merged
merged 2 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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