From f912628831007d0dbcb16e4cf2041788da2aff16 Mon Sep 17 00:00:00 2001 From: Pavlo Yatsukhnenko Date: Fri, 7 Nov 2025 13:41:50 +0200 Subject: [PATCH 1/3] Run benchmarks against APCu and Relay\Table --- benchmarks/Support/Benchmark.php | 62 +++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/benchmarks/Support/Benchmark.php b/benchmarks/Support/Benchmark.php index ade5889..53c0955 100644 --- a/benchmarks/Support/Benchmark.php +++ b/benchmarks/Support/Benchmark.php @@ -5,6 +5,7 @@ use Exception; use Redis as PhpRedis; use Relay\Relay; +use Relay\Table; use Predis\Client as Predis; abstract class Benchmark @@ -50,7 +51,11 @@ abstract class Benchmark protected Predis $predis; - protected PhpRedis $phpredis; + protected Table $table; + + protected ?PhpRedis $phpredis; + + protected ?object $apcu; /** * @param string $host @@ -204,10 +209,9 @@ public function setUpClients(): void $this->predis = $this->createPredis(); $this->relay = $this->createRelay(); $this->relayNoCache = $this->createRelayNoCache(); - - if (extension_loaded('redis')) { - $this->phpredis = $this->createPhpRedis(); - } + $this->phpredis = $this->createPhpRedis(); + $this->table = $this->createRelayTable(); + $this->apcu = $this->createAPCu(); } /** @@ -264,8 +268,12 @@ protected function createRelayNoCache(): Relay return $relay; } - protected function createPhpRedis(): PhpRedis + protected function createPhpRedis(): ?PhpRedis { + if (! extension_loaded('redis')) { + return null; + } + $phpredis = new PhpRedis; $phpredis->connect($this->host, $this->port, 0.5, '', 0, 0.5); $phpredis->setOption(PhpRedis::OPT_MAX_RETRIES, 0); @@ -305,19 +313,45 @@ protected function createPredis(): Predis ]); } + public function createRelayTable(): Table + { + return new Table; + } + + protected function createAPCu(): ?object + { + if (! extension_loaded('apcu')) { + return null; + } + + return new class + { + public function get($key) + { + return apcu_fetch($key); + } + }; + } + /** * @return array */ public function getBenchmarkMethods(string $filter): array { - $exclude = null; + $exclude = []; if (! extension_loaded('redis')) { - $exclude = 'PhpRedis'; + $exclude[] = 'PhpRedis'; Reporter::printWarning('Skipping PhpRedis runs, extension is not loaded'); } + if (! extension_loaded('apcu')) { + $exclude[] = 'APCu'; + + Reporter::printWarning('Skipping APCu runs, extension is not loaded'); + } + return array_filter( get_class_methods($this), function ($method) use ($exclude, $filter) { @@ -327,7 +361,7 @@ function ($method) use ($exclude, $filter) { $method = substr($method, strlen('benchmark')); - if ($method === $exclude) { + if (in_array($method, $exclude, true)) { return false; } @@ -359,4 +393,14 @@ public function benchmarkRelay(): int { return $this->runBenchmark($this->relay); } + + public function benchmarkRelayTable(): int + { + return $this->runBenchmark($this->table); + } + + public function benchmarkAPCu(): int + { + return $this->runBenchmark($this->apcu); + } } From 7da2028435e6ff57386d7d1c9233dbef5bb5157f Mon Sep 17 00:00:00 2001 From: Pavlo Yatsukhnenko Date: Tue, 11 Nov 2025 14:24:21 +0200 Subject: [PATCH 2/3] Add BenchmarkInMemoryGet --- benchmarks/Cases/BenchmarkInMemoryGet.php | 30 +++++++++++ benchmarks/Support/Benchmark.php | 17 +++++-- .../Support/BenchmarkInMemoryCommand.php | 50 +++++++++++++++++++ 3 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 benchmarks/Cases/BenchmarkInMemoryGet.php create mode 100644 benchmarks/Support/BenchmarkInMemoryCommand.php diff --git a/benchmarks/Cases/BenchmarkInMemoryGet.php b/benchmarks/Cases/BenchmarkInMemoryGet.php new file mode 100644 index 0000000..ca620f9 --- /dev/null +++ b/benchmarks/Cases/BenchmarkInMemoryGet.php @@ -0,0 +1,30 @@ +clients(); + + foreach ($this->loadJsonFile('meteorites.json') as $item) { + foreach ($clients as $client) { + $client->set((string) $item['id'], $item); + } + $this->keys[] = $item['id']; + } + } +} diff --git a/benchmarks/Support/Benchmark.php b/benchmarks/Support/Benchmark.php index 53c0955..cc9c9e0 100644 --- a/benchmarks/Support/Benchmark.php +++ b/benchmarks/Support/Benchmark.php @@ -224,10 +224,7 @@ public function setUpClients(): void public function refreshClients(): void { $this->predis = $this->createPredis(); - - if (extension_loaded('redis')) { - $this->phpredis = $this->createPhpRedis(); - } + $this->phpredis = $this->createPhpRedis(); } /** @@ -326,10 +323,20 @@ protected function createAPCu(): ?object return new class { - public function get($key) + public function clear(): bool + { + return apcu_clear_cache(); + } + + public function get(string $key): mixed { return apcu_fetch($key); } + + public function set(string $key, $value): bool + { + return apcu_store($key, $value); + } }; } diff --git a/benchmarks/Support/BenchmarkInMemoryCommand.php b/benchmarks/Support/BenchmarkInMemoryCommand.php new file mode 100644 index 0000000..d14e24d --- /dev/null +++ b/benchmarks/Support/BenchmarkInMemoryCommand.php @@ -0,0 +1,50 @@ + + */ + protected array $keys; + + public function setUp(): void + { + $this->setUpClients(); + $this->flush(); + + if (method_exists($this, 'seed')) { + $this->seed(); + } + } + + protected function clients(): array + { + return array_filter([ + $this->table, + $this->apcu, + ]); + } + + protected function flush(): void + { + foreach ($this->clients() as $client) { + $client->clear(); + } + } + + /** + * @param mixed $client + */ + protected function runBenchmark($client): int + { + $cmd = $this->command(); + + foreach ($this->keys as $key) { + $client->{$cmd}($key); + } + + return count($this->keys); + } +} From 4f788c241e7cbfb12525e884e1cb9763c9d08aaf Mon Sep 17 00:00:00 2001 From: Pavlo Yatsukhnenko Date: Tue, 20 Jan 2026 10:56:29 +0200 Subject: [PATCH 3/3] Relay\Table wrapper --- benchmarks/Cases/BenchmarkInMemoryGet.php | 2 +- benchmarks/Support/Benchmark.php | 27 +++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/benchmarks/Cases/BenchmarkInMemoryGet.php b/benchmarks/Cases/BenchmarkInMemoryGet.php index ca620f9..f829a30 100644 --- a/benchmarks/Cases/BenchmarkInMemoryGet.php +++ b/benchmarks/Cases/BenchmarkInMemoryGet.php @@ -8,7 +8,7 @@ class BenchmarkInMemoryGet extends BenchmarkInMemoryCommand { public static function flags(): int { - return self::STRING | self::READ | self::DEFAULT; + return self::STRING | self::READ; } public function command(): string diff --git a/benchmarks/Support/Benchmark.php b/benchmarks/Support/Benchmark.php index cc9c9e0..72baa50 100644 --- a/benchmarks/Support/Benchmark.php +++ b/benchmarks/Support/Benchmark.php @@ -51,10 +51,10 @@ abstract class Benchmark protected Predis $predis; - protected Table $table; - protected ?PhpRedis $phpredis; + protected object $table; + protected ?object $apcu; /** @@ -225,6 +225,8 @@ public function refreshClients(): void { $this->predis = $this->createPredis(); $this->phpredis = $this->createPhpRedis(); + $this->table = $this->createRelayTable(); + $this->apcu = $this->createAPCu(); } /** @@ -310,9 +312,26 @@ protected function createPredis(): Predis ]); } - public function createRelayTable(): Table + public function createRelayTable(): object { - return new Table; + return new class + { + public function clear(): bool + { + return Table::clearAll(); + } + + public function get(string $key): mixed + { + return Table::get($key); + } + + public function set(string $key, $value): bool + { + return Table::set($key, $value); + } + }; + } protected function createAPCu(): ?object