Class: OML4R::Benchmark

Inherits:
Object
  • Object
show all
Defined in:
lib/oml4r/benchmark.rb

Overview

Monitor the CPU consumption of a block and report it to OML

Defined Under Namespace

Classes: BenchmarkMP

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bm(label, opts = {}, &block) ⇒ Object



23
24
25
26
27
# File 'lib/oml4r/benchmark.rb', line 23

def self.bm(label, opts = {}, &block)
  inst = self.new(label, opts)
  inst.measure(&block) if block
  inst
end

Instance Method Details

#measure(&block) ⇒ Object

Measure execution of ‘block’. Benchmarking is assumed to be finished when block finished. Don’t attempt to call again



33
34
35
36
37
38
# File 'lib/oml4r/benchmark.rb', line 33

def measure(&block)
  raise "Missing block" unless block
  start
  block.arity == 0 ? block.call() : block.call(self)
  _stop
end

#pauseObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/oml4r/benchmark.rb', line 70

def pause()
  t, r = Process.times, Time.now
  @lock.synchronize do
   return unless @running
    return if @paused
    @paused = true

    @paused_t = t
    @paused_r = r.to_f
  end
end

#report(label = nil) ⇒ Object

Push out an intermediate report. Does nothing if already finished.

NOTE: Not thread safe



117
118
119
120
# File 'lib/oml4r/benchmark.rb', line 117

def report(label = nil)
  return unless @running
  _report(label)
end

#resumeObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/oml4r/benchmark.rb', line 82

def resume()
  @lock.synchronize do
    unless @running
      # hasn't been kicked off yet
      start
      return
    end

    return unless @paused
    t, r = Process.times, Time.now
    off_u = t.utime - @paused_t.utime
    off_s = t.stime - @paused_t.stime
    off_t = r.to_f - @paused_r
    @t0 = Struct::Tms.new(@t0.utime + off_u, @t0.stime + off_s)
    @r0 = @r0 + off_t
    if @t1
      @t1 = Struct::Tms.new(@t1.utime + off_u, @t1.stime + off_s)
      @r1 = @r1 + off_t
    end
    @paused = false
  end
end

#startObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/oml4r/benchmark.rb', line 51

def start()
  @lock.synchronize do
    raise "Don't call this directly" if @running
    @running = true
  end

  if @monitor_interval > 0
    Thread.new do
      while @running
        sleep @monitor_interval
        _report()
      end
    end
  end
  @t0, r0 = Process.times, Time.now
  @t1 = @t0
  @r1 = @r0 = r0.to_f
end

#step(cnt = 1) ⇒ Object

Report a step in the processing. Used to calculate a progress rate

NOTE: Not thread safe



127
128
129
# File 'lib/oml4r/benchmark.rb', line 127

def step(cnt = 1)
  @step_cnt += cnt
end

#stopObject



105
106
107
108
109
110
# File 'lib/oml4r/benchmark.rb', line 105

def stop
  @lock.synchronize do
    return unless @running
  end
  _stop
end

#task(&block) ⇒ Object

Execute block and add execution time to overall measurements. Can be called multiple time. Need to finally call ‘#stop’ to report overall stats.



44
45
46
47
48
49
# File 'lib/oml4r/benchmark.rb', line 44

def task(&block)
  raise "Missing block" unless block
  resume
  block.arity == 0 ? block.call() : block.call(self)
  pause
end