Skip to content

Instantly share code, notes, and snippets.

@tangentus
Created February 8, 2021 17:16
Show Gist options
  • Save tangentus/1955976675d96be25efd827ce83cf4a7 to your computer and use it in GitHub Desktop.
Save tangentus/1955976675d96be25efd827ce83cf4a7 to your computer and use it in GitHub Desktop.
A Concern to ease benchmarking methods and allow for file output
module Benchmarkable
extend ActiveSupport::Concern
class_methods do
attr_reader :benchmarkable_file_path
def benchmark_to(file_path)
@benchmarkable_file_path = file_path
end
end
included do
def report(label, &block)
result = nil
File.open(self.class.benchmarkable_file_path, "a") do |f|
report = Benchmark.bm do |x|
x.report(label) do
result = block.call
end
end
f.write("user system total real\n")
report.each_entry do |entry|
f.write(label.to_s + entry.to_s)
end
end
result
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment