Skip to content

Instantly share code, notes, and snippets.

@gmcabrita
Created May 9, 2020 21:33
Show Gist options
  • Save gmcabrita/d8e09b73a9be1c4d93e6c38756bd22f0 to your computer and use it in GitHub Desktop.
Save gmcabrita/d8e09b73a9be1c4d93e6c38756bd22f0 to your computer and use it in GitHub Desktop.
rack-mini-profiler + speedscope.app
javascript:void(function(){params=new URLSearchParams(document.location.search.slice(1));params.set("pp","flamegraph");open('https://www.speedscope.app/#profileURL='+encodeURIComponent(`${document.location.origin}${document.location.pathname}?${params.toString()}`))}())
require "zlib"
require "json"
require "stackprof"
module Flamegraph
def self.generate(filename = nil, opts = {})
fidelity = opts[:fidelity] || 0.5
profile = JSON.generate(
StackProf.run(
mode: :wall,
raw: true,
aggregate: false,
interval: (fidelity * 1000).to_i
) do
yield
end
)
if filename
File.open(filename, "w") do |f|
f.write(profile)
end
end
profile
end
end
module Rack
class MiniProfiler
def flamegraph(graph)
headers = {
"Content-Type" => "application/x-download",
"Content-disposition" => "attachment; filename=\"#{Time.now.to_i}.json.gz\"",
"Access-Control-Allow-Origin" => "https://www.speedscope.app"
}
gz = Zlib::GzipWriter.new(StringIO.new)
gz << graph
[200, headers, [gz.close.string]]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment