Class: Profiler::TestProfiler

Inherits:
Object
  • Object
show all
Defined in:
lib/profiler/test_profiler.rb

Constant Summary collapse

TEST_COLLECTOR_CLASSES =
[
  Collectors::DatabaseCollector,
  Collectors::CacheCollector,
  Collectors::ExceptionCollector,
  Collectors::EnvCollector,
  Collectors::FlameGraphCollector
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test_name:, test_file:, test_line:, framework:) ⇒ TestProfiler

Returns a new instance of TestProfiler.



33
34
35
36
37
38
# File 'lib/profiler/test_profiler.rb', line 33

def initialize(test_name:, test_file:, test_line:, framework:)
  @test_name = test_name
  @test_file = test_file
  @test_line = test_line
  @framework = framework
end

Class Method Details

.profile(test_name:, test_file:, test_line:, framework:, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/profiler/test_profiler.rb', line 22

def self.profile(test_name:, test_file:, test_line:, framework:, &block)
  return block.call unless Profiler.enabled?

  new(
    test_name: test_name,
    test_file: test_file,
    test_line: test_line,
    framework: framework
  ).run(&block)
end

Instance Method Details

#run(&block) ⇒ Object



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
# File 'lib/profiler/test_profiler.rb', line 40

def run(&block)
  profile = Models::Profile.new
  profile.profile_type = "test"
  profile.path = @test_file
  profile.method = "TEST"

  test_collector = Collectors::TestCollector.new(
    profile,
    test_name: @test_name,
    test_file: @test_file,
    test_line: @test_line,
    framework: @framework
  )

  collectors = [test_collector] + TEST_COLLECTOR_CLASSES.map { |klass| klass.new(profile) }
  collectors.each { |c| c.subscribe if c.respond_to?(:subscribe) }

  exception_collector = collectors.find { |c| c.is_a?(Collectors::ExceptionCollector) }

  memory_before = current_memory if Profiler.configuration.track_memory

  test_status = "passed"
  error_message = nil

  previous_token = Profiler::CurrentContext.token
  Profiler::CurrentContext.token = profile.token

  begin
    result = block.call

    # Minitest: Test#run catches assertion errors internally and returns self.
    # Failures don't propagate — detect them from the result object.
    if result.respond_to?(:passed?)
      unless result.passed?
        test_status = result.skipped? ? "pending" : "failed"
        msg = result.failure&.message.to_s
        error_message = msg.empty? ? nil : msg
      end
      test_collector.update_extra(
        assertions: result.respond_to?(:assertions) ? result.assertions : nil,
        skip_reason: (result.skipped? && result.failure) ? result.failure.message : nil
      )
    end

    # RSpec: example.run catches errors internally and stores them in execution_result.
    if result.respond_to?(:execution_result)
      er = result.execution_result
      rspec_status = er.status&.to_s
      if rspec_status && rspec_status != "passed" && test_status == "passed"
        test_status = rspec_status
        msg = er.exception&.message.to_s
        error_message = msg.empty? ? nil : msg
      end
    end

    result
  rescue Exception => e # rubocop:disable Lint/RescueException
    # Capture all exceptions (including test failures which may subclass Exception)
    test_status = "failed"
    error_message = "#{e.class}: #{e.message}"
    exception_collector&.capture(e) if e.is_a?(StandardError)
    raise
  ensure
    Profiler::CurrentContext.token = previous_token

    if Profiler.configuration.track_memory
      profile.memory = current_memory - memory_before
    end

    test_collector.update_status(test_status, error_message)
    profile.finish(test_status == "passed" ? 200 : 500)

    collectors.each do |collector|
      begin
        collector.collect if collector.respond_to?(:collect)
        profile.(collector)
      rescue => e
        warn "Profiler TestProfiler: Collector #{collector.class} failed: #{e.message}"
      end
    end

    Profiler.storage.save(profile.token, profile)
  end
end