Class: MiniProf

Inherits:
Object
  • Object
show all
Defined in:
lib/miniprof.rb,
lib/miniprof/version.rb

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(marker = "", opts = {}) ⇒ MiniProf

Initialize the profiler

The optional marker will prefix each message, so you can distingish the output of multiple profilers.



14
15
16
17
18
19
20
# File 'lib/miniprof.rb', line 14

def initialize marker="", opts={}
  @start_time = seconds_since_epoch
  @last_tick_time = @start_time
  @marker = marker
  @enabled = true
  @enabled = opts[:enabled] if opts.has_key?(:enabled)
end

Class Method Details

.profile(marker = "", opts = {}, &block) ⇒ Object

Create a profiler object and execute it. The optional marker will print at the beginning of each line of output.



51
52
53
54
# File 'lib/miniprof.rb', line 51

def self.profile marker="",opts={}, &block
  mp = MiniProf.new marker, opts
  mp.profile &block
end

Instance Method Details

#<<(msg) ⇒ Object

Print out a mesage indicating how long it’s been since the last step and since the start of profiling



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/miniprof.rb', line 36

def << msg
  return if @enabled == false

  new_time = seconds_since_epoch
  since_start = seconds_to_ms(new_time - @start_time)
  since_last = seconds_to_ms(new_time - @last_tick_time)
  @last_tick_time = new_time

  msg = "Tick: (#{since_last} ms since last, #{since_start} ms since start) #{msg}"
  msg = @marker + " " + msg if !@marker.empty?
  puts msg
end

#profile(&block) ⇒ Object

Main loop



23
24
25
26
27
28
# File 'lib/miniprof.rb', line 23

def profile &block
  self.<< "Started mini_prof..."
  return_value = yield self
  self.<< "Ended mini_prof..."
  return return_value
end

#seconds_since_epochObject

Get the seconds since epoch. using to_f provides sub-second accuracy



6
7
8
# File 'lib/miniprof.rb', line 6

def seconds_since_epoch
  Time.now.to_f
end

#seconds_to_ms(seconds) ⇒ Object



30
31
32
# File 'lib/miniprof.rb', line 30

def seconds_to_ms seconds
  (seconds * 1000).to_i
end