-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbenchmark.php
51 lines (41 loc) · 1.8 KB
/
benchmark.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
declare(strict_types=1);
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/assets/CreatorFactory.php';
$packages = require_once __DIR__ . '/assets/packages.php';
if (empty($argv[1])) {
throw new InvalidArgumentException('Specify the package for running.');
}
if (!isset($packages[$argv[1]])) {
throw new InvalidArgumentException(sprintf(
'Unsupported package `%s`; must be an any of the set (%s).',
$argv[1],
implode(', ', array_keys($packages))
));
}
$creator = new CreatorFactory();
$package = $packages[$argv[1]];
$runs = isset($argv[2]) ? (int) $argv[2] : 30000;
$start = microtime(true);
for ($i = 0; $i < $runs; $i++) {
$creator->createRequest(new $package[RequestFactoryInterface::class]());
$creator->createResponse(new $package[ResponseFactoryInterface::class]());
$creator->createServerRequest(new $package[ServerRequestFactoryInterface::class]());
$creator->createStream(new $package[StreamFactoryInterface::class]());
$creator->createUploadedFile(
new $package[UploadedFileFactoryInterface::class](),
new $package[StreamFactoryInterface::class](),
);
$creator->createUri(new $package[UriFactoryInterface::class]());
}
$totalTime = microtime(true) - $start;
echo 'Runs: ' . number_format($runs) . PHP_EOL;
echo 'Runs per second: ' . floor($runs / $totalTime) . PHP_EOL;
echo 'Average time per run: ' . number_format(($totalTime / $runs) * 1000, 4) . ' ms' . PHP_EOL;
echo 'Total time: ' . number_format($totalTime, 4) . ' s' . PHP_EOL;