Skip to content

Instantly share code, notes, and snippets.

@markstory
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markstory/4d946a3ea7be7fad1f7c to your computer and use it in GitHub Desktop.
Save markstory/4d946a3ea7be7fad1f7c to your computer and use it in GitHub Desktop.
Router optimization test file
<?php
namespace App\Console\Command;
use Cake\Console\Shell;
use Cake\Routing\Router;
class BookRouteShell extends Shell {
public function main() {
$this->scoped();
}
public function scoped() {
Router::scope('/', function($routes) {
$routes->connect('/', ['controller' => 'pages', 'action' => 'display', 'home']);
# accounts
$routes->connect(
'/login',
['controller' => 'login', 'action' => 'get', '[method]' => 'GET'],
['_name' => 'login']
);
$routes->connect('/login', ['controller' => 'login', 'action' => 'post', '[method]' => 'POST']);
$routes->connect('/logout', ['controller' => 'logout', 'action' => 'get', '[method]' => 'GET']);
$routes->connect('/account', ['controller' => 'account', 'action' => 'get', '[method]' => 'GET']);
$routes->connect('/account', ['controller' => 'account', 'action' => 'post', '[method]' => 'POST']);
# user dashboard
$routes->connect('/dashboard', ['controller' => 'dashboard', 'action' => 'get', '[method]' => 'GET']);
$routes->resources('Books');
$routes->resources('Chapters');
});
Router::scope('/books', function($routes) {
# comments
$routes->connect(
'/:book_id/comments',
['controller' => 'comments', 'action' => 'book', '[method]' => 'GET']);
$routes->connect(
'/:book_id/chapters/:chapter_id',
['controller' => 'chapters', 'action' => 'get', '[method]' => 'GET']);
$routes->connect(
'/:book_id/chapters/:chapter_id/enable',
['controller' => 'chapters', 'action' => 'enable', '[method]' => 'GET']);
$routes->connect(
'/:book_id/chapters/:chapter_id/disable',
['controller' => 'chapters', 'action' => 'disable', '[method]' => 'GET']);
$routes->connect(
'/:book_id/chapters/:chapter_id/position',
['controller' => 'chapters', 'action' => 'position', '[method]' => 'PATCH']);
$routes->connect(
'/:book_id/chapters/:chapter_id/comments',
['controller' => 'comments', 'action' => 'chapter', '[method]' => 'GET'],
['_name' => 'chapter-comments']
);
$routes->connect(
'/:book_id/chapters/:chapter_id/comments/:element_uuid',
['controller' => 'comments', 'action' => 'post', '[method]' => 'POST']);
$routes->connect(
'/:book_id/chapters/:chapter_id/comments/:element_uuid',
['controller' => 'elements', 'action' => 'index', '[method]' => 'GET']);
$routes->connect(
'/:book_id/chapters/:chapter_id',
['controller' => 'chapters', 'action' => 'patch', '[method]' => 'PATCH']);
$routes->connect(
'/:book_id/chapters/:chapter_id',
['controller' => 'chapters', 'action' => 'patch', '[method]' => 'PATCH']);
});
$this->profile();
}
public function existing() {
Router::connect('/', ['controller' => 'pages', 'action' => 'display', 'home']);
# accounts
Router::connect(
'/login',
['controller' => 'login', 'action' => 'get', '[method]' => 'GET'],
['_name' => 'login']
);
Router::connect('/login', ['controller' => 'login', 'action' => 'post', '[method]' => 'POST']);
Router::connect('/logout', ['controller' => 'logout', 'action' => 'get', '[method]' => 'GET']);
Router::connect('/account', ['controller' => 'account', 'action' => 'get', '[method]' => 'GET']);
Router::connect('/account', ['controller' => 'account', 'action' => 'post', '[method]' => 'POST']);
# user dashboard
Router::connect('/dashboard', ['controller' => 'dashboard', 'action' => 'get', '[method]' => 'GET']);
# comments
Router::connect('/books/:book_id/comments', ['controller' => 'comments', 'action' => 'book', '[method]' => 'GET']);
Router::connect('/books/:book_id/chapters/:chapter_id', ['controller' => 'chapters', 'action' => 'get', '[method]' => 'GET']);
Router::connect('/books/:book_id/chapters/:chapter_id/enable', ['controller' => 'chapters', 'action' => 'enable', '[method]' => 'GET']);
Router::connect('/books/:book_id/chapters/:chapter_id/disable', ['controller' => 'chapters', 'action' => 'disable', '[method]' => 'GET']);
Router::connect('/books/:book_id/chapters/:chapter_id/position', ['controller' => 'chapters', 'action' => 'position', '[method]' => 'PATCH']);
Router::connect('/books/:book_id/chapters/:chapter_id/comments', ['controller' => 'comments', 'action' => 'chapter', '[method]' => 'GET'], ['_name' => 'chapter-comments']);
Router::connect('/books/:book_id/chapters/:chapter_id/comments/:element_uuid', ['controller' => 'comments', 'action' => 'post', '[method]' => 'POST']);
Router::connect('/books/:book_id/chapters/:chapter_id/comments/:element_uuid', ['controller' => 'elements', 'action' => 'index', '[method]' => 'GET']);
Router::mapResources(['books', 'chapters']);
Router::connect('/books/:book_id/chapters/:chapter_id', ['controller' => 'chapters', 'action' => 'patch', '[method]' => 'PATCH']);
$this->profile();
}
public function profile() {
$start = microtime(true);
for ($i = 0; $i < 5000; $i++) {
Router::url(['controller' => 'login', 'action' => 'get', '[method]' => 'GET']);
Router::url(['controller' => 'comments', 'action' => 'book', 'book_id' => 3, '[method]' => 'GET']);
Router::url(['controller' => 'chapters', 'action' => 'disable', 'book_id' => 3, 'chapter_id' => 2, '[method]' => 'GET']);
Router::url(['controller' => 'chapters', 'action' => 'patch', 'book_id' => 3, 'chapter_id' => 2, '[method]' => 'PATCH']);
}
$end = microtime(true);
$this->out('Reverse routing URLs took ' . ($end - $start));
$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
Router::url('login');
Router::url('chapter-comments', ['book_id' => 4, 'chapter_id' => 10]);
}
$end = microtime(true);
$this->out('Reverse routing with named routes took ' . ($end - $start));
$start = microtime(true);
for ($i = 0; $i < 5000; $i++) {
Router::parse('/login');
Router::parse('/books/2/chapters/4');
Router::parse('/books/3/chapters/5/comments');
Router::parse('/books/3/comments');
}
$end = microtime(true);
$this->out('Parsing URLs took ' . ($end - $start));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment