ruby-anything 0.2.1 → 0.2.3

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.
@@ -1,134 +0,0 @@
1
- # PortPilot Ruby adapter.
2
- #
3
- # Runs in a separate process (never inside Rails). Reads one JSON request on stdin and
4
- # writes JSON lines on stdout. The migrated code must define `module Migrated` with
5
- # singleton methods named like the original functions.
6
- #
7
- # Commands: replay (run each case once, capture behaviour), bench (time N iterations per case).
8
- require "json"
9
- require "stringio"
10
- require "tmpdir"
11
- require "digest"
12
- require "set"
13
-
14
- REAL_STDOUT = $stdout.dup
15
- REAL_STDOUT.sync = true
16
-
17
- def emit(event, **data)
18
- REAL_STDOUT.write(JSON.generate(data.merge(event: event)) + "\n")
19
- end
20
-
21
- def encode(value)
22
- case value
23
- when nil, true, false, String, Integer then value
24
- when Float
25
- return { "$float" => "nan" } if value.nan?
26
- return { "$float" => value.positive? ? "inf" : "-inf" } if value.infinite?
27
- return { "$float" => "-0.0" } if value.zero? && (1.0 / value).negative?
28
- return { "$float" => value.to_s } if value == value.floor && value.abs > 2**53 # inexact integral double
29
-
30
- value
31
- when Array then value.map { |v| encode(v) }
32
- when Hash then value.to_h { |k, v| [ k.to_s, encode(v) ] }
33
- when Set then { "$set" => value.map { |v| encode(v) }.sort_by { |v| JSON.generate(v) } }
34
- else { "$repr" => value.inspect[0, 200] }
35
- end
36
- end
37
-
38
- def decode(value)
39
- case value
40
- when Array then value.map { |v| decode(v) }
41
- when Hash
42
- if value.key?("$float")
43
- { "nan" => Float::NAN, "inf" => Float::INFINITY, "-inf" => -Float::INFINITY, "-0.0" => -0.0 }.fetch(value["$float"]) { Float(value["$float"]) }
44
- elsif value.key?("$bytes") then value["$bytes"].unpack1("m0").b
45
- elsif value.key?("$set") then Set.new(decode(value["$set"]))
46
- else value.transform_values { |v| decode(v) }
47
- end
48
- else value
49
- end
50
- end
51
-
52
- def snapshot(root)
53
- Dir.glob("**/*", File::FNM_DOTMATCH, base: root).filter_map do |rel|
54
- full = File.join(root, rel)
55
- next unless File.file?(full)
56
- data = File.binread(full)
57
- { "path" => rel, "size" => data.bytesize, "sha256" => Digest::SHA256.hexdigest(data) }
58
- end.sort_by { |e| e["path"] }
59
- end
60
-
61
- def call(function, case_data)
62
- args = decode(case_data.fetch("args", []))
63
- kwargs = decode(case_data.fetch("kwargs", {})).transform_keys(&:to_sym)
64
- kwargs.empty? ? Migrated.public_send(function, *args) : Migrated.public_send(function, *args, **kwargs)
65
- end
66
-
67
- def run_case(function, case_data)
68
- out = StringIO.new
69
- err = StringIO.new
70
- Dir.mktmpdir("pp-case-") do |scratch|
71
- Dir.chdir(scratch) do
72
- started = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
73
- $stdout, $stderr = out, err
74
- outcome =
75
- begin
76
- { "kind" => "return", "value" => encode(call(function, case_data)) }
77
- rescue StandardError => e
78
- { "kind" => "raise", "type" => e.class.name, "message" => e.message }
79
- ensure
80
- $stdout, $stderr = STDOUT, STDERR
81
- end
82
- duration = (Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond) - started) / 1000
83
- { "outcome" => outcome, "stdout" => out.string, "stderr" => err.string,
84
- "effects" => snapshot(scratch), "duration_us" => duration }
85
- end
86
- end
87
- end
88
-
89
- def load_code(path)
90
- # Silence output produced while loading the file itself.
91
- $stdout = StringIO.new
92
- load(path)
93
- ensure
94
- $stdout = STDOUT
95
- end
96
-
97
- request = JSON.parse($stdin.read)
98
- load_code(request.fetch("code_file"))
99
- function = request.fetch("function")
100
- unless defined?(Migrated) && Migrated.respond_to?(function)
101
- emit("fatal", message: "Migrated.#{function} is not defined. Define `module Migrated; def self.#{function}(...)`.")
102
- exit 2
103
- end
104
-
105
- emit("plan", total: request["cases"].size, runtime: "ruby #{RUBY_VERSION}")
106
- case request.fetch("command")
107
- when "replay"
108
- request["cases"].each { |c| emit("case", ref: c["ref"], **run_case(function, c).transform_keys(&:to_sym)) }
109
- when "bench"
110
- iterations = request.fetch("iterations", 30).to_i.clamp(1, 10_000)
111
- request["cases"].each do |c|
112
- samples = Dir.mktmpdir("pp-bench-") do |scratch|
113
- Dir.chdir(scratch) do
114
- $stdout = StringIO.new
115
- Array.new(iterations) do
116
- started = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
117
- begin
118
- call(function, c)
119
- rescue StandardError
120
- nil
121
- end
122
- Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond) - started
123
- end
124
- ensure
125
- $stdout = STDOUT
126
- end
127
- end
128
- emit("case", ref: c["ref"], samples_ns: samples, peak_memory_bytes: nil)
129
- end
130
- else
131
- emit("fatal", message: "unknown command")
132
- exit 2
133
- end
134
- emit("done")