From d87e7e207d33c685fa28dce26d27449fd1405b3c Mon Sep 17 00:00:00 2001 From: Volodymyr Panivko Date: Wed, 31 Aug 2022 12:26:12 +0200 Subject: [PATCH 1/2] Fix recursive time tracking --- src/Client.php | 15 +++++++++------ tests/unit/ClientTest.php | 26 ++++++++++++++++++++++++++ tests/unit/ConnectionMock.php | 5 +++++ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/Client.php b/src/Client.php index c77cdcd..4520c25 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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); } /** @@ -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; } @@ -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 { diff --git a/tests/unit/ClientTest.php b/tests/unit/ClientTest.php index 752255f..92bd409 100644 --- a/tests/unit/ClientTest.php +++ b/tests/unit/ClientTest.php @@ -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]); + } } diff --git a/tests/unit/ConnectionMock.php b/tests/unit/ConnectionMock.php index c5b6fd2..7928e7d 100644 --- a/tests/unit/ConnectionMock.php +++ b/tests/unit/ConnectionMock.php @@ -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); From f789d002dff4bce80ca42e291bef10de05b3af15 Mon Sep 17 00:00:00 2001 From: Brian Feaver Date: Thu, 23 Nov 2023 23:59:00 -0800 Subject: [PATCH 2/2] chore: upgrade php runner to v2 --- .github/workflows/php.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 16d161d..6d55afe 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -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