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
66 changes: 52 additions & 14 deletions benchmarks/Support/Benchmark.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

Check warning on line 1 in benchmarks/Support/Benchmark.php

View workflow job for this annotation

GitHub Actions / Pint

Found violation(s) of type: not_operator_with_successor_space

Check warning on line 1 in benchmarks/Support/Benchmark.php

View workflow job for this annotation

GitHub Actions / Pint

Found violation(s) of type: unary_operator_spaces

Check warning on line 1 in benchmarks/Support/Benchmark.php

View workflow job for this annotation

GitHub Actions / Pint

Found violation(s) of type: new_with_parentheses

namespace CacheWerk\Relay\Benchmarks\Support;

use Exception;
use Memcached;
use Redis as PhpRedis;
use Relay\Relay;
use Predis\Client as Predis;
Expand Down Expand Up @@ -50,7 +51,9 @@

protected Predis $predis;

protected PhpRedis $phpredis;
protected ?PhpRedis $phpredis;

protected ?Memcached $memcached;

/**
* @param string $host
Expand Down Expand Up @@ -101,6 +104,9 @@
protected function flush(): void
{
$this->createPredis()->flushall();
if (($memcached = $this->createMemcached()) !== null) {
$memcached->flush();
}
}

/**
Expand Down Expand Up @@ -150,13 +156,17 @@
$keys = [];

$redis = $this->createPredis();
$memcached = $this->createMemcached();
$items = $this->loadJsonFile('meteorites.json');

foreach ($items as $item) {
$key = (string) $item['id'];

if ($this->flags() & self::STRING) {
$redis->set($key, serialize($item));
if ($memcached) {
$memcached->set($key, serialize($item));
}
} elseif ($this->flags() & self::LIST) {
$redis->rpush($key, $this->flattenArray($item));
} elseif ($this->flags() & self::HASH) {
Expand Down Expand Up @@ -204,10 +214,8 @@
$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->memcached = $this->createMemcached();
}

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

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

/**
Expand Down Expand Up @@ -264,8 +270,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 +315,42 @@
]);
}

protected function createMemcached(): ?Memcached
{
if (! extension_loaded('memcached')) {
return null;
}

$memcached = new Memcached();
$memcached->setOptions([
Memcached::OPT_CONNECT_TIMEOUT => 500,
Memcached::OPT_SEND_TIMEOUT => 500,
Memcached::OPT_RECV_TIMEOUT => 500,
]);
$memcached->addServer($this->host, 11211); // TODO: add CLI options

return $memcached;
}

/**
* @return array<string>
*/
public function getBenchmarkMethods(string $filter): array
{
$exclude = null;
$exclude = [];

if (! extension_loaded('redis')) {
$exclude = 'PhpRedis';
if ($this->phpredis === null) {
$exclude[] = 'PhpRedis';

Reporter::printWarning('Skipping PhpRedis runs, extension is not loaded');
}

if ($this->memcached === null) {
$exclude[] = 'Memcached';

Reporter::printWarning('Skipping Memcached runs, extension is not loaded');
}

return array_filter(
get_class_methods($this),
function ($method) use ($exclude, $filter) {
Expand All @@ -327,7 +360,7 @@

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

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

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

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

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

public function benchmarkMemcached(): int
{
return $this->runBenchmark($this->memcached);

Check failure on line 398 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, Memcached|null given.
}
}
2 changes: 1 addition & 1 deletion benchmarks/Support/BenchmarkKeyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function setUp(): void
}

/**
* @param \Relay\Relay|\Redis|\Predis\Client $client
* @param \Relay\Relay|\Redis|\Predis\Client|\Memcached $client
*/
protected function runBenchmark($client): int
{
Expand Down
12 changes: 10 additions & 2 deletions benchmarks/Support/ConcurrentRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ protected function getConcurrentStart(string $nonce): int
protected function runConcurrentOnce(Benchmark $benchmark, Reporter $reporter, Subject $subject, string $class, string $method): Iteration
{
$start = hrtime(true);
[$rx1, $tx1] = $this->getNetworkStats();
if ($method === 'benchmarkMemcached') {
[$rx1, $tx1] = $this->getMemcachedNetworkStats();
} else {
[$rx1, $tx1] = $this->getRedisNetworkStats();
}
$cmd1 = $this->getRedisCommandCount();

$pids = [];
Expand Down Expand Up @@ -116,7 +120,11 @@ protected function runConcurrentOnce(Benchmark $benchmark, Reporter $reporter, S
pcntl_waitpid($pid, $status, WUNTRACED);
}

[$rx2, $tx2] = $this->getNetworkStats();
if ($method === 'benchmarkMemcached') {
[$rx2, $tx2] = $this->getMemcachedNetworkStats();
} else {
[$rx2, $tx2] = $this->getRedisNetworkStats();
}
$cmd2 = $this->getRedisCommandCount();

$end = $max_mem = $tot_ops = 0;
Expand Down
63 changes: 57 additions & 6 deletions benchmarks/Support/Runner.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

Check warning on line 1 in benchmarks/Support/Runner.php

View workflow job for this annotation

GitHub Actions / Pint

Found violation(s) of type: not_operator_with_successor_space

Check warning on line 1 in benchmarks/Support/Runner.php

View workflow job for this annotation

GitHub Actions / Pint

Found violation(s) of type: unary_operator_spaces

Check warning on line 1 in benchmarks/Support/Runner.php

View workflow job for this annotation

GitHub Actions / Pint

Found violation(s) of type: new_with_parentheses

namespace CacheWerk\Relay\Benchmarks\Support;

use Memcached;
use Predis\Client as Predis;

class Runner
Expand All @@ -17,6 +18,8 @@

protected Predis $redis;

protected ?Memcached $memcached;

protected string $run_id;

protected string $filter;
Expand Down Expand Up @@ -66,10 +69,24 @@

fprintf(
STDERR,
"Connected to Redis (%s) at %s\n\n",
"Connected to Redis (%s) at %s\n",
$this->redis->info()['Server']['redis_version'],
$this->port ? "tcp://{$host}:{$port}" : "unix:{$host}",
);

$this->setUpMemcached();

if ($this->memcached) {
if (($version = $this->memcached->getVersion()) === false) {
throw new \ErrorException("Connection refused [tcp://{$host}:11211]");
}
fprintf(
STDERR,
"Connected to Memcached (%s) at %s\n",
$version["{$host}:11211"],
"tcp://{$host}:11211", // TODO: support unix socket
);
}
}

protected function setUpRedis(): void
Expand Down Expand Up @@ -100,6 +117,23 @@
]);
}

