Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created March 4, 2015 00:49
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 tenderlove/532909889e556acbca86 to your computer and use it in GitHub Desktop.
Save tenderlove/532909889e556acbca86 to your computer and use it in GitHub Desktop.
require 'abstract_controller/railties/routes_helpers'
require 'action_controller'
require 'action_controller/railties/helpers'
require 'action_view'
require 'action_dispatch'
require 'active_record'
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
ActiveRecord::Base.connection.instance_eval do
create_table(:articles)
create_table(:comments)
end
class Article < ActiveRecord::Base; end
class Comment < ActiveRecord::Base; end
ROUTES = ActionDispatch::Routing::RouteSet.new
ROUTES.draw {
resources(:articles) {
resources :comments
}
}
class ApplicationController < ActionController::Base
include ROUTES.mounted_helpers
extend ::AbstractController::Railties::RoutesHelpers.with(ROUTES)
extend ::ActionController::Railties::Helpers
end
class Articles < ApplicationController
end
req = ActionDispatch::Request.new({"GATEWAY_INTERFACE"=>"CGI/1.1",
"PATH_INFO"=>"/users",
"QUERY_STRING"=>"",
"REMOTE_ADDR"=>"::1",
"REMOTE_HOST"=>"::1",
"REQUEST_METHOD"=>"GET",
"REQUEST_URI"=>"http://localhost:3000/users",
"SCRIPT_NAME"=>"",
"SERVER_NAME"=>"localhost",
"SERVER_PORT"=>"3000",
"SERVER_PROTOCOL"=>"HTTP/1.1",
"SERVER_SOFTWARE"=>"WEBrick/1.3.1 (Ruby/2.3.0/2015-02-17)",
"HTTP_USER_AGENT"=>"curl/7.37.1",
"HTTP_HOST"=>"localhost:3000",
"HTTP_ACCEPT"=>"*/*",
"rack.version"=>[1, 3],
"rack.input"=> StringIO.new,
"rack.errors"=> STDERR,
"rack.multithread"=>false,
"rack.multiprocess"=>false,
"rack.run_once"=>false,
"rack.url_scheme"=>"http",
"HTTP_VERSION"=>"HTTP/1.1",
"REQUEST_PATH"=>"/users",
"ORIGINAL_FULLPATH"=>"/users",
"ORIGINAL_SCRIPT_NAME"=>"",
"action_dispatch.request_id"=>"da9c324b-ba19-4ba1-8bdb-6518412c2947"})
controller = Articles.new
controller.request = req
view = Class.new(ActionView::Base) {
include ROUTES.url_helpers(true)
include ROUTES.mounted_helpers
}.new(nil, {}, controller)
comment = Comment.new.tap(&:save!)
article = Article.new.tap(&:save!)
view.url_for article
view.article_path article
controller.article_path article
puts "CONTROLLER"
Benchmark.ips do |x|
x.report("article_path(model)") { controller.article_path article }
end
puts "VIEW"
Benchmark.ips do |x|
x.report("article_path(model)") { view.article_path article }
end
ObjectSpace::AllocationTracer.trace do
3000.times { view.article_path article }
end
p ObjectSpace::AllocationTracer.allocated_count_table.values.inject :+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment