Skip to content

Commit

Permalink
Include every "length" command + generic way to set typed keys (#96)
Browse files Browse the repository at this point in the history
* Relay caches in-memory the "length" information about keys of any
  type, so we should include all of these commands in our benchmark
  suite.

* Add a generic helper method that uses our flags to set keys of a given
  type in a generic way.
  • Loading branch information
michael-grunder authored Jul 20, 2023
1 parent 40132f2 commit 93913cb
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 24 deletions.
13 changes: 13 additions & 0 deletions benchmarks/Cases/BenchmarkHlen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace CacheWerk\Relay\Benchmarks\Cases;

use CacheWerk\Relay\Benchmarks\Support\BenchmarkLenCommand;

class BenchmarkHLEN extends BenchmarkLenCommand
{
public static function flags(): int
{
return self::HASH | self::READ;
}
}
13 changes: 13 additions & 0 deletions benchmarks/Cases/BenchmarkLlen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace CacheWerk\Relay\Benchmarks\Cases;

use CacheWerk\Relay\Benchmarks\Support\BenchmarkLenCommand;

class BenchmarkLLEN extends BenchmarkLenCommand
{
public static function flags(): int
{
return self::LIST | self::READ;
}
}
26 changes: 2 additions & 24 deletions benchmarks/Cases/BenchmarkScard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,12 @@

namespace CacheWerk\Relay\Benchmarks\Cases;

use CacheWerk\Relay\Benchmarks\Support\BenchmarkKeyCommand;
use CacheWerk\Relay\Benchmarks\Support\BenchmarkLenCommand;

class BenchmarkSCARD extends BenchmarkKeyCommand
class BenchmarkSCARD extends BenchmarkLenCommand
{
/**
* @var array<int, string>
*/
protected array $keys;

public static function flags(): int
{
return self::SET | self::READ;
}

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

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

foreach ($this->loadJsonFile('meteorites.json') as $item) {
$redis->sadd((string) $item['id'], array_keys($this->flattenArray($item)));
$this->keys[] = $item['id'];
}
}
}
13 changes: 13 additions & 0 deletions benchmarks/Cases/BenchmarkStrlen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace CacheWerk\Relay\Benchmarks\Cases;

use CacheWerk\Relay\Benchmarks\Support\BenchmarkLenCommand;

class BenchmarkSTRLEN extends BenchmarkLenCommand
{
public static function flags(): int
{
return self::STRING | self::READ;
}
}
13 changes: 13 additions & 0 deletions benchmarks/Cases/BenchmarkZcard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace CacheWerk\Relay\Benchmarks\Cases;

use CacheWerk\Relay\Benchmarks\Support\BenchmarkLenCommand;

class BenchmarkZCARD extends BenchmarkLenCommand
{
public static function flags(): int
{
return self::ZSET | self::READ;
}
}
38 changes: 38 additions & 0 deletions benchmarks/Support/Benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,44 @@ protected function loadJsonFile(string $file, bool $assoc = true) // @phpstan-ig
return json_decode((string) $data, $assoc, 512, JSON_THROW_ON_ERROR);
}

/**
* Generic function to seed keys of whatever type this benchmark operates
* against. Will throw an exception if the command does not operate against
* a single key type (e.g. DEL).
*
* @return string[] An array of string key names.
**/
protected function seedSimpleKeys(): array {
$keys = [];

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

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

if ($this->flags() & Self::STRING) {
$redis->set($key, serialize($item));
} else if ($this->flags() & Self::LIST) {
$redis->rpush($key, $this->flattenArray($item));
} else if ($this->flags() & Self::HASH) {
$redis->hmset($key, $this->flattenArray($item));
} else if ($this->flags() & Self::SET) {
$redis->sadd($key, array_keys($this->flattenArray($item)));
} else if ($this->flags() & Self::ZSET) {
$redis->zadd($key, [array_rand($item) => mt_rand()/mt_getrandmax()]);
} else if ($this->flags() & Self::HYPERLOGLOG) {
$redis->pfadd($key, [array_rand($item)]);
} else {
throw new Exception("Unsupported key type");
}

$keys[] = $item['id'];
}

return $keys;
}

public function setUpClients(): void
{
$this->predis = $this->createPredis();
Expand Down
25 changes: 25 additions & 0 deletions benchmarks/Support/BenchmarkLenCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace CacheWerk\Relay\Benchmarks\Support;

use CacheWerk\Relay\Benchmarks\Support\BenchmarkKeyCommand;

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

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

public function seed(): void
{
$this->keys = $this->seedSimpleKeys();
}
}

0 comments on commit 93913cb

Please sign in to comment.