Module: BenchBloc::BenchmarkHelpers

Included in:
BenchBloc
Defined in:
lib/bench_bloc/helpers/benchmark_helpers.rb

Instance Method Summary collapse

Instance Method Details

#bm_format_result(result) ⇒ Object



28
29
30
# File 'lib/bench_bloc/helpers/benchmark_helpers.rb', line 28

def bm_format_result result
  "\t\t#{result.label}\n\t\t\t#{result.real.round(2)} seconds"
end

#bm_log_results(results, title) ⇒ Object



2
3
4
5
6
7
8
9
# File 'lib/bench_bloc/helpers/benchmark_helpers.rb', line 2

def bm_log_results results, title
  results.sort! { |a,b| b.real <=> a.real }
  formatted_results = results.map { |res| bm_format_result(res) }
  header = "\n---\n\t#{title}\n"
  summary = "\tTotal Time: #{bm_summarize_real_time(results).round(2)} seconds\n\n"
  final_results = header + summary + formatted_results.join("\n")
  write_to_log final_results
end

#bm_run_results(new_task, to_profs) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/bench_bloc/helpers/benchmark_helpers.rb', line 32

def bm_run_results new_task, to_profs
  Benchmark.bm do |x|
    to_profs.each do |tp|
      x.report(new_task[:label].call(tp)) do
        new_task[:prof].call(tp)
      end
    end
  end
end

#bm_summarize_real_time(results) ⇒ Object



22
23
24
25
26
# File 'lib/bench_bloc/helpers/benchmark_helpers.rb', line 22

def bm_summarize_real_time results
  results.inject(0) do |agg, res|
    agg + res.real
  end
end

#write_to_log(results) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/bench_bloc/helpers/benchmark_helpers.rb', line 11

def write_to_log results
  if defined?(Rails)
    log = Logger.new("#{Rails.root}/log/benchmarks.log")
    log.info(results)
  else
    f = File.new("benchmarks.log", "w")
    f.puts(results)
    f.close
  end
end