Skip to content

Commit e9e39b1

Browse files
Anton Shaboutazloyuser
authored andcommitted
Benchmarks refactored
1 parent 3501298 commit e9e39b1

File tree

8 files changed

+114
-57
lines changed

8 files changed

+114
-57
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ We accept contributions via Pull Requests on [Github](https://github.com/phpinna
77

88
## Pull Requests
99

10-
- **[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``.
10+
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)**
1111

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

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ $ composer test
4646
$ composer bench
4747
```
4848

49+
Some results with pure PHP realisation:
50+
51+
| Benchmark | Best (μs) | Mean (μs) | Mode (μs) | Worst (μs) |
52+
|---|---|---|---|---|
53+
| appendIntegers | 11.605 | 12.115 | 12.047 | 12.888 |
54+
| appendFloats | 10.464 | 10.913 | 10.786 | 17.943 |
55+
| appendString | 8.857 | 41.021 | 20.611 | 362.174 |
56+
| consume | 48.916 | 50.721 | 50.399 | 61.542 |
57+
| read | 26.617 | 27.665 | 27.500 | 31.744 |
58+
59+
And results with enabled [extension][link-extension]:
60+
61+
| Benchmark | Best (μs) | Mean (μs) | Mode (μs) | Worst (μs) |
62+
|---|---|---|---|---|
63+
| appendIntegers | 2.522 | 2.657 | 2.625 | 3.031 |
64+
| appendFloats | 1.987 | 2.136 | 2.095 | 3.307 |
65+
| appendString | 3.692 | 3.854 | 3.806 | 5.695 |
66+
| consume | 13.701 | 14.654 | 14.454 | 17.977 |
67+
| read | 5.128 | 5.425 | 5.313 | 6.625 |
68+
4969
## Contributing
5070

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

91+
[link-extension]: https://github.com/phpinnacle/ext-buffer
7192
[link-packagist]: https://packagist.org/packages/phpinnacle/buffer
7293
[link-scrutinizer]: https://scrutinizer-ci.com/g/phpinnacle/buffer/code-structure
7394
[link-downloads]: https://packagist.org/packages/phpinnacle/buffer

benchmarks/BufferWriteBench.php renamed to benchmarks/AppendBench.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,30 @@
44

55
/**
66
* @BeforeMethods({"init"})
7-
* @AfterMethods({"clear"})
87
*/
9-
class BufferWriteBench
8+
class AppendBench
109
{
1110
/**
1211
* @var ByteBuffer
1312
*/
1413
private $buffer;
1514

15+
/**
16+
* @var string
17+
*/
18+
private $string;
19+
1620
/**
1721
* @return void
1822
*/
1923
public function init(): void
2024
{
21-
$this->buffer = new ByteBuffer();
25+
$this->buffer = new ByteBuffer;
26+
$this->string = \str_repeat('str', 1000);
2227
}
2328

2429
/**
25-
* @Revs(5)
30+
* @Revs(1000)
2631
* @Iterations(100)
2732
*
2833
* @return void
@@ -42,7 +47,7 @@ public function benchAppendIntegers(): void
4247
}
4348

4449
/**
45-
* @Revs(5)
50+
* @Revs(1000)
4651
* @Iterations(100)
4752
*
4853
* @return void
@@ -60,7 +65,7 @@ public function benchAppendFloats(): void
6065
}
6166

6267
/**
63-
* @Revs(5)
68+
* @Revs(1000)
6469
* @Iterations(100)
6570
*
6671
* @return void
@@ -70,15 +75,7 @@ public function benchAppendString(): void
7075
$this->buffer
7176
->append('some string')
7277
->append("other string")
73-
->append(str_repeat('str', 1000))
78+
->append($this->string)
7479
;
7580
}
76-
77-
/**
78-
* @return void
79-
*/
80-
public function clear(): void
81-
{
82-
$this->buffer->flush();
83-
}
8481
}

benchmarks/ConsumeBench.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
use PHPinnacle\Buffer\ByteBuffer;
4+
5+
/**
6+
* @BeforeMethods({"init"})
7+
*/
8+
class ConsumeBench
9+
{
10+
/**
11+
* @var ByteBuffer
12+
*/
13+
private $buffer;
14+
15+
/**
16+
* @var int
17+
*/
18+
private $revs = 1000;
19+
20+
/**
21+
* @return void
22+
*/
23+
public function init(): void
24+
{
25+
$this->buffer = new ByteBuffer;
26+
27+
for ($i = 0; $i < $this->revs; ++$i) {
28+
$this->buffer
29+
->appendInt8(1)
30+
->appendInt16(1)
31+
->appendInt32(1)
32+
->appendInt64(1)
33+
->appendUint8(1)
34+
->appendUint16(1)
35+
->appendUint32(1)
36+
->appendUint64(1)
37+
->appendFloat(1.1)
38+
->appendFloat(-1.1)
39+
->appendFloat(\M_PI)
40+
->appendDouble(1.1)
41+
->appendDouble(-1.1)
42+
->appendDouble(\M_PI)
43+
->append('some string')
44+
->append("other string")
45+
;
46+
}
47+
}
48+
49+
/**
50+
* @Revs(1000)
51+
* @Iterations(100)
52+
*
53+
* @return void
54+
*/
55+
public function benchConsume(): void
56+
{
57+
$this->buffer->consumeInt8();
58+
$this->buffer->consumeInt16();
59+
$this->buffer->consumeInt32();
60+
$this->buffer->consumeInt64();
61+
$this->buffer->consumeUint8();
62+
$this->buffer->consumeUint16();
63+
$this->buffer->consumeUint32();
64+
$this->buffer->consumeUint64();
65+
$this->buffer->consumeFloat();
66+
$this->buffer->consumeFloat();
67+
$this->buffer->consumeFloat();
68+
$this->buffer->consumeDouble();
69+
$this->buffer->consumeDouble();
70+
$this->buffer->consumeDouble();
71+
$this->buffer->consume(11);
72+
$this->buffer->consume(12);
73+
}
74+
}

benchmarks/BufferReadBench.php renamed to benchmarks/ReadBench.php

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
/**
66
* @BeforeMethods({"init"})
7-
* @AfterMethods({"clear"})
87
*/
9-
class BufferReadBench
8+
class ReadBench
109
{
1110
/**
1211
* @var ByteBuffer
@@ -18,7 +17,7 @@ class BufferReadBench
1817
*/
1918
public function init(): void
2019
{
21-
$this->buffer = new ByteBuffer();
20+
$this->buffer = new ByteBuffer;
2221
$this->buffer
2322
->appendInt8(1)
2423
->appendInt16(1)
@@ -40,33 +39,7 @@ public function init(): void
4039
}
4140

4241
/**
43-
* @Revs(1)
44-
* @Iterations(100)
45-
*
46-
* @return void
47-
*/
48-
public function benchConsume(): void
49-
{
50-
$this->buffer->consumeInt8();
51-
$this->buffer->consumeInt16();
52-
$this->buffer->consumeInt32();
53-
$this->buffer->consumeInt64();
54-
$this->buffer->consumeUint8();
55-
$this->buffer->consumeUint16();
56-
$this->buffer->consumeUint32();
57-
$this->buffer->consumeUint64();
58-
$this->buffer->consumeFloat();
59-
$this->buffer->consumeFloat();
60-
$this->buffer->consumeFloat();
61-
$this->buffer->consumeDouble();
62-
$this->buffer->consumeDouble();
63-
$this->buffer->consumeDouble();
64-
$this->buffer->consume(11);
65-
$this->buffer->consume(12);
66-
}
67-
68-
/**
69-
* @Revs(1)
42+
* @Revs(1000)
7043
* @Iterations(100)
7144
*
7245
* @return void
@@ -90,12 +63,4 @@ public function benchRead(): void
9063
$this->buffer->read(11, 66);
9164
$this->buffer->read(12, 77);
9265
}
93-
94-
/**
95-
* @return void
96-
*/
97-
public function clear(): void
98-
{
99-
$this->buffer->flush();
100-
}
10166
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": "^7.1"
2020
},
2121
"require-dev": {
22-
"phpbench/phpbench": "^0.16.9",
22+
"phpbench/phpbench": "^0.16",
2323
"phpunit/phpunit": "^8.0"
2424
},
2525
"suggest": {

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ByteBuffer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public function shift(int $n): self
165165

166166
if ($this->size === $n) {
167167
$buffer = $this->data;
168-
168+
169169
$this->data = '';
170170
$this->size = 0;
171171
} else {

0 commit comments

Comments
 (0)