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;
}

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'];
}
}
}
96 changes: 83 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 ?PhpRedis $phpredis;

protected object $table;

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,9 @@
public function refreshClients(): void
{
$this->predis = $this->createPredis();

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 +267,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 +312,72 @@
]);
}

public function createRelayTable(): object
{
return new class
{
public function clear(): bool
{
return Table::clearAll();

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

View workflow job for this annotation

GitHub Actions / PHPStan

Method class@anonymous/benchmarks/Support/Benchmark.php:317::clear() should return bool but returns int.
}

public function get(string $key): mixed
{
return Table::get($key);

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

View workflow job for this annotation

GitHub Actions / PHPStan

Static call to instance method Relay\Table::get().
}

public function set(string $key, $value): bool

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:317::set() has parameter $value with no type specified.
{
return Table::set($key, $value);

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

View workflow job for this annotation

GitHub Actions / PHPStan

Static call to instance method Relay\Table::set().
}
};

}

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 355 in benchmarks/Support/Benchmark.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method class@anonymous/benchmarks/Support/Benchmark.php:343::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 +387,7 @@

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

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

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

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

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

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

Check failure on line 425 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 given.
}

public function benchmarkAPCu(): int
{
return $this->runBenchmark($this->apcu);
}
}
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
{
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