Skip to content

Instantly share code, notes, and snippets.

@jhawthorn
Last active September 29, 2021 21:37
Show Gist options
  • Save jhawthorn/bded5bc1d5f1afd4cdd7fb5b800312e1 to your computer and use it in GitHub Desktop.
Save jhawthorn/bded5bc1d5f1afd4cdd7fb5b800312e1 to your computer and use it in GitHub Desktop.
$ be ruby benchmark_controller_callback.rb
Warming up --------------------------------------
conditional callback 392.000 i/100ms
method callback 397.000 i/100ms
proc callback 193.000 i/100ms
no callback 425.000 i/100ms
Calculating -------------------------------------
conditional callback 3.945k (± 0.5%) i/s - 19.992k in 5.068115s
method callback 3.978k (± 0.4%) i/s - 20.247k in 5.090186s
proc callback 1.941k (± 8.8%) i/s - 9.650k in 5.013008s
no callback 4.234k (± 0.7%) i/s - 21.250k in 5.019698s
Comparison:
no callback: 4233.5 i/s
method callback: 3977.7 i/s - 1.06x (± 0.00) slower
conditional callback: 3944.7 i/s - 1.07x (± 0.00) slower
proc callback: 1941.2 i/s - 2.18x (± 0.00) slower
$ be ruby benchmark_controller_callback.rb
Warming up --------------------------------------
conditional callback 188.000 i/100ms
method callback 401.000 i/100ms
proc callback 198.000 i/100ms
no callback 426.000 i/100ms
Calculating -------------------------------------
conditional callback 1.932k (± 8.6%) i/s - 9.588k in 5.002391s
method callback 3.977k (± 1.2%) i/s - 20.050k in 5.041633s
proc callback 1.966k (± 8.7%) i/s - 9.900k in 5.078494s
no callback 4.280k (± 2.1%) i/s - 21.726k in 5.078692s
Comparison:
no callback: 4280.0 i/s
method callback: 3977.5 i/s - 1.08x (± 0.00) slower
proc callback: 1965.7 i/s - 2.18x (± 0.00) slower
conditional callback: 1932.4 i/s - 2.21x (± 0.00) slower
require "openssl"
require "securerandom"
require "benchmark/ips"
require "action_view"
require "action_pack"
require "action_controller"
require "rails"
require 'stackprof'
ENV["RAILS_ENV"] = "production"
class BenchmarkController < 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 ConditionalCallbackController < BenchmarkController
def simple_method
end
before_action :simple_method, only: :index
end
class MethodCallbackController < BenchmarkController
def simple_method
end
before_action :simple_method
end
class ProcCallbackController < BenchmarkController
before_action { }
end
#pp MyController.__callbacks
class MyApp < Rails::Application
config.eager_load = true
config.cache_classes = true
config.hosts << //
config.secret_key_base = SecureRandom.hex
config.consider_all_requests_local = true
config.public_file_server.enabled = false
config.cache_store = :null_store
#config.action_controller.perform_caching = true
config.middleware.delete(ActionDispatch::HostAuthorization)
config.middleware.delete(ActionDispatch::RequestId)
config.middleware.delete(ActionDispatch::RemoteIp)
config.middleware.delete(ActionDispatch::Cookies)
config.middleware.delete(ActionDispatch::ContentSecurityPolicy::Middleware)
config.middleware.delete(Rack::Sendfile)
config.middleware.delete(Rack::ETag)
config.middleware.delete(Rails::Rack::Logger)
config.middleware.delete(ActionDispatch::ShowExceptions)
config.middleware.delete(ActionDispatch::DebugExceptions)
config.log_level = :warn
routes.append do
get "/conditional_callback", to: "conditional_callback#index"
get "/method_callback", to: "method_callback#index"
get "/proc_callback", to: "proc_callback#index"
get "/no_callback", to: "benchmark#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]
}
result = Benchmark.ips do |x|
x.report "conditional callback" do
make_request.call("conditional_callback")
end
x.report "method callback" do
make_request.call("method_callback")
end
x.report "proc callback" do
make_request.call("proc_callback")
end
x.report "no callback" do
make_request.call("no_callback")
end
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment