Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions benchmarks/Cases/BenchmarkInMemoryGet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace CacheWerk\Relay\Benchmarks\Cases;

use CacheWerk\Relay\Benchmarks\Support\BenchmarkInMemoryCommand;

class BenchmarkInMemoryGet extends BenchmarkInMemoryCommand
{
public static function flags(): int
{
return self::STRING | self::READ | self::DEFAULT;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's NOT run these by default.

But add a README entry on how to run them.

}

public function command(): string
{
return str_replace('inmemory', '', parent::command());
}

public function seed(): void
{
$clients = $this->clients();

foreach ($this->loadJsonFile('meteorites.json') as $item) {
foreach ($clients as $client) {
$client->set((string) $item['id'], $item);
}
$this->keys[] = $item['id'];
}
}
}
77 changes: 64 additions & 13 deletions benchmarks/Support/Benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Redis as PhpRedis;
use Relay\Relay;
use Relay\Table;
use Predis\Client as Predis;

abstract class Benchmark
Expand Down Expand Up @@ -50,7 +51,11 @@

protected Predis $predis;

protected PhpRedis $phpredis;
protected Table $table;

protected ?PhpRedis $phpredis;

protected ?object $apcu;

/**
* @param string $host
Expand Down Expand Up @@ -204,10 +209,9 @@
$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();
}

/**
Expand All @@ -220,10 +224,7 @@
public function refreshClients(): void
{
$this->predis = $this->createPredis();

if (extension_loaded('redis')) {
$this->phpredis = $this->createPhpRedis();
}
$this->phpredis = $this->createPhpRedis();
}

/**
Expand Down Expand Up @@ -264,8 +265,12 @@
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);
Expand Down Expand Up @@ -305,19 +310,55 @@
]);
}

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

Check failure on line 336 in benchmarks/Support/Benchmark.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method class@anonymous/benchmarks/Support/Benchmark.php:324::set() has parameter $value with no type specified.
{
return apcu_store($key, $value);
}
};
}

/**
* @return array<string>
*/
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) {
Expand All @@ -327,7 +368,7 @@

$method = substr($method, strlen('benchmark'));

if ($method === $exclude) {
if (in_array($method, $exclude, true)) {
return false;
}

Expand All @@ -347,7 +388,7 @@

public function benchmarkPhpRedis(): int
{
return $this->runBenchmark($this->phpredis);

Check failure on line 391 in benchmarks/Support/Benchmark.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $client of method CacheWerk\Relay\Benchmarks\Support\Benchmark::runBenchmark() expects Predis\Client|Redis|Relay\Relay, Redis|null given.
}

public function benchmarkRelayNoCache(): int
Expand All @@ -359,4 +400,14 @@
{
return $this->runBenchmark($this->relay);
}

public function benchmarkRelayTable(): int
{
return $this->runBenchmark($this->table);

Check failure on line 406 in benchmarks/Support/Benchmark.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $client of method CacheWerk\Relay\Benchmarks\Support\Benchmark::runBenchmark() expects Predis\Client|Redis|Relay\Relay, Relay\Table given.
}

public function benchmarkAPCu(): int
{
return $this->runBenchmark($this->apcu);

Check failure on line 411 in benchmarks/Support/Benchmark.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $client of method CacheWerk\Relay\Benchmarks\Support\Benchmark::runBenchmark() expects Predis\Client|Redis|Relay\Relay, object|null given.
}
}
50 changes: 50 additions & 0 deletions benchmarks/Support/BenchmarkInMemoryCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace CacheWerk\Relay\Benchmarks\Support;

abstract class BenchmarkInMemoryCommand extends Benchmark
{
/**
* @var array<int, string>
*/
protected array $keys;

public function setUp(): void
{
$this->setUpClients();
$this->flush();

if (method_exists($this, 'seed')) {
$this->seed();
}
}

protected function clients(): array

Check failure on line 22 in benchmarks/Support/BenchmarkInMemoryCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method CacheWerk\Relay\Benchmarks\Support\BenchmarkInMemoryCommand::clients() return type has no value type specified in iterable type 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);
}
}
Loading