polyrun-legacy 0.1.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.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +8 -0
  3. data/CODE_OF_CONDUCT.md +26 -0
  4. data/CONTRIBUTING.md +23 -0
  5. data/LICENSE +21 -0
  6. data/README.md +115 -0
  7. data/SECURITY.md +25 -0
  8. data/bin/polyrun +5 -0
  9. data/lib/polyrun/legacy/cli.rb +102 -0
  10. data/lib/polyrun/legacy/compat.rb +53 -0
  11. data/lib/polyrun/legacy/config_commands.rb +52 -0
  12. data/lib/polyrun/legacy/configuration.rb +115 -0
  13. data/lib/polyrun/legacy/coverage_formats.rb +95 -0
  14. data/lib/polyrun/legacy/coverage_reports.rb +74 -0
  15. data/lib/polyrun/legacy/failure_reports.rb +100 -0
  16. data/lib/polyrun/legacy/junit_reports.rb +115 -0
  17. data/lib/polyrun/legacy/lifecycle.rb +95 -0
  18. data/lib/polyrun/legacy/merge.rb +139 -0
  19. data/lib/polyrun/legacy/options.rb +48 -0
  20. data/lib/polyrun/legacy/partition.rb +22 -0
  21. data/lib/polyrun/legacy/partition_plan.rb +105 -0
  22. data/lib/polyrun/legacy/paths.rb +59 -0
  23. data/lib/polyrun/legacy/planning_commands.rb +104 -0
  24. data/lib/polyrun/legacy/process_launch.rb +78 -0
  25. data/lib/polyrun/legacy/report_commands.rb +61 -0
  26. data/lib/polyrun/legacy/run_shards.rb +59 -0
  27. data/lib/polyrun/legacy/shard_commands.rb +132 -0
  28. data/lib/polyrun/legacy/shard_env.rb +34 -0
  29. data/lib/polyrun/legacy/shard_heap.rb +35 -0
  30. data/lib/polyrun/legacy/table.rb +54 -0
  31. data/lib/polyrun/legacy/timings.rb +103 -0
  32. data/lib/polyrun/legacy/version.rb +5 -0
  33. data/lib/polyrun/legacy/worker_wait.rb +80 -0
  34. data/lib/polyrun/legacy/yaml_data.rb +116 -0
  35. data/lib/polyrun/legacy.rb +7 -0
  36. data/polyrun-legacy.gemspec +44 -0
  37. metadata +91 -0
