Skip to content

Commit

Permalink
Benchmarks refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Shabouta authored and zloyuser committed Mar 21, 2019
1 parent 3501298 commit e9e39b1
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 57 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ We accept contributions via Pull Requests on [Github](https://github.com/phpinna

## Pull Requests

- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - Check the code style with ``$ composer check-style`` and fix it with ``$ composer fix-style``.
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)**

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ $ composer test
$ composer bench
```

Some results with pure PHP realisation:

| Benchmark | Best (μs) | Mean (μs) | Mode (μs) | Worst (μs) |
|---|---|---|---|---|
| appendIntegers | 11.605 | 12.115 | 12.047 | 12.888 |
| appendFloats | 10.464 | 10.913 | 10.786 | 17.943 |
| appendString | 8.857 | 41.021 | 20.611 | 362.174 |
| consume | 48.916 | 50.721 | 50.399 | 61.542 |
| read | 26.617 | 27.665 | 27.500 | 31.744 |

And results with enabled [extension][link-extension]:

| Benchmark | Best (μs) | Mean (μs) | Mode (μs) | Worst (μs) |
|---|---|---|---|---|
| appendIntegers | 2.522 | 2.657 | 2.625 | 3.031 |
| appendFloats | 1.987 | 2.136 | 2.095 | 3.307 |
| appendString | 3.692 | 3.854 | 3.806 | 5.695 |
| consume | 13.701 | 14.654 | 14.454 | 17.977 |
| read | 5.128 | 5.425 | 5.313 | 6.625 |

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details.
Expand All @@ -68,6 +88,7 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/phpinnacle/buffer.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/phpinnacle/buffer.svg?style=flat-square

[link-extension]: https://github.com/phpinnacle/ext-buffer
[link-packagist]: https://packagist.org/packages/phpinnacle/buffer
[link-scrutinizer]: https://scrutinizer-ci.com/g/phpinnacle/buffer/code-structure
[link-downloads]: https://packagist.org/packages/phpinnacle/buffer
Expand Down
27 changes: 12 additions & 15 deletions benchmarks/BufferWriteBench.php → benchmarks/AppendBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,30 @@

/**
* @BeforeMethods({"init"})
* @AfterMethods({"clear"})
*/
class BufferWriteBench
class AppendBench
{
/**
* @var ByteBuffer
*/
private $buffer;

/**
* @var string
*/
private $string;

/**
* @return void
*/
public function init(): void
{
$this->buffer = new ByteBuffer();
$this->buffer = new ByteBuffer;
$this->string = \str_repeat('str', 1000);
}

/**
* @Revs(5)
* @Revs(1000)
* @Iterations(100)
*
* @return void
Expand All @@ -42,7 +47,7 @@ public function benchAppendIntegers(): void
}

/**
* @Revs(5)
* @Revs(1000)
* @Iterations(100)
*
* @return void
Expand All @@ -60,7 +65,7 @@ public function benchAppendFloats(): void
}

/**
* @Revs(5)
* @Revs(1000)
* @Iterations(100)
*
* @return void
Expand All @@ -70,15 +75,7 @@ public function benchAppendString(): void
$this->buffer
->append('some string')
->append("other string")
->append(str_repeat('str', 1000))
->append($this->string)
;
}

/**
* @return void
*/
public function clear(): void
{
$this->buffer->flush();
}
}
74 changes: 74 additions & 0 deletions benchmarks/ConsumeBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

use PHPinnacle\Buffer\ByteBuffer;

/**
* @BeforeMethods({"init"})
*/
class ConsumeBench
{
/**
* @var ByteBuffer
*/
private $buffer;

/**
* @var int
*/
private $revs = 1000;

/**
* @return void
*/
public function init(): void
{
$this->buffer = new ByteBuffer;

for ($i = 0; $i < $this->revs; ++$i) {
$this->buffer
->appendInt8(1)
->appendInt16(1)
->appendInt32(1)
->appendInt64(1)
->appendUint8(1)
->appendUint16(1)
->appendUint32(1)
->appendUint64(1)
->appendFloat(1.1)
->appendFloat(-1.1)
->appendFloat(\M_PI)
->appendDouble(1.1)
->appendDouble(-1.1)
->appendDouble(\M_PI)
->append('some string')
->append("other string")
;
}
}

/**
* @Revs(1000)
* @Iterations(100)
*
* @return void
*/
public function benchConsume(): void
{
$this->buffer->consumeInt8();
$this->buffer->consumeInt16();
$this->buffer->consumeInt32();
$this->buffer->consumeInt64();
$this->buffer->consumeUint8();
$this->buffer->consumeUint16();
$this->buffer->consumeUint32();
$this->buffer->consumeUint64();
$this->buffer->consumeFloat();
$this->buffer->consumeFloat();
$this->buffer->consumeFloat();
$this->buffer->consumeDouble();
$this->buffer->consumeDouble();
$this->buffer->consumeDouble();
$this->buffer->consume(11);
$this->buffer->consume(12);
}
}
41 changes: 3 additions & 38 deletions benchmarks/BufferReadBench.php → benchmarks/ReadBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

/**
* @BeforeMethods({"init"})
* @AfterMethods({"clear"})
*/
class BufferReadBench
class ReadBench
{
/**
* @var ByteBuffer
Expand All @@ -18,7 +17,7 @@ class BufferReadBench
*/
public function init(): void
{
$this->buffer = new ByteBuffer();
$this->buffer = new ByteBuffer;
$this->buffer
->appendInt8(1)
->appendInt16(1)
Expand All @@ -40,33 +39,7 @@ public function init(): void
}

/**
* @Revs(1)
* @Iterations(100)
*
* @return void
*/
public function benchConsume(): void
{
$this->buffer->consumeInt8();
$this->buffer->consumeInt16();
$this->buffer->consumeInt32();
$this->buffer->consumeInt64();
$this->buffer->consumeUint8();
$this->buffer->consumeUint16();
$this->buffer->consumeUint32();
$this->buffer->consumeUint64();
$this->buffer->consumeFloat();
$this->buffer->consumeFloat();
$this->buffer->consumeFloat();
$this->buffer->consumeDouble();
$this->buffer->consumeDouble();
$this->buffer->consumeDouble();
$this->buffer->consume(11);
$this->buffer->consume(12);
}

/**
* @Revs(1)
* @Revs(1000)
* @Iterations(100)
*
* @return void
Expand All @@ -90,12 +63,4 @@ public function benchRead(): void
$this->buffer->read(11, 66);
$this->buffer->read(12, 77);
}

/**
* @return void
*/
public function clear(): void
{
$this->buffer->flush();
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"php": "^7.1"
},
"require-dev": {
"phpbench/phpbench": "^0.16.9",
"phpbench/phpbench": "^0.16",
"phpunit/phpunit": "^8.0"
},
"suggest": {
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/ByteBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public function shift(int $n): self

if ($this->size === $n) {
$buffer = $this->data;

$this->data = '';
$this->size = 0;
} else {
Expand Down

0 comments on commit e9e39b1

Please sign in to comment.