Skip to content

Instantly share code, notes, and snippets.

@volkanunsal
Last active July 25, 2017 22: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 volkanunsal/1247d12d11a324fc487542060431d7cd to your computer and use it in GitHub Desktop.
Save volkanunsal/1247d12d11a324fc487542060431d7cd to your computer and use it in GitHub Desktop.
namespace :perf do
task :rails_load do
ENV["RAILS_ENV"] ||= "production"
ENV['RACK_ENV'] = ENV["RAILS_ENV"]
ENV["DISABLE_SPRING"] = "true"
ENV["SECRET_KEY_BASE"] ||= "foofoofoo"
ENV['LOG_LEVEL'] ||= "FATAL"
require 'rails'
puts "Booting: #{Rails.env}"
%W{ . lib test config }.each do |file|
$LOAD_PATH << file
end
require 'application'
Rails.env = ENV["RAILS_ENV"]
APP = Rails.application
if APP.respond_to?(:initialized?)
APP.initialize! unless APP.initialized?
else
APP.initialize! unless APP.instance_variable_get(:@initialized)
end
if ENV["DERAILED_SKIP_ACTIVE_RECORD"] && defined? ActiveRecord
ActiveRecord::Tasks::DatabaseTasks.create_current
ActiveRecord::Migrator.migrations_paths = APP.paths['db/migrate'].to_a
ActiveRecord::Migration.verbose = true
ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, nil)
end
APP.config.consider_all_requests_local = true
end
task :setup => [:rails_load] do
require 'derailed_benchmarks'
TEST_COUNT = (ENV['TEST_COUNT'] || ENV['CNT'] || 1_000).to_i
PATH_TO_HIT = ENV["PATH_TO_HIT"] || ENV['ENDPOINT'] || "/"
puts "Endpoint: #{ PATH_TO_HIT.inspect }"
require 'rack/test'
require 'rack/file'
def get_path
File.join("/", URI.escape(PATH_TO_HIT))
end
DERAILED_APP = DerailedBenchmarks.add_auth(APP)
if server = ENV["USE_SERVER"]
@port = (3000..3900).to_a.sample
puts "Port: #{ @port.inspect }"
puts "Server: #{ server.inspect }"
puts "Disable SSL: #{ENV['DISABLE_SSL']}"
thread = Thread.new do
Rack::Server.start(
app: DERAILED_APP,
Port: @port,
environment: 'none',
server: server
)
end
sleep 1
puts "serving: http://localhost:#{@port}#{get_path}"
def call_app(path = get_path)
cmd = "curl 'http://localhost:#{@port}#{path}' -g -s --fail 2>&1"
response = `#{cmd}`
unless $? && $?.success?
raise "Bad request to #{cmd.inspect} Response:\n#{ response.inspect }"
end
end
else
@app = Rack::MockRequest.new(DERAILED_APP)
def call_app
response = @app.get(get_path, ENV.to_h)
unless response.status == 200
raise "Bad request: #{ response }"
end
response
end
end
end
desc "stackprof"
task :stackprof => [:setup] do
require 'stackprof'
TEST_COUNT = (ENV["TEST_COUNT"] ||= "100").to_i
file = "tmp/#{Time.now.iso8601}-stackprof-cpu-myapp.dump"
# Available profiling modes:
# - :wall
# - :cpu
# - :object
StackProf.run(mode: :wall, out: file) do
Rake::Task["perf:test"].invoke
end
cmd = "stackprof #{file}"
puts "Running `#{cmd}`. Execute `stackprof --help` for more info"
puts `#{cmd}`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment