1 | # frozen_string_literal: true |
||
2 | |||
3 | 1 | module NoSE |
|
4 | # Tracks the runtime of various functions and outputs a measurement |
||
5 | 1 | class Timer |
|
6 | # Start tracking function runtime |
||
7 | # @return [void] |
||
8 | 1 | def self.enable |
|
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
9 | traced = { |
||
10 | IndexEnumerator => [ |
||
0 ignored issues
–
show
|
|||
11 | :indexes_for_workload, |
||
12 | :support_indexes, |
||
13 | :combine_indexes |
||
14 | ], |
||
15 | Search::Search => [ |
||
0 ignored issues
–
show
|
|||
16 | :query_costs, |
||
17 | :update_costs, |
||
18 | :search_overlap, |
||
19 | :solve_mipper |
||
20 | ], |
||
21 | Search::Problem => [ |
||
0 ignored issues
–
show
|
|||
22 | :setup_model, |
||
23 | :add_variables, |
||
24 | :add_constraints, |
||
25 | :define_objective, |
||
26 | :total_cost, |
||
27 | :add_update_costs, |
||
28 | :total_size, |
||
29 | :total_indexes, |
||
30 | :solve |
||
31 | ], |
||
32 | MIPPeR::CbcModel => [ |
||
0 ignored issues
–
show
|
|||
33 | :add_constraints, |
||
34 | :add_variables, |
||
35 | :update, |
||
36 | :optimize |
||
37 | ] |
||
38 | } |
||
39 | @old_methods = Hash.new { |h, k| h[k] = {} } |
||
40 | |||
41 | # Redefine each method to capture timing information on each call |
||
42 | traced.each do |cls, methods| |
||
43 | methods.each do |method| |
||
44 | old_method = cls.instance_method(method) |
||
45 | cls.send(:define_method, method) do |*args| |
||
46 | $stderr.puts "#{cls}##{method}\tSTART" |
||
0 ignored issues
–
show
|
|||
47 | |||
48 | start = Time.now.utc |
||
49 | result = old_method.bind(self).call(*args) |
||
50 | elapsed = Time.now.utc - start |
||
51 | |||
52 | # Allow a block to be called with the timing results |
||
53 | yield cls, method, elapsed if block_given? |
||
54 | |||
55 | $stderr.puts "#{cls}##{method}\tEND\t#{elapsed}" |
||
0 ignored issues
–
show
|
|||
56 | |||
57 | result |
||
58 | end |
||
59 | |||
60 | # Save a copy of the old method for later |
||
61 | @old_methods[cls][method] = old_method |
||
62 | end |
||
63 | end |
||
64 | end |
||
65 | |||
66 | # Stop tracking function runtime |
||
67 | # @return [void] |
||
68 | 1 | def self.disable |
|
69 | @old_methods.each do |cls, methods| |
||
70 | methods.each do |method, old_method| |
||
71 | cls.send(:define_method, method, old_method) |
||
72 | end |
||
73 | end |
||
74 | |||
75 | # Remove the saved method definitions |
||
76 | @old_methods.clear |
||
77 | end |
||
78 | end |
||
79 | end |
||
80 |