rails-profiler 0.25.0 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "concurrent"
4
+ require "securerandom"
5
+
6
+ module Profiler
7
+ module TestRunner
8
+ class RunStore
9
+ TTL = 3600 # 1 hour
10
+
11
+ Run = Struct.new(:id, :status, :pid, :started_at, :finished_at, :output_lines, :exit_code, :files, :framework, keyword_init: true) do
12
+ def to_h
13
+ super.merge(
14
+ started_at: started_at&.iso8601,
15
+ finished_at: finished_at&.iso8601,
16
+ output: output_lines.join,
17
+ duration: finished_at && started_at ? ((finished_at - started_at) * 1000).round(2) : nil
18
+ ).except(:output_lines)
19
+ end
20
+ end
21
+
22
+ TERMINAL_STATUSES = %w[passed failed killed error].freeze
23
+
24
+ def initialize
25
+ @runs = Concurrent::Hash.new
26
+ @locks = Concurrent::Hash.new
27
+ end
28
+
29
+ def create(files:, framework:)
30
+ id = SecureRandom.hex(8)
31
+ run = Run.new(
32
+ id: id,
33
+ status: "pending",
34
+ pid: nil,
35
+ started_at: Time.now,
36
+ finished_at: nil,
37
+ output_lines: Concurrent::Array.new,
38
+ exit_code: nil,
39
+ files: files,
40
+ framework: framework.to_s
41
+ )
42
+ @runs[id] = run
43
+ @locks[id] = { mutex: Mutex.new, cond: ConditionVariable.new }
44
+ cleanup_old_runs
45
+ run
46
+ end
47
+
48
+ def find(id)
49
+ @runs[id]
50
+ end
51
+
52
+ def update(id, **attrs)
53
+ run = @runs[id]
54
+ return unless run
55
+
56
+ attrs.each { |k, v| run.send(:"#{k}=", v) }
57
+ signal(id)
58
+ run
59
+ end
60
+
61
+ def append_output(id, chunk)
62
+ run = @runs[id]
63
+ return unless run
64
+
65
+ run.output_lines.push(chunk)
66
+ signal(id)
67
+ end
68
+
69
+ # Block until new output is available at +position+ or the run terminates.
70
+ # Returns { chunks: [...], status: "...", position: N, finished: bool }.
71
+ def wait_for_output(id, position:, timeout: 10)
72
+ run = @runs[id]
73
+ return { chunks: [], status: "not_found", position: 0, finished: true } unless run
74
+
75
+ lock = @locks[id]
76
+ return snapshot(run, position) unless lock
77
+
78
+ lock[:mutex].synchronize do
79
+ current = run.output_lines.size
80
+ if current <= position && !TERMINAL_STATUSES.include?(run.status)
81
+ lock[:cond].wait(lock[:mutex], timeout)
82
+ end
83
+ end
84
+
85
+ snapshot(run, position)
86
+ end
87
+
88
+ def all
89
+ @runs.values.sort_by { |r| r.started_at || Time.at(0) }.reverse
90
+ end
91
+
92
+ private
93
+
94
+ def signal(id)
95
+ lock = @locks[id]
96
+ return unless lock
97
+ lock[:mutex].synchronize { lock[:cond].broadcast }
98
+ end
99
+
100
+ def snapshot(run, position)
101
+ lines = run.output_lines.dup
102
+ {
103
+ chunks: lines[position..] || [],
104
+ status: run.status,
105
+ position: lines.size,
106
+ finished: TERMINAL_STATUSES.include?(run.status)
107
+ }
108
+ end
109
+
110
+ def cleanup_old_runs
111
+ cutoff = Time.now - TTL
112
+ @runs.delete_if { |id, r| r.finished_at && r.finished_at < cutoff && @locks.delete(id) }
113
+ end
114
+ end
115
+
116
+ def self.run_store
117
+ @run_store ||= RunStore.new
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "run_store"
4
+ require_relative "discovery"
5
+
6
+ module Profiler
7
+ module TestRunner
8
+ class Runner
9
+ def self.start(files:, framework:)
10
+ run = Profiler::TestRunner.run_store.create(files: files, framework: framework)
11
+ spawn_async(run)
12
+ run
13
+ end
14
+
15
+ def self.kill(run_id)
16
+ run = Profiler::TestRunner.run_store.find(run_id)
17
+ return false unless run && run.pid && run.status == "running"
18
+
19
+ begin
20
+ Process.kill("TERM", run.pid)
21
+ Profiler::TestRunner.run_store.update(run_id, status: "killed", finished_at: Time.now)
22
+ true
23
+ rescue Errno::ESRCH
24
+ # Process already exited
25
+ false
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def self.spawn_async(run)
32
+ Thread.new do
33
+ run_process(run)
34
+ rescue => e
35
+ Profiler::TestRunner.run_store.update(
36
+ run.id,
37
+ status: "error",
38
+ finished_at: Time.now
39
+ )
40
+ Profiler::TestRunner.run_store.append_output(run.id, "\n[Profiler] Error: #{e.message}\n")
41
+ end
42
+ end
43
+
44
+ def self.run_process(run)
45
+ cmd = build_command(run.files, run.framework)
46
+ env = build_env
47
+
48
+ Profiler::TestRunner.run_store.update(run.id, status: "running", started_at: Time.now)
49
+
50
+ IO.popen([env, *cmd, err: [:child, :out]], "r") do |io|
51
+ Profiler::TestRunner.run_store.update(run.id, pid: io.pid)
52
+
53
+ while (chunk = io.read(256))
54
+ break if chunk.empty?
55
+
56
+ Profiler::TestRunner.run_store.append_output(run.id, chunk)
57
+ end
58
+ end
59
+
60
+ exit_code = $?.exitstatus || 0
61
+ status = exit_code == 0 ? "passed" : "failed"
62
+
63
+ Profiler::TestRunner.run_store.update(
64
+ run.id,
65
+ status: status,
66
+ finished_at: Time.now,
67
+ exit_code: exit_code
68
+ )
69
+ end
70
+
71
+ def self.build_command(files, framework)
72
+ root = defined?(Rails) ? Rails.root.to_s : Dir.pwd
73
+ absolute_files = files.map { |f| File.join(root, f) }
74
+
75
+ case framework.to_sym
76
+ when :rspec
77
+ ["bundle", "exec", "rspec", "--format", "progress", "--color", *absolute_files]
78
+ when :minitest
79
+ ["bundle", "exec", "rails", "test", *absolute_files]
80
+ else
81
+ ["bundle", "exec", "rspec", "--format", "progress", "--color", *absolute_files]
82
+ end
83
+ end
84
+
85
+ BLOCKED_ENV_KEYS = %w[RAILS_ENV RACK_ENV DATABASE_URL SECRET_KEY_BASE].freeze
86
+
87
+ def self.build_env
88
+ base = ENV.to_h
89
+
90
+ # Inject env var overrides configured in the profiler — skip blocked keys
91
+ overrides = Profiler.env_override_store.all_overrides
92
+ overrides.each do |key, entry|
93
+ next if BLOCKED_ENV_KEYS.include?(key.upcase)
94
+ value = entry.is_a?(Hash) ? entry["value"] : entry
95
+ base[key] = value
96
+ end
97
+
98
+ # Ensure test environment regardless of overrides
99
+ base["RAILS_ENV"] = "test"
100
+ base["RACK_ENV"] = "test"
101
+
102
+ base
103
+ end
104
+ end
105
+ end
106
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Profiler
4
- VERSION = "0.25.0"
4
+ VERSION = "0.26.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-profiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.25.0
4
+ version: 0.26.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sébastien Duplessy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-06-01 00:00:00.000000000 Z
11
+ date: 2026-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -114,14 +114,18 @@ files:
114
114
  - app/controllers/profiler/api/jobs_controller.rb
