polyrun 3.2.0-java

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.
Files changed (225) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +234 -0
  3. data/CODE_OF_CONDUCT.md +26 -0
  4. data/CONTRIBUTING.md +23 -0
  5. data/LICENSE +21 -0
  6. data/README.md +251 -0
  7. data/SECURITY.md +25 -0
  8. data/bin/polyrun +6 -0
  9. data/docs/QUALITY.md +82 -0
  10. data/docs/QUALITY_CONFIGURATION.md +95 -0
  11. data/docs/SETUP_PROFILE.md +139 -0
  12. data/lib/polyrun/benchmark/pressure/cooker.rb +191 -0
  13. data/lib/polyrun/benchmark/pressure/process_sample.rb +127 -0
  14. data/lib/polyrun/benchmark/pressure.rb +144 -0
  15. data/lib/polyrun/benchmark/profile.rb +191 -0
  16. data/lib/polyrun/benchmark/report.rb +87 -0
  17. data/lib/polyrun/cli/benchmark_commands.rb +66 -0
  18. data/lib/polyrun/cli/ci_shard_hooks.rb +129 -0
  19. data/lib/polyrun/cli/ci_shard_run_command.rb +177 -0
  20. data/lib/polyrun/cli/ci_shard_run_parse.rb +68 -0
  21. data/lib/polyrun/cli/config_command.rb +42 -0
  22. data/lib/polyrun/cli/coverage_commands.rb +150 -0
  23. data/lib/polyrun/cli/coverage_merge_io.rb +150 -0
  24. data/lib/polyrun/cli/database_commands.rb +163 -0
  25. data/lib/polyrun/cli/default_run.rb +144 -0
  26. data/lib/polyrun/cli/doctor_command.rb +94 -0
  27. data/lib/polyrun/cli/env_commands.rb +43 -0
  28. data/lib/polyrun/cli/failure_commands.rb +99 -0
  29. data/lib/polyrun/cli/graph_command.rb +68 -0
  30. data/lib/polyrun/cli/help.rb +83 -0
  31. data/lib/polyrun/cli/helpers.rb +162 -0
  32. data/lib/polyrun/cli/hooks_command.rb +97 -0
  33. data/lib/polyrun/cli/init_command.rb +108 -0
  34. data/lib/polyrun/cli/mutation_command.rb +67 -0
  35. data/lib/polyrun/cli/partition_diagnostics.rb +22 -0
  36. data/lib/polyrun/cli/plan_command.rb +184 -0
  37. data/lib/polyrun/cli/prepare_command.rb +70 -0
  38. data/lib/polyrun/cli/prepare_recipe.rb +82 -0
  39. data/lib/polyrun/cli/quality_commands.rb +65 -0
  40. data/lib/polyrun/cli/queue_command.rb +161 -0
  41. data/lib/polyrun/cli/quick_command.rb +13 -0
  42. data/lib/polyrun/cli/report_commands.rb +108 -0
  43. data/lib/polyrun/cli/run_queue_command.rb +127 -0
  44. data/lib/polyrun/cli/run_shards_command.rb +164 -0
  45. data/lib/polyrun/cli/run_shards_parallel_children.rb +112 -0
  46. data/lib/polyrun/cli/run_shards_parallel_wait.rb +252 -0
  47. data/lib/polyrun/cli/run_shards_plan_boot_phases.rb +207 -0
  48. data/lib/polyrun/cli/run_shards_plan_options.rb +74 -0
  49. data/lib/polyrun/cli/run_shards_planning.rb +123 -0
  50. data/lib/polyrun/cli/run_shards_run.rb +171 -0
  51. data/lib/polyrun/cli/run_shards_worker_interrupt.rb +101 -0
  52. data/lib/polyrun/cli/skip_env.rb +34 -0
  53. data/lib/polyrun/cli/spec_quality_commands.rb +149 -0
  54. data/lib/polyrun/cli/start_bootstrap.rb +126 -0
  55. data/lib/polyrun/cli/timing_command.rb +31 -0
  56. data/lib/polyrun/cli/tool_options.rb +48 -0
  57. data/lib/polyrun/cli/validate_command.rb +26 -0
  58. data/lib/polyrun/cli.rb +272 -0
  59. data/lib/polyrun/config/dotted_path.rb +21 -0
  60. data/lib/polyrun/config/effective.rb +82 -0
  61. data/lib/polyrun/config/resolver.rb +114 -0
  62. data/lib/polyrun/config/schema.rb +39 -0
  63. data/lib/polyrun/config/suite.rb +109 -0
  64. data/lib/polyrun/config/tool_defaults.rb +42 -0
  65. data/lib/polyrun/config/tool_validation.rb +79 -0
  66. data/lib/polyrun/config/tools.rb +120 -0
  67. data/lib/polyrun/config.rb +97 -0
  68. data/lib/polyrun/coverage/cobertura_zero_lines.rb +32 -0
  69. data/lib/polyrun/coverage/collector.rb +169 -0
  70. data/lib/polyrun/coverage/collector_blob.rb +53 -0
  71. data/lib/polyrun/coverage/collector_finish.rb +98 -0
  72. data/lib/polyrun/coverage/collector_fragment_meta.rb +57 -0
  73. data/lib/polyrun/coverage/config_resolve.rb +131 -0
  74. data/lib/polyrun/coverage/example_diff.rb +184 -0
  75. data/lib/polyrun/coverage/example_diff_snapshot.rb +44 -0
  76. data/lib/polyrun/coverage/example_diff_track_filter.rb +51 -0
  77. data/lib/polyrun/coverage/file_stats_report.rb +36 -0
  78. data/lib/polyrun/coverage/filter.rb +22 -0
  79. data/lib/polyrun/coverage/formatter.rb +137 -0
  80. data/lib/polyrun/coverage/merge/formatters.rb +188 -0
  81. data/lib/polyrun/coverage/merge/formatters_html.rb +221 -0
  82. data/lib/polyrun/coverage/merge/html/_file_list.html.erb +21 -0
  83. data/lib/polyrun/coverage/merge/html/_file_section.html.erb +26 -0
  84. data/lib/polyrun/coverage/merge/html/_groups_table.html.erb +18 -0
  85. data/lib/polyrun/coverage/merge/html/_overview.html.erb +47 -0
  86. data/lib/polyrun/coverage/merge/html/report.css +147 -0
  87. data/lib/polyrun/coverage/merge/html/report.js +48 -0
  88. data/lib/polyrun/coverage/merge/html/template.html.erb +30 -0
  89. data/lib/polyrun/coverage/merge.rb +160 -0
  90. data/lib/polyrun/coverage/merge_fragment_meta.rb +58 -0
  91. data/lib/polyrun/coverage/merge_merge_two.rb +168 -0
  92. data/lib/polyrun/coverage/merge_native.rb +39 -0
  93. data/lib/polyrun/coverage/rails.rb +126 -0
  94. data/lib/polyrun/coverage/reporting.rb +41 -0
  95. data/lib/polyrun/coverage/result.rb +18 -0
  96. data/lib/polyrun/coverage/track_files.rb +150 -0
  97. data/lib/polyrun/data/cached_fixtures.rb +122 -0
  98. data/lib/polyrun/data/factory_counts.rb +48 -0
  99. data/lib/polyrun/data/factory_instrumentation.rb +50 -0
  100. data/lib/polyrun/data/fixtures.rb +68 -0
  101. data/lib/polyrun/data/parallel_provisioning.rb +93 -0
  102. data/lib/polyrun/data/snapshot.rb +84 -0
  103. data/lib/polyrun/database/clone_shards.rb +106 -0
  104. data/lib/polyrun/database/provision.rb +83 -0
  105. data/lib/polyrun/database/shard.rb +64 -0
  106. data/lib/polyrun/database/url_builder/connection/infer.rb +53 -0
  107. data/lib/polyrun/database/url_builder/connection/url_builders.rb +43 -0
  108. data/lib/polyrun/database/url_builder/connection.rb +191 -0
  109. data/lib/polyrun/database/url_builder/template_prepare.rb +21 -0
  110. data/lib/polyrun/database/url_builder.rb +188 -0
  111. data/lib/polyrun/debug.rb +81 -0
  112. data/lib/polyrun/env/ci.rb +65 -0
  113. data/lib/polyrun/error.rb +3 -0
  114. data/lib/polyrun/export/csv.rb +25 -0
  115. data/lib/polyrun/export/markdown.rb +47 -0
  116. data/lib/polyrun/graph/client.rb +64 -0
  117. data/lib/polyrun/graph/error.rb +13 -0
  118. data/lib/polyrun/graph/index.rb +75 -0
  119. data/lib/polyrun/graph/inspect.rb +58 -0
  120. data/lib/polyrun/graph/plan.rb +134 -0
  121. data/lib/polyrun/graph/planner.rb +117 -0
  122. data/lib/polyrun/graph/syntax.rb +114 -0
  123. data/lib/polyrun/graph/transaction.rb +81 -0
  124. data/lib/polyrun/graph/worker.rb +55 -0
  125. data/lib/polyrun/graph/workspace.rb +117 -0
  126. data/lib/polyrun/hooks/dsl.rb +128 -0
  127. data/lib/polyrun/hooks/shell_runner.rb +33 -0
  128. data/lib/polyrun/hooks/worker_runner.rb +27 -0
  129. data/lib/polyrun/hooks/worker_shell.rb +50 -0
  130. data/lib/polyrun/hooks.rb +190 -0
  131. data/lib/polyrun/log.rb +86 -0
  132. data/lib/polyrun/minitest_support.rb +60 -0
  133. data/lib/polyrun/mutation/candidates.rb +91 -0
  134. data/lib/polyrun/mutation/operators.rb +51 -0
  135. data/lib/polyrun/mutation/runner.rb +133 -0
  136. data/lib/polyrun/mutation/subjects.rb +88 -0
  137. data/lib/polyrun/partition/constraints.rb +80 -0
  138. data/lib/polyrun/partition/hrw.rb +70 -0
  139. data/lib/polyrun/partition/min_heap.rb +64 -0
  140. data/lib/polyrun/partition/paths.rb +175 -0
  141. data/lib/polyrun/partition/paths_build.rb +141 -0
  142. data/lib/polyrun/partition/plan.rb +250 -0
  143. data/lib/polyrun/partition/plan_lpt.rb +91 -0
  144. data/lib/polyrun/partition/plan_sharding.rb +56 -0
  145. data/lib/polyrun/partition/reports.rb +139 -0
  146. data/lib/polyrun/partition/stable_shuffle.rb +18 -0
  147. data/lib/polyrun/partition/suite.rb +120 -0
  148. data/lib/polyrun/partition/timing_diagnostics.rb +138 -0
  149. data/lib/polyrun/partition/timing_keys.rb +86 -0
  150. data/lib/polyrun/prepare/artifacts.rb +40 -0
  151. data/lib/polyrun/prepare/assets.rb +64 -0
  152. data/lib/polyrun/process_stdio.rb +96 -0
  153. data/lib/polyrun/quality/command.rb +84 -0
  154. data/lib/polyrun/quality/measure.rb +38 -0
  155. data/lib/polyrun/quality/observe.rb +53 -0
  156. data/lib/polyrun/quality/observe_hook.rb +103 -0
  157. data/lib/polyrun/quality/sandbox.rb +70 -0
  158. data/lib/polyrun/queue/duration.rb +30 -0
  159. data/lib/polyrun/queue/file_store.rb +413 -0
  160. data/lib/polyrun/queue/file_store_intent.rb +188 -0
  161. data/lib/polyrun/queue/file_store_pending.rb +48 -0
  162. data/lib/polyrun/queue/worker_loop.rb +73 -0
  163. data/lib/polyrun/quick/assertions.rb +32 -0
  164. data/lib/polyrun/quick/config.rb +56 -0
  165. data/lib/polyrun/quick/errors.rb +6 -0
  166. data/lib/polyrun/quick/example_group.rb +111 -0
  167. data/lib/polyrun/quick/example_runner.rb +129 -0
  168. data/lib/polyrun/quick/matchers.rb +159 -0
  169. data/lib/polyrun/quick/matchers_predicates.rb +122 -0
  170. data/lib/polyrun/quick/reporter.rb +76 -0
  171. data/lib/polyrun/quick/runner.rb +224 -0
  172. data/lib/polyrun/quick.rb +1 -0
  173. data/lib/polyrun/railtie.rb +7 -0
  174. data/lib/polyrun/reporting/failure_merge.rb +171 -0
  175. data/lib/polyrun/reporting/junit.rb +126 -0
  176. data/lib/polyrun/reporting/junit_emit.rb +58 -0
  177. data/lib/polyrun/reporting/junit_report.rb +87 -0
  178. data/lib/polyrun/reporting/rspec_failure_fragment_formatter.rb +95 -0
  179. data/lib/polyrun/reporting/rspec_junit.rb +39 -0
  180. data/lib/polyrun/rspec_support/active_record_timeout_recovery.rb +51 -0
  181. data/lib/polyrun/rspec_support/example_debug.rb +167 -0
  182. data/lib/polyrun/rspec_support/example_debug_instrumentation.rb +73 -0
  183. data/lib/polyrun/rspec_support/sharded_formatter_compat.rb +57 -0
  184. data/lib/polyrun/rspec_support.rb +110 -0
  185. data/lib/polyrun/spec_quality/config.rb +139 -0
  186. data/lib/polyrun/spec_quality/fragment.rb +39 -0
  187. data/lib/polyrun/spec_quality/merge.rb +78 -0
  188. data/lib/polyrun/spec_quality/minitest_hook.rb +42 -0
  189. data/lib/polyrun/spec_quality/plan_loader.rb +47 -0
  190. data/lib/polyrun/spec_quality/profile.rb +102 -0
  191. data/lib/polyrun/spec_quality/report.rb +363 -0
  192. data/lib/polyrun/spec_quality/rspec_hook.rb +55 -0
  193. data/lib/polyrun/spec_quality/sql_counter.rb +34 -0
  194. data/lib/polyrun/spec_quality.rb +212 -0
  195. data/lib/polyrun/sus_support.rb +55 -0
  196. data/lib/polyrun/templates/POLYRUN.md +51 -0
  197. data/lib/polyrun/templates/ci_matrix.polyrun.yml +22 -0
  198. data/lib/polyrun/templates/minimal_gem.polyrun.yml +13 -0
  199. data/lib/polyrun/templates/polyrun_hooks_spec_quality.rb +12 -0
  200. data/lib/polyrun/templates/polyrun_spec_quality.yml +20 -0
  201. data/lib/polyrun/templates/rails_prepare.polyrun.yml +51 -0
  202. data/lib/polyrun/templates/rails_prepare_task.rake +16 -0
  203. data/lib/polyrun/timing/merge.rb +36 -0
  204. data/lib/polyrun/timing/rspec_example_formatter.rb +60 -0
  205. data/lib/polyrun/timing/stats.rb +76 -0
  206. data/lib/polyrun/timing/summary.rb +78 -0
  207. data/lib/polyrun/timing/variance_report.rb +51 -0
  208. data/lib/polyrun/version.rb +3 -0
  209. data/lib/polyrun/worker_output.rb +181 -0
  210. data/lib/polyrun/worker_output_forwarders.rb +94 -0
  211. data/lib/polyrun/worker_ping.rb +77 -0
  212. data/lib/polyrun.rb +59 -0
  213. data/polyrun.gemspec +49 -0
  214. data/sig/polyrun/cli.rbs +6 -0
  215. data/sig/polyrun/config.rbs +20 -0
  216. data/sig/polyrun/debug.rbs +12 -0
  217. data/sig/polyrun/log.rbs +12 -0
  218. data/sig/polyrun/minitest_support.rbs +7 -0
  219. data/sig/polyrun/quick.rbs +19 -0
  220. data/sig/polyrun/rspec_support.rbs +21 -0
  221. data/sig/polyrun/sus_support.rbs +7 -0
  222. data/sig/polyrun/worker_output.rbs +11 -0
  223. data/sig/polyrun/worker_ping.rbs +10 -0
  224. data/sig/polyrun.rbs +11 -0
  225. metadata +463 -0
