|
| 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 | +} |
0 commit comments