Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created March 26, 2015 21:15
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 tenderlove/33e8496c3dc3e0a7da42 to your computer and use it in GitHub Desktop.
Save tenderlove/33e8496c3dc3e0a7da42 to your computer and use it in GitHub Desktop.
require 'sprockets'
require 'stackprof'
require 'tmpdir'
require 'fileutils'
require 'allocation_tracer'
require 'minitest/autorun'
Class.new(Minitest::Test) do
attr_reader :path, :environment, :env, :n
def setup
@path = Dir.mktmpdir
@pwd = Dir.pwd
Dir.chdir @path
js_dir = File.join path, 'app', 'assets', 'javascripts'
js_contents = "function test() { }"
FileUtils.mkdir_p js_dir
File.open(File.join(js_dir, 'test.js'), 'w') { |f|
f.write js_contents
}
# sanity check
assert_equal js_contents, File.read(File.join(js_dir, 'test.js'))
@env = {
"REQUEST_METHOD" => "GET",
"QUERY_STRING" => "body=1",
"PATH_INFO" => "test.js"
}
@environment = Sprockets::Environment.new
@environment.append_path 'app/assets/javascripts'
@n = 5000
end
def teardown
Dir.chdir @pwd
FileUtils.remove_entry @path
end
def test_allocations
ObjectSpace::AllocationTracer.setup(%i{path line type})
result = ObjectSpace::AllocationTracer.trace {
n.times { |i|
status, headers, body = environment.call env
body.each { |x| x }
}
}
result.sort_by { |k,v| v.first }.each do |k,v|
p k => v
end
end
def test_runtime
StackProf.run(mode: :wall, out: 'out.dump') do
n.times { |i|
status, headers, body = environment.call env
body.each { |x| x }
}
end
system "stackprof out.dump"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment