Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions benchmarks/lobsters/benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,53 @@
Dir.chdir __dir__
use_gemfile

require_relative 'config/environment'
require_relative "route_generator"

# For an in-mem DB, we need to load all data on every boot
mem_db = ActiveRecord::Base.connection.raw_connection
file_db = SQLite3::Database.new('db/production.sqlite3')
b = SQLite3::Backup.new(mem_db, 'main', file_db, 'main')
b.step(-1) # import until finished
b.finish # destroy the Backup object
# The fixture has no planner statistics. Without them, SQLite misplans the
# hottest-stories query behind /, /recent and /rss.
mem_db.execute("ANALYZE")

app = Rails.application
generator = RouteGenerator.new(app)
generator.routes # Make sure routes have been pregenerated
# Avoid calendar-dependent DST and holiday-logo work.
FROZEN_NOW = Time.utc(2026, 7, 1, 12, 0, 0, 123456.789r).getlocal.freeze
REAL_NOW = Time.method(:now)
def Time.now = $clock_frozen ? FROZEN_NOW.dup : REAL_NOW.call

begin
$clock_frozen = true
require_relative 'config/environment'
require_relative "route_generator"

# For an in-mem DB, we need to load all data on every boot
mem_db = ActiveRecord::Base.connection.raw_connection
file_db = SQLite3::Database.new('db/production.sqlite3')
b = SQLite3::Backup.new(mem_db, 'main', file_db, 'main')
b.step(-1) # import until finished
b.finish # destroy the Backup object
# The fixture has no planner statistics. Without them, SQLite misplans the
# hottest-stories query behind /, /recent and /rss.
mem_db.execute("ANALYZE")

app = Rails.application
generator = RouteGenerator.new(app)
generator.routes # Make sure routes have been pregenerated
ensure
$clock_frozen = false
end

# Track ActiveRecord time
if ENV['TRACK_AR_TIME']
ar_total_duration = 0.0
process_start_t = Time.now
process_start_t = Process.clock_gettime(Process::CLOCK_MONOTONIC)

# Track sql.active_record events
ActiveSupport::Notifications.subscribe "sql.active_record" do |name, started, finished, unique_id, data|
ActiveSupport::Notifications.monotonic_subscribe "sql.active_record" do |name, started, finished, unique_id, data|
duration = finished - started
ar_total_duration += duration
end

at_exit do
process_duration = Time.now - process_start_t
process_duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - process_start_t
ar_percent = ar_total_duration * 100.0 / process_duration
puts "ActiveRecord time: #{ar_total_duration.round(2)}s (#{ar_percent.round(2)}%) PID: #{Process.pid}"
end
end

run_benchmark(10) do
$clock_frozen = true
# Routes are from ./routes_generator.rb
generator.routes.each_with_index do |env, idx|
path = env["PATH_INFO"] # app.call mutates the path
Expand All @@ -52,6 +63,8 @@
end
response_array.last.close # Response might be a Rack::BodyProxy and MUST be closed.
end
ensure
$clock_frozen = false
end

# This benchmark will keep writing the production log on every request. It adds up.
Expand Down
Loading