Module: Etalon
- Defined in:
- lib/etalon.rb,
lib/etalon/version.rb
Overview
Etalon is a simple tool to instrument Ruby code and output basic metrics to a logger or store them in a hash.
Constant Summary collapse
- VERSION =
:no-doc
"1.0.0"
Class Method Summary collapse
-
.activate ⇒ Boolean
Activates Etalon.
-
.active? ⇒ Boolean
Whether Etalon is active and recording metrics.
-
.deactivate ⇒ Boolean
Deactivates Etalon.
-
.print_timings ⇒ Hash
Uses either Rails.logger or a Syslog::Logger instance to print out iteration count, minimum, maximum, average and standard deviation timings for each individual call instrumented by #time.
-
.reset_timings ⇒ type
Resets Etalon’s internal storage to remove all stored timings.
-
.time(identifier) ⇒ Object
Runs timing metrics on the supplied block of code and stores those metrics for later logging or analysis using the supplied @identifier.
Class Method Details
.activate ⇒ Boolean
Activates Etalon.
93 94 95 |
# File 'lib/etalon.rb', line 93 def activate !!ENV["ETALON_ACTIVE"] = "true" end |
.active? ⇒ Boolean
Returns whether Etalon is active and recording metrics.
10 11 12 13 14 |
# File 'lib/etalon.rb', line 10 def active? # rubocop:disable Style/DoubleNegation !!ENV["ETALON_ACTIVE"] # rubocop:enable Style/DoubleNegation end |
.deactivate ⇒ Boolean
Deactivates Etalon.
101 102 103 |
# File 'lib/etalon.rb', line 101 def deactivate !!ENV["ETALON_ACTIVE"] = nil end |
.print_timings ⇒ Hash
Uses either Rails.logger or a Syslog::Logger instance to print out iteration count, minimum, maximum, average and standard deviation timings for each individual call instrumented by #time.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/etalon.rb', line 55 def print_timings if active? instrument_store.each_with_object({}) do |(key, metrics), memo| count, min, max, all = metrics.values_at(:count, :min, :max, :all) top = all.sort.reverse.take(5) mean = mean(all).floor(2) deviation = standard_deviation(all).floor(2) title = key.to_s.titleize output = [ "count: #{count}", "min: #{min}", "max: #{max}", "mean: #{mean}", "deviation: ±#{deviation}%", "top 5: #{top}", ] logger.debug("#{title} - #{output.join(" | ")}") memo[key] = output end end end |
.reset_timings ⇒ type
Resets Etalon’s internal storage to remove all stored timings.
85 86 87 |
# File 'lib/etalon.rb', line 85 def reset_timings @instrument_store = nil end |
.time(identifier) ⇒ Object
Runs timing metrics on the supplied block of code and stores those metrics for later logging or analysis using the supplied @identifier.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/etalon.rb', line 23 def time(identifier) if active? unless block_given? raise "Please supply a block of code for Etalon to instrument" end start = Time.now return_value = yield duration = elapsed(start) key = key_from(identifier: identifier) store = instrument_store_for(key: key) store[:count] += 1 store[:min] = duration if duration < store[:min] store[:max] = duration if duration > store[:max] store[:all] << duration return_value else yield end end |