protected function setUpMemcached(): void
{
if (! extension_loaded('memcached')) {
return;
}

$memcached = new Memcached();
$memcached->setOptions([
Memcached::OPT_CONNECT_TIMEOUT => 500,
Memcached::OPT_SEND_TIMEOUT => 500,
Memcached::OPT_RECV_TIMEOUT => 500,
]);
$memcached->addServer($this->host, 11211);

$this->memcached = $memcached;
}

protected function resetStats(): void
{
$this->redis->config('RESETSTAT');
Expand All @@ -112,13 +146,26 @@
/**
* @return array<int, int>
*/
protected function getNetworkStats(): array
protected function getMemcachedNetworkStats(): array
{
$info = $this->redis->info('STATS')['Stats'];
$stats = $this->memcached->getStats()["{$this->host}:11211"];

Check failure on line 151 in benchmarks/Support/Runner.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot call method getStats() on Memcached|null.

return [
$info['total_net_input_bytes'],
$info['total_net_output_bytes'],
$stats['bytes_read'],
$stats['bytes_written'],
];
}

/**
* @return array<int, int>
*/
protected function getRedisNetworkStats(): array
{
$stats = $this->redis->info('STATS')['Stats'];

return [
$stats['total_net_input_bytes'],
$stats['total_net_output_bytes'],
];
}

Expand Down Expand Up @@ -157,7 +204,11 @@
$t2 = microtime(true);
} while ($t2 - $t1 < $this->duration);

[$rx, $tx] = $this->getNetworkStats();
if ($method === 'benchmarkMemcached') {
[$rx, $tx] = $this->getMemcachedNetworkStats();
} else {
[$rx, $tx] = $this->getRedisNetworkStats();
}
$millis = ($t2 - $t1) * 1000;
$memory = memory_get_peak_usage();
$cmds = $this->getRedisCommandCount() - $cmds1;
Expand Down
Loading