123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- /**
- * @package koseven/Codebench
- * @category Tests
- * @author Geert De Deckere <geert@idoe.be>
- */
- class Bench_URLSite extends Codebench {
- public $description = 'http://koseven.dev/issues/3110';
- public $loops = 1000;
- public $subjects = [
- '',
- 'news',
- 'news/',
- '/news/',
- 'news/page/5',
- 'news/page:5',
- 'http://example.com/',
- 'http://example.com/hello',
- 'http://example.com:80/',
- 'http://user:pass@example.com/',
- ];
- public function __construct()
- {
- foreach ($this->subjects as $subject)
- {
- // Automatically create URIs with query string and/or fragment part appended
- $this->subjects[] = $subject.'?query=string';
- $this->subjects[] = $subject.'#fragment';
- $this->subjects[] = $subject.'?query=string#fragment';
- }
- parent::__construct();
- }
- public function bench_original($uri)
- {
- // Get the path from the URI
- $path = trim(parse_url($uri, PHP_URL_PATH), '/');
- if ($query = parse_url($uri, PHP_URL_QUERY))
- {
- $query = '?'.$query;
- }
- if ($fragment = parse_url($uri, PHP_URL_FRAGMENT))
- {
- $fragment = '#'.$fragment;
- }
- return $path.$query.$fragment;
- }
- public function bench_explode($uri)
- {
- // Chop off possible scheme, host, port, user and pass parts
- $path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
- $fragment = '';
- $explode = explode('#', $path, 2);
- if (isset($explode[1]))
- {
- $path = $explode[0];
- $fragment = '#'.$explode[1];
- }
- $query = '';
- $explode = explode('?', $path, 2);
- if (isset($explode[1]))
- {
- $path = $explode[0];
- $query = '?'.$explode[1];
- }
- return $path.$query.$fragment;
- }
- public function bench_regex($uri)
- {
- preg_match('~^(?:[-a-z0-9+.]++://[^/]++/?)?([^?#]++)?(\?[^#]*+)?(#.*)?~', trim($uri, '/'), $matches);
- $path = Arr::get($matches, 1, '');
- $query = Arr::get($matches, 2, '');
- $fragment = Arr::get($matches, 3, '');
- return $path.$query.$fragment;
- }
- public function bench_regex_without_arrget($uri)
- {
- preg_match('~^(?:[-a-z0-9+.]++://[^/]++/?)?([^?#]++)?(\?[^#]*+)?(#.*)?~', trim($uri, '/'), $matches);
- $path = isset($matches[1]) ? $matches[1] : '';
- $query = isset($matches[2]) ? $matches[2] : '';
- $fragment = isset($matches[3]) ? $matches[3] : '';
- return $path.$query.$fragment;
- }
- // And then I thought, why do all the work of extracting the query and fragment parts and then reappending them?
- // Just leaving them alone should be fine, right? As a bonus we get a very nice speed boost.
- public function bench_less_is_more($uri)
- {
- // Chop off possible scheme, host, port, user and pass parts
- $path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
- return $path;
- }
- public function bench_less_is_more_with_strpos_optimization($uri)
- {
- if (strpos($uri, '://') !== FALSE)
- {
- // Chop off possible scheme, host, port, user and pass parts
- $uri = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
- }
- return $uri;
- }
- }
|