Class: TimeMethod::StoreMeasurement

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/time_method/store_measurement.rb

Instance Method Summary collapse

Constructor Details

#initializeStoreMeasurement

Returns a new instance of StoreMeasurement.



4
5
6
7
8
9
# File 'lib/time_method/store_measurement.rb', line 4

def initialize
  @time_for_method     = []
  @methods_in_order    = []
  @depth               = 0
  @full_stack         = []
end

Instance Method Details

#add_to_stack(in_or_out, klass_name, method_name) ⇒ Object



11
12
13
14
15
16
# File 'lib/time_method/store_measurement.rb', line 11

def add_to_stack(in_or_out,klass_name,method_name)
  @depth+=1 if in_or_out == "in"
  @depth-=1 if in_or_out == "out"
  mod_klass_name=klass_name.split("::").last
  name="#{mod_klass_name}.#{method_name}"
end

#make_summary_arr(sort_by:) ⇒ Object



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
# File 'lib/time_method/store_measurement.rb', line 25

def make_summary_arr(sort_by:)
  updated_time_for_method = @time_for_method.map{|x| [x[0],(x[1].real*1000).round(2),"   "*x[2]]}
  h=updated_time_for_method.group_by{|a| a[0]}
  new_h={}
  h.each do |k,v|
    sum = 0
    count=0
    v.each do |el|
      sum+=el[1]
      count+=1
    end   
    avg = (sum/count)
    tabs = v[0][2]
    new_h[k]=[sum,count,avg,tabs]
  end 
  new_arr=[]    
  new_h.each do |k,v|
    k_with_tabs = v[3]+k
    method_and_class_name=k_with_tabs.scan(/[a-zA-Z::_0-9\t ]+/)
    new_arr.push([*method_and_class_name,*v])
  end

  if sort_by == "run_time"
    new_arr.map! {|x| [x[0].gsub(" ",""),x[1],x[2].round(0),x[3].round(0),x[4].round(0)]}
    new_arr.sort! {|a,b| b[2] <=> a[2]}
    title = "Sorted List of Methods By Total Run Time"
  elsif sort_by == "execution_order"
    new_arr.map! {|x| [x[0],x[1],x[2].round(0),x[3].round(0),x[4].round(0)]}
    new_arr = new_arr.reverse
    title = "Sorted List of Methods By Total Execution Order"
  end 
  
  headings=[ "Class Name", "Method Name", "Total Run Time (ms)","# of Calls","Average Time Per Call (ms)"] 
  table = Terminal::Table.new :title => title ,:headings => headings do |t|
    t.rows = new_arr
  end 
  return table
end

#make_summary_arr_execution_orderObject



73
74
75
# File 'lib/time_method/store_measurement.rb', line 73

def make_summary_arr_execution_order
  @summary_arr_execution_order = make_summary_arr(sort_by:"execution_order")
end

#make_summary_arr_run_timeObject



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

def make_summary_arr_run_time
  @summary_arr = make_summary_arr(sort_by:"run_time")
end


85
86
87
88
89
90
91
# File 'lib/time_method/store_measurement.rb', line 85

def print_all
  make_summary_arr_run_time
  make_summary_arr_execution_order
  print_summary_arr_run_time
  print_summary_arr_execution_order
  @time_for_method=[]
end


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

def print_summary_arr
  puts @summary_arr
end


77
78
79
# File 'lib/time_method/store_measurement.rb', line 77

def print_summary_arr_execution_order
  puts @summary_arr_execution_order
end


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

def print_summary_arr_run_time
  puts @summary_arr
end

#store(klass_name:, method_name:, time:) ⇒ Object

can add some logic here when/while storing as well



19
20
21
22
23
# File 'lib/time_method/store_measurement.rb', line 19

def store(klass_name:,method_name:,time:)
  mod_klass_name=klass_name.split("::").last
  name="#{mod_klass_name}.#{method_name}"
  @time_for_method.push([name,time,@depth])
end