115
115
  - app/controllers/profiler/api/outbound_http_controller.rb
116
116
  - app/controllers/profiler/api/profiles_controller.rb
117
+ - app/controllers/profiler/api/test_runner_controller.rb
118
+ - app/controllers/profiler/api/tests_controller.rb
117
119
  - app/controllers/profiler/api/toolbar_controller.rb
118
120
  - app/controllers/profiler/application_controller.rb
119
121
  - app/controllers/profiler/assets_controller.rb
120
122
  - app/controllers/profiler/profiles_controller.rb
123
+ - app/controllers/profiler/test_runner_controller.rb
121
124
  - app/views/layouts/profiler/application.html.erb
122
125
  - app/views/layouts/profiler/embedded.html.erb
123
126
  - app/views/profiler/profiles/index.html.erb
124
127
  - app/views/profiler/profiles/show.html.erb
128
+ - app/views/profiler/test_runner/index.html.erb
125
129
  - config/routes.rb
126
130
  - exe/profiler-mcp
127
131
  - lib/profiler.rb
@@ -142,6 +146,7 @@ files:
142
146
  - lib/profiler/collectors/mailer_collector.rb
143
147
  - lib/profiler/collectors/request_collector.rb
144
148
  - lib/profiler/collectors/routes_collector.rb
149
+ - lib/profiler/collectors/test_collector.rb
145
150
  - lib/profiler/collectors/view_collector.rb
146
151
  - lib/profiler/configuration.rb
147
152
  - lib/profiler/console_profiler.rb
@@ -158,10 +163,12 @@ files:
158
163
  - lib/profiler/mcp/body_formatter.rb
159
164
  - lib/profiler/mcp/file_cache.rb
160
165
  - lib/profiler/mcp/path_extractor.rb
166
+ - lib/profiler/mcp/resources/failing_tests.rb
161
167
  - lib/profiler/mcp/resources/n1_patterns.rb
162
168
  - lib/profiler/mcp/resources/recent_jobs.rb
163
169
  - lib/profiler/mcp/resources/recent_requests.rb
164
170
  - lib/profiler/mcp/resources/slow_queries.rb
171
+ - lib/profiler/mcp/resources/slow_tests.rb
165
172
  - lib/profiler/mcp/server.rb
166
173
  - lib/profiler/mcp/tools/analyze_queries.rb
167
174
  - lib/profiler/mcp/tools/clear_profiles.rb
@@ -171,12 +178,15 @@ files:
171
178
  - lib/profiler/mcp/tools/get_profile_detail.rb
172
179
  - lib/profiler/mcp/tools/get_profile_dumps.rb
173
180
  - lib/profiler/mcp/tools/get_profile_http.rb
181
+ - lib/profiler/mcp/tools/get_test_profile_detail.rb
174
182
  - lib/profiler/mcp/tools/list_env_vars.rb
175
183
  - lib/profiler/mcp/tools/query_jobs.rb
176
184
  - lib/profiler/mcp/tools/query_mailers.rb
177
185
  - lib/profiler/mcp/tools/query_profiles.rb
186
+ - lib/profiler/mcp/tools/query_test_profiles.rb
178
187
  - lib/profiler/mcp/tools/reset_all_env_vars.rb
179
188
  - lib/profiler/mcp/tools/reset_env_var.rb
189
+ - lib/profiler/mcp/tools/run_tests.rb
180
190
  - lib/profiler/mcp/tools/set_env_var.rb
181
191
  - lib/profiler/middleware/cors_middleware.rb
182
192
  - lib/profiler/middleware/profiler_middleware.rb
@@ -192,6 +202,13 @@ files:
192
202
  - lib/profiler/storage/redis_store.rb
193
203
  - lib/profiler/storage/sqlite_store.rb
194
204
  - lib/profiler/tasks/profiler.rake
205
+ - lib/profiler/test_helpers/minitest_support.rb
206
+ - lib/profiler/test_helpers/reporter.rb
207
+ - lib/profiler/test_helpers/rspec_support.rb
208
+ - lib/profiler/test_profiler.rb
209
+ - lib/profiler/test_runner/discovery.rb
210
+ - lib/profiler/test_runner/run_store.rb
211
+ - lib/profiler/test_runner/runner.rb
195
212
  - lib/profiler/version.rb
196
213
  homepage: https://git.duplessy.eu/sebastien/rails-profiler-gem
197
214
  licenses: