Module: BenchmarkMethods

Defined in:
lib/benchmark_methods.rb,
lib/benchmark_methods/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"0.7"

Class Method Summary collapse

Class Method Details

.included(base_klass) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/benchmark_methods.rb', line 8

def self.included(base_klass)
  unless const_defined?("#{base_klass.name.demodulize_for_bm}Interceptor")
    base_klass.extend(ClassMethods)
    interceptor = const_set("#{base_klass.name.demodulize_for_bm}Interceptor", Module.new)
    base_klass.send(:prepend, interceptor)
  end
end

.log(report) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/benchmark_methods.rb', line 16

def self.log(report)
  if const_defined?("Rails")
    Rails.logger.debug("  --> #{report.label}")
    Rails.logger.debug("#{Benchmark::CAPTION}#{report.to_s}")
  else
    puts("  --> #{report.label}")
    puts(Benchmark::CAPTION)
    puts(report)
  end
end

.measure(label, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/benchmark_methods.rb', line 27

def self.measure(label, &block)
  t0, r0 = Process.times, Time.now
  result = yield
  t1, r1 = Process.times, Time.now
  report = Benchmark::Tms.new(t1.utime  - t0.utime,
                     t1.stime  - t0.stime,
                     t1.cutime - t0.cutime,
                     t1.cstime - t0.cstime,
                     r1 - r0,
                     label)
  BenchmarkMethods::log(report)
  result
end