URLSite.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @package koseven/Codebench
  4. * @category Tests
  5. * @author Geert De Deckere <geert@idoe.be>
  6. */
  7. class Bench_URLSite extends Codebench {
  8. public $description = 'http://koseven.dev/issues/3110';
  9. public $loops = 1000;
  10. public $subjects = [
  11. '',
  12. 'news',
  13. 'news/',
  14. '/news/',
  15. 'news/page/5',
  16. 'news/page:5',
  17. 'http://example.com/',
  18. 'http://example.com/hello',
  19. 'http://example.com:80/',
  20. 'http://user:pass@example.com/',
  21. ];
  22. public function __construct()
  23. {
  24. foreach ($this->subjects as $subject)
  25. {
  26. // Automatically create URIs with query string and/or fragment part appended
  27. $this->subjects[] = $subject.'?query=string';
  28. $this->subjects[] = $subject.'#fragment';
  29. $this->subjects[] = $subject.'?query=string#fragment';
  30. }
  31. parent::__construct();
  32. }
  33. public function bench_original($uri)
  34. {
  35. // Get the path from the URI
  36. $path = trim(parse_url($uri, PHP_URL_PATH), '/');
  37. if ($query = parse_url($uri, PHP_URL_QUERY))
  38. {
  39. $query = '?'.$query;
  40. }
  41. if ($fragment = parse_url($uri, PHP_URL_FRAGMENT))
  42. {
  43. $fragment = '#'.$fragment;
  44. }
  45. return $path.$query.$fragment;
  46. }
  47. public function bench_explode($uri)
  48. {
  49. // Chop off possible scheme, host, port, user and pass parts
  50. $path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
  51. $fragment = '';
  52. $explode = explode('#', $path, 2);
  53. if (isset($explode[1]))
  54. {
  55. $path = $explode[0];
  56. $fragment = '#'.$explode[1];
  57. }
  58. $query = '';
  59. $explode = explode('?', $path, 2);
  60. if (isset($explode[1]))
  61. {
  62. $path = $explode[0];
  63. $query = '?'.$explode[1];
  64. }
  65. return $path.$query.$fragment;
  66. }
  67. public function bench_regex($uri)
  68. {
  69. preg_match('~^(?:[-a-z0-9+.]++://[^/]++/?)?([^?#]++)?(\?[^#]*+)?(#.*)?~', trim($uri, '/'), $matches);
  70. $path = Arr::get($matches, 1, '');
  71. $query = Arr::get($matches, 2, '');
  72. $fragment = Arr::get($matches, 3, '');
  73. return $path.$query.$fragment;
  74. }
  75. public function bench_regex_without_arrget($uri)
  76. {
  77. preg_match('~^(?:[-a-z0-9+.]++://[^/]++/?)?([^?#]++)?(\?[^#]*+)?(#.*)?~', trim($uri, '/'), $matches);
  78. $path = isset($matches[1]) ? $matches[1] : '';
  79. $query = isset($matches[2]) ? $matches[2] : '';
  80. $fragment = isset($matches[3]) ? $matches[3] : '';
  81. return $path.$query.$fragment;
  82. }
  83. // And then I thought, why do all the work of extracting the query and fragment parts and then reappending them?
  84. // Just leaving them alone should be fine, right? As a bonus we get a very nice speed boost.
  85. public function bench_less_is_more($uri)
  86. {
  87. // Chop off possible scheme, host, port, user and pass parts
  88. $path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
  89. return $path;
  90. }
  91. public function bench_less_is_more_with_strpos_optimization($uri)
  92. {
  93. if (strpos($uri, '://') !== FALSE)
  94. {
  95. // Chop off possible scheme, host, port, user and pass parts
  96. $uri = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
  97. }
  98. return $uri;
  99. }
  100. }