Class: Profiler

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

Constant Summary collapse

INDENT =
"  "

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.measurementsObject (readonly)

Returns the value of attribute measurements.



9
10
11
# File 'lib/profiler.rb', line 9

def measurements
  @measurements
end

Class Method Details

.add_measurement(tag, seconds) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/profiler.rb', line 19

def add_measurement(tag, seconds)
	return unless profiling?

	@measurements[tag] ||= []

	@measurements[tag] << seconds
end

.measure(tag) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/profiler.rb', line 11

def measure(tag)
	start = time_now
	result = yield
	add_measurement(tag, time_now - start)

	result
end

.profiling?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/profiler.rb', line 39

def profiling?
	!ENV["OPS_PROFILE"].nil?
end

.summaryObject



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

def summary
	return unless profiling?

	@summary ||= measurements.reverse_each.each_with_object([]) do |(tag, values), output|
		output << "#{tag}:\n"
		values.sort.reverse.each do |value|
			value_str = format("%.3f", value * 1000)
			output << format("%<indent>s%9<value>sms\n", indent: INDENT, value: value_str)
		end
	end.join
end

.time_nowObject



43
44
45
# File 'lib/profiler.rb', line 43

def time_now
	Process.clock_gettime(Process::CLOCK_MONOTONIC)
end