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 ade5889..cc9c9e0 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(); } /** @@ -220,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(); } /** @@ -264,8 +265,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 +310,55 @@ 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 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); + } + }; + } + /** * @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 +368,7 @@ function ($method) use ($exclude, $filter) { $method = substr($method, strlen('benchmark')); - if ($method === $exclude) { + if (in_array($method, $exclude, true)) { return false; } @@ -359,4 +400,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); + } } 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); + } +}