Skip to content

Instantly share code, notes, and snippets.

@hiroara
Created May 29, 2014 04:09
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 hiroara/8e425ef1046ef313d865 to your computer and use it in GitHub Desktop.
Save hiroara/8e425ef1046ef313d865 to your computer and use it in GitHub Desktop.
Rspec Benchmarking script. Put this file under `spec/supports/` and execute `BENCHMARK=1 rake spec`.
RSpec.configure do |config|
next unless ENV['BENCHMARK'] == '1'
benchmarks = []
metadata_keys = %i[ file_path line_number described_class ]
info_keys = %i[ type ] + metadata_keys + %i[ full_description ]
log_file = Rails.root.join('log', 'rspec_benchmark.csv')
config.before(:suite) do
CSV.open(log_file, "wb") do |csv|
csv << info_keys + %i[ utime stime cutime cstime real]
end
end
config.around(:each) do |ex|
example_info = metadata_keys.inject({}) { |memo, key| memo[key] = example.metadata[key]; memo }
example_info[:type] = example.metadata[:file_path].match(/spec\/([^\/]*)/)[1]
example_info[:full_description] = example.metadata.full_description
bm = Benchmark.measure { ex.run }
CSV.open(log_file, "a") do |csv|
csv << ((info_keys).map { |key| example_info[key] }) + bm.to_a[1..-1]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment