Skip to content

Commit 9d4da7a

Browse files
committed
Add phpbench
Signed-off-by: William Desportes <williamdes@wdes.fr>
1 parent c8a83aa commit 9d4da7a

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"symfony/polyfill-php80": "^1.16"
2828
},
2929
"require-dev": {
30+
"phpbench/phpbench": "^1.1",
3031
"phpmyadmin/coding-standard": "^3.0",
3132
"phpmyadmin/motranslator": "^4.0 || ^5.0",
3233
"phpstan/extension-installer": "^1.1",
@@ -66,6 +67,7 @@
6667
"phpstan": "@php phpstan analyse",
6768
"psalm": "@php psalm --no-diff",
6869
"phpunit": "@php phpunit --color=always",
70+
"phpbench": "@php phpbench run tests/benchmarks --report=aggregate --progress=dots",
6971
"test": [
7072
"@phpcs",
7173
"@phpstan",

phpbench.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "./vendor/phpbench/phpbench/phpbench.schema.json",
3+
"runner.bootstrap": "vendor/autoload.php"
4+
}

tests/benchmarks/UtfStringBench.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\SqlParser\Tests\benchmarks;
6+
7+
use PhpMyAdmin\SqlParser\UtfString;
8+
9+
use function chr;
10+
use function file_get_contents;
11+
12+
class UtfStringBench
13+
{
14+
/** @var string */
15+
private $testContents;
16+
17+
/**
18+
* @BeforeMethods("setUp")
19+
* @Iterations(20)
20+
* @Revs(4)
21+
* @OutputTimeUnit("milliseconds")
22+
* @Assert("mode(variant.time.avg) < 38 milliseconds +/- 10%")
23+
* @Assert("mode(variant.time.avg) > 20 milliseconds +/- 10%")
24+
*/
25+
public function benchBuildUtfString(): void
26+
{
27+
$str1 = new UtfString($this->testContents);
28+
for ($i = 0; $i < $str1->length(); $i++) {
29+
$str1[$i];// Make offset offsetGet work
30+
}
31+
}
32+
33+
/**
34+
* @BeforeMethods("setUp")
35+
* @Iterations(2)
36+
* @Revs(2)
37+
* @OutputTimeUnit("microseconds")
38+
* @Assert("mode(variant.time.avg) < 75 microseconds +/- 10%")
39+
* @Assert("mode(variant.time.avg) > 60 microseconds +/- 10%")
40+
*/
41+
public function benchGetCharLength(): void
42+
{
43+
UtfString::getCharLength(chr(0x00)); // 00000000
44+
UtfString::getCharLength(chr(0x7F)); // 01111111
45+
46+
UtfString::getCharLength(chr(0xC0)); // 11000000
47+
UtfString::getCharLength(chr(0xDF)); // 11011111
48+
49+
UtfString::getCharLength(chr(0xE0)); // 11100000
50+
UtfString::getCharLength(chr(0xEF)); // 11101111
51+
52+
UtfString::getCharLength(chr(0xF0)); // 11110000
53+
UtfString::getCharLength(chr(0xF7)); // 11110111
54+
55+
UtfString::getCharLength(chr(0xF8)); // 11111000
56+
UtfString::getCharLength(chr(0xFB)); // 11111011
57+
58+
UtfString::getCharLength(chr(0xFC)); // 11111100
59+
UtfString::getCharLength(chr(0xFD)); // 11111101
60+
}
61+
62+
public function setUp(): void
63+
{
64+
$contentsPath = __DIR__ . '/../../LICENSE.txt';
65+
$this->testContents = (string) file_get_contents($contentsPath);
66+
}
67+
}

0 commit comments

Comments
 (0)