Class: Neolytics::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/neolytics/recorder.rb

Instance Method Summary collapse

Constructor Details

#initialize(neo4j_session) ⇒ Recorder

Returns a new instance of Recorder.



8
9
10
11
# File 'lib/neolytics/recorder.rb', line 8

def initialize(neo4j_session)
  @neo4j_session = neo4j_session
  @neo4apis_session = Neo4Apis::Neolytics.new(neo4j_session)
end

Instance Method Details

#record(&block) ⇒ Object



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
# File 'lib/neolytics/recorder.rb', line 13

def record(&block)
  @neo4apis_session.batch do
    record_execution_trace do
      begin
        block.call
      rescue Exception => e
        nil
      end
    end

    file_paths = @neo4j_session.query("MATCH (tp) WHERE NOT(tp.path IS NULL) RETURN DISTINCT tp.path AS path").map(&:path)
    file_paths.each(&method(:record_ast))

    link_query = <<QUERY
MATCH (tp:TracePoint), (node:ASTNode)
WHERE
  node.file_path = tp.path AND
  node.first_line = tp.lineno AND
  node.name = tp.method_id AND
  node.type = 'def'
MERGE (tp)-[:HAS_AST_NODE]->(node)
QUERY
    @neo4j_session.query(link_query)
  end
end

#record_ast(file_path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/neolytics/recorder.rb', line 40

def record_ast(file_path)
  full_path = Pathname.new(file_path).realpath.to_s

  code = File.read(full_path)

  require 'parser/current'
  root = Parser::CurrentRuby.parse(code)

  file_node = @neo4apis_session.import :File, full_path, code
  node_node = @neo4apis_session.import :ASTNode, root, file_node
end

#record_execution_traceObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/neolytics/recorder.rb', line 53

def record_execution_trace
  execution_index = 0
  indent = 0
  output = ''
  last_tracepoint_node = nil
  last_start_time = nil
  ancestor_stack = []
  run_time_stack = []

  last_tracepoint_end_time = nil
  last_run_time = nil

  trace = TracePoint.new do |tp|
    begin
      last_run_time = 1_000_000.0 * (Time.now - last_tracepoint_end_time) if last_tracepoint_end_time

      output << tracepoint_string(tp, indent)

      last_method_time = nil
      if [:call, :c_call].include?(tp.event)
        run_time_stack.push(0)
      elsif [:return, :c_return].include?(tp.event)
        last_method_time = run_time_stack.pop
      else
        run_time_stack[-1] += last_run_time if run_time_stack[-1] && last_run_time
      end

      associated_call = nil
      if [:return, :c_return].include?(tp.event) && indent.nonzero?
        indent -= 1
        associated_call = ancestor_stack.pop
      elsif [:call, :c_call].include?(tp.event)
        indent += 1
      end

      last_tracepoint_node = @neo4apis_session.import :TracePoint, tp,
                          last_method_time,
                          (execution_index += 1),
                          last_tracepoint_node,
                          ancestor_stack.last,
                          associated_call
      
      if [:call, :c_call].include?(tp.event)
        ancestor_stack.push(last_tracepoint_node)
      end

      last_tracepoint_end_time = Time.now
    rescue Exception => e
      puts 'EXCEPTION!!'
      puts e.message
      puts e.backtrace
      exit!
    end
  end

  trace.enable
  yield
ensure
  trace.disable
  puts output
end