|
| 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 | +} |
0 commit comments