Class: BetterRailsDebugger::Analyzer

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/better_rails_debugger/analyzer.rb

Instance Method Summary collapse

Instance Method Details

#analyze(identifier, group_id) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/better_rails_debugger/analyzer.rb', line 8

def analyze(identifier, group_id)
  begin
    group = ::BetterRailsDebugger::AnalysisGroup.find group_id
  rescue Mongoid::Errors::DocumentNotFound
    # If the group does not exist, just run the code and return
    yield
    return
  end
  start_trace_point group
  # If we reached the max time to execute the code, just execute the code and do not collect information
  if times_to_run_exceeded?(group) or skip_instance?(group, identifier)
    yield
  else
    ::ObjectSpace.trace_object_allocations do
      yield
    end
  end
  end_trace_point
  collect_information(identifier, group_id)
end

#clear_trackingObject

Clean ObjectSpace abject allocation tracer history



50
51
52
# File 'lib/better_rails_debugger/analyzer.rb', line 50

def clear_tracking
  ::ObjectSpace.trace_object_allocations_clear
end

#collect_information(identifier, group_id) ⇒ Object

Record into db, information about object creation



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/better_rails_debugger/analyzer.rb', line 63

def collect_information(identifier, group_id)
  group = ::BetterRailsDebugger::AnalysisGroup.find group_id
  if not group.present?
    Rails.logger.error "[BetterRailsDebugger] Group '#{recorded[:group_id]}' not found. Skiping..."
    return
  end

  # Load Mongo db if required
  if not Mongoid::Config.configured?
    Mongoid.load!(BetterRailsDebugger::Configuration.instance.mongoid_config_file, Rails.env.to_sym)
    Mongoid.logger.level = Logger::FATAL
  end

  instance = ::BetterRailsDebugger::GroupInstance.create identifier: identifier, analysis_group_id: group_id, caller_file: caller[3][/[^:]+/], status: 'pending'

  collect_memory_information(instance)
  collect_trace_point_history(instance)

  # Now, it's time to analyze all collected data and generate a report
  ::BetterRailsDebugger::AnalysisRecorderJob.perform_later({ instance_id: instance.id.to_s })
end

#end_trace_pointObject



37
38
39
# File 'lib/better_rails_debugger/analyzer.rb', line 37

def end_trace_point
  tracer.disable
end

#skip_instance?(group, identifier) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/better_rails_debugger/analyzer.rb', line 58

def skip_instance?(group, identifier)
  !group.analyze_repeated_instances and GroupInstance.where(identifier: identifier, analysis_group: group.id).count > 0
end

#start_trace_point(group) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/better_rails_debugger/analyzer.rb', line 29

def start_trace_point(group)
  if group.generate_method_execution_history
    @trace_point_history = []
    tracer
    tracer.enable
  end
end

#times_to_run_exceeded?(group) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/better_rails_debugger/analyzer.rb', line 54

def times_to_run_exceeded?(group)
  group.times_to_run.to_i > 0 and group.group_instances.count >= group.times_to_run
end

#tracerObject



41
42
43
44
45
46
47
# File 'lib/better_rails_debugger/analyzer.rb', line 41

def tracer
  return @tracer if @tracer
  @tracer = TracePoint.new do |tp|
    # Record everything but us
    @trace_point_history << {source_file: tp.path, source_line: tp.lineno, method_id: tp.method_id, event: tp.event.to_s} if tp.path !~ /better_rails_debugger/
  end
end