Skip to content

Commit 3501298

Browse files
authored
PHPBench integration (#2)
1 parent 1cc25ad commit 3501298

File tree

6 files changed

+1347
-27
lines changed

6 files changed

+1347
-27
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
[![Latest Version on Packagist][ico-version]][link-packagist]
44
[![Software License][ico-license]](LICENSE.md)
55
[![Coverage Status][ico-scrutinizer]][link-scrutinizer]
6-
[![Quality Score][ico-code-quality]][link-code-quality]
76
[![Total Downloads][ico-downloads]][link-downloads]
87

98
PHPinnacle Buffer is a simple tool for operating binary data in PHP. Mostly it simply wraps PHP pack/unpack functions.
109

11-
Code is mostly based on [bunnyphp](https://github.com/jakubkulhan/bunny), Buffer class implemenation.
12-
1310
## Install
1411

1512
Via Composer
@@ -23,10 +20,10 @@ $ composer require phpinnacle/buffer
2320
```php
2421
<?php
2522

26-
use PHPinnacle\Buffer\ByteBuffer as Buffer;
23+
use PHPinnacle\Buffer\ByteBuffer;
2724

2825
// AMQP protocol header
29-
$buffer = new Buffer;
26+
$buffer = new ByteBuffer;
3027
$buffer
3128
->append('AMQP')
3229
->appendUint8(0)
@@ -43,6 +40,12 @@ $buffer
4340
$ composer test
4441
```
4542

43+
## Benchmarks
44+
45+
```bash
46+
$ composer bench
47+
```
48+
4649
## Contributing
4750

4851
Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details.
@@ -63,12 +66,10 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
6366
[ico-version]: https://img.shields.io/packagist/v/phpinnacle/buffer.svg?style=flat-square
6467
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
6568
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/phpinnacle/buffer.svg?style=flat-square
66-
[ico-code-quality]: https://img.shields.io/scrutinizer/g/phpinnacle/buffer.svg?style=flat-square
6769
[ico-downloads]: https://img.shields.io/packagist/dt/phpinnacle/buffer.svg?style=flat-square
6870

6971
[link-packagist]: https://packagist.org/packages/phpinnacle/buffer
7072
[link-scrutinizer]: https://scrutinizer-ci.com/g/phpinnacle/buffer/code-structure
71-
[link-code-quality]: https://scrutinizer-ci.com/g/phpinnacle/buffer
7273
[link-downloads]: https://packagist.org/packages/phpinnacle/buffer
7374
[link-author]: https://github.com/phpinnacle
7475
[link-contributors]: https://github.com/phpinnacle/buffer/graphs/contributors

benchmarks/BufferReadBench.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
use PHPinnacle\Buffer\ByteBuffer;
4+
5+
/**
6+
* @BeforeMethods({"init"})
7+
* @AfterMethods({"clear"})
8+
*/
9+
class BufferReadBench
10+
{
11+
/**
12+
* @var ByteBuffer
13+
*/
14+
private $buffer;
15+
16+
/**
17+
* @return void
18+
*/
19+
public function init(): void
20+
{
21+
$this->buffer = new ByteBuffer();
22+
$this->buffer
23+
->appendInt8(1)
24+
->appendInt16(1)
25+
->appendInt32(1)
26+
->appendInt64(1)
27+
->appendUint8(1)
28+
->appendUint16(1)
29+
->appendUint32(1)
30+
->appendUint64(1)
31+
->appendFloat(1.1)
32+
->appendFloat(-1.1)
33+
->appendFloat(\M_PI)
34+
->appendDouble(1.1)
35+
->appendDouble(-1.1)
36+
->appendDouble(\M_PI)
37+
->append('some string')
38+
->append("other string")
39+
;
40+
}
41+
42+
/**
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)
70+
* @Iterations(100)
71+
*
72+
* @return void
73+
*/
74+
public function benchRead(): void
75+
{
76+
$this->buffer->readInt8();
77+
$this->buffer->readInt16(1);
78+
$this->buffer->readInt32(3);
79+
$this->buffer->readInt64(7);
80+
$this->buffer->readUint8(15);
81+
$this->buffer->readUint16(16);
82+
$this->buffer->readUint32(18);
83+
$this->buffer->readUint64(22);
84+
$this->buffer->readFloat(30);
85+
$this->buffer->readFloat(34);
86+
$this->buffer->readFloat(38);
87+
$this->buffer->readDouble(42);
88+
$this->buffer->readDouble(50);
89+
$this->buffer->readDouble(58);
90+
$this->buffer->read(11, 66);
91+
$this->buffer->read(12, 77);
92+
}
93+
94+
/**
95+
* @return void
96+
*/
97+
public function clear(): void
98+
{
99+
$this->buffer->flush();
100+
}
101+
}

benchmarks/BufferWriteBench.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
use PHPinnacle\Buffer\ByteBuffer;
4+
5+
/**
6+
* @BeforeMethods({"init"})
7+
* @AfterMethods({"clear"})
8+
*/
9+
class BufferWriteBench
10+
{
11+
/**
12+
* @var ByteBuffer
13+
*/
14+
private $buffer;
15+
16+
/**
17+
* @return void
18+
*/
19+
public function init(): void
20+
{
21+
$this->buffer = new ByteBuffer();
22+
}
23+
24+
/**
25+
* @Revs(5)
26+
* @Iterations(100)
27+
*
28+
* @return void
29+
*/
30+
public function benchAppendIntegers(): void
31+
{
32+
$this->buffer
33+
->appendInt8(1)
34+
->appendInt16(1)
35+
->appendInt32(1)
36+
->appendInt64(1)
37+
->appendUint8(1)
38+
->appendUint16(1)
39+
->appendUint32(1)
40+
->appendUint64(1)
41+
;
42+
}
43+
44+
/**
45+
* @Revs(5)
46+
* @Iterations(100)
47+
*
48+
* @return void
49+
*/
50+
public function benchAppendFloats(): void
51+
{
52+
$this->buffer
53+
->appendFloat(1.0)
54+
->appendFloat(-1.0)
55+
->appendFloat(\M_PI)
56+
->appendDouble(1.0)
57+
->appendDouble(-1.0)
58+
->appendDouble(\M_PI)
59+
;
60+
}
61+
62+
/**
63+
* @Revs(5)
64+
* @Iterations(100)
65+
*
66+
* @return void
67+
*/
68+
public function benchAppendString(): void
69+
{
70+
$this->buffer
71+
->append('some string')
72+
->append("other string")
73+
->append(str_repeat('str', 1000))
74+
;
75+
}
76+
77+
/**
78+
* @return void
79+
*/
80+
public function clear(): void
81+
{
82+
$this->buffer->flush();
83+
}
84+
}

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"php": "^7.1"
2020
},
2121
"require-dev": {
22+
"phpbench/phpbench": "^0.16.9",
2223
"phpunit/phpunit": "^8.0"
2324
},
2425
"suggest": {
@@ -35,7 +36,8 @@
3536
}
3637
},
3738
"scripts": {
38-
"test": "phpunit"
39+
"test": "phpunit",
40+
"bench": "phpbench run benchmarks --report=aggregate"
3941
},
4042
"extra": {
4143
"branch-alias": {

0 commit comments

Comments
 (0)