Skip to content

Instantly share code, notes, and snippets.

@jhawthorn
Created November 20, 2021 23:45
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 jhawthorn/42559732de3c5755ba1f3f6e2796536c to your computer and use it in GitHub Desktop.
Save jhawthorn/42559732de3c5755ba1f3f6e2796536c to your computer and use it in GitHub Desktop.
[200,
{"X-Frame-Options"=>"SAMEORIGIN",
"X-XSS-Protection"=>"1; mode=block",
"X-Content-Type-Options"=>"nosniff",
"X-Download-Options"=>"noopen",
"X-Permitted-Cross-Domain-Policies"=>"none",
"Referrer-Policy"=>"strict-origin-when-cross-origin",
"Content-Type"=>"text/html",
"Cache-Control"=>"no-cache",
"X-Request-Id"=>"9342103d-6d8a-4644-aabf-239da57ca359",
"X-Runtime"=>"0.001139"},
"#<Rack::BodyProxy:0x000055f03cedb110>"]
Warming up --------------------------------------
basic controller 352.000 i/100ms
callback controller 191.000 i/100ms
Calculating -------------------------------------
basic controller 3.569k (± 1.3%) i/s - 17.952k in 5.030744s
callback controller 1.804k (± 7.3%) i/s - 8.977k in 5.004902s
Comparison:
basic controller: 3569.0 i/s
callback controller: 1804.0 i/s - 1.98x (± 0.00) slower
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'actionpack'
gem "rails"
gem "stackprof"
gem 'benchmark-ips'
end
require "rails"
require "action_controller/railtie"
ENV["RAILS_ENV"] = "production"
class BaseController < ActionController::Base
N = ENV.fetch("N", 1000).to_i
N.times do |i|
class_eval "def foo#{i}; end"
end
class_eval <<~RUBY
def index
#{N.times.map { |i| "foo#{i}" }.join("\n") }
head :ok
end
RUBY
end
class BasicController < BaseController
end
class CallbackController < BaseController
before_action do
# no nothing
end
end
class MyApp < Rails::Application
config.eager_load = true
config.cache_classes = true
config.secret_key_base = "secretsecretsecret"
config.consider_all_requests_local = true
config.public_file_server.enabled = false
config.log_level = :warn
routes.append do
get '/basic', to: "basic#index"
get '/callback', to: "callback#index"
end
initialize!
end
app = Rails.application
make_request = -> (path) {
env = Rack::MockRequest.env_for("http://example.org/#{path}")
status, headers, body = app.call(env)
body.close if body.respond_to?(:close)
unless status == 200
puts body.map(&:to_s)
raise
end
[status, headers, body.to_s]
}
pp make_request.call("/basic")
result = Benchmark.ips do |x|
x.report "basic controller" do
make_request.call("basic")
end
x.report "callback controller" do
make_request.call("callback")
end
x.compare!
end
[200,
{"X-Frame-Options"=>"SAMEORIGIN",
"X-XSS-Protection"=>"1; mode=block",
"X-Content-Type-Options"=>"nosniff",
"X-Download-Options"=>"noopen",
"X-Permitted-Cross-Domain-Policies"=>"none",
"Referrer-Policy"=>"strict-origin-when-cross-origin",
"Content-Type"=>"text/html",
"Cache-Control"=>"no-cache",
"X-Request-Id"=>"9f9e31af-abb3-42e4-a796-ea03b8f807b5",
"X-Runtime"=>"0.001149"},
"#<Rack::BodyProxy:0x00007ffb92502220>"]
Warming up --------------------------------------
basic controller 351.000 i/100ms
callback controller 341.000 i/100ms
Calculating -------------------------------------
basic controller 3.580k (± 1.3%) i/s - 17.901k in 5.001053s
callback controller 3.512k (± 1.1%) i/s - 17.732k in 5.049550s
Comparison:
basic controller: 3580.1 i/s
callback controller: 3512.0 i/s - same-ish: difference falls within error
[200,
{"X-Frame-Options"=>"SAMEORIGIN",
"X-XSS-Protection"=>"1; mode=block",
"X-Content-Type-Options"=>"nosniff",
"X-Download-Options"=>"noopen",
"X-Permitted-Cross-Domain-Policies"=>"none",
"Referrer-Policy"=>"strict-origin-when-cross-origin",
"Content-Type"=>"text/html",
"Cache-Control"=>"no-cache",
"X-Request-Id"=>"53ed7c39-e22c-4f20-af8f-6444a59ebecc",
"X-Runtime"=>"0.001090"},
"#<Rack::BodyProxy:0x000055e322981dd0>"]
Warming up --------------------------------------
basic controller 359.000 i/100ms
callback controller 191.000 i/100ms
Calculating -------------------------------------
basic controller 3.604k (± 0.7%) i/s - 18.309k in 5.080686s
callback controller 1.794k (± 7.3%) i/s - 8.977k in 5.033098s
Comparison:
basic controller: 3603.8 i/s
callback controller: 1794.0 i/s - 2.01x (± 0.00) slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment