Skip to content

Commit

Permalink
Improved benchmarking routes
Browse files Browse the repository at this point in the history
  • Loading branch information
divineniiquaye committed May 9, 2022
1 parent 1de6df3 commit 5b46781
Showing 1 changed file with 57 additions and 7 deletions.
64 changes: 57 additions & 7 deletions tests/Benchmarks/RouteBench.php
Expand Up @@ -66,14 +66,14 @@ public function initUnoptimized(): void
$collection = [];

for ($i = 1; $i <= self::$maxRoutes; ++$i) {
$collection[] = Route::to("/route/{$i}", ['GET'])->bind('static_' . $i);
$collection[] = Route::to("/route/$i", ['GET'])->bind('static_' . $i);
$collection[] = Route::to("/route/{$i}/{foo}", ['GET'])->bind('no_static_' . $i);
}

$collection[] = Route::to("//localhost.com/route/401", ['GET'])->bind('static_' . 401);
$collection[] = Route::to("//{host}/route/{foo}", ['GET'])->bind('no_static_' . 401);
$collection[] = Route::to("//{host}/route/401/{foo}", ['GET'])->bind('no_static_' . 401);

$routes->routes($collection);
$routes->routes($collection, false);
});

$this->router = $router;
Expand All @@ -86,14 +86,14 @@ public function initOptimized(): void
$collection = [];

for ($i = 1; $i <= self::$maxRoutes; ++$i) {
$collection[] = Route::to("/route/{$i}", ['GET'])->bind('static_' . $i);
$collection[] = Route::to("/route/$i", ['GET'])->bind('static_' . $i);
$collection[] = Route::to("/route/{$i}/{foo}", ['GET'])->bind('no_static_' . $i);
}

$collection[] = Route::to("//localhost.com/route/400", ['GET'])->bind('static_' . 401);
$collection[] = Route::to("//{host}/route/{foo}", ['GET'])->bind('no_static_' . 401);
$collection[] = Route::to("//localhost.com/route/401", ['GET'])->bind('static_' . 401);
$collection[] = Route::to("//{host}/route/401/{foo}", ['GET'])->bind('no_static_' . 401);

$routes->routes($collection);
$routes->routes($collection, false);
});

$this->router = $router;
Expand Down Expand Up @@ -209,6 +209,31 @@ public function benchOptimizedDynamic(array $params): void
assert($this->runScope($params[0], $result), new \RuntimeException(\sprintf('Route match failed, expected a route instance for "%s" request path.', $params[0])));
}

/**
* @Groups(value={"optimized"})
* @Revs(4)
*/
public function benchOptimized(): void
{
$this->initOptimized();
$s = $d = 0;

for (;;) {
if ($s <= self::$maxRoutes) {
$path = '/route/' . $s;
$s++;
} elseif ($d <= self::$maxRoutes) {
$path = '/route' . $d . '/foo';
$d++;
} else {
break;
}

$result = $this->router->match('GET', new Uri($path));
assert($this->runScope($path, $result), new \RuntimeException(\sprintf('Route match failed, expected a route instance for "%s" request path.', $path)));
}
}

/**
* @Groups(value={"unoptimized:static"})
* @ParamProviders({"init"})
Expand All @@ -233,6 +258,31 @@ public function benchUnoptimizedDynamic(array $params): void
assert($this->runScope($params[0], $result), new \RuntimeException(\sprintf('Route match failed, expected a route instance for "%s" request path.', $params[0])));
}

/**
* @Groups(value={"unoptimized"})
* @Revs(4)
*/
public function benchUnoptimized(): void
{
$this->initUnoptimized();
$s = $d = 0;

for (;;) {
if ($s <= self::$maxRoutes) {
$path = '/route/' . $s;
$s++;
} elseif ($d <= self::$maxRoutes) {
$path = '/route' . $d . '/foo';
$d++;
} else {
break;
}

$result = $this->router->match('GET', new Uri($path));
assert($this->runScope($path, $result), new \RuntimeException(\sprintf('Route match failed, expected a route instance for "%s" request path.', $path)));
}
}

private function runScope(string $requestPath, ?Route $route): bool
{
if ($route instanceof Route) {
Expand Down

0 comments on commit 5b46781

Please sign in to comment.