File: example.rb

package info (click to toggle)
ruby-rspec-profiling 0.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 240 kB
  • sloc: ruby: 617; makefile: 51
file content (152 lines) | stat: -rw-r--r-- 3,437 bytes parent folder | download
1
2
3
4
5
6
7
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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
require "benchmark"
require "rspec_profiling/vcs/git"
require "rspec_profiling/vcs/svn"
require "rspec_profiling/vcs/git_svn"
require "rspec_profiling/collectors/csv"
require "rspec_profiling/collectors/json"

module RspecProfiling
  class Example
    IGNORED_QUERIES_PATTERN = %r{(
      pg_table|
      pg_attribute|
      pg_namespace|
      show\stables|
      pragma|
      sqlite_master/rollback|
      ^TRUNCATE TABLE|
      ^ALTER TABLE|
      ^BEGIN|
      ^COMMIT|
      ^ROLLBACK|
      ^RELEASE|
      ^SAVEPOINT
    )}xi

    def initialize(example)
      @example = example
      @counts  = Hash.new(0)
      @event_counts = Hash.new(0)
      @event_times = Hash.new(0)
      @event_events = Hash.new()
    end

    def file
      metadata[:file_path]
    end

    def line_number
      metadata[:line_number]
    end

    def description
      metadata[:full_description]
    end

    def status
      execution_result.status
    end

    def exception
      execution_result.exception
    end

    def time
      execution_result.run_time
    end

    def owner_tag
      ownership_for_file(metadata[:file_path])
    end

    def query_count
      counts[:query_count]
    end

    def query_time
      counts[:query_time]
    end

    def request_count
      counts[:request_count]
    end

    def request_time
      counts[:request_time]
    end

    attr_reader :event_counts, :event_times, :event_events

    def log_query(query, start, finish)
      unless query[:sql] =~ IGNORED_QUERIES_PATTERN
        counts[:query_count] += 1
        counts[:query_time] += (finish - start)
      end
    end

    def log_request(request, start, finish)
      counts[:request_count] += 1
      counts[:request_time] += request[:view_runtime].to_f
    end

    def log_event(event_name, event, start, finish)
      event_counts[event_name] += 1
      event_times[event_name] += (finish - start)
      event_events[event_name] ||= []
      if verbose_record_event?(event_name)
        begin
          event_events[event_name] << event.as_json
        rescue => e
          # no op
        end
      end
    end

    private

    attr_reader :example, :counts

    def execution_result
      @execution_result ||= begin
        result = example.execution_result
        result = OpenStruct.new(result) if result.is_a?(Hash)
        result
      end
    end

    def metadata
      example.metadata
    end

    def verbose_record_event?(event_name)
      metadata[:record_events].to_a.include?(event_name)
    end

    def ownership_for_file(file_path)
      return nil if RspecProfiling.config.magic_comment.empty?
      ownership_regex = /(^#\s*#{RspecProfiling.config.magic_comment}:\s*)\K(?<#{RspecProfiling.config.magic_comment}>.*$)/.freeze
      comments = top_comments_from_file(file_path)
      matching_line = comments.detect { |line| line.match?(ownership_regex) }
      extract_ownership(matching_line, ownership_regex) if matching_line
    end

    def top_comments_from_file(file_path)
      with_file(file_path) do |f|
        f.take_while { |line| line.start_with?('#', "\n") }
      end
    end

    def with_file(file_path)
      if File.exist?(file_path)
        File.open(file_path)
      else
        puts "File not found: #{file_path}"
        []
      end
    end

    def extract_ownership(matching_line, regex)
      matching_line.match(regex)[RspecProfiling.config.magic_comment.to_sym]
    end
  end
end