-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathbenchmark.rb
More file actions
84 lines (69 loc) · 2.94 KB
/
benchmark.rb
File metadata and controls
84 lines (69 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require_relative '../../harness/loader'
ENV['RAILS_ENV'] ||= 'production'
ENV['DISABLE_DATABASE_ENVIRONMENT_CHECK'] = '1' # Benchmarks don't really have 'production', so trash it at will.
ENV['SHIPIT_DISABLE_AUTH'] = '1' # Saves us lots of trouble
Dir.chdir __dir__
use_gemfile
# sassc uses FFI with hardcoded paths to find its compiled libsass shared object.
# RubyGems 4.x (Ruby 4.1+) no longer copies extensions into the gem's lib/ tree,
# so sassc can't find it. Copy it into place.
if RUBY_VERSION >= "4.1"
spec = Gem::Specification.find_by_name("sassc") rescue nil
if spec
dl_ext = RbConfig::MAKEFILE_CONFIG['DLEXT']
target = File.join(spec.gem_dir, "ext", "libsass.#{dl_ext}")
source = File.join(spec.extension_dir, "sassc", "libsass.#{dl_ext}")
if !File.exist?(target) && File.exist?(source)
require 'fileutils'
FileUtils.cp(source, target)
end
end
end
require 'securerandom'
ENV['SECRET_KEY_BASE'] = SecureRandom.hex(128)
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.committed.sqlite3')
b = SQLite3::Backup.new(mem_db, 'main', file_db, 'main')
b.step(-1) # import until finished
b.finish # destroy the Backup object
app = Rails.application
api_key = Shipit::ApiClient.create!(
permissions: Shipit::ApiClient::PERMISSIONS,
creator: Shipit::User.first,
name: "Bench key",
).authentication_token
generator = RouteGenerator.new(app, api_key: api_key)
generator.routes # Make sure routes have been pregenerated
# Track ActiveRecord time
if ENV['TRACK_AR_TIME']
ar_total_duration = 0.0
process_start_t = Time.now
# Track sql.active_record events
ActiveSupport::Notifications.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
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
# Routes are from ./routes_generator.rb
generator.routes.each_with_index do |env, idx|
path = env["PATH_INFO"] # app.call mutates the path
response_array = generator.visit(env) # Track HTTP cookies as we go along
unless response_array.first == 200
puts response_array.inspect
raise "HTTP status is #{response_array.first} instead of 200 for req #{idx}/#{generator.routes.size}, #{path.inspect}. Is the benchmark app properly set up? See README.md."
end
response_array.last.close # Response might be a Rack::BodyProxy and MUST be closed.
end
end
# This benchmark will keep writing the production log on every request. It adds up.
# Let's not fill the disk.
File.unlink(File.join(__dir__, "log/#{ENV['RAILS_ENV']}.log")) rescue nil