Class: NoSE::Timer

Inherits:
Object show all
Defined in:
lib/nose/timing.rb

Overview

Tracks the runtime of various functions and outputs a measurement

Class Method Summary collapse

Class Method Details

.disablevoid

This method returns an undefined value.

Stop tracking function runtime



68
69
70
71
72
73
74
75
76
77
# File 'lib/nose/timing.rb', line 68

def self.disable
  @old_methods.each do |cls, methods|
    methods.each do |method, old_method|
      cls.send(:define_method, method, old_method)
    end
  end

  # Remove the saved method definitions
  @old_methods.clear
end

.enablevoid

This method returns an undefined value.

Start tracking function runtime



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nose/timing.rb', line 8

def self.enable
  traced = {
    IndexEnumerator => [
      :indexes_for_workload,
      :support_indexes,
      :combine_indexes
    ],
    Search::Search => [
      :query_costs,
      :update_costs,
      :search_overlap,
      :solve_mipper
    ],
    Search::Problem => [
      :setup_model,
      :add_variables,
      :add_constraints,
      :define_objective,
      :total_cost,
      :add_update_costs,
      :total_size,
      :total_indexes,
      :solve
    ],
    MIPPeR::CbcModel => [
      :add_constraints,
      :add_variables,
      :update,
      :optimize
    ]
  }
  @old_methods = Hash.new { |h, k| h[k] = {} }

  # Redefine each method to capture timing information on each call
  traced.each do |cls, methods|
    methods.each do |method|
      old_method = cls.instance_method(method)
      cls.send(:define_method, method) do |*args|
        $stderr.puts "#{cls}##{method}\tSTART"

        start = Time.now.utc
        result = old_method.bind(self).call(*args)
        elapsed = Time.now.utc - start

        # Allow a block to be called with the timing results
        yield cls, method, elapsed if block_given?

        $stderr.puts "#{cls}##{method}\tEND\t#{elapsed}"

        result
      end

      # Save a copy of the old method for later
      @old_methods[cls][method] = old_method
    end
  end
end