@@ -0,0 +1,54 @@
1
+ require "cgi"
2
+
3
+ module Polyrun
4
+ module Legacy
5
+ module Table
6
+ HTML_STYLE = "body{font:1rem system-ui,sans-serif;line-height:1.45;margin:1.25rem;color:#1b2635;background:#fff}" \
7
+ "h1{font-size:1.55rem;margin:0 0 1rem}table{width:100%;border-collapse:collapse;table-layout:fixed}" \
8
+ "th,td{text-align:left;overflow-wrap:anywhere;padding:.5rem;border-bottom:1px solid #c8d1dc}th{background:#edf1f7}" \
9
+ "th:first-child{width:36%}" \
10
+ "@media(max-width:520px){body{margin:.75rem}th,td{font-size:.875rem;padding:.375rem}th:first-child{width:32%}}"
11
+
12
+ module_function
13
+
14
+ def csv(headers, rows)
15
+ ([headers] + rows).map { |row| row.map { |cell| csv_cell(cell) }.join(",") }.join("\n") + "\n"
16
+ end
17
+
18
+ def csv_cell(value)
19
+ cell = value.to_s
20
+ (cell =~ /[",\r\n]/) ? "\"" + cell.gsub('"', '""') + "\"" : cell
21
+ end
22
+
23
+ def markdown(title, headers, rows)
24
+ lines = ([headers, headers.map { "---" }] + rows).map do |row|
25
+ "| " + row.map { |cell| markdown_text(cell) }.join(" | ") + " |"
26
+ end
27
+ "# #{markdown_text(title)}\n\n" + lines.join("\n") + "\n"
28
+ end
29
+
30
+ def markdown_text(cell)
31
+ xml(cell).tr("\r\n", " ").gsub(/[\\|`\[\]*_]/) { |character| "\\" + character }
32
+ end
33
+
34
+ def html(title, headers, rows)
35
+ content = rows.empty? ? "<p>No files in this report.</p>" : html_table(headers, rows)
36
+ heading = xml(title)
37
+ "<!doctype html>\n<html lang=\"en\"><head><meta charset=\"utf-8\">" \
38
+ "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">" \
39
+ "<title>#{heading}</title><style>#{HTML_STYLE}</style></head>" \
40
+ "<body><h1>#{heading}</h1>#{content}</body></html>\n"
41
+ end
42
+
43
+ def html_table(headers, rows)
44
+ columns = headers.map { |cell| "<th scope=\"col\">#{xml(cell)}</th>" }.join
45
+ body = rows.map { |row| "<tr>" + row.map { |cell| "<td>#{xml(cell)}</td>" }.join + "</tr>" }.join("\n")
46
+ "<table><thead><tr>#{columns}</tr></thead><tbody>#{body}</tbody></table>"
47
+ end
48
+
49
+ def xml(value)
50
+ CGI.escapeHTML(value.to_s.gsub(/[\x00-\x08\x0B\x0C\x0E-\x1F]/, ""))
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,103 @@
1
+ module Polyrun
2
+ module Legacy
3
+ module Timings
4
+ SECONDS = %w[last_seconds min max mean p95].freeze
5
+ COUNTERS = %w[runs failures timeouts].freeze
6
+
7
+ module_function
8
+
9
+ def read(path)
10
+ data = Compat.read_json(path)
11
+ raise ArgumentError, "#{path}: expected timing data by path" unless data.is_a?(Hash)
12
+
13
+ timings = {}
14
+ data.each { |item, entry| timings[item] = normalize(entry) }
15
+ timings
16
+ rescue ArgumentError => error
17
+ raise ArgumentError, "#{path}: #{error.message}" unless error.message.index("#{path}:") == 0
18
+
19
+ raise
20
+ end
21
+
22
+ def normalize(entry)
23
+ entry = {"last_seconds" => entry} if entry.is_a?(Numeric)
24
+ raise ArgumentError, "timing entries must be numbers or objects" unless entry.is_a?(Hash)
25
+
26
+ last = seconds(entry["last_seconds"] || entry["seconds"] || entry["mean"] || 0)
27
+ stats = {"last_seconds" => last}
28
+ %w[min max mean].each { |key| stats[key] = seconds(entry.fetch(key, last)) }
29
+ stats["p95"] = seconds(entry.fetch("p95", stats["max"]))
30
+ COUNTERS.each do |key|
31
+ stats[key] = Options.integer(entry.fetch(key, (key == "runs") ? 1 : 0), key, 0)
32
+ end
33
+ stats
34
+ end
35
+
36
+ def seconds(value)
37
+ number = Float(value)
38
+ return number if number.finite? && number >= 0
39
+
40
+ raise ArgumentError, "timing seconds must be finite and nonnegative"
41
+ rescue TypeError
42
+ raise ArgumentError, "timing seconds must be a number"
43
+ end
44
+
45
+ def weight(stats)
46
+ last = stats["last_seconds"]
47
+ (last > 0) ? last : stats["mean"]
48
+ end
49
+
50
+ def merge(paths)
51
+ merged = {}
52
+ paths.each do |path|
53
+ read(path).each do |item, stats|
54
+ previous = merged[item]
55
+ merged[item] = previous ? combine(previous, stats) : stats
56
+ end
57
+ end
58
+ merged
59
+ end
60
+
61
+ def combine(left, right)
62
+ combined = {}
63
+ COUNTERS.each { |key| combined[key] = left[key] + right[key] }
64
+ %w[last_seconds max p95].each { |key| combined[key] = [left[key], right[key]].max }
65
+ combined["min"] = [left["min"], right["min"]].min
66
+ runs = combined["runs"]
67
+ combined["mean"] = (runs == 0) ? 0.0 : left["mean"] * (left["runs"].to_f / runs) + right["mean"] * (right["runs"].to_f / runs)
68
+ combined
69
+ end
70
+
71
+ def report(data, limit, format_name)
72
+ ranked = data.sort_by { |path, stats| [-weight(stats), path] }.take(limit)
73
+ case format_name
74
+ when "text", "console", "txt"
75
+ summary(ranked)
76
+ when "csv"
77
+ rows = numbered(ranked) { |stats| stats.values_at(*(SECONDS + COUNTERS)) }
78
+ Table.csv(%w[rank path] + SECONDS + COUNTERS, rows)
79
+ when "markdown", "md"
80
+ rows = numbered(ranked) { |stats| [format("%.4f", weight(stats))] }
81
+ Table.markdown("Polyrun slowest files", %w[rank path seconds], rows)
82
+ else
83
+ raise ArgumentError, "choose text, csv, or markdown for --format"
84
+ end
85
+ end
86
+
87
+ def numbered(ranked)
88
+ rows = []
89
+ ranked.each_with_index { |(path, stats), index| rows << [index + 1, path] + yield(stats) }
90
+ rows
91
+ end
92
+
93
+ def summary(ranked)
94
+ title = "Polyrun slowest files (by wall time, seconds)"
95
+ return "#{title}\n (no data)\n" if ranked.empty?
96
+
97
+ lines = [title, ""]
98
+ numbered(ranked) { |stats| [weight(stats)] }.each { |row| lines << format(" %2d. %s %.4f", *row) }
99
+ lines.join("\n") + "\n"
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,5 @@
1
+ module Polyrun
2
+ module Legacy
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,80 @@
1
+ module Polyrun
2
+ module Legacy
3
+ module WorkerWait
4
+ module_function
5
+
6
+ def now
7
+ if Process.respond_to?(:clock_gettime)
8
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
9
+ else
10
+ Time.now.to_f
11
+ end
12
+ end
13
+
14
+ def wait(children, timeout)
15
+ return wait_in_order(children) unless timeout
16
+
17
+ failed_index = nil
18
+ failed_status = 0
19
+ until children.empty?
20
+ children.delete_if do |pid, index, _started, failure|
21
+ unless failure
22
+ result = Process.waitpid2(pid, Process::WNOHANG)
23
+ failure = exit_code(result.last) if result
24
+ end
25
+ next false unless failure
26
+
27
+ if failure != 0 && (failed_index.nil? || index < failed_index)
28
+ failed_index = index
29
+ failed_status = failure
30
+ end
31
+ true
32
+ end
33
+ time = now
34
+ overdue = children.find { |_pid, _index, started| time - started >= timeout }
35
+ if overdue
36
+ warn "polyrun run-shards: shard #{overdue[1]} timed out after #{timeout}s"
37
+ return 124
38
+ end
39
+ sleep([0.02, timeout].min) unless children.empty?
40
+ end
41
+
42
+ failed_status
43
+ end
44
+
45
+ def wait_in_order(children)
46
+ result = 0
47
+ until children.empty?
48
+ pid, _index, _started, failure = children.first
49
+ failure ||= exit_code(Process.waitpid2(pid).last)
50
+ children.shift
51
+ result = failure if result == 0
52
+ end
53
+ result
54
+ end
55
+
56
+ def exit_code(status)
57
+ status.exitstatus || (128 + status.termsig)
58
+ end
59
+
60
+ def stop(children)
61
+ children.each { |pid, _index, _started, _failure| kill_group(pid) if pid }
62
+ children.each { |pid, _index, _started, _failure| reap(pid) if pid }
63
+ end
64
+
65
+ def kill_group(pid)
66
+ Process.kill("KILL", -pid)
67
+ rescue Errno::ESRCH
68
+ begin
69
+ Process.kill("KILL", pid)
70
+ rescue Errno::ESRCH
71
+ end
72
+ end
73
+
74
+ def reap(pid)
75
+ Process.waitpid(pid)
76
+ rescue Errno::ECHILD
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,116 @@
1
+ module Polyrun
2
+ module Legacy
3
+ module YamlData
4
+ BARE_SCALARS = {
5
+ "true" => true, "yes" => true, "on" => true,
6
+ "false" => false, "no" => false, "off" => false,
7
+ "null" => nil, "~" => nil, "" => nil
8
+ }.freeze
9
+ TAG_PREFIX = "tag:yaml.org,2002:"
10
+
11
+ module_function
12
+
13
+ def read(path)
14
+ require "yaml"
15
+ document = parse_file(path)
16
+ return {} unless document
17
+
18
+ if defined?(YAML::Syck::Node) && document.is_a?(YAML::Syck::Node)
19
+ syck(document, {}, 0)
20
+ else
21
+ psych(document.root, 0)
22
+ end
23
+ end
24
+
25
+ def parse_file(path)
26
+ YAML.parse_file(path)
27
+ rescue => error
28
+ raise ArgumentError, "#{path}: #{error.message}"
29
+ end
30
+
31
+ def syck(node, visited, depth)
32
+ check_depth(depth)
33
+ raise ArgumentError, "YAML aliases are unsupported" if visited[node.object_id]
34
+
35
+ visited[node.object_id] = true
36
+ case node.kind
37
+ when :map
38
+ check_tag(node.type_id, "map")
39
+ mapping(node.value) { |child| syck(child, visited, depth + 1) }
40
+ when :seq
41
+ check_tag(node.type_id, "seq")
42
+ node.value.map { |child| syck(child, visited, depth + 1) }
43
+ when :scalar
44
+ scalar(node.value, node.type_id, true)
45
+ else
46
+ raise ArgumentError, "unsupported YAML node"
47
+ end
48
+ end
49
+
50
+ def psych(node, depth)
51
+ return nil unless node
52
+
53
+ check_depth(depth)
54
+ case node
55
+ when Psych::Nodes::Mapping
56
+ check_tag(node.tag, "map")
57
+ pairs = []
58
+ node.children.each_slice(2) { |pair| pairs << pair }
59
+ mapping(pairs) { |child| psych(child, depth + 1) }
60
+ when Psych::Nodes::Sequence
61
+ check_tag(node.tag, "seq")
62
+ node.children.map { |child| psych(child, depth + 1) }
63
+ when Psych::Nodes::Scalar
64
+ scalar(node.value, node.tag, node.plain)
65
+ else
66
+ raise ArgumentError, "YAML aliases are unsupported"
67
+ end
68
+ end
69
+
70
+ def mapping(pairs)
71
+ result = {}
72
+ pairs.each do |key, value|
73
+ name = yield key
74
+ raise ArgumentError, "YAML mapping keys must be strings" unless name.is_a?(String)
75
+ raise ArgumentError, "duplicate YAML key #{name.inspect}" if result.key?(name)
76
+
77
+ result[name] = yield value
78
+ end
79
+ result
80
+ end
81
+
82
+ def scalar(text, tag, plain)
83
+ return plain ? bare_scalar(text) : text unless tag
84
+
85
+ case tag.sub(TAG_PREFIX, "")
86
+ when "str" then text
87
+ when "null" then nil
88
+ when "bool", "bool#yes", "bool#no" then BARE_SCALARS.fetch(text.downcase)
89
+ when "int" then Integer(text)
90
+ when "float" then Float(text)
91
+ else
92
+ raise ArgumentError, "unsupported YAML tag #{tag.inspect}"
93
+ end
94
+ rescue IndexError
95
+ raise ArgumentError, "invalid YAML scalar #{text.inspect}"
96
+ end
97
+
98
+ def bare_scalar(text)
99
+ folded = text.downcase
100
+ return BARE_SCALARS[folded] if BARE_SCALARS.key?(folded)
101
+ return text.to_i if text.index(/\A[-+]?\d+\z/)
102
+ return Float(text) if text.index(/\A[-+]?\d+\.\d+(?:e[-+]?\d+)?\z/i)
103
+
104
+ text
105
+ end
106
+
107
+ def check_depth(depth)
108
+ raise ArgumentError, "YAML configuration is too deeply nested" if depth > 64
109
+ end
110
+
111
+ def check_tag(tag, kind)
112
+ raise ArgumentError, "unsupported YAML tag #{tag.inspect}" if tag && tag != TAG_PREFIX + kind
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,7 @@
1
+ %w[
2
+ version compat options shard_env yaml_data configuration config_commands paths partition shard_heap
3
+ partition_plan worker_wait process_launch run_shards lifecycle shard_commands planning_commands table merge
4
+ coverage_formats coverage_reports timings report_commands failure_reports junit_reports cli
5
+ ].each do |component|
6
+ require File.expand_path("legacy/#{component}", File.dirname(__FILE__))
7
+ end
@@ -0,0 +1,44 @@
1
+ directory = File.expand_path(File.dirname(__FILE__)) # rubocop:disable Style/Dir
2
+ require File.join(directory, "lib/polyrun/legacy/version")
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "polyrun-legacy"
6
+ spec.version = Polyrun::Legacy::VERSION
7
+ spec.authors = ["Andrei Makarov"]
8
+ spec.email = ["contact@kiskolabs.com"]
9
+ spec.summary = "Experimental test sharding and reporting for older MRI Ruby"
10
+ spec.description = "Polyrun Legacy provides local and CI test sharding, suite configuration, preparation, and coverage, timing, and failure reports on MRI Ruby 1.8.7 and newer on POSIX. Support is experimental."
11
+ spec.homepage = "https://github.com/amkisko/polyrun.rb"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = ">= 1.8.7"
14
+
15
+ packaged = %w[
16
+ bin/polyrun
17
+ README.md
18
+ CHANGELOG.md
19
+ LICENSE
20
+ CONTRIBUTING.md
21
+ CODE_OF_CONDUCT.md
22
+ SECURITY.md
23
+ polyrun-legacy.gemspec
24
+ ]
25
+ library = Dir["#{directory}/lib/**/*.rb"].map { |file| file.slice(directory.length + 1, file.length) }
26
+ spec.files = (packaged + library).uniq
27
+ spec.bindir = "bin"
28
+ spec.executables = ["polyrun"]
29
+ spec.require_paths = ["lib"]
30
+ if spec.respond_to?(:extra_rdoc_files=)
31
+ spec.extra_rdoc_files = %w[README.md CHANGELOG.md LICENSE CONTRIBUTING.md CODE_OF_CONDUCT.md SECURITY.md]
32
+ end
33
+ if spec.respond_to?(:metadata=)
34
+ spec.metadata = {
35
+ "homepage_uri" => spec.homepage,
36
+ "source_code_uri" => "#{spec.homepage}/tree/main/legacy",
37
+ "changelog_uri" => "#{spec.homepage}/blob/main/legacy/CHANGELOG.md",
38
+ "bug_tracker_uri" => "#{spec.homepage}/issues",
39
+ "documentation_uri" => "#{spec.homepage}/tree/main/legacy#readme",
40
+ "allowed_push_host" => "https://rubygems.org",
41
+ "rubygems_mfa_required" => "true"
42
+ }
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polyrun-legacy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrei Makarov
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Polyrun Legacy provides local and CI test sharding, suite configuration,
13
+ preparation, and coverage, timing, and failure reports on MRI Ruby 1.8.7 and newer
14
+ on POSIX. Support is experimental.
15
+ email:
16
+ - contact@kiskolabs.com
17
+ executables:
18
+ - polyrun
19
+ extensions: []
20
+ extra_rdoc_files:
21
+ - CHANGELOG.md
22
+ - CODE_OF_CONDUCT.md
23
+ - CONTRIBUTING.md
24
+ - LICENSE
25
+ - README.md
26
+ - SECURITY.md
27
+ files:
28
+ - CHANGELOG.md
29
+ - CODE_OF_CONDUCT.md
30
+ - CONTRIBUTING.md
31
+ - LICENSE
32
+ - README.md
33
+ - SECURITY.md
34
+ - bin/polyrun
35
+ - lib/polyrun/legacy.rb
36
+ - lib/polyrun/legacy/cli.rb
37
+ - lib/polyrun/legacy/compat.rb
38
+ - lib/polyrun/legacy/config_commands.rb
39
+ - lib/polyrun/legacy/configuration.rb
40
+ - lib/polyrun/legacy/coverage_formats.rb
41
+ - lib/polyrun/legacy/coverage_reports.rb
42
+ - lib/polyrun/legacy/failure_reports.rb
43
+ - lib/polyrun/legacy/junit_reports.rb
44
+ - lib/polyrun/legacy/lifecycle.rb
45
+ - lib/polyrun/legacy/merge.rb
46
+ - lib/polyrun/legacy/options.rb
47
+ - lib/polyrun/legacy/partition.rb
48
+ - lib/polyrun/legacy/partition_plan.rb
49
+ - lib/polyrun/legacy/paths.rb
50
+ - lib/polyrun/legacy/planning_commands.rb
51
+ - lib/polyrun/legacy/process_launch.rb
52
+ - lib/polyrun/legacy/report_commands.rb
53
+ - lib/polyrun/legacy/run_shards.rb
54
+ - lib/polyrun/legacy/shard_commands.rb
55
+ - lib/polyrun/legacy/shard_env.rb
56
+ - lib/polyrun/legacy/shard_heap.rb
57
+ - lib/polyrun/legacy/table.rb
58
+ - lib/polyrun/legacy/timings.rb
59
+ - lib/polyrun/legacy/version.rb
60
+ - lib/polyrun/legacy/worker_wait.rb
61
+ - lib/polyrun/legacy/yaml_data.rb
62
+ - polyrun-legacy.gemspec
63
+ homepage: https://github.com/amkisko/polyrun.rb
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ homepage_uri: https://github.com/amkisko/polyrun.rb
68
+ source_code_uri: https://github.com/amkisko/polyrun.rb/tree/main/legacy
69
+ changelog_uri: https://github.com/amkisko/polyrun.rb/blob/main/legacy/CHANGELOG.md
70
+ bug_tracker_uri: https://github.com/amkisko/polyrun.rb/issues
71
+ documentation_uri: https://github.com/amkisko/polyrun.rb/tree/main/legacy#readme
72
+ allowed_push_host: https://rubygems.org
73
+ rubygems_mfa_required: 'true'
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 1.8.7
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 4.0.6
89
+ specification_version: 4
90
+ summary: Experimental test sharding and reporting for older MRI Ruby
91
+ test_files: []