Module: Kantox::Chronoscope::Generic

Defined in:
lib/kantox/chronoscope/generic.rb,
lib/kantox/chronoscope.rb

Overview

Dummy module to be used in production-like environments.

Constant Summary collapse

COLOR_VIVID =
"\033[#{Kantox::Chronoscope.kungfuig.colors!.vivid || '01;38;05;226'}m".freeze
COLOR_PALE =
"\033[#{Kantox::Chronoscope.kungfuig.colors!.pale || '01;38;05;178'}m".freeze
COLOR_WARN =
"\033[#{Kantox::Chronoscope.kungfuig.colors!.warn || '01;38;05;173'}m".freeze
LOGGER =
Kantox::Chronoscope.kungfuig.logger && Kernel.const_get(Kantox::Chronoscope.kungfuig.logger).new ||
Kernel.const_defined?(:Rails) && ::Rails.logger ||
Logger.new(STDOUT)
LANG =
Kantox::Chronoscope.kungfuig.lang || :en
I18N =
Kantox::Chronoscope.kungfuig.i18n!.public_send("#{LANG}!")
BM_DELIMITER =
(I18N.bm_delimiter || ' :: ').to_s.freeze
ARROW =
(I18N.arrow || '').to_s.freeze
METHOD_LABEL =
(I18N.name || 'method').freeze
TIMES_LABEL =
(I18N.times || 'times').freeze
AVERAGE_LABEL =
(I18N.average || 'average').freeze
TOTAL_LABEL =
(I18N.total || 'total').freeze
COLOR_NONE =
"\033[0m".freeze
DEFAULT_TAG =
'N/A'.freeze
LOGGER_TAG =
'CHRONOS'.freeze
@@chronoscope_data =
{}
@@★ =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inject(top = nil) ⇒ Object

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength rubocop:enable Style/MethodName



146
147
148
149
# File 'lib/kantox/chronoscope/generic.rb', line 146

def inject(top = nil)
  top ||= Object
  top.send :prepend, Kantox::Chronoscope::Generic
end

Instance Method Details

#(arg = DEFAULT_TAG, log = false) ⇒ Object Also known as: watch

rubocop:disable Style/MethodName rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kantox/chronoscope/generic.rb', line 32

def (arg = DEFAULT_TAG, log = false)
  return (@@chronoscope_data[arg.to_s] = nil) unless block_given? # pass no block to reset

  result = nil # needed for it to be defined in this scope
  @@chronoscope_data[arg.to_s] ||= { count: 0, total: 0, stack: @@★.dup }
  @@★.unshift arg.to_s
  begin
    (Benchmark.measure { result = yield }).tap do |bm|
      @@chronoscope_data[arg.to_s][:count] += 1
      @@chronoscope_data[arg.to_s][:total] += bm.real
      LOGGER.debug log_bm arg, bm if log
    end
    result
  rescue => e
    LOGGER.debug log_problem arg, e
    yield # re-try without any wrappers. If it will raise anyway— fine
  ensure
    @@★.shift
  end
end

#(cleanup: true, count: 18, log: true) ⇒ Object Also known as: harvest

FIXME: total currently adds up all calls, including nested

I am not sure if it is correct ot not, so leaving it for now


56
57
58
59
60
61
62
63
64
# File 'lib/kantox/chronoscope/generic.rb', line 56

def (cleanup: true, count: 18, log: true) # Yes, 18 is my fave number)
  return if @@chronoscope_data.empty?

  log_report(count).tap do |log_hash|
    LOGGER.debug(log_hash[:string]) if log
    log_hash[:data] = @@chronoscope_data.map { |k, v| [k, v.dup] }.to_h
    @@chronoscope_data.clear if cleanup
  end
end