Created
May 29, 2014 04:09
-
-
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`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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