Skip to content

Instantly share code, notes, and snippets.

@jhawthorn
Created October 21, 2023 01:26
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/1540799e0d5c0a4be7f929fa6edd8d5b to your computer and use it in GitHub Desktop.
Save jhawthorn/1540799e0d5c0a4be7f929fa6edd8d5b to your computer and use it in GitHub Desktop.
require "action_pack"
require "action_controller"
require "rails"
ENV["RAILS_ENV"] = "production"
ENV["SECRET_KEY_BASE_DUMMY"] = "1"
N = ENV.fetch("N", 1000).to_i
M = ENV.fetch("M", 50).to_i
class ApplicationController < ActionController::Base
around_action { |_controller, action| action.call }
unless ENV["NO_CALLBACK"]
M.times do |i|
class_eval <<~RUBY
def foo#{i}; end
before_action :foo#{i}
RUBY
end
end
end
N.times do |i|
controller = Class.new(ApplicationController) do
def bar; end
before_action :bar
def index
head :ok
end
end
Object.const_set(:"Foo#{i}Controller", controller)
end
#pp MyController.__callbacks
class MyApp < Rails::Application
config.load_defaults "7.1"
config.eager_load = true
config.cache_classes = true
config.hosts << ->(_) { true }
config.consider_all_requests_local = true
config.public_file_server.enabled = false
config.cache_store = :null_store
config.log_level = :warn
routes.append do
N.times do |i|
get "/foo#{i}", to: "foo#{i}#index"
end
end
initialize!
end
def rss
Integer(`ps -q #{$$} -o rss=`.strip)
end
GC.start
before = GC.stat(:heap_live_slots)
rss_before = rss
puts "live objects before: #{before}"
puts "RSS before: #{rss_before}"
# Warm the applcation: make one request to each controller
app = Rails.application
N.times do |i|
env = Rack::MockRequest.env_for("http://example.org/foo#{i}")
status, headers, body = app.call(env)
body.close if body.respond_to?(:close)
end
GC.start
GC.start
GC.start
after = GC.stat(:heap_live_slots)
rss_after = rss
puts
puts "live objects after: #{after}"
puts "RSS after: #{rss_after}"
puts
puts "object delta: #{after - before}"
puts "RSS delta: #{rss_after - rss_before}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment