Skip to content

Commit 2519edf

Browse files
committed
- added phpbench tests
1 parent 1e0dfb4 commit 2519edf

8 files changed

Lines changed: 232 additions & 0 deletions

File tree

Tests/PhpBench/AbstractBench.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Koded\Logging\Tests\PhpBench;
4+
5+
use Koded\Logging\Log;
6+
7+
/**
8+
* @BeforeMethods({"setUp"})
9+
* @AfterMethods({"tearDown"})
10+
* @OutputTimeUnit("milliseconds")
11+
*/
12+
abstract class AbstractBench
13+
{
14+
/**
15+
* @var Log
16+
*/
17+
protected $log;
18+
19+
public function setUp(): void
20+
{
21+
$this->log = new Log($this->getConfig());
22+
}
23+
24+
public function tearDown(): void
25+
{
26+
$this->log = null;
27+
}
28+
29+
abstract protected function getConfig(): array;
30+
31+
protected function message(): array
32+
{
33+
$messages = [
34+
['One morning, when {1} woke from troubled dreams, he found himself {2} in his bed into a {3}.', ['1' => 'Gregor Samsa', '2' => 'transformed', '3' => 'horrible vermin']],
35+
['He lay on his {back}, and if he lifted his head a little he could see his brown belly, slightly {state} by arches into stiff sections.', ['back' => 'armour-like back', 'state' => 'domed and divided']],
36+
['The bedding was hardly able to cover it and seemed ready to slide off any moment.', ['' => '', '' => '', '' => '']],
37+
['His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked.', ['' => '', '' => '', '' => '']],
38+
['"What\'s happened to {0}?" he thought. It wasn\'t a {1}.', ['me', 'dream']],
39+
['His room, a proper {room} although a little too small, lay peacefully {position} familiar walls.', ['room' => 'human room', 'position' => 'between its four']],
40+
['A collection of textile samples lay spread out on the table - {name} was a travelling salesman - and above it there hung a picture that he had recently cut out of an {object} and housed in a nice, gilded frame.', ['name' => 'Samsa', 'object' => 'illustrated magazine']],
41+
['It showed a {someone} who sat upright, raising a {something} that covered {somewhere} towards the viewer. ', ['someone' => 'lady fitted out with a fur hat and fur boa', 'something' => 'heavy fur muff', 'somewhere' => 'the whole of her lower arm']],
42+
];
43+
44+
return $messages[rand(0, 7)];
45+
}
46+
}

Tests/PhpBench/CliBench.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Koded\Logging\Tests\PhpBench;
4+
5+
use Koded\Logging\Processors\Cli;
6+
7+
class CliBench extends AbstractBench
8+
{
9+
/**
10+
* @Revs(1000)
11+
* @Iterations(3)
12+
*/
13+
public function benchCli()
14+
{
15+
$this->log->debug(...$this->message());
16+
$this->log->info(...$this->message());
17+
$this->log->notice(...$this->message());
18+
$this->log->warning(...$this->message());
19+
$this->log->error(...$this->message());
20+
$this->log->critical(...$this->message());
21+
$this->log->alert(...$this->message());
22+
$this->log->emergency(...$this->message());
23+
}
24+
25+
protected function getConfig(): array
26+
{
27+
return [
28+
'deferred' => false,
29+
'loggers' => [
30+
['class' => Cli::class],
31+
]
32+
];
33+
}
34+
}

Tests/PhpBench/ErrorLogBench.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Koded\Logging\Tests\PhpBench;
4+
5+
use Koded\Logging\Processors\ErrorLog;
6+
7+
class ErrorLogBench extends AbstractBench
8+
{
9+
/**
10+
* @Revs(1000)
11+
* @Iterations(3)
12+
*/
13+
public function benchErrorLog()
14+
{
15+
$this->log->debug(...$this->message());
16+
$this->log->info(...$this->message());
17+
$this->log->notice(...$this->message());
18+
$this->log->warning(...$this->message());
19+
$this->log->error(...$this->message());
20+
$this->log->critical(...$this->message());
21+
$this->log->alert(...$this->message());
22+
$this->log->emergency(...$this->message());
23+
}
24+
25+
protected function getConfig(): array
26+
{
27+
return [
28+
'deferred' => false,
29+
'loggers' => [
30+
['class' => ErrorLog::class],
31+
]
32+
];
33+
}
34+
}

Tests/PhpBench/FileBench.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Koded\Logging\Tests\PhpBench;
4+
5+
use Koded\Logging\Processors\File;
6+
7+
class FileBench extends AbstractBench
8+
{
9+
/**
10+
* @Revs(1000)
11+
* @Iterations(3)
12+
*/
13+
public function benchFile()
14+
{
15+
$this->log->debug(...$this->message());
16+
$this->log->info(...$this->message());
17+
$this->log->notice(...$this->message());
18+
$this->log->warning(...$this->message());
19+
$this->log->error(...$this->message());
20+
$this->log->critical(...$this->message());
21+
$this->log->alert(...$this->message());
22+
$this->log->emergency(...$this->message());
23+
}
24+
25+
protected function getConfig(): array
26+
{
27+
return [
28+
'deferred' => false,
29+
'loggers' => [
30+
['class' => File::class, 'dir' => sys_get_temp_dir()],
31+
]
32+
];
33+
}
34+
}

Tests/PhpBench/MemoryBench.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Koded\Logging\Tests\PhpBench;
4+
5+
use Koded\Logging\Processors\Memory;
6+
7+
class MemoryBench extends AbstractBench
8+
{
9+
/**
10+
* @Revs(1000)
11+
* @Iterations(3)
12+
*/
13+
public function benchMemory()
14+
{
15+
$this->log->debug(...$this->message());
16+
$this->log->info(...$this->message());
17+
$this->log->notice(...$this->message());
18+
$this->log->warning(...$this->message());
19+
$this->log->error(...$this->message());
20+
$this->log->critical(...$this->message());
21+
$this->log->alert(...$this->message());
22+
$this->log->emergency(...$this->message());
23+
}
24+
25+
protected function getConfig(): array
26+
{
27+
return [
28+
'deferred' => false,
29+
'loggers' => [
30+
['class' => Memory::class],
31+
]
32+
];
33+
}
34+
}

Tests/PhpBench/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```bash
2+
vendor/bin/phpbench run --progress=none --report='generator: "table", cols: ["subject", "mem_peak", "mean", "diff"]'
3+
```

Tests/PhpBench/SyslogBench.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Koded\Logging\Tests\PhpBench;
4+
5+
use Koded\Logging\Processors\Syslog;
6+
7+
class SyslogBench extends AbstractBench
8+
{
9+
/**
10+
* @Revs(1000)
11+
* @Iterations(3)
12+
*/
13+
public function benchSyslog()
14+
{
15+
$this->log->debug(...$this->message());
16+
$this->log->info(...$this->message());
17+
$this->log->notice(...$this->message());
18+
$this->log->warning(...$this->message());
19+
$this->log->error(...$this->message());
20+
$this->log->critical(...$this->message());
21+
$this->log->alert(...$this->message());
22+
$this->log->emergency(...$this->message());
23+
}
24+
25+
protected function getConfig(): array
26+
{
27+
return [
28+
'deferred' => false,
29+
'loggers' => [
30+
['class' => Syslog::class],
31+
]
32+
];
33+
}
34+
}

phpbench.json.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"bootstrap": "./vendor/autoload.php",
3+
"path": "Tests/PhpBench",
4+
"time_unit": "milliseconds",
5+
"php_disable_ini": false,
6+
"reports": [
7+
{
8+
"default": {
9+
"generator": "table"
10+
}
11+
}
12+
]
13+
}

0 commit comments

Comments
 (0)