Skip to content

Commit

Permalink
chore: update tests around sampling rates
Browse files Browse the repository at this point in the history
  • Loading branch information
drwho725 committed Nov 15, 2024
1 parent 5ef33da commit ec03bd2
Showing 1 changed file with 48 additions and 32 deletions.
80 changes: 48 additions & 32 deletions tests/unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,32 +58,42 @@ public function testCountWithFloatValue()
);
}

public function sampleRateData()
{
return [
[0.9, 1, '0.9'],
[0.9, 0.5, '0.5'],
];
}

/**
* @dataProvider sampleRateData
* @group sampling
*/
public function testCountWithSamplingRate()
public function testCountWithSamplingRate(float $globalSampleRate, float $sampleRate, string $expectedSampleRate)
{
$client = new Client($this->connection, 'test', 9 / 10);
$client = new Client($this->connection, 'test', $globalSampleRate);
for ($i = 0; $i < 10; $i++) {
$client->count('foo.baz', 100, 1);
$client->count('foo.baz', 100, $sampleRate);
}
$this->assertEquals(
'test.foo.baz:100|c|@0.9',
"test.foo.baz:100|c|@{$expectedSampleRate}",
$this->connection->getLastMessage()
);
}

/**
* @dataProvider sampleRateData
* @group sampling
*/
public function testCountWithSamplingRateAndTags()
public function testCountWithSamplingRateAndTags(float $globalSampleRate, float $sampleRate, string $expectedSampleRate)
{
$client = new Client($this->connection, 'test', 9 / 10);
$client = new Client($this->connection, 'test', $globalSampleRate);
for ($i = 0; $i < 10; $i++) {
$client->count('foo.baz', 100, 1, ['tag' => 'value']);
$client->count('foo.baz', 100, $sampleRate, ['tag' => 'value']);
}
$this->assertEquals(
'test.foo.baz:100|c|@0.9|#tag:value',
"test.foo.baz:100|c|@{$expectedSampleRate}|#tag:value",
$this->connection->getLastMessage()
);
}
Expand All @@ -98,31 +108,33 @@ public function testIncrement()
}

/**
* @dataProvider sampleRateData
* @group sampling
*/
public function testIncrementWithSamplingRate()
public function testIncrementWithSamplingRate(float $globalSampleRate, float $sampleRate, string $expectedSampleRate)
{
$client = new Client($this->connection, 'test', 0.9);
$client = new Client($this->connection, 'test', $globalSampleRate);
for ($i = 0; $i < 10; $i++) {
$client->increment('foo.baz', 1);
$client->increment('foo.baz', $sampleRate);
}
$this->assertEquals(
'test.foo.baz:1|c|@0.9',
"test.foo.baz:1|c|@{$expectedSampleRate}",
$this->connection->getLastMessage()
);
}

/**
* @dataProvider sampleRateData
* @group sampling
*/
public function testIncrementWithSamplingRateAndTags()
public function testIncrementWithSamplingRateAndTags(float $globalSampleRate, float $sampleRate, string $expectedSampleRate)
{
$client = new Client($this->connection, 'test', 0.9);
$client = new Client($this->connection, 'test', $globalSampleRate);
for ($i = 0; $i < 10; $i++) {
$client->increment('foo.baz', 1, ['tag' => 'value']);
$client->increment('foo.baz', $sampleRate, ['tag' => 'value']);
}
$this->assertEquals(
'test.foo.baz:1|c|@0.9|#tag:value',
"test.foo.baz:1|c|@{$expectedSampleRate}|#tag:value",
$this->connection->getLastMessage()
);
}
Expand All @@ -137,31 +149,33 @@ public function testDecrement()
}

/**
* @dataProvider sampleRateData
* @group sampling
*/
public function testDecrementWithSamplingRate()
public function testDecrementWithSamplingRate(float $globalSampleRate, float $sampleRate, string $expectedSampleRate)
{
$client = new Client($this->connection, 'test', 0.9);
$client = new Client($this->connection, 'test', $globalSampleRate);
for ($i = 0; $i < 10; $i++) {
$client->decrement('foo.baz', 1);
$client->decrement('foo.baz', $sampleRate);
}
$this->assertEquals(
'test.foo.baz:-1|c|@0.9',
"test.foo.baz:-1|c|@{$expectedSampleRate}",
$this->connection->getLastMessage()
);
}

/**
* @dataProvider sampleRateData
* @group sampling
*/
public function testDecrementWithSamplingRateAndTags()
public function testDecrementWithSamplingRateAndTags(float $globalSampleRate, float $sampleRate, string $expectedSampleRate)
{
$client = new Client($this->connection, 'test', 0.9);
$client = new Client($this->connection, 'test', $globalSampleRate);
for ($i = 0; $i < 10; $i++) {
$client->decrement('foo.baz', 1, ['tag' => 'value']);
$client->decrement('foo.baz', $sampleRate, ['tag' => 'value']);
}
$this->assertEquals(
'test.foo.baz:-1|c|@0.9|#tag:value',
"test.foo.baz:-1|c|@{$expectedSampleRate}|#tag:value",
$this->connection->getLastMessage()
);
}
Expand All @@ -177,16 +191,17 @@ public function testCanMeasureTimingWithClosure()


/**
* @dataProvider sampleRateData
* @group sampling
*/
public function testTimingWithSamplingRate()
public function testTimingWithSamplingRate(float $globalSampleRate, float $sampleRate, string $expectedSampleRate)
{
$client = new Client($this->connection, 'test', 0.9);
$client = new Client($this->connection, 'test', $globalSampleRate);
for ($i = 0; $i < 10; $i++) {
$client->timing('foo.baz', 2000, 1);
$client->timing('foo.baz', 2000, $sampleRate);
}
$this->assertEquals(
'test.foo.baz:2000|ms|@0.9',
"test.foo.baz:2000|ms|@{$expectedSampleRate}",
$this->connection->getLastMessage()
);
}
Expand Down Expand Up @@ -218,20 +233,21 @@ public function testEndTimingReturnsTiming()
}

/**
* @dataProvider sampleRateData
* @group sampling
*/
public function testStartEndTimingWithSamplingRate()
public function testStartEndTimingWithSamplingRate(float $globalSampleRate, float $sampleRate, string $expectedSampleRate)
{
$client = new Client($this->connection, 'test', 0.9);
$client = new Client($this->connection, 'test', $globalSampleRate);
for ($i = 0; $i < 10; $i++) {
$client->startTiming('foo.baz');
usleep(10000);
$client->endTiming('foo.baz');
$client->endTiming('foo.baz', $sampleRate);
}

// ranges between 1000 and 1001ms
$this->assertMatchesRegularExpression(
'/^test\.foo\.baz:1[0-9](.[0-9]+)?\|ms\|@0.9$/',
"/^test\.foo\.baz:1[0-9](.[0-9]+)?\|ms\|@{$expectedSampleRate}$/",
$this->connection->getLastMessage()
);
}
Expand Down

0 comments on commit ec03bd2

Please sign in to comment.