Module: TimeMethod

Defined in:
lib/time_method.rb,
lib/time_method/version.rb,
lib/time_method/measurement.rb,
lib/time_method/class_methods.rb,
lib/time_method/configuration.rb,
lib/time_method/store_measurement.rb

Defined Under Namespace

Modules: ClassMethods Classes: Configuration, Measurement, StoreMeasurement

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.class_interceptor_name_for(base_class) ⇒ Object



67
68
69
# File 'lib/time_method.rb', line 67

def class_interceptor_name_for(base_class)
  "#{base_class.time_method_name}ClassInterceptor"
end

.configurationObject



40
41
42
# File 'lib/time_method.rb', line 40

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



8
9
10
# File 'lib/time_method.rb', line 8

def configure
  yield(configuration)
end

.included(base_class) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/time_method.rb', line 44

def included(base_class)
  base_class.extend ClassMethods
  # below creates two modules and then makes
  # them accessible through a constant name
  instance_interceptor = const_set(
    instance_interceptor_name_for(base_class),
    interceptor_module_for(base_class)
  )
  class_interceptor = const_set(
    class_interceptor_name_for(base_class),
    interceptor_module_for(base_class)
  )

  return unless should_run_and_time_methods?

  base_class.prepend instance_interceptor
  base_class.singleton_class.prepend class_interceptor
end

.instance_interceptor_name_for(base_class) ⇒ Object



63
64
65
# File 'lib/time_method.rb', line 63

def instance_interceptor_name_for(base_class)
  "#{base_class.time_method_name}InstanceInterceptor"
end

.interceptor_module_for(base_class) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/time_method.rb', line 72

def interceptor_module_for(base_class)
  Module.new do 
    @klass_name = base_class
    def self.klass_name
      @klass_name
    end 
  end
end

.measure(klass_name: nil, method_name: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/time_method.rb', line 12

def measure(klass_name: nil, method_name: nil)
  t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  StoreMeasurement.instance.add_to_stack(
    "in",klass_name,method_name
    )
  block_return_value = yield if block_given?
  StoreMeasurement.instance.add_to_stack(
    "out", klass_name, method_name
  )
  t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  begin
    measurement = TimeMethod::Measurement.new(
      klass_name: klass_name.to_s, 
      method_name: method_name.to_s,
      t0: t0, 
      t1: t1
    )
  rescue => e
    puts  e,e.backtrace[0..3]
  end

  block_return_value
end


36
37
38
# File 'lib/time_method.rb', line 36

def print_all_run_times
  StoreMeasurement.instance.print_all
end

.should_run_and_time_methods?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/time_method.rb', line 81

def should_run_and_time_methods?
  configuration.enable_time_method
end