Skip to content

Commit d5b7ee6

Browse files
committed
Add benchmark test.
Signed-off-by: Arris Ray <[email protected]>
1 parent 517c791 commit d5b7ee6

11 files changed

+844
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/vendor/
22
*.iml
33
/.idea/
4+
benchmark.csv
45
composer.lock
56
composer.phar
67
.phpunit.result.cache

phpunit.xml.dist

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<groups>
2626
<exclude>
2727
<group>Performance</group>
28+
<group>Benchmark</group>
2829
</exclude>
2930
</groups>
3031
</phpunit>

tests/Test/Benchmark/AdapterType.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Test\Benchmark;
4+
5+
use InvalidArgumentException;
6+
7+
class AdapterType
8+
{
9+
const REDIS = 1<<0;
10+
const REDISNG = 1<<1;
11+
const REDISTXN = 1<<2;
12+
13+
/**
14+
* @param int $type
15+
* @return string
16+
*/
17+
public static function toString(int $type): string
18+
{
19+
switch ($type) {
20+
case AdapterType::REDIS:
21+
return 'redis';
22+
case AdapterType::REDISNG:
23+
return 'redisng';
24+
case AdapterType::REDISTXN:
25+
return 'redistxn';
26+
}
27+
28+
throw new InvalidArgumentException("Invalid adapter type: {$type}");
29+
}
30+
}
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Test\Benchmark;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Test\Benchmark\TestCase as BenchmarkTestCase;
9+
10+
class BenchmarkTest extends TestCase
11+
{
12+
/**
13+
* @var string
14+
*/
15+
public const RESULT_FILENAME = 'benchmark.csv';
16+
17+
/**
18+
* @inheritDoc
19+
*/
20+
public static function setUpBeforeClass(): void
21+
{
22+
file_put_contents(self::RESULT_FILENAME, implode(',', [
23+
'adapter',
24+
'metric',
25+
'num-keys',
26+
'num-samples',
27+
'write-p50',
28+
'write-p75',
29+
'write-p95',
30+
'write-p99',
31+
'write-min',
32+
'write-max',
33+
'write-avg',
34+
'render-p50',
35+
'render-p75',
36+
'render-p95',
37+
'render-p99',
38+
'render-min',
39+
'render-max',
40+
'render-avg',
41+
]));
42+
parent::setUpBeforeClass();
43+
}
44+
45+
/**
46+
* @return array
47+
*/
48+
public function benchmarkProvider(): array
49+
{
50+
return [
51+
[AdapterType::REDISNG, MetricType::SUMMARY, 1000, 10],
52+
[AdapterType::REDISNG, MetricType::SUMMARY, 2000, 10],
53+
[AdapterType::REDISNG, MetricType::SUMMARY, 5000, 10],
54+
[AdapterType::REDISNG, MetricType::SUMMARY, 10000, 10],
55+
[AdapterType::REDISTXN, MetricType::SUMMARY, 1000, 10],
56+
[AdapterType::REDISTXN, MetricType::SUMMARY, 2000, 10],
57+
[AdapterType::REDISTXN, MetricType::SUMMARY, 5000, 10],
58+
[AdapterType::REDISTXN, MetricType::SUMMARY, 10000, 10],
59+
];
60+
}
61+
62+
/**
63+
* @dataProvider benchmarkProvider
64+
* @group Benchmark
65+
* @param int $adapter
66+
* @param int $metric
67+
* @param int $numKeys
68+
* @param int $numSamples
69+
* @return void
70+
* @test
71+
*/
72+
public function benchmark(
73+
int $adapter,
74+
int $metric,
75+
int $numKeys,
76+
int $numSamples
77+
): void
78+
{
79+
// Create and execute test case
80+
$testCase = BenchmarkTestCase::newBuilder()
81+
->withAdapterType($adapter)
82+
->withMetricType($metric)
83+
->withReportType(ReportType::CSV)
84+
->withNumKeys($numKeys)
85+
->withNumSamples($numSamples)
86+
->build();
87+
88+
// Sanity check test structure
89+
$this->assertEquals($adapter, $testCase->getAdapterType());
90+
$this->assertEquals($metric, $testCase->getMetricType());
91+
$this->assertEquals($numKeys, $testCase->getNumKeys());
92+
$this->assertEquals($numSamples, $testCase->getNumSamples());
93+
94+
// Record results
95+
$result = $testCase->execute();
96+
file_put_contents(self::RESULT_FILENAME, $result->report() . PHP_EOL, FILE_APPEND);
97+
}
98+
}

tests/Test/Benchmark/MetricType.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Test\Benchmark;
4+
5+
use InvalidArgumentException;
6+
7+
class MetricType
8+
{
9+
const COUNTER = 1<<0;
10+
const GAUGE = 1<<1;
11+
const HISTOGRAM = 1<<2;
12+
const SUMMARY = 1<<3;
13+
14+
/**
15+
* @param int $type
16+
* @return string
17+
*/
18+
public static function toString(int $type): string
19+
{
20+
switch ($type) {
21+
case MetricType::COUNTER:
22+
return 'counter';
23+
case MetricType::GAUGE:
24+
return 'gauge';
25+
case MetricType::HISTOGRAM:
26+
return 'histogram';
27+
case MetricType::SUMMARY:
28+
return 'timer';
29+
}
30+
31+
throw new InvalidArgumentException("Invalid adapter type: {$type}");
32+
}
33+
}

tests/Test/Benchmark/ReportType.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Test\Benchmark;
4+
5+
class ReportType
6+
{
7+
const CSV = 1<<0;
8+
const JSON = 1<<1;
9+
}

0 commit comments

Comments
 (0)