Skip to content

Instantly share code, notes, and snippets.

@thisduck
Created May 20, 2017 16:45
Show Gist options
  • Save thisduck/734b0f2c81549b92ff7206eb0052ee36 to your computer and use it in GitHub Desktop.
Save thisduck/734b0f2c81549b92ff7206eb0052ee36 to your computer and use it in GitHub Desktop.
delayed job profiling
# config/initializer/delayed_job.rb
module Delayed
module Plugins
class Flaming < Plugin
callbacks do |lifecycle|
lifecycle.around(:invoke_job) do |job, *args, &block|
HelperMethods.flaming(name: "DELAYEDJOB--#{job.name}") do
block.call(job, *args)
end
end
end
end
end
end
Delayed::Worker.plugins << Delayed::Plugins::Flaming if ENV['WITH_FLAME']
def flaming(name: "flame", filters: [Rails.root.to_s, ".html.haml"])
require 'flamegraph'
require 'stackprof'
return yield if @aflame
@aflame = true
result = nil
start_time = Time.now
end_time = nil
exception = nil
html = Flamegraph.generate(nil, js_include_filters: filters) do
begin
result = yield
end_time = Time.now
rescue => e
exception = e
end
end
time_taken = (end_time || Time.now) - start_time
dir = Rails.root.join("tmp/flames").tap(&:mkpath)
file_name = dir.join "#{Time.now}--#{name}".parameterize + "--#{time_taken}.html"
File.write(file_name, html)
@aflame = nil
raise exception if exception
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment