public
Created

  • Download Gist
perf.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
require "diffbench"
$:.unshift "../railties/lib"
$:.unshift "../activesupport/lib"
require "active_support"
require 'rails'
class App < Rails::Application
# Enable the asset pipeline
config.assets.enabled = true
 
self.routes.draw do
resources :inboxes do
resources :messages do
resources :attachments
end
end
 
namespace :admin do
resources :users
end
 
scope "/returns/:return" do
resources :objects
end
resources :returns
 
scope "(/optional/:optional_id)" do
resources :things
end
match "/other_optional/(:optional_id)" => "foo#foo", :as => :foo
 
match 'books/*section/:title' => 'books#show', :as => :book
end
 
end
 
 
def url_helpers_module
routes = App.routes
Module.new do
extend ActiveSupport::Concern
include ActionDispatch::Routing::UrlFor
 
# Define url_for in the singleton level so one can do:
# Rails.application.routes.url_helpers.url_for(args)
@_routes = routes
class << self
delegate :url_for, :optimize_routes_generation?, :url_options, :to => '@_routes'
def url_options
{}
end
end
 
# Make named_routes available in the module singleton
# as well, so one can do:
# Rails.application.routes.url_helpers.posts_path
extend routes.named_routes.module
 
# Any class that includes this module will get all
# named routes...
include routes.named_routes.module
 
# plus a singleton class method called _routes ...
included do
singleton_class.send(:redefine_method, :_routes) { routes }
end
 
# And an instance method _routes. Note that
# UrlFor (included in this module) add extra
# conveniences for working with @_routes.
define_method(:_routes) { @_routes || routes }
end
end
 
url_helpers = url_helpers_module
 
DiffBench.bm do
report "Url helper module builder" do
1000.times do
url_helpers_module
end
end
 
report "simple URL generation" do
1000.times do
url_helpers.inbox_path(1)
end
end
 
report "URL generation with optional parameters" do
1000.times do
url_helpers.inbox_message_path(inbox_id: 1, id: 2)
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.