pbt 0.8.0 → 0.9.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +38 -78
- data/lib/pbt/check/runner_methods.rb +6 -2
- data/lib/pbt/reporter/run_details_reporter.rb +33 -4
- data/lib/pbt/stateful/command.rb +215 -0
- data/lib/pbt/stateful/errors.rb +58 -0
- data/lib/pbt/stateful/property.rb +276 -280
- data/lib/pbt/stateful/shrinker.rb +295 -0
- data/lib/pbt/stateful/spec.rb +299 -0
- data/lib/pbt/stateful/types.rb +308 -0
- data/lib/pbt/version.rb +1 -1
- data/lib/pbt.rb +19 -19
- data/sig/manifest.yaml +2 -0
- data/sig/pbt/arbitrary.rbs +139 -0
- data/sig/pbt/check.rbs +74 -0
- data/sig/pbt/reporter.rbs +44 -0
- data/sig/pbt/stateful.rbs +241 -0
- data/sig/pbt.rbs +40 -1
- metadata +11 -19
- data/.rspec +0 -3
- data/.standard.yml +0 -3
- data/CLAUDE.md +0 -104
- data/Rakefile +0 -70
- data/benchmark/FIBER.md +0 -97
- data/benchmark/README.md +0 -103
- data/benchmark/example.txt +0 -299
- data/benchmark/failure_simple.rb +0 -29
- data/benchmark/fiber_experiment.rb +0 -39
- data/benchmark/fiber_scheduler/Gemfile +0 -7
- data/benchmark/fiber_scheduler/experiment.rb +0 -131
- data/benchmark/fiber_scheduler/spec/experiment_spec.rb +0 -38
- data/benchmark/success_cpu_bound.rb +0 -35
- data/benchmark/success_io_bound.rb +0 -28
- data/benchmark/success_simple.rb +0 -24
- data/docs/plans/fiber-experiment.md +0 -12
- data/docs/plans/generator-statistics.md +0 -16
- data/docs/plans/stateful-pbt-redesign.md +0 -415
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "pbt"
|
|
4
|
-
require "async"
|
|
5
|
-
require "socket"
|
|
6
|
-
|
|
7
|
-
# Benchmark only: does not execute Pbt.check or implement a public worker.
|
|
8
|
-
module SchedulerExperiment
|
|
9
|
-
# One server thread delays each response independently. It never uses a Fiber
|
|
10
|
-
# scheduler, so all three client modes see the same delayed TCP protocol.
|
|
11
|
-
class Server
|
|
12
|
-
attr_reader :port
|
|
13
|
-
|
|
14
|
-
def initialize(num_runs:, delay:)
|
|
15
|
-
@listener = TCPServer.new("127.0.0.1", 0)
|
|
16
|
-
@port = @listener.addr[1]
|
|
17
|
-
@thread = Thread.new { serve(num_runs, delay) }
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def serve(num_runs, delay)
|
|
21
|
-
pending = {}
|
|
22
|
-
completed = 0
|
|
23
|
-
peak = 0
|
|
24
|
-
until completed == num_runs
|
|
25
|
-
timeout = pending.empty? ? nil : [pending.values.min - SchedulerExperiment.now, 0].max
|
|
26
|
-
if IO.select([@listener], nil, nil, timeout)
|
|
27
|
-
loop do
|
|
28
|
-
socket = @listener.accept_nonblock(exception: false)
|
|
29
|
-
break if socket == :wait_readable
|
|
30
|
-
pending[socket] = SchedulerExperiment.now + delay
|
|
31
|
-
peak = [peak, pending.size].max
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
pending.keys.each do |socket|
|
|
35
|
-
next if pending.fetch(socket) > SchedulerExperiment.now
|
|
36
|
-
socket.write("ok")
|
|
37
|
-
socket.close
|
|
38
|
-
pending.delete(socket)
|
|
39
|
-
completed += 1
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
peak
|
|
43
|
-
ensure
|
|
44
|
-
pending&.each_key { |socket| socket.close unless socket.closed? }
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
def peak
|
|
48
|
-
@thread.value
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
def close
|
|
52
|
-
@thread.kill if @thread.alive?
|
|
53
|
-
@thread.join
|
|
54
|
-
@listener.close
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
def self.now
|
|
59
|
-
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
def self.measure(mode:, num_runs:, concurrency:, delay:)
|
|
63
|
-
raise ArgumentError, "existing scheduler must not be replaced" if Fiber.scheduler
|
|
64
|
-
raise ArgumentError, "unknown mode" unless [:sequential, :fiber, :scheduler].include?(mode)
|
|
65
|
-
raise ArgumentError, "positive runs and concurrency required" unless num_runs.positive? && concurrency.positive?
|
|
66
|
-
|
|
67
|
-
rng = Random.new(42)
|
|
68
|
-
values = Array.new(num_runs) { Pbt.integer(min: 0, max: 1000).generate(rng) }
|
|
69
|
-
property = Pbt.property(Pbt.integer) { |value| yield(value) if block_given? }
|
|
70
|
-
server = Server.new(num_runs: num_runs, delay: delay)
|
|
71
|
-
observed = Array.new(num_runs)
|
|
72
|
-
request = lambda do |index|
|
|
73
|
-
socket = TCPSocket.new("127.0.0.1", server.port)
|
|
74
|
-
raise "incorrect response" unless socket.read(2) == "ok"
|
|
75
|
-
property.run(values.fetch(index))
|
|
76
|
-
observed[index] = values.fetch(index)
|
|
77
|
-
ensure
|
|
78
|
-
socket&.close
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
started = now
|
|
82
|
-
if mode == :scheduler
|
|
83
|
-
Async do |parent|
|
|
84
|
-
(0...num_runs).each_slice(concurrency) do |batch|
|
|
85
|
-
tasks = []
|
|
86
|
-
begin
|
|
87
|
-
batch.each { |index| tasks << parent.async { request.call(index) } }
|
|
88
|
-
tasks.each(&:wait)
|
|
89
|
-
ensure
|
|
90
|
-
tasks.each(&:stop)
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
end.wait
|
|
94
|
-
else
|
|
95
|
-
num_runs.times do |index|
|
|
96
|
-
if mode == :fiber
|
|
97
|
-
Fiber.new { request.call(index) }.resume
|
|
98
|
-
else
|
|
99
|
-
request.call(index)
|
|
100
|
-
end
|
|
101
|
-
end
|
|
102
|
-
end
|
|
103
|
-
elapsed = now - started
|
|
104
|
-
raise "incorrect generated inputs" unless observed == values
|
|
105
|
-
{elapsed: elapsed, values: observed, completed: observed.size, max_pending: server.peak}
|
|
106
|
-
ensure
|
|
107
|
-
server&.close
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
def self.run(num_runs: 100, concurrency: 10, delay: 0.01, repeats: 5)
|
|
111
|
-
raise ArgumentError, "repeats must be a positive odd number" unless repeats.positive? && repeats.odd?
|
|
112
|
-
|
|
113
|
-
puts "Ruby #{RUBY_VERSION}; async #{Async::VERSION}; #{num_runs} cases; seed 42; delay #{delay}s; concurrency #{concurrency}; #{repeats} repeats"
|
|
114
|
-
[:sequential, :fiber, :scheduler].each do |mode|
|
|
115
|
-
# Warm up separately; exclude startup/gem loading from each timed request loop.
|
|
116
|
-
measure(mode: mode, num_runs: concurrency, concurrency: concurrency, delay: delay)
|
|
117
|
-
results = Array.new(repeats) { measure(mode: mode, num_runs: num_runs, concurrency: concurrency, delay: delay) }
|
|
118
|
-
times = results.map { |result| result.fetch(:elapsed) }
|
|
119
|
-
puts "#{mode}: seconds=#{times.map { |time| format("%.6f", time) }.join(",")}; median=#{format("%.6f", times.sort.fetch(repeats / 2))}; completed=#{results.map { |r| r.fetch(:completed) }.uniq.inspect}; max_pending=#{results.map { |r| r.fetch(:max_pending) }.inspect}; checksum=#{results.first.fetch(:values).sum}"
|
|
120
|
-
end
|
|
121
|
-
end
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
if $PROGRAM_NAME == __FILE__
|
|
125
|
-
SchedulerExperiment.run(
|
|
126
|
-
num_runs: Integer(ENV.fetch("NUM_RUNS", "100")),
|
|
127
|
-
concurrency: Integer(ENV.fetch("CONCURRENCY", "10")),
|
|
128
|
-
delay: Float(ENV.fetch("IO_DELAY", "0.01")),
|
|
129
|
-
repeats: Integer(ENV.fetch("REPEATS", "5"))
|
|
130
|
-
)
|
|
131
|
-
end
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "rspec"
|
|
4
|
-
require_relative "../experiment"
|
|
5
|
-
|
|
6
|
-
RSpec.describe SchedulerExperiment do
|
|
7
|
-
it "preserves the seeded inputs and bounds concurrent socket requests" do
|
|
8
|
-
results = [:sequential, :fiber, :scheduler].map do |mode|
|
|
9
|
-
described_class.measure(mode: mode, num_runs: 12, concurrency: 4, delay: 0.01)
|
|
10
|
-
end
|
|
11
|
-
expect(results.map { |result| result.fetch(:values) }.uniq.size).to eq(1)
|
|
12
|
-
expect(results.map { |result| result.fetch(:completed) }).to eq([12, 12, 12])
|
|
13
|
-
expect(results.first(2).map { |result| result.fetch(:max_pending) }).to eq([1, 1])
|
|
14
|
-
expect(results.last.fetch(:max_pending)).to be_between(1, 4)
|
|
15
|
-
expect(Fiber.scheduler).to be_nil
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
it "propagates predicate failures and cleans up the server and scheduler" do
|
|
19
|
-
server_threads = Thread.list
|
|
20
|
-
expect {
|
|
21
|
-
described_class.measure(mode: :scheduler, num_runs: 8, concurrency: 4, delay: 0.001) do
|
|
22
|
-
raise "predicate failure"
|
|
23
|
-
end
|
|
24
|
-
}.to raise_error("predicate failure")
|
|
25
|
-
expect(Fiber.scheduler).to be_nil
|
|
26
|
-
expect(Thread.list - server_threads).to be_empty
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
it "does not replace a caller's scheduler" do
|
|
30
|
-
Async do
|
|
31
|
-
scheduler = Fiber.scheduler
|
|
32
|
-
expect {
|
|
33
|
-
described_class.measure(mode: :scheduler, num_runs: 1, concurrency: 1, delay: 0)
|
|
34
|
-
}.to raise_error(ArgumentError, /existing scheduler/)
|
|
35
|
-
expect(Fiber.scheduler).to equal(scheduler)
|
|
36
|
-
end.wait
|
|
37
|
-
end
|
|
38
|
-
end
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
require "pbt"
|
|
2
|
-
require "benchmark/ips"
|
|
3
|
-
|
|
4
|
-
# tarai function with
|
|
5
|
-
# (x=10, y=5, z=0) calls 343073 times.
|
|
6
|
-
# (x=10, y=4, z=0) calls 271097 times.
|
|
7
|
-
# (x=9, y=5, z=0) calls 71973 times.
|
|
8
|
-
# (x=9, y=4, z=0) calls 55229 times.
|
|
9
|
-
# (x=8, y=4, z=0) calls 12605 times.
|
|
10
|
-
# (x=8, y=3, z=0) calls 305 times.
|
|
11
|
-
def task(x, y, z)
|
|
12
|
-
(x <= y) ? y : task(task(x - 1, y, z),
|
|
13
|
-
task(y - 1, z, x),
|
|
14
|
-
task(z - 1, x, y))
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
a, b, c = [9, 4, 0]
|
|
18
|
-
|
|
19
|
-
Benchmark.ips do |x|
|
|
20
|
-
x.report("ractor") do
|
|
21
|
-
Pbt.assert(worker: :ractor, num_runs: 100) do
|
|
22
|
-
Pbt.property(Pbt.constant([a, b, c])) do |x, y, z|
|
|
23
|
-
task(x, y, z)
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
x.report("none") do
|
|
29
|
-
Pbt.assert(worker: :none, num_runs: 100) do
|
|
30
|
-
Pbt.property(Pbt.constant([a, b, c])) do |x, y, z|
|
|
31
|
-
task(x, y, z)
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
end
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
require "pbt"
|
|
2
|
-
require "benchmark/ips"
|
|
3
|
-
|
|
4
|
-
# Read file generated by https://www.lipsum.com/
|
|
5
|
-
# 150 paragraphs, 13689 words, 92206 bytes.
|
|
6
|
-
def task(str)
|
|
7
|
-
File.read(File.join(__dir__, "example.txt")) + str
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
seed = 17243810592888013452170775373100387856
|
|
11
|
-
|
|
12
|
-
Benchmark.ips do |x|
|
|
13
|
-
x.report("ractor") do
|
|
14
|
-
Pbt.assert(worker: :ractor, seed:, num_runs: 100) do
|
|
15
|
-
Pbt.property(Pbt.ascii_string) do |str|
|
|
16
|
-
task(str)
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
x.report("none") do
|
|
22
|
-
Pbt.assert(worker: :none, seed:, num_runs: 100) do
|
|
23
|
-
Pbt.property(Pbt.ascii_string) do |str|
|
|
24
|
-
task(str)
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
end
|
data/benchmark/success_simple.rb
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
require "pbt"
|
|
2
|
-
require "benchmark/ips"
|
|
3
|
-
|
|
4
|
-
seed = 17243810592888013452170775373100387856
|
|
5
|
-
|
|
6
|
-
def task(x) = x + 1
|
|
7
|
-
|
|
8
|
-
Benchmark.ips do |x|
|
|
9
|
-
x.report("ractor") do
|
|
10
|
-
Pbt.assert(worker: :ractor, seed:, num_runs: 100) do
|
|
11
|
-
Pbt.property(Pbt.integer) do |x|
|
|
12
|
-
task(x)
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
x.report("none") do
|
|
18
|
-
Pbt.assert(worker: :none, seed:, num_runs: 100) do
|
|
19
|
-
Pbt.property(Pbt.integer) do |x|
|
|
20
|
-
task(x)
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
Fiber experiment: add reproducible benchmark comparing ordinary sequential calls with
|
|
2
|
-
creating/resuming one Fiber per case, same generated inputs and predicates. Include CPU
|
|
3
|
-
and blocking sleep workloads; configurable small runs. No production worker yet.
|
|
4
|
-
Document that bare Fibers do not overlap blocking work without a scheduler; using an
|
|
5
|
-
external scheduler introduces dependency/lifecycle/cancellation decisions. Validate same
|
|
6
|
-
successful count and deterministic failure+shrinking by running each predicate inside a Fiber.
|
|
7
|
-
Decision: keep :none/:ractor for v1; revisit scheduler-backed worker for concrete IO use case.
|
|
8
|
-
|
|
9
|
-
Completed: red specs committed a439561; adapter and benchmark added.
|
|
10
|
-
The existing halving strategy reaches 7 (not global minimum 5) with seed42;
|
|
11
|
-
regression expectation corrected to the serial baseline. Full rake: 183 examples,
|
|
12
|
-
0 failures; Standard passes. Smoke benchmark recorded in benchmark/FIBER.md.
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# Generator statistics
|
|
2
|
-
|
|
3
|
-
Goal: inspect generated value distributions independently of property execution.
|
|
4
|
-
API: `Pbt.statistics(arb, num_runs: 100, seed: Random.new_seed) { |value| label }`.
|
|
5
|
-
Return seed, num_runs, counts and unrounded percentages; no block counts values.
|
|
6
|
-
Runs must be a nonnegative integer. Zero generates/classifies nothing. Use a local
|
|
7
|
-
seeded RNG, propagate errors and keep each invocation independent. Values or classifier
|
|
8
|
-
labels used as Hash keys must be stable (not mutated by later generation).
|
|
9
|
-
|
|
10
|
-
- [x] Write tests, run red (12 failures, missing API), review contract/tests.
|
|
11
|
-
- [x] Commit tests before implementation.
|
|
12
|
-
- [x] Add statistics class and Pbt entry point, docs and full suite/lint.
|
|
13
|
-
- [x] Independent review, fix/re-review, commit, PR referencing #26.
|
|
14
|
-
|
|
15
|
-
Verification: 13 red tests committed 04fb022; full suite 194 examples, 0 failures;
|
|
16
|
-
Standard passes. Independent review clean, including global RNG isolation regression.
|