@@ -0,0 +1,212 @@
1
+ # rubocop:disable Polyrun/FileLength -- per-example recorder API
2
+ require "json"
3
+ require "fileutils"
4
+
5
+ require_relative "coverage/example_diff"
6
+ require_relative "partition/timing_keys"
7
+ require_relative "spec_quality/config"
8
+ require_relative "spec_quality/profile"
9
+ require_relative "spec_quality/fragment"
10
+ require_relative "spec_quality/sql_counter"
11
+ require_relative "spec_quality/merge"
12
+ require_relative "spec_quality/plan_loader"
13
+ require_relative "spec_quality/report"
14
+ require_relative "spec_quality/rspec_hook"
15
+ require_relative "spec_quality/minitest_hook"
16
+
17
+ module Polyrun
18
+ # Per-example spec quality: coverage line deltas, resource use, optional SQL counts.
19
+ # Opt-in with +POLYRUN_SPEC_QUALITY=1+ or +Polyrun::SpecQuality.start!+.
20
+ module SpecQuality
21
+ class << self
22
+ def start!(root: Dir.pwd, config_path: nil, output_path: nil, **overrides)
23
+ return if Config.disabled?
24
+
25
+ @config = Config.load(root: root, config_path: config_path, **overrides)
26
+ @output_path = output_path || Fragment.default_fragment_path
27
+ @pause_depth = 0
28
+ @current = nil
29
+ @rng = Random.new(Process.pid ^ Time.now.to_i)
30
+
31
+ Fragment.truncate_fragment!(@output_path) unless fragment_append_mode?
32
+ SqlCounter.install! if @config["sql_counter"]
33
+ self
34
+ end
35
+
36
+ def started?
37
+ instance_variable_defined?(:@config) && @config
38
+ end
39
+
40
+ def enabled?(root: Dir.pwd)
41
+ Config.enabled?(ENV, root: root) && !Config.disabled?
42
+ end
43
+
44
+ attr_reader :config
45
+
46
+ attr_reader :output_path
47
+
48
+ def recording?
49
+ started? && @current && @pause_depth.zero?
50
+ end
51
+
52
+ def start_example!(location:, wall_start: nil)
53
+ return unless started?
54
+ return if paused?
55
+ return if Config.ignored_example?(location, @config["ignore_examples"])
56
+ return unless sample_example?
57
+
58
+ @current = {
59
+ location: normalize_location(location),
60
+ wall_start: wall_start || Process.clock_gettime(Process::CLOCK_MONOTONIC),
61
+ coverage_before: Coverage::ExampleDiff.peek_blob(
62
+ root: @config["root"],
63
+ track_under: @config["track_under"],
64
+ ignore_paths: @config["ignore_paths"]
65
+ ),
66
+ profile_before: Profile.snapshot(dimensions: @config["profile"]),
67
+ sql_count: 0,
68
+ sql_fingerprints: Hash.new(0),
69
+ factory_counts: {}
70
+ }
71
+ Polyrun::Data::FactoryCounts.reset_example! if defined?(Polyrun::Data::FactoryCounts)
72
+ nil
73
+ end
74
+
75
+ def finish_example!(location: nil, pending: false)
76
+ return unless started?
77
+ cur = @current
78
+ @current = nil
79
+ return if cur.nil? || paused?
80
+ return if pending
81
+
82
+ loc = location || cur[:location]
83
+ return if loc.nil? || loc.to_s.empty?
84
+
85
+ delta = coverage_delta_for_example(cur)
86
+
87
+ profile_after = Profile.snapshot(dimensions: @config["profile"])
88
+ profile_delta = Profile.diff(cur[:profile_before], profile_after)
89
+ wall = Process.clock_gettime(Process::CLOCK_MONOTONIC) - cur[:wall_start]
90
+ profile_delta["wall"] = wall
91
+
92
+ factory_counts =
93
+ if defined?(Polyrun::Data::FactoryCounts)
94
+ Polyrun::Data::FactoryCounts.example_counts
95
+ else
96
+ {}
97
+ end
98
+
99
+ row = build_row(cur, loc, delta, profile_delta, factory_counts)
100
+ Fragment.append_row!(@output_path, row)
101
+ row
102
+ end
103
+
104
+ def pause
105
+ @pause_depth += 1
106
+ if block_given?
107
+ begin
108
+ yield
109
+ ensure
110
+ resume
111
+ end
112
+ end
113
+ end
114
+
115
+ def resume
116
+ @pause_depth -= 1 if @pause_depth.positive?
117
+ end
118
+
119
+ def paused?
120
+ @pause_depth.positive?
121
+ end
122
+
123
+ def record_sql!(sql)
124
+ return unless recording?
125
+
126
+ @current[:sql_count] += 1
127
+ fp = normalize_sql(sql)
128
+ @current[:sql_fingerprints][fp] += 1
129
+ end
130
+
131
+ def spec_quality_requested_for_quick?(root = Dir.pwd)
132
+ return false if Config.disabled?
133
+ return true if Config.enabled?(ENV, root: root)
134
+
135
+ path = File.join(File.expand_path(root), Config::DEFAULT_CONFIG_RELATIVE)
136
+ return false unless File.file?(path)
137
+
138
+ %w[1 true yes].include?(ENV["POLYRUN_QUICK_SPEC_QUALITY"]&.to_s&.downcase)
139
+ end
140
+
141
+ private
142
+
143
+ def fragment_append_mode?
144
+ truthy?(ENV["POLYRUN_SPEC_QUALITY_FRAGMENT_APPEND"])
145
+ end
146
+
147
+ def sample_example?
148
+ rate = @config["sample"].to_f
149
+ return true if rate >= 1.0
150
+ return false if rate <= 0.0
151
+
152
+ @rng.rand < rate
153
+ end
154
+
155
+ def normalize_location(location)
156
+ s = location.to_s.strip
157
+ return s if s.empty?
158
+
159
+ root = @config["root"]
160
+ if (m = s.match(/\A(.+):(\d+)\z/)) && m[2].match?(/\A\d+\z/)
161
+ fp = Polyrun::Partition::TimingKeys.canonical_file_path(File.expand_path(m[1], root))
162
+ return "#{fp}:#{m[2]}"
163
+ end
164
+
165
+ Polyrun::Partition::TimingKeys.canonical_file_path(File.expand_path(s, root))
166
+ end
167
+
168
+ def coverage_delta_for_example(cur)
169
+ after_source = Coverage::ExampleDiff.coverage_active? ? ::Coverage.peek_result : {}
170
+ Coverage::ExampleDiff.diff(
171
+ cur[:coverage_before],
172
+ after_source,
173
+ root: @config["root"],
174
+ track_under: @config["track_under"],
175
+ ignore_paths: @config["ignore_paths"]
176
+ )
177
+ end
178
+
179
+ def build_row(cur, location, delta, profile_delta, factory_counts)
180
+ profile = Profile.slice_profile(profile_delta, @config["profile"])
181
+ repeated_sql = cur[:sql_fingerprints].select { |_sql, n| n >= min_query_count }.transform_keys(&:to_s)
182
+
183
+ {
184
+ "example" => location.to_s,
185
+ "unique_lines" => delta[:unique_lines],
186
+ "line_churn" => delta[:line_churn],
187
+ "max_line_churn" => delta[:max_line_churn],
188
+ "lines" => delta[:lines],
189
+ "profile" => profile,
190
+ "sql_count" => cur[:sql_count],
191
+ "repeated_sql" => repeated_sql,
192
+ "factory_counts" => factory_counts.transform_keys(&:to_s),
193
+ "polyrun_shard_index" => ENV["POLYRUN_SHARD_INDEX"],
194
+ "polyrun_shard_total" => ENV["POLYRUN_SHARD_TOTAL"]
195
+ }.compact
196
+ end
197
+
198
+ def min_query_count
199
+ @config["min_query_count"].to_i
200
+ end
201
+
202
+ def normalize_sql(sql)
203
+ sql.to_s.gsub(/\s+/, " ").strip[0, 500]
204
+ end
205
+
206
+ def truthy?(value)
207
+ Config.send(:truthy?, value)
208
+ end
209
+ end
210
+ end
211
+ end
212
+ # rubocop:enable Polyrun/FileLength
@@ -0,0 +1,55 @@
1
+ require_relative "../polyrun"
2
+
3
+ module Polyrun
4
+ # Optional Sus-oriented wiring (require +polyrun/sus_support+ explicitly).
5
+ #
6
+ # Does not load the +sus+ gem. Call installers from +config/sus.rb+ after Sus is available
7
+ # when a hook needs +Sus::Config+.
8
+ module SusSupport
9
+ module WorkerPingConfigHook
10
+ def before_tests(assertions, output: self.output)
11
+ Polyrun::WorkerPing.ping!(location: "sus:before_tests")
12
+ super
13
+ end
14
+
15
+ def after_tests(assertions, output: self.output)
16
+ super
17
+ Polyrun::WorkerPing.ping!(location: "sus:after_tests")
18
+ end
19
+ end
20
+
21
+ module_function
22
+
23
+ # Runs {Data::ParallelProvisioning.run_suite_hooks!} (serial vs shard worker hooks).
24
+ def install_parallel_provisioning!
25
+ Polyrun::Data::ParallelProvisioning.run_suite_hooks!
26
+ end
27
+
28
+ # Start {Coverage::Collector} when Polyrun or Covered coverage env is on.
29
+ # Prefer this for shard fragment merge. If Covered::Sus is already included on +Sus::Config+,
30
+ # skip starting the collector so +Coverage.start+ is not contested in one process.
31
+ def install_coverage!(root: Dir.pwd, **kwargs)
32
+ require_relative "coverage/collector"
33
+ return unless Polyrun::Coverage::Collector.coverage_requested_for_quick?(root)
34
+
35
+ if defined?(::Covered::Sus) && defined?(::Sus::Config) && ::Sus::Config.ancestors.include?(::Covered::Sus)
36
+ Polyrun::Log.warn "polyrun sus_support: install_coverage! skipped (Covered::Sus already on Sus::Config; use one coverage collector per process)"
37
+ return
38
+ end
39
+
40
+ Polyrun::Coverage::Collector.start!(root: root, **kwargs)
41
+ end
42
+
43
+ # Ping at Sus +before_tests+ / +after_tests+ when +Sus::Config+ is defined.
44
+ def install_worker_ping!
45
+ require_relative "worker_ping"
46
+ unless defined?(::Sus::Config)
47
+ Polyrun::Log.warn "polyrun sus_support: install_worker_ping! skipped (load Sus before calling)"
48
+ return
49
+ end
50
+
51
+ ::Sus::Config.prepend(WorkerPingConfigHook)
52
+ Polyrun::WorkerPing.ensure_interval_ping_thread!
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,51 @@
1
+ # Polyrun in this project
2
+
3
+ This repo uses [Polyrun](https://github.com/amkisko/polyrun.rb) for parallel RSpec, merged coverage, and optional CI report formats.
4
+
5
+ ## Setup profile
6
+
7
+ Fill in and keep updated: dimensions are summarized in Polyrun’s [SETUP_PROFILE checklist](https://github.com/amkisko/polyrun.rb/blob/main/docs/SETUP_PROFILE.md) (project type, DB, prepare, CI model).
8
+
9
+ ## Canonical commands
10
+
11
+ ```bash
12
+ bundle exec polyrun -c polyrun.yml build-paths
13
+ bundle exec polyrun -c polyrun.yml parallel-rspec --workers 5
14
+ ```
15
+
16
+ Adjust `--workers` or use `bin/rspec_parallel` if your repo provides a wrapper.
17
+
18
+ ## CI model (choose one and match your workflows)
19
+
20
+ ### Model A — single CI job, N worker processes on one runner
21
+
22
+ - Run `polyrun -c polyrun.yml parallel-rspec --workers N` (or `polyrun start`).
23
+ - Merge coverage in the same job (or a follow-up step) from `coverage/polyrun-fragment-*.json`.
24
+
25
+ ### Model B — matrix: one shard per job
26
+
27
+ - Matrix sets `POLYRUN_SHARD_INDEX` and `POLYRUN_SHARD_TOTAL` explicitly (many runners do not set `CI_NODE_*` by default).
28
+ - Each job runs `polyrun ci-shard-run -- …` (e.g. `-- bundle exec rspec` or `ci-shard-rspec`), or `build-paths` + `plan` + your runner manually. Legacy: `bin/polyrun-rspec` or `bin/rspec_ci_shard` patterns.
29
+ - Upload `coverage/polyrun-fragment-<shard>.json` per job; a `merge-coverage` job downloads all fragments and merges.
30
+
31
+ Do not combine Model A and Model B in one workflow without a documented reason (nested parallelism and duplicate merges).
32
+
33
+ ## Configuration contract
34
+
35
+ - `polyrun.yml` — partition, optional `prepare`, optional `databases`. This file is the source of truth for shard indices and paths.
36
+ - Adapters — thin scripts (`bin/rspec_parallel`, `bin/polyrun-rspec`, `database.yml` ERB, prepare scripts) must match `polyrun.yml`.
37
+
38
+ ## Coverage
39
+
40
+ - `spec/spec_helper.rb`: `require "polyrun"` and collector or Rails helper as appropriate.
41
+ - Fragments: `coverage/polyrun-fragment-<shard>.json` → `polyrun merge-coverage` → `polyrun report-coverage` / `report-junit` for CI.
42
+
43
+ ## Spec quality (optional)
44
+
45
+ - `POLYRUN_SPEC_QUALITY=1` and `Polyrun::RSpecSupport.install_spec_quality!` in `spec_helper` (requires coverage enabled for line deltas).
46
+ - `config/polyrun_spec_quality.yml` — thresholds and CI gates (`polyrun init --profile spec-quality`).
47
+ - Parallel: `run-shards --merge-spec-quality` → `coverage/polyrun-spec-quality.json` → `report-spec-quality`.
48
+
49
+ ## Further reading
50
+
51
+ - Polyrun README: partition, prepare, databases, merge-coverage, `Polyrun::Env::Ci`
@@ -0,0 +1,22 @@
1
+ # Polyrun — partition contract for CI matrix (one job per POLYRUN_SHARD_INDEX).
2
+ # Each matrix row: set POLYRUN_SHARD_INDEX and POLYRUN_SHARD_TOTAL; run:
3
+ # bundle exec polyrun -c polyrun.yml ci-shard-run -- bundle exec rspec
4
+ # (or ci-shard-rspec; or e.g. ci-shard-run -- bundle exec polyrun quick).
5
+ # Equivalent to build-paths, plan --shard/--total, then run that command with this slice's paths.
6
+ # A separate CI job downloads coverage/polyrun-fragment-*.json (e.g. shard<S>-worker<W>.json per N×M process) and runs merge-coverage.
7
+ # For N×M (N matrix jobs × M processes per job): set shard_processes: M or POLYRUN_SHARD_PROCESSES,
8
+ # or pass --shard-processes M to ci-shard-run / ci-shard-rspec (local split is round-robin).
9
+ # See: docs/SETUP_PROFILE.md
10
+
11
+ partition:
12
+ paths_file: spec/spec_paths.txt
13
+ shard_total: 5
14
+ shard_index: 0
15
+ strategy: round_robin
16
+ paths_build:
17
+ all_glob: spec/**/*_spec.rb
18
+ stages: []
19
+
20
+ # Optional reporting (see docs/SPEC_QUALITY.md):
21
+ # reporting:
22
+ # merge_spec_quality: true
@@ -0,0 +1,13 @@
1
+ # Polyrun — minimal gem/library layout (no Rails prepare, no databases: block).
2
+ # Fill in paths_build.stages if you need regex or substring ordering for slow specs.
3
+ # Next: spec_helper — require "polyrun" + Polyrun::Coverage::Collector (or your collector config)
4
+ # See: docs/SETUP_PROFILE.md
5
+
6
+ partition:
7
+ paths_file: spec/spec_paths.txt
8
+ shard_total: 5
9
+ shard_index: 0
10
+ strategy: round_robin
11
+ paths_build:
12
+ all_glob: spec/**/*_spec.rb
13
+ stages: []
@@ -0,0 +1,12 @@
1
+ # Optional Polyrun hooks — copy to config/polyrun_hooks.rb and reference from polyrun.yml:
2
+ # hooks:
3
+ # ruby_file: config/polyrun_hooks.rb
4
+ #
5
+ # Worker-phase hooks run in each parallel test child (around bundle exec rspec).
6
+
7
+ before(:each) do |env|
8
+ next unless env["POLYRUN_HOOK_ORCHESTRATOR"] == "0"
9
+ next unless defined?(Polyrun::SpecQuality) && Polyrun::SpecQuality.enabled?
10
+
11
+ Polyrun::SpecQuality::RspecHook.ensure_started!
12
+ end
@@ -0,0 +1,20 @@
1
+ # Opt-in per-example spec quality (coverage deltas, resource use). See docs/SPEC_QUALITY.md.
2
+ track_under:
3
+ - lib
4
+ - app
5
+ min_line_churn: 50
6
+ min_query_count: 20
7
+ hot_line_example_overlap: 10
8
+ strict: false
9
+ sample: 1.0
10
+ ignore_examples: []
11
+ ignore_paths: []
12
+ ignore_query_patterns: []
13
+ profile:
14
+ - cpu
15
+ - mem
16
+ sql_counter: false
17
+ # CI gates (used with POLYRUN_SPEC_QUALITY_STRICT=1 or strict: true):
18
+ # minimum_unique_lines_per_example: 1
19
+ # max_zero_hit_examples: 0
20
+ # max_hot_line_overlap: 100
@@ -0,0 +1,51 @@
1
+ # Polyrun — Rails app with one-shot prepare before parallel workers.
2
+ # Adjust prepare.command to your repo (assets, Playwright, webapp build, etc.).
3
+ # Gate per-worker duplicate work in application code when POLYRUN_SHARD_TOTAL > 1.
4
+ # See: docs/SETUP_PROFILE.md
5
+
6
+ version: 1
7
+
8
+ partition:
9
+ paths_file: spec/spec_paths.txt
10
+ shard_total: 5
11
+ shard_index: 0
12
+ strategy: round_robin
13
+ paths_build:
14
+ all_glob: spec/**/*_spec.rb
15
+ stages: []
16
+
17
+ prepare:
18
+ recipe: shell
19
+ rails_root: .
20
+ command: bundle exec ruby bin/test_prepare.rb
21
+
22
+ start:
23
+ prepare: true
24
+ databases: true
25
+
26
+ # Optional suite hooks (uncomment and set commands):
27
+ # hooks:
28
+ # before_suite: bundle exec ruby script/polyrun_before_suite.rb
29
+ # after_suite: bundle exec ruby script/polyrun_after_suite.rb
30
+ hooks: {}
31
+
32
+ # Optional reporting (see docs/SPEC_QUALITY.md):
33
+ # reporting:
34
+ # merge_failures: true
35
+ # merge_spec_quality: true
36
+ # merge_spec_quality_output: coverage/polyrun-spec-quality.json
37
+
38
+ # Uncomment and edit when using Postgres template + per-shard DB names from polyrun env.
39
+ # CloneShards / polyrun start auto provision is PostgreSQL-only. For mysql2 (or other adapters),
40
+ # keep template_db + shard_db_pattern for URL shaping, set start.databases: false, and provision shards in prepare.
41
+ # databases:
42
+ # template_db: my_app_template
43
+ # shard_db_pattern: "my_app_test_%{shard}"
44
+ # postgresql:
45
+ # host: localhost
46
+ # username: postgres
47
+ # connections:
48
+ # - name: analytics
49
+ # template_db: my_app_analytics_template # second migrate + CREATE … TEMPLATE for parallel tests
50
+ # shard_db_pattern: "my_app_analytics_test_%{shard}"
51
+ # env_key: ANALYTICS_DATABASE_URL # must match database.yml / Rails multi-db
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Generated by: polyrun init --profile rails-prepare-task
4
+ # Runs once before parallel workers. Skip: POLYRUN_SKIP_PREPARE=1 (legacy: POLYRUN_START_SKIP_PREPARE=1).
5
+
6
+ namespace :polyrun do
7
+ desc "One-shot Polyrun prepare (delegates to `polyrun prepare`)"
8
+ task prepare: :environment do
9
+ skip = %w[1 true yes].include?(ENV["POLYRUN_SKIP_PREPARE"].to_s.downcase) ||
10
+ %w[1 true yes].include?(ENV["POLYRUN_START_SKIP_PREPARE"].to_s.downcase)
11
+ next if skip
12
+
13
+ ok = system({"RUBYOPT" => nil}, "bundle", "exec", "polyrun", "prepare")
14
+ abort "polyrun prepare failed" unless ok
15
+ end
16
+ end
@@ -0,0 +1,36 @@
1
+ require "json"
2
+
3
+ require_relative "../debug"
4
+ require_relative "stats"
5
+ require_relative "variance_report"
6
+
7
+ module Polyrun
8
+ module Timing
9
+ module Merge
10
+ module_function
11
+
12
+ def merge_files(paths)
13
+ merged = {}
14
+ paths.each do |p|
15
+ data = JSON.parse(File.read(p))
16
+ next unless data.is_a?(Hash)
17
+
18
+ data.each do |file, sec|
19
+ f = file.to_s
20
+ entry = sec
21
+ merged[f] = merged.key?(f) ? Stats.merge_entries(merged[f], entry) : Stats.normalize_entry(entry)
22
+ end
23
+ end
24
+ merged
25
+ end
26
+
27
+ def merge_and_write(paths, output_path)
28
+ Polyrun::Debug.log_kv(merge_timing: "merge_and_write", input_count: paths.size, output_path: output_path)
29
+ merged = Polyrun::Debug.time("Timing::Merge.merge_files") { merge_files(paths) }
30
+ Polyrun::Debug.time("Timing::Merge.write JSON") { File.write(output_path, JSON.pretty_generate(merged)) }
31
+ Timing::VarianceReport.emit_warnings!(merged)
32
+ merged
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,60 @@
1
+ require "json"
2
+
3
+ require "rspec/core/formatters"
4
+
5
+ module Polyrun
6
+ module Timing
7
+ # Experimental: records +absolute_path:line_number+ => wall seconds per example for
8
+ # {Partition::Plan} +timing_granularity: :example+ and +merge-timing+.
9
+ #
10
+ # Use after RSpec is loaded:
11
+ # require "polyrun/timing/rspec_example_formatter"
12
+ # RSpec.configure { |c| c.add_formatter Polyrun::Timing::RSpecExampleFormatter }
13
+ # Or {Polyrun::RSpecSupport.install_example_timing!} (+output_path:+ avoids touching +ENV+).
14
+ #
15
+ # Default output path: +ENV["POLYRUN_EXAMPLE_TIMING_OUT"]+ if set, else +polyrun_timing_examples.json+.
16
+ class RSpecExampleFormatter
17
+ ::RSpec::Core::Formatters.register self, :example_finished, :close
18
+
19
+ def initialize(output)
20
+ @output = output
21
+ @times = {}
22
+ end
23
+
24
+ def example_finished(notification)
25
+ ex = notification.example
26
+ return if ex.pending?
27
+
28
+ result = ex.execution_result
29
+ t = result.run_time
30
+ return unless t
31
+
32
+ path = example_absolute_path(ex)
33
+ return unless path
34
+
35
+ line = ex.metadata[:line_number]
36
+ return unless line
37
+
38
+ key = "#{File.expand_path(path)}:#{line}"
39
+ cur = @times[key]
40
+ @times[key] = cur ? [cur, t].max : t
41
+ end
42
+
43
+ def close(_notification)
44
+ File.write(timing_output_path, JSON.pretty_generate(@times))
45
+ end
46
+
47
+ # Override in a subclass from {Polyrun::RSpecSupport.install_example_timing!(output_path: ...)}.
48
+ def timing_output_path
49
+ ENV["POLYRUN_EXAMPLE_TIMING_OUT"] || "polyrun_timing_examples.json"
50
+ end
51
+
52
+ private
53
+
54
+ def example_absolute_path(example)
55
+ metadata = example.metadata
56
+ metadata[:absolute_file_path] || metadata[:file_path] || metadata[:absolute_path]
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,76 @@
1
+ module Polyrun
2
+ module Timing
3
+ # Normalizes scalar or object timing entries for merge and binpack weight lookup.
4
+ module Stats
5
+ STAT_KEYS = %w[last_seconds min max mean p95 runs failures timeouts].freeze
6
+
7
+ module_function
8
+
9
+ def normalize_entry(value)
10
+ case value
11
+ when Hash
12
+ normalize_hash(value)
13
+ else
14
+ sec = value.to_f
15
+ {
16
+ "last_seconds" => sec,
17
+ "min" => sec,
18
+ "max" => sec,
19
+ "mean" => sec,
20
+ "p95" => sec,
21
+ "runs" => 1,
22
+ "failures" => 0,
23
+ "timeouts" => 0
24
+ }
25
+ end
26
+ end
27
+
28
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity -- timing hash key coercion
29
+ def normalize_hash(h)
30
+ out = {}
31
+ sec = h["last_seconds"] || h[:last_seconds] || h["seconds"] || h[:seconds]
32
+ sec = sec.to_f if sec
33
+ mean = (h["mean"] || h[:mean] || sec)&.to_f
34
+ out["last_seconds"] = (sec || mean || 0.0).to_f
35
+ out["min"] = (h["min"] || h[:min] || out["last_seconds"]).to_f
36
+ out["max"] = (h["max"] || h[:max] || out["last_seconds"]).to_f
37
+ out["mean"] = (mean || out["last_seconds"]).to_f
38
+ out["p95"] = (h["p95"] || h[:p95] || out["max"]).to_f
39
+ out["runs"] = Integer(h["runs"] || h[:runs] || 1)
40
+ out["failures"] = Integer(h["failures"] || h[:failures] || 0)
41
+ out["timeouts"] = Integer(h["timeouts"] || h[:timeouts] || 0)
42
+ out
43
+ end
44
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
45
+
46
+ def binpack_weight(entry)
47
+ h = normalize_entry(entry)
48
+ h["last_seconds"].positive? ? h["last_seconds"] : h["mean"]
49
+ end
50
+
51
+ # rubocop:disable Metrics/AbcSize -- weighted mean merge
52
+ def merge_entries(a, b)
53
+ ha = normalize_entry(a)
54
+ hb = normalize_entry(b)
55
+ runs = ha["runs"] + hb["runs"]
56
+ mean =
57
+ if runs.positive?
58
+ ((ha["mean"] * ha["runs"]) + (hb["mean"] * hb["runs"])) / runs.to_f
59
+ else
60
+ 0.0
61
+ end
62
+ {
63
+ "last_seconds" => [ha["last_seconds"], hb["last_seconds"]].max,
64
+ "min" => [ha["min"], hb["min"]].min,
65
+ "max" => [ha["max"], hb["max"]].max,
66
+ "mean" => mean,
67
+ "p95" => [ha["p95"], hb["p95"]].max,
68
+ "runs" => runs,
69
+ "failures" => ha["failures"] + hb["failures"],
70
+ "timeouts" => ha["timeouts"] + hb["timeouts"]
71
+ }
72
+ end
73
+ # rubocop:enable Metrics/AbcSize
74
+ end
75
+ end
76
+ end