Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.github/ export-ignore
/benchmarks/ export-ignore
/docker/ export-ignore
/examples/ export-ignore
/stubs/ export-ignore
Expand All @@ -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
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
$config = new Config()
->setFinder(
Finder::create()
->in(__DIR__ . '/benchmarks')
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
->append([
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions benchmarks/BlobBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Thesis\Protobuf\Benchmarks;

use PhpBench\Attributes as Bench;
use Thesis\Protobuf\Benchmarks\Fixture\Blob;
use Thesis\Protobuf\Benchmarks\Fixture\Text;

/**
* 1 MB of bytes and 1 MB of valid non-ASCII UTF-8 text: identical today, they part ways once strings are validated.
*
* @extends CodecBench<array{kind: 'bytes'|'string'}>
*/
#[Bench\Revs(1000)]
#[Bench\ParamProviders('provideKinds')]
final class BlobBench extends CodecBench
{
/**
* @return iterable<string, array{kind: 'bytes'|'string'}>
*/
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)),
};
}
}
68 changes: 68 additions & 0 deletions benchmarks/CodecBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Thesis\Protobuf\Benchmarks;

use PhpBench\Attributes as Bench;
use Thesis\Protobuf\Decoder;
use Thesis\Protobuf\Encoder;

/**
* Encode and decode of one message built from the benchmark parameters.
*
* Every iteration runs in a separate process, so mem_peak is the peak of that process. The setup resets it
* after building the message, so the reported peak is the memory in use by the fixture plus the peak
* reached by the measured operation.
*
* @template TParams of array<string, scalar>
*/
#[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;
}
15 changes: 15 additions & 0 deletions benchmarks/Fixture/Blob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Thesis\Protobuf\Benchmarks\Fixture;

use Thesis\Protobuf\Reflection;

final readonly class Blob
{
public function __construct(
#[Reflection\Field(1, Reflection\BytesT::T)]
public string $data = '',
) {}
}
22 changes: 22 additions & 0 deletions benchmarks/Fixture/Item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Thesis\Protobuf\Benchmarks\Fixture;

use Thesis\Protobuf\Reflection;

final readonly class Item
{
/**
* @param list<int> $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 = [],
) {}
}
18 changes: 18 additions & 0 deletions benchmarks/Fixture/Items.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Thesis\Protobuf\Benchmarks\Fixture;

use Thesis\Protobuf\Reflection;

final readonly class Items
{
/**
* @param list<Item> $items
*/
public function __construct(
#[Reflection\Field(1, new Reflection\ListT(new Reflection\ObjectT(Item::class)))]
public array $items = [],
) {}
}
18 changes: 18 additions & 0 deletions benchmarks/Fixture/PackedInts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Thesis\Protobuf\Benchmarks\Fixture;

use Thesis\Protobuf\Reflection;

final readonly class PackedInts
{
/**
* @param list<int> $values
*/
public function __construct(
#[Reflection\Field(1, new Reflection\ListT(Reflection\Int64T::T))]
public array $values = [],
) {}
}
19 changes: 19 additions & 0 deletions benchmarks/Fixture/Small.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Thesis\Protobuf\Benchmarks\Fixture;

use Thesis\Protobuf\Reflection;

final readonly class Small
{
public function __construct(
#[Reflection\Field(1, Reflection\StringT::T)]
public string $name = '',
#[Reflection\Field(2, Reflection\Int32T::T)]
public int $id = 0,
#[Reflection\Field(3, Reflection\BoolT::T)]
public bool $active = false,
) {}
}
19 changes: 19 additions & 0 deletions benchmarks/Fixture/StringMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Thesis\Protobuf\Benchmarks\Fixture;

use Thesis\Protobuf\Map;
use Thesis\Protobuf\Reflection;

final readonly class StringMap
{
/**
* @param Map<string, int> $values
*/
public function __construct(
#[Reflection\Field(1, new Reflection\MapT(Reflection\StringT::T, Reflection\Int32T::T))]
public Map $values = new Map(),
) {}
}
15 changes: 15 additions & 0 deletions benchmarks/Fixture/Text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Thesis\Protobuf\Benchmarks\Fixture;

use Thesis\Protobuf\Reflection;

final readonly class Text
{
public function __construct(
#[Reflection\Field(1, Reflection\StringT::T)]
public string $text = '',
) {}
}
38 changes: 38 additions & 0 deletions benchmarks/MapBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Thesis\Protobuf\Benchmarks;

use PhpBench\Attributes as Bench;
use Thesis\Protobuf\Benchmarks\Fixture\StringMap;
use Thesis\Protobuf\Map;

/**
* @extends CodecBench<array{count: positive-int}>
*/
#[Bench\Revs(1)]
#[Bench\ParamProviders('provideCounts')]
final class MapBench extends CodecBench
{
/**
* @return iterable<string, array{count: positive-int}>
*/
public function provideCounts(): iterable
{
yield 'x10k' => ['count' => 10_000];
}

#[\Override]
protected function message(array $params): object
{
/** @var Map<string, int> $map */
$map = new Map();

for ($i = 0; $i < $params['count']; ++$i) {
$map["key-{$i}"] = $i;
}

return new StringMap($map);
}
}
37 changes: 37 additions & 0 deletions benchmarks/NestedBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Thesis\Protobuf\Benchmarks;

use PhpBench\Attributes as Bench;
use Thesis\Protobuf\Benchmarks\Fixture\Item;
use Thesis\Protobuf\Benchmarks\Fixture\Items;

/**
* A list of nested messages: two sizes to show how the cost grows with the input.
*
* @extends CodecBench<array{count: positive-int}>
*/
#[Bench\Revs(5)]
#[Bench\ParamProviders('provideCounts')]
final class NestedBench extends CodecBench
{
/**
* @return iterable<string, array{count: positive-int}>
*/
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']),
));
}
}
36 changes: 36 additions & 0 deletions benchmarks/PackedBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Thesis\Protobuf\Benchmarks;

use PhpBench\Attributes as Bench;
use Thesis\Protobuf\Benchmarks\Fixture\PackedInts;

/**
* A packed repeated int64: two sizes to show how the cost grows with the input.
*
* @extends CodecBench<array{count: positive-int}>
*/
#[Bench\Revs(1)]
#[Bench\ParamProviders('provideCounts')]
final class PackedBench extends CodecBench
{
/**
* @return iterable<string, array{count: positive-int}>
*/
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']),
));
}
}
Loading
Loading