diff --git a/.gitattributes b/.gitattributes index 90af4b3..cef1daf 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,5 @@ /.github/ export-ignore +/benchmarks/ export-ignore /docker/ export-ignore /examples/ export-ignore /stubs/ export-ignore @@ -11,6 +12,7 @@ /composer.lock export-ignore /infection.json5.dist export-ignore /Makefile export-ignore +/phpbench.json export-ignore /phpstan.dist.neon export-ignore /phpunit.xml.dist export-ignore /rector.php export-ignore \ No newline at end of file diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 7eb0b34..1396b51 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -10,6 +10,7 @@ $config = new Config() ->setFinder( Finder::create() + ->in(__DIR__ . '/benchmarks') ->in(__DIR__ . '/src') ->in(__DIR__ . '/tests') ->append([ diff --git a/Makefile b/Makefile index 04cdd75..92ed5bb 100644 --- a/Makefile +++ b/Makefile @@ -99,6 +99,10 @@ test: var vendor ## Run tests using PHPUnit $(RUN) vendor/bin/phpunit $(ARGS) --colors .PHONY: test +bench: var vendor ## Run benchmarks: `make bench ARGS='--filter=Packed --tag=baseline'`, then compare with `ARGS='--ref=baseline'` + $(RUN) vendor/bin/phpbench run --report=protobuf $(ARGS) +.PHONY: bench + infect: var vendor up ## Run mutation tests using Infection $(RUN) infection --show-mutations $(ARGS) .PHONY: infect diff --git a/benchmarks/BlobBench.php b/benchmarks/BlobBench.php new file mode 100644 index 0000000..72a6731 --- /dev/null +++ b/benchmarks/BlobBench.php @@ -0,0 +1,38 @@ + + */ +#[Bench\Revs(1000)] +#[Bench\ParamProviders('provideKinds')] +final class BlobBench extends CodecBench +{ + /** + * @return iterable + */ + public function provideKinds(): iterable + { + yield 'bytes 1MB' => ['kind' => 'bytes']; + yield 'string 1MB' => ['kind' => 'string']; + } + + #[\Override] + protected function message(array $params): object + { + return match ($params['kind']) { + 'bytes' => new Blob(str_repeat("\xAB", 1 << 20)), + // 4-byte UTF-8 sequences. + default => new Text(str_repeat("\u{1F680}", 1 << 18)), + }; + } +} diff --git a/benchmarks/CodecBench.php b/benchmarks/CodecBench.php new file mode 100644 index 0000000..3477848 --- /dev/null +++ b/benchmarks/CodecBench.php @@ -0,0 +1,68 @@ + + */ +#[Bench\BeforeMethods('setUp')] +#[Bench\Warmup(1)] +#[Bench\Iterations(10)] +abstract class CodecBench +{ + private Encoder $encoder; + + private Decoder $decoder; + + private object $message; + + /** @var class-string */ + private string $class; + + private string $bytes; + + /** + * @param TParams $params + */ + final public function setUp(array $params): void + { + $this->encoder = Encoder\Builder::buildDefault(); + $this->decoder = Decoder\Builder::buildDefault(); + $this->message = $this->message($params); + $this->class = $this->message::class; + $this->bytes = $this->encoder->encode($this->message); + + if ($this->encoder->encode($this->decoder->decode($this->bytes, $this->class)) !== $this->bytes) { + throw new \LogicException(\sprintf('%s does not round-trip.', $this->class)); + } + + memory_reset_peak_usage(); + } + + final public function benchEncode(): void + { + $this->encoder->encode($this->message); + } + + final public function benchDecode(): void + { + $this->decoder->decode($this->bytes, $this->class); + } + + /** + * @param TParams $params + */ + abstract protected function message(array $params): object; +} diff --git a/benchmarks/Fixture/Blob.php b/benchmarks/Fixture/Blob.php new file mode 100644 index 0000000..a2b9adb --- /dev/null +++ b/benchmarks/Fixture/Blob.php @@ -0,0 +1,15 @@ + $values + */ + public function __construct( + #[Reflection\Field(1, Reflection\Uint32T::T)] + public int $id = 0, + #[Reflection\Field(2, Reflection\StringT::T)] + public string $name = '', + #[Reflection\Field(3, new Reflection\ListT(Reflection\Int64T::T))] + public array $values = [], + ) {} +} diff --git a/benchmarks/Fixture/Items.php b/benchmarks/Fixture/Items.php new file mode 100644 index 0000000..02fbfe1 --- /dev/null +++ b/benchmarks/Fixture/Items.php @@ -0,0 +1,18 @@ + $items + */ + public function __construct( + #[Reflection\Field(1, new Reflection\ListT(new Reflection\ObjectT(Item::class)))] + public array $items = [], + ) {} +} diff --git a/benchmarks/Fixture/PackedInts.php b/benchmarks/Fixture/PackedInts.php new file mode 100644 index 0000000..b58c172 --- /dev/null +++ b/benchmarks/Fixture/PackedInts.php @@ -0,0 +1,18 @@ + $values + */ + public function __construct( + #[Reflection\Field(1, new Reflection\ListT(Reflection\Int64T::T))] + public array $values = [], + ) {} +} diff --git a/benchmarks/Fixture/Small.php b/benchmarks/Fixture/Small.php new file mode 100644 index 0000000..f044455 --- /dev/null +++ b/benchmarks/Fixture/Small.php @@ -0,0 +1,19 @@ + $values + */ + public function __construct( + #[Reflection\Field(1, new Reflection\MapT(Reflection\StringT::T, Reflection\Int32T::T))] + public Map $values = new Map(), + ) {} +} diff --git a/benchmarks/Fixture/Text.php b/benchmarks/Fixture/Text.php new file mode 100644 index 0000000..d83cb9a --- /dev/null +++ b/benchmarks/Fixture/Text.php @@ -0,0 +1,15 @@ + + */ +#[Bench\Revs(1)] +#[Bench\ParamProviders('provideCounts')] +final class MapBench extends CodecBench +{ + /** + * @return iterable + */ + public function provideCounts(): iterable + { + yield 'x10k' => ['count' => 10_000]; + } + + #[\Override] + protected function message(array $params): object + { + /** @var Map $map */ + $map = new Map(); + + for ($i = 0; $i < $params['count']; ++$i) { + $map["key-{$i}"] = $i; + } + + return new StringMap($map); + } +} diff --git a/benchmarks/NestedBench.php b/benchmarks/NestedBench.php new file mode 100644 index 0000000..90fb115 --- /dev/null +++ b/benchmarks/NestedBench.php @@ -0,0 +1,37 @@ + + */ +#[Bench\Revs(5)] +#[Bench\ParamProviders('provideCounts')] +final class NestedBench extends CodecBench +{ + /** + * @return iterable + */ + public function provideCounts(): iterable + { + yield 'x1000' => ['count' => 1000]; + yield 'x4000' => ['count' => 4000]; + } + + #[\Override] + protected function message(array $params): object + { + return new Items(array_map( + static fn(int $i): Item => new Item($i, "item-{$i}", [$i, -$i, $i * 1000]), + range(1, $params['count']), + )); + } +} diff --git a/benchmarks/PackedBench.php b/benchmarks/PackedBench.php new file mode 100644 index 0000000..f4a83e9 --- /dev/null +++ b/benchmarks/PackedBench.php @@ -0,0 +1,36 @@ + + */ +#[Bench\Revs(1)] +#[Bench\ParamProviders('provideCounts')] +final class PackedBench extends CodecBench +{ + /** + * @return iterable + */ + public function provideCounts(): iterable + { + yield 'x10k' => ['count' => 10_000]; + yield 'x100k' => ['count' => 100_000]; + } + + #[\Override] + protected function message(array $params): object + { + return new PackedInts(array_map( + static fn(int $i): int => $i * 7919 - 500_000, + range(1, $params['count']), + )); + } +} diff --git a/benchmarks/SmallMessageBench.php b/benchmarks/SmallMessageBench.php new file mode 100644 index 0000000..5d38de2 --- /dev/null +++ b/benchmarks/SmallMessageBench.php @@ -0,0 +1,21 @@ + + */ +#[Bench\Revs(1000)] +final class SmallMessageBench extends CodecBench +{ + #[\Override] + protected function message(array $params): object + { + return new Small('John Doe', 50, true); + } +} diff --git a/composer-dependency-analyser.php b/composer-dependency-analyser.php index e57cb66..4222251 100644 --- a/composer-dependency-analyser.php +++ b/composer-dependency-analyser.php @@ -7,4 +7,5 @@ $config = new Configuration(); return $config + ->addPathToExclude(__DIR__ . '/benchmarks') ->addPathToExclude(__DIR__ . '/tests'); diff --git a/composer.json b/composer.json index 8aba2a9..4f5b549 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,7 @@ }, "require-dev": { "ext-filter": "*", + "phpbench/phpbench": "^1.7", "phpunit/phpunit": "^12.4.4", "symfony/var-dumper": "^6.4.15 || ^7.3.5" }, @@ -43,7 +44,8 @@ }, "autoload-dev": { "psr-4": { - "Thesis\\Protobuf\\": "tests/" + "Thesis\\Protobuf\\": "tests/", + "Thesis\\Protobuf\\Benchmarks\\": "benchmarks/" } }, "config": { diff --git a/phpbench.json b/phpbench.json new file mode 100644 index 0000000..3a7f48e --- /dev/null +++ b/phpbench.json @@ -0,0 +1,18 @@ +{ + "$schema": "./vendor/phpbench/phpbench/phpbench.schema.json", + "runner.bootstrap": "vendor/autoload.php", + "runner.path": "benchmarks", + "runner.file_pattern": "*Bench.php", + "runner.php_config": { + "xdebug.mode": "off", + "zend.assertions": -1 + }, + "storage.xml_storage_path": "var/phpbench", + "report.generators": { + "protobuf": { + "generator": "expression", + "cols": ["benchmark", "subject", "set", "revs", "its", "mem_peak", "mode", "rstdev"], + "break": [] + } + } +} diff --git a/phpstan.dist.neon b/phpstan.dist.neon index 06e244e..5b057ee 100644 --- a/phpstan.dist.neon +++ b/phpstan.dist.neon @@ -6,10 +6,13 @@ includes: parameters: level: max paths: + - benchmarks - examples - src - tests tmpDir: var/phpstan + additionalConstructors: + - Thesis\Protobuf\Benchmarks\CodecBench::setUp editorUrl: '' editorUrlTitle: '%%relFile%%:%%line%%' diff --git a/rector.php b/rector.php index 85161fb..122d782 100644 --- a/rector.php +++ b/rector.php @@ -9,6 +9,7 @@ return RectorConfig::configure() ->withPaths([ + __DIR__ . '/benchmarks', __DIR__ . '/src', __DIR__ . '/tests', ]) diff --git a/src/Reflection/Reflector.php b/src/Reflection/Reflector.php index 5e8d8da..0711541 100644 --- a/src/Reflection/Reflector.php +++ b/src/Reflection/Reflector.php @@ -25,7 +25,7 @@ final class Reflector { private readonly ToProtobufTypeTypeVisitor $typeVisitor; - /** @var ToProtobufValueTypeVisitor<*> */ + /** @var ToProtobufValueTypeVisitor */ private readonly ToProtobufValueTypeVisitor $valueVisitor; private readonly ClassReflector $classReflector; diff --git a/src/Type/Visitor/TypeSerializerVisitor.php b/src/Type/Visitor/TypeSerializerVisitor.php index 6368d1c..62a45ad 100644 --- a/src/Type/Visitor/TypeSerializerVisitor.php +++ b/src/Type/Visitor/TypeSerializerVisitor.php @@ -148,7 +148,9 @@ public function string(StringT $type): SerializeValue #[\Override] public function list(ListT $type): SerializeValue { + // The visitor erases element types: SerializeValue is invariant, so phpstan cannot relate them. return $this->aggregated( + /** @phpstan-ignore argument.type */ new SerializeList( $type ->element @@ -168,8 +170,11 @@ public function map(MapT $type): SerializeValue fieldT(2, $type->valueT), ); + // The visitor erases element types: SerializeValue is invariant, so phpstan cannot relate them. return $this->aggregated( + /** @phpstan-ignore argument.type */ new SerializeMap( + /** @phpstan-ignore argument.type */ new SerializeList( $messageT ->accept($this)