Class: TimedReport

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

Instance Method Summary collapse

Constructor Details

#initialize(named = nil, enabled = true) ⇒ TimedReport

Creates a new TimedReport instance. If named is given it will show the name in the report.

Example:

>> tr = TimedReport.new("Test")

Arguments:

named: (String)
enabled: (boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/timed_report.rb', line 13

def initialize named = nil, enabled = true
  @enabled = enabled
  @groups = {}
  @infos = []
  @intermediate_output = false

  @start_time = Time.now
  @step_time = Time.now

  @output = "\n=========================="
  
  if named != nil
    @output += "\nReport: #{named}"
    @output += "\n--------------------------"
  end

  @output_methods = [lambda{|m| puts(m)}]
end

Instance Method Details

#_puke(txt) ⇒ Object

Puke a string to all specified output methods.

Arguments:

txt: (String)


152
153
154
155
156
# File 'lib/timed_report.rb', line 152

def _puke txt
  @output_methods.each do |om|
    om.call(txt)
  end
end

#add(txt, with_time = true) ⇒ Object

Adds a line of text including the difference in seconds between the last #add or #time_step.

Example:

>> tr.add("one step")

Arguments:

txt: (String)
with_time: (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/timed_report.rb', line 74

def add txt, with_time = true
  return nil unless @enabled
  if with_time
    timing_info = "%.5f: " % (Time.now - @step_time)
    time_step()
  else
    timing_info = ""
  end

  @output += "\n#{timing_info}#{txt}"

  _puke("\n#{timing_info}#{txt}") if @intermediate_output
  "❤"
end

#add_g(group, with_time = true) ⇒ Object

Adds a time count with key between the last #add or #add_g or #time_step.

Example:

>> tr.add_g("one step")

Arguments:

group: (String)
with_time: (Boolean)


97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/timed_report.rb', line 97

def add_g group, with_time = true
  return nil unless @enabled
  @groups[group.to_sym] ||= {t:0,n:0}

  if with_time
    @groups[group.to_sym][:t] += (Time.now - @step_time)
    time_step()
  end

  @groups[group.to_sym][:n] += 1

  "❤"
end

#add_output_method(method) ⇒ Object

Add a Proc or lambda that takes the report as argument.

This way you can for example add Rails.logger.info or send an email besides printing to STDOUT.

Example:

>> tr.add_output_method(lambda{|report| Dummy.send_email(report)})

Arguments:

method: (Proc or lambda)


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

def add_output_method method
  return nil unless @enabled
  @output_methods.push method
end

#finishObject

Finish the report. Call this add the end to print the report!

Example:

>> tr.finish()


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/timed_report.rb', line 129

def finish
  return nil unless @enabled
  
  @output += "\n--------------------------" if @groups.length > 0
  @groups.each do |k,v|
    @output += "\n#{k}: %.5f (%d calls, %.5f avg.)" % [v[:t], v[:n], v[:t]/v[:n].to_f]
  end
  
  @output += "\n---------- INFO ----------"
  @infos.each do |v|
    @output += "\n#{v}"
  end

  @output += "\nTotal time: %.5f" % (Time.now-@start_time)
  @output += "\n=========================="
  _puke(@output)
  "❤"
end

#info(txt) ⇒ Object

Adds info without time.

Example:

>> tr.info("happy: yes")

Arguments:

txt: (String)


118
119
120
121
122
123
# File 'lib/timed_report.rb', line 118

def info txt
  return nil unless @enabled
  @infos.push(txt)

  "❤"
end

#intermediate_output(val = @intermediate_output) ⇒ Object

Set to true if you want intermediate output to show

Arguments:

val: (Boolean)


36
37
38
39
40
# File 'lib/timed_report.rb', line 36

def intermediate_output val = @intermediate_output
  return nil unless @enabled
  @intermediate_output = val
  val
end

#time_stepObject

Increments time when called.

Example:

>> tr.time_step()


60
61
62
63
64
# File 'lib/timed_report.rb', line 60

def time_step
  return nil unless @enabled
  @step_time = Time.now
  "❤"
end