Skip to content

Commit

Permalink
Adding URI specific benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Sep 13, 2022
1 parent 34f43a9 commit 198be7f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 18 deletions.
15 changes: 15 additions & 0 deletions src/HttpBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,19 @@ public function benchBuildingAnUriFromUriComponents(): void
Http::createFromComponents($components);
}
}

#[Bench\OutputTimeUnit('seconds')]
#[Bench\Assert('mode(variant.mem.peak) < 2097152'), Bench\Assert('mode(variant.time.avg) < 10000000')]
public function benchBuildingAnUriFromUriComponentsMutation(): void
{
for ($i = 0; $i < 100_000; $i++) {
Http::createFromString()
->withPath('/5.0')
->withQuery('q=val1&q=val2&query[3]=val3')
->withFragment('foobar')
->withHost('uri.thephpleague.com')
->withUserInfo('user', 'pass')
->withScheme('https');
}
}
}
27 changes: 9 additions & 18 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -647,19 +647,12 @@ public static function createFromUri(Psr7UriInterface|UriInterface $uri): self
*/
public static function createFromServer(array $server): self
{
[$user, $pass] = self::fetchUserInfo($server);
[$host, $port] = self::fetchHostname($server);
[$path, $query] = self::fetchRequestUri($server);
$components = ['scheme' => self::fetchScheme($server)];
[$components['user'], $components['pass']] = self::fetchUserInfo($server);
[$components['host'], $components['port']] = self::fetchHostname($server);
[$components['path'], $components['query']] = self::fetchRequestUri($server);

return Uri::createFromComponents([
'scheme' => self::fetchScheme($server),
'user' => $user,
'pass' => $pass,
'host' => $host,
'port' => $port,
'path' => $path,
'query' => $query,
]);
return Uri::createFromComponents($components);
}

/**
Expand Down Expand Up @@ -941,6 +934,8 @@ private function assertValidState(): void
throw new SyntaxError('In absence of a scheme and an authority the first path segment cannot contain a colon (":") character.');
}

$this->uri = null;

if (! match ($this->scheme) {
'data' => $this->isUriWithSchemeAndPathOnly(),
'file' => $this->isUriWithSchemeHostAndPathOnly(),
Expand All @@ -949,10 +944,8 @@ private function assertValidState(): void
'ws', 'wss' => $this->isNonEmptyHostUriWithoutFragment(),
default => true,
}) {
throw new SyntaxError('The uri `'.$this->toString().'` is invalid for the `'.$this->scheme.'` scheme.');
throw new SyntaxError('The uri `'.$this.'` is invalid for the `'.$this->scheme.'` scheme.');
}

$this->uri = null;
}

/**
Expand Down Expand Up @@ -1037,15 +1030,13 @@ private function getUriString(

public function toString(): string
{
$this->uri ??= $this->getUriString(
return $this->uri ??= $this->getUriString(
$this->scheme,
$this->authority,
$this->path,
$this->query,
$this->fragment
);

return $this->uri;
}

/**
Expand Down
54 changes: 54 additions & 0 deletions src/UriBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace League\Uri;

use PhpBench\Attributes as Bench;

final class UriBench
{
#[Bench\OutputTimeUnit('seconds')]
#[Bench\Assert('mode(variant.mem.peak) < 2097152'), Bench\Assert('mode(variant.time.avg) < 10000000')]
public function benchBuildingAnUriFromUriComponents(): void
{
$components = [
'scheme' => 'https',
'host' => 'uri.thephpleague.com',
'user' => 'php-fig',
'pass' => 'psr7',
'port' => 1337,
'path' => '/5.0',
'query' => 'q=val1&q=val2&query[3]=val3',
'fragment' => 'foobar',
];

for ($i = 0; $i < 100_000; $i++) {
Uri::createFromComponents($components);
}
}

#[Bench\OutputTimeUnit('seconds')]
#[Bench\Assert('mode(variant.mem.peak) < 2097152'), Bench\Assert('mode(variant.time.avg) < 10000000')]
public function benchBuildingAnUriFromUriComponentsMutation(): void
{
for ($i = 0; $i < 100_000; $i++) {
Uri::createFromString()
->withPath('/5.0')
->withQuery('q=val1&q=val2&query[3]=val3')
->withFragment('foobar')
->withHost('uri.thephpleague.com')
->withUserInfo('user', 'pass')
->withScheme('https');
}
}
}

0 comments on commit 198be7f

Please sign in to comment.