Skip to content
Open
Changes from 1 commit
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
62 changes: 53 additions & 9 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 Down Expand Up @@ -264,8 +268,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 +313,45 @@
]);
}

public function createRelayTable(): Table
{
return new Table;
}

protected function createAPCu(): ?object
{
if (! extension_loaded('apcu')) {
return null;
}

return new class
{
public function get($key)

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

View workflow job for this annotation

GitHub Actions / PHPStan

Method class@anonymous/benchmarks/Support/Benchmark.php:327::get() has parameter $key with no type specified.

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

View workflow job for this annotation

GitHub Actions / PHPStan

Method class@anonymous/benchmarks/Support/Benchmark.php:327::get() has no return type specified.
{
return apcu_fetch($key);
}
};
}

/**
* @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 +361,7 @@

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

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

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

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

Check failure on line 384 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 +393,14 @@
{
return $this->runBenchmark($this->relay);
}

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

Check failure on line 399 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 404 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.
}
}
Loading