Created
February 8, 2021 17:16
-
-
Save tangentus/1955976675d96be25efd827ce83cf4a7 to your computer and use it in GitHub Desktop.
A Concern to ease benchmarking methods and allow for file output
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
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