Skip to content

Instantly share code, notes, and snippets.

@timfjord
Last active April 12, 2018 07:57
Show Gist options
  • Save timfjord/5a14d6f622f7a73135ae25422dc7f9b9 to your computer and use it in GitHub Desktop.
Save timfjord/5a14d6f622f7a73135ae25422dc7f9b9 to your computer and use it in GitHub Desktop.
url_generation_test.rb with benchmark
# frozen_string_literal: true
require "abstract_unit"
module TestUrlGeneration
class WithMountPoint < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new
include Routes.url_helpers
class ::MyRouteGeneratingController < ActionController::Base
include Routes.url_helpers
def index
render plain: foo_path
end
end
Routes.draw do
get "/foo", to: "my_route_generating#index", as: :foo
resources :bars
mount MyRouteGeneratingController.action(:index), at: "/bar"
get "(/:locale)/baz(/:year(/:month(/:day)))", to: "my_route_generating#index", as: :baz,
constraints: { locale: /en|de|fr/, year: /\d{4}/, month: /\d{2}/, day: /\d{2}/ },
defaults: { locale: "en", year: "2018", month: "01", day: "01" }
get "(/:locale)/lol/:id(/:year(/:month(/:day)))", to: "my_route_generating#index", as: :lol,
constraints: { locale: /en|de|fr/, year: /\d{4}/, month: /\d{2}/, day: /\d{2}/ },
defaults: { locale: "en", year: "2018", month: "01", day: "01" }
end
APP = build_app Routes
def _routes
Routes
end
def app
APP
end
test "benchmark" do
require 'benchmark/ips'
Benchmark.ips do |x|
x.report('static url'){ foo_path }
x.report('url without id with optional segments without params'){ baz_path }
x.report('url without id with optional segments with params'){ baz_path(locale: :de, day: '01') }
x.report('url with id with optional segments without params'){ lol_path(id: 1) }
x.report('url with id with optional segments with params'){ lol_path(id: 1, locale: :de, day: '01') }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment