Module: Devformance::PerformanceHelpers

Defined in:
lib/devformance.rb

Class Method Summary collapse

Class Method Details

.finish_test_run!Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/devformance.rb', line 159

def self.finish_test_run!
  return unless ENV["DEVMETRICS_TRACKING"] == "true" && @start_time

  total_duration = (Time.current - @start_time).round(3)

  summary = {
    type: "SUMMARY",
    total_time_s: total_duration,
    total_tests: @total_tests,
    passed: @passed_tests,
    failed: @failed_tests,
    timestamp: Time.current.to_s
  }

  File.open(Devformance.log_path, "a") do |f|
    f.puts "\n--- Summary ---"
    f.puts summary.to_json
  end
end

.log_example_result(example) ⇒ Object



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
153
154
155
156
157
# File 'lib/devformance.rb', line 123

def self.log_example_result(example)
  return unless ENV["DEVMETRICS_TRACKING"] == "true"
  @total_tests += 1
  if example.exception
    @failed_tests += 1
  else
    @passed_tests += 1
  end

  action_data = Thread.current[:devformance_current_action] || {}
  slow_queries = Thread.current[:devformance_slow_queries] || []

  n_plus_one_count = 0
  if defined?(Bullet) && Bullet.notification_collector.notifications_present?
    n_plus_one_count = Bullet.notification_collector.collection.size
  end

  log_row = {
    timestamp: Time.current.strftime("%H:%M:%S"),
    controller: action_data[:controller] || "N/A",
    action: action_data[:action] || "N/A",
    duration_ms: action_data[:duration] || 0.0,
    slow_queries: slow_queries.size,
    n_plus_one_issues: n_plus_one_count,
    status: example.exception ? "FAILED" : "PASSED",
    example: example.full_description.truncate(100)
  }

  File.open(Devformance.log_path, "a") { |f| f.puts(log_row.to_json) }

  # Reset state for next example
  Thread.current[:devformance_current_action] = nil
  Thread.current[:devformance_slow_queries] = nil
  Bullet.notification_collector.clear if defined?(Bullet)
end

.setup_test_run!Object



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
# File 'lib/devformance.rb', line 84

def self.setup_test_run!
  return unless ENV["DEVMETRICS_TRACKING"] == "true"

  # Clear and initialize the log file
  File.write(Devformance.log_path, "--- Devformance Performance Run: #{Time.current} ---\n")

  @total_tests = 0
  @passed_tests = 0
  @failed_tests = 0
  @start_time = Time.current

  # SQL subscriptions
  ActiveSupport::Notifications.subscribe("sql.active_record") do |_, start, finish, _, payload|
    next if payload[:sql] =~ /\A\s*(BEGIN|COMMIT|ROLLBACK|SAVEPOINT|RELEASE|SET|SHOW|pragma)/i
    next if payload[:name]&.match?(/SCHEMA|ActiveRecord/)

    duration = ((finish - start) * 1000).round(2)
    if duration > Devformance.configuration.slow_query_threshold_ms
      Thread.current[:devformance_slow_queries] ||= []
      Thread.current[:devformance_slow_queries] << { sql: payload[:sql].squish.truncate(100), duration: duration }
    end
  end

  # Controller processing subscriptions
  ActiveSupport::Notifications.subscribe("process_action.action_controller") do |_, start, finish, _, payload|
    Thread.current[:devformance_current_action] = {
      controller: payload[:controller],
      action: payload[:action],
      duration: ((finish - start) * 1000).round(2),
      db_runtime: payload[:db_runtime]&.round(2)
    }
  end

  if defined?(Bullet)
    Bullet.enable = true
    Bullet.bullet_logger = true
  end
end