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
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
module Pbt
|
|
2
|
+
module Stateful
|
|
3
|
+
type failure_stage = :sut | :run | :verify | :invariant | :teardown
|
|
4
|
+
|
|
5
|
+
class InvalidSpec < InvalidConfiguration
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
class GenerationError < StandardError
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class ModelError < StandardError
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class FailureSignature < Data
|
|
15
|
+
attr_reader stage: failure_stage
|
|
16
|
+
attr_reader target: Symbol | String | nil
|
|
17
|
+
attr_reader exception_class: singleton(Exception)
|
|
18
|
+
|
|
19
|
+
def initialize: (stage: failure_stage, target: Symbol | String | nil, exception_class: singleton(Exception)) -> void
|
|
20
|
+
def with: (?stage: failure_stage, ?target: Symbol | String | nil, ?exception_class: singleton(Exception)) -> FailureSignature
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class StepFailure < StandardError
|
|
24
|
+
attr_reader step_index: Integer?
|
|
25
|
+
attr_reader step: Step?
|
|
26
|
+
attr_reader stage: failure_stage
|
|
27
|
+
attr_reader target: Symbol | String | nil
|
|
28
|
+
attr_reader signature: FailureSignature
|
|
29
|
+
attr_reader teardown_error: Exception?
|
|
30
|
+
|
|
31
|
+
def initialize: (
|
|
32
|
+
String message,
|
|
33
|
+
step_index: Integer?,
|
|
34
|
+
step: Step?,
|
|
35
|
+
stage: failure_stage,
|
|
36
|
+
target: Symbol | String | nil,
|
|
37
|
+
exception_class: singleton(Exception),
|
|
38
|
+
?teardown_error: Exception?
|
|
39
|
+
) -> void
|
|
40
|
+
|
|
41
|
+
def attach_teardown_error: (Exception error) -> self
|
|
42
|
+
def message: () -> String
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
class Ref < Data
|
|
46
|
+
attr_reader step_id: Integer
|
|
47
|
+
|
|
48
|
+
def initialize: (step_id: Integer) -> void
|
|
49
|
+
def with: (?step_id: Integer) -> Ref
|
|
50
|
+
def to_s: () -> String
|
|
51
|
+
alias inspect to_s
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
class Step < Data
|
|
55
|
+
attr_reader id: Integer
|
|
56
|
+
attr_reader command: Symbol
|
|
57
|
+
attr_reader args: untyped
|
|
58
|
+
|
|
59
|
+
def initialize: (id: Integer, command: Symbol, args: untyped) -> void
|
|
60
|
+
def with: (?id: Integer, ?command: Symbol, ?args: untyped) -> Step
|
|
61
|
+
def format: (?assign: bool) -> String
|
|
62
|
+
def to_s: () -> String
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
class Run < Data
|
|
66
|
+
attr_reader initial_state: untyped
|
|
67
|
+
attr_reader steps: Array[Step]
|
|
68
|
+
|
|
69
|
+
def initialize: (initial_state: untyped, steps: Array[Step]) -> void
|
|
70
|
+
def with: (?initial_state: untyped, ?steps: Array[Step]) -> Run
|
|
71
|
+
def size: () -> Integer
|
|
72
|
+
def empty?: () -> bool
|
|
73
|
+
def command_names: () -> Array[Symbol]
|
|
74
|
+
def format: (?failed_step_index: Integer?, ?failed_stage: failure_stage?) -> String
|
|
75
|
+
def to_s: () -> String
|
|
76
|
+
alias inspect to_s
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
class Context < Data
|
|
80
|
+
attr_reader step: Step
|
|
81
|
+
attr_reader before: untyped
|
|
82
|
+
attr_reader after: untyped
|
|
83
|
+
attr_reader args: untyped
|
|
84
|
+
attr_reader result: untyped
|
|
85
|
+
attr_reader sut: untyped
|
|
86
|
+
|
|
87
|
+
def initialize: (
|
|
88
|
+
step: Step,
|
|
89
|
+
before: untyped,
|
|
90
|
+
after: untyped,
|
|
91
|
+
args: untyped,
|
|
92
|
+
result: untyped,
|
|
93
|
+
sut: untyped
|
|
94
|
+
) -> void
|
|
95
|
+
|
|
96
|
+
def with: (
|
|
97
|
+
?step: Step,
|
|
98
|
+
?before: untyped,
|
|
99
|
+
?after: untyped,
|
|
100
|
+
?args: untyped,
|
|
101
|
+
?result: untyped,
|
|
102
|
+
?sut: untyped
|
|
103
|
+
) -> Context
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
module Resolver
|
|
107
|
+
def self.arbitrary?: (untyped obj) -> bool
|
|
108
|
+
def self.deep_dup: (untyped obj, ?Hash[untyped, untyped] cache) -> untyped
|
|
109
|
+
def self.resolve: (untyped obj, Hash[Integer, untyped] env, ?detach: bool, ?cache: Hash[untyped, untyped]) -> untyped
|
|
110
|
+
def self.collect_ref_ids: (untyped obj, ?Set[Integer] ids, ?Hash[untyped, bool] visited) -> Set[Integer]
|
|
111
|
+
def self.referenced_step_ids: (Array[Step] steps) -> Set[Integer]
|
|
112
|
+
def self.format_value: (untyped obj, ?Hash[untyped, bool] visited) -> String
|
|
113
|
+
def self.deep_freeze: (untyped obj) -> untyped
|
|
114
|
+
def self.invoke_with_arity: (untyped callable, *untyped args) -> untyped
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
class Command
|
|
118
|
+
attr_reader name: Symbol
|
|
119
|
+
attr_reader weight: Integer
|
|
120
|
+
|
|
121
|
+
def initialize: (
|
|
122
|
+
name: Symbol,
|
|
123
|
+
weight: Integer,
|
|
124
|
+
guard_block: Proc?,
|
|
125
|
+
arguments_source: untyped,
|
|
126
|
+
precondition_block: Proc?,
|
|
127
|
+
next_state_block: Proc?,
|
|
128
|
+
run_block: Proc,
|
|
129
|
+
verify_block: Proc?
|
|
130
|
+
) -> void
|
|
131
|
+
|
|
132
|
+
def guard?: (untyped state) -> bool
|
|
133
|
+
def arguments_for: (untyped state) -> Arbitrary::_Arbitrary[untyped]
|
|
134
|
+
def precondition?: (untyped state, untyped args) -> bool
|
|
135
|
+
def next_state: (untyped state, untyped args, Ref ref) -> untyped
|
|
136
|
+
def run!: (untyped sut, untyped concrete_args) -> untyped
|
|
137
|
+
def verify!: (Context context) -> void
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
class CommandBuilder
|
|
141
|
+
def initialize: (name: Symbol, weight: Integer) -> void
|
|
142
|
+
def guard: () { (untyped state) -> boolish } -> void
|
|
143
|
+
def arguments: (Arbitrary::_Arbitrary[untyped] arb) -> void
|
|
144
|
+
| (**Arbitrary::_Arbitrary[untyped] kwargs) -> void
|
|
145
|
+
| () { (untyped state) -> Arbitrary::_Arbitrary[untyped] } -> void
|
|
146
|
+
def precondition: () { (untyped state, untyped args) -> boolish } -> void
|
|
147
|
+
def next_state: () { (untyped state, untyped args, Ref ref) -> untyped } -> void
|
|
148
|
+
def run: () { (untyped sut, untyped args) -> untyped } -> void
|
|
149
|
+
def verify: () { (Context ctx) -> untyped } -> void
|
|
150
|
+
def build: () -> Command
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
class Invariant < Data
|
|
154
|
+
attr_reader name: Symbol | String | nil
|
|
155
|
+
attr_reader block: Proc
|
|
156
|
+
|
|
157
|
+
def initialize: (name: Symbol | String | nil, block: Proc) -> void
|
|
158
|
+
def with: (?name: Symbol | String | nil, ?block: Proc) -> Invariant
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
class Spec
|
|
162
|
+
attr_reader commands: Array[Command]
|
|
163
|
+
attr_reader invariants: Array[Invariant]
|
|
164
|
+
|
|
165
|
+
def initialize: (
|
|
166
|
+
initial_state_kind: :constant | :block | :arbitrary,
|
|
167
|
+
initial_state_source: untyped,
|
|
168
|
+
lifecycle: Proc,
|
|
169
|
+
commands: Array[Command],
|
|
170
|
+
invariants: Array[Invariant]
|
|
171
|
+
) -> void
|
|
172
|
+
|
|
173
|
+
def arbitrary_initial_state?: () -> bool
|
|
174
|
+
def sample_initial_state: (Random rng) -> [untyped, Arbitrary::Sample[untyped]?]
|
|
175
|
+
def command_by_name: (Symbol name) -> Command
|
|
176
|
+
def with_sut: (untyped initial_state) { (untyped sut) -> untyped } -> void
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
class SpecBuilder
|
|
180
|
+
def initialize: () -> void
|
|
181
|
+
def initial_state: (untyped value_or_arb) -> void
|
|
182
|
+
| () { () -> untyped } -> void
|
|
183
|
+
def sut: () { (untyped initial_state) -> untyped } -> void
|
|
184
|
+
def teardown: () { (untyped sut) -> untyped } -> void
|
|
185
|
+
def around: () { (untyped initial_state, ^(untyped) -> void run) -> untyped } -> void
|
|
186
|
+
def command: (Symbol name, ?weight: Integer) { (CommandBuilder) -> untyped } -> void
|
|
187
|
+
def invariant: (?Symbol | String | nil name) { (untyped state, untyped sut) -> untyped } -> void
|
|
188
|
+
def build: () -> Spec
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
class ShrinkSession
|
|
192
|
+
attr_reader attempts: Integer
|
|
193
|
+
attr_reader max_attempts: Integer
|
|
194
|
+
|
|
195
|
+
def initialize: (max_attempts: Integer) -> void
|
|
196
|
+
def exhausted?: () -> bool
|
|
197
|
+
def seen?: (Run run) -> bool
|
|
198
|
+
def mark_seen: (Run run) -> void
|
|
199
|
+
def consume_attempt!: () -> Integer
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
class RunSample < Arbitrary::Sample[Run]
|
|
203
|
+
attr_reader initial_state_sample: Arbitrary::Sample[untyped]?
|
|
204
|
+
attr_reader step_samples: Hash[Integer, Arbitrary::Sample[untyped]]
|
|
205
|
+
attr_reader session: ShrinkSession
|
|
206
|
+
|
|
207
|
+
def initialize: (
|
|
208
|
+
Run run,
|
|
209
|
+
initial_state_sample: Arbitrary::Sample[untyped]?,
|
|
210
|
+
step_samples: Hash[Integer, Arbitrary::Sample[untyped]],
|
|
211
|
+
session: ShrinkSession
|
|
212
|
+
) -> void
|
|
213
|
+
|
|
214
|
+
def run: () -> Run
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
class Shrinker
|
|
218
|
+
def initialize: (Spec spec) -> void
|
|
219
|
+
def shrink: (RunSample run_sample, failure: Exception?) -> Enumerator[RunSample, untyped]
|
|
220
|
+
def valid_run?: (Run run) -> bool
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
class Property
|
|
224
|
+
attr_reader spec: Spec
|
|
225
|
+
attr_reader max_steps: Integer
|
|
226
|
+
attr_reader min_steps: Integer
|
|
227
|
+
attr_reader max_shrink_attempts: Integer
|
|
228
|
+
|
|
229
|
+
def initialize: (spec: Spec, ?max_steps: Integer, ?min_steps: Integer, ?max_shrink_attempts: Integer) -> void
|
|
230
|
+
def stateful?: () -> true
|
|
231
|
+
def generate: (?Random rng) -> Run
|
|
232
|
+
def generate_for_check: (?Random rng) -> RunSample
|
|
233
|
+
def run: (Run run) -> void
|
|
234
|
+
def replay: (Run run) -> void
|
|
235
|
+
def same_failure?: (Exception initial_err, Exception candidate_err) -> bool
|
|
236
|
+
def shrink_case: (Check::Case test_case) -> Enumerator[RunSample, untyped]
|
|
237
|
+
def shrink: (Run run) -> Enumerator[Run, untyped]
|
|
238
|
+
def run_in_ractor: (untyped run) -> bot
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
end
|
data/sig/pbt.rbs
CHANGED
|
@@ -1,4 +1,43 @@
|
|
|
1
1
|
module Pbt
|
|
2
2
|
VERSION: String
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
class PropertyFailure < StandardError
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
class TooManySkipsError < PropertyFailure
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
class PreconditionNotMet < StandardError
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class InvalidConfiguration < StandardError
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
extend Arbitrary::ArbitraryMethods
|
|
17
|
+
extend Check::RunnerMethods
|
|
18
|
+
extend Check::ConfigurationMethods
|
|
19
|
+
|
|
20
|
+
def self.assume: (untyped condition) -> void
|
|
21
|
+
|
|
22
|
+
def self.property: [T] (Arbitrary::_Arbitrary[T] arb) { (T) -> untyped } -> Check::Property
|
|
23
|
+
| [T1, T2] (Arbitrary::_Arbitrary[T1] arb1, Arbitrary::_Arbitrary[T2] arb2) { (T1, T2) -> untyped } -> Check::Property
|
|
24
|
+
| [T1, T2, T3] (Arbitrary::_Arbitrary[T1] arb1, Arbitrary::_Arbitrary[T2] arb2, Arbitrary::_Arbitrary[T3] arb3) { (T1, T2, T3) -> untyped } -> Check::Property
|
|
25
|
+
| (*Arbitrary::_Arbitrary[untyped] args) { (*untyped) -> untyped } -> Check::Property
|
|
26
|
+
| (**Arbitrary::_Arbitrary[untyped] kwargs) { (**untyped) -> untyped } -> Check::Property
|
|
27
|
+
|
|
28
|
+
def self.stateful: (?max_steps: Integer, ?min_steps: Integer, ?max_shrink_attempts: Integer) { (Stateful::SpecBuilder) -> untyped } -> Stateful::Property
|
|
29
|
+
|
|
30
|
+
def self.statistics: [T] (Arbitrary::_Generatable[T] arbitrary, ?num_runs: Integer, ?seed: Integer) -> Statistics[T]
|
|
31
|
+
| [T, L] (Arbitrary::_Generatable[T] arbitrary, ?num_runs: Integer, ?seed: Integer) { (T) -> L } -> Statistics[L]
|
|
32
|
+
|
|
33
|
+
class Statistics[out L = untyped]
|
|
34
|
+
attr_reader seed: Integer
|
|
35
|
+
attr_reader num_runs: Integer
|
|
36
|
+
attr_reader counts: Hash[L, Integer]
|
|
37
|
+
|
|
38
|
+
def initialize: (Arbitrary::_Generatable[L] arbitrary, num_runs: Integer, seed: Integer) -> void
|
|
39
|
+
| [T] (Arbitrary::_Generatable[T] arbitrary, num_runs: Integer, seed: Integer) { (T) -> L } -> void
|
|
40
|
+
|
|
41
|
+
def percentages: () -> Hash[L, Float]
|
|
42
|
+
end
|
|
4
43
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: pbt
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.9.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- ohbarye
|
|
@@ -15,28 +15,10 @@ executables: []
|
|
|
15
15
|
extensions: []
|
|
16
16
|
extra_rdoc_files: []
|
|
17
17
|
files:
|
|
18
|
-
- ".rspec"
|
|
19
|
-
- ".standard.yml"
|
|
20
18
|
- CHANGELOG.md
|
|
21
|
-
- CLAUDE.md
|
|
22
19
|
- CODE_OF_CONDUCT.md
|
|
23
20
|
- LICENSE.txt
|
|
24
21
|
- README.md
|
|
25
|
-
- Rakefile
|
|
26
|
-
- benchmark/FIBER.md
|
|
27
|
-
- benchmark/README.md
|
|
28
|
-
- benchmark/example.txt
|
|
29
|
-
- benchmark/failure_simple.rb
|
|
30
|
-
- benchmark/fiber_experiment.rb
|
|
31
|
-
- benchmark/fiber_scheduler/Gemfile
|
|
32
|
-
- benchmark/fiber_scheduler/experiment.rb
|
|
33
|
-
- benchmark/fiber_scheduler/spec/experiment_spec.rb
|
|
34
|
-
- benchmark/success_cpu_bound.rb
|
|
35
|
-
- benchmark/success_io_bound.rb
|
|
36
|
-
- benchmark/success_simple.rb
|
|
37
|
-
- docs/plans/fiber-experiment.md
|
|
38
|
-
- docs/plans/generator-statistics.md
|
|
39
|
-
- docs/plans/stateful-pbt-redesign.md
|
|
40
22
|
- lib/pbt.rb
|
|
41
23
|
- lib/pbt/arbitrary/arbitrary.rb
|
|
42
24
|
- lib/pbt/arbitrary/arbitrary_methods.rb
|
|
@@ -63,10 +45,20 @@ files:
|
|
|
63
45
|
- lib/pbt/reporter/run_details.rb
|
|
64
46
|
- lib/pbt/reporter/run_details_reporter.rb
|
|
65
47
|
- lib/pbt/reporter/run_execution.rb
|
|
48
|
+
- lib/pbt/stateful/command.rb
|
|
49
|
+
- lib/pbt/stateful/errors.rb
|
|
66
50
|
- lib/pbt/stateful/property.rb
|
|
51
|
+
- lib/pbt/stateful/shrinker.rb
|
|
52
|
+
- lib/pbt/stateful/spec.rb
|
|
53
|
+
- lib/pbt/stateful/types.rb
|
|
67
54
|
- lib/pbt/statistics.rb
|
|
68
55
|
- lib/pbt/version.rb
|
|
56
|
+
- sig/manifest.yaml
|
|
69
57
|
- sig/pbt.rbs
|
|
58
|
+
- sig/pbt/arbitrary.rbs
|
|
59
|
+
- sig/pbt/check.rbs
|
|
60
|
+
- sig/pbt/reporter.rbs
|
|
61
|
+
- sig/pbt/stateful.rbs
|
|
70
62
|
homepage: https://github.com/ohbarye/pbt
|
|
71
63
|
licenses:
|
|
72
64
|
- MIT
|
data/.rspec
DELETED
data/.standard.yml
DELETED
data/CLAUDE.md
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
# CLAUDE.md
|
|
2
|
-
|
|
3
|
-
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
-
|
|
5
|
-
## Project Overview
|
|
6
|
-
|
|
7
|
-
PBT (Property-Based Testing) is a Ruby gem that provides property-based testing capabilities with experimental features for parallel test execution using Ractor. The gem allows developers to specify properties that code should satisfy and automatically generates test cases to verify these properties.
|
|
8
|
-
|
|
9
|
-
## Development Commands
|
|
10
|
-
|
|
11
|
-
### Setup
|
|
12
|
-
```bash
|
|
13
|
-
bundle install
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
### Testing
|
|
17
|
-
```bash
|
|
18
|
-
# Run all tests
|
|
19
|
-
bundle exec rspec
|
|
20
|
-
|
|
21
|
-
# Run a specific test file
|
|
22
|
-
bundle exec rspec spec/path/to/spec.rb
|
|
23
|
-
|
|
24
|
-
# Run tests matching a pattern
|
|
25
|
-
bundle exec rspec -e "pattern"
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
### Linting
|
|
29
|
-
```bash
|
|
30
|
-
# Check code style
|
|
31
|
-
bundle exec rake standard
|
|
32
|
-
|
|
33
|
-
# Auto-fix code style issues
|
|
34
|
-
bundle exec rake standard:fix
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
### Combined Testing and Linting
|
|
38
|
-
```bash
|
|
39
|
-
# Default rake task runs both tests and linting
|
|
40
|
-
bundle exec rake
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
### Benchmarking
|
|
44
|
-
```bash
|
|
45
|
-
# Run all benchmarks
|
|
46
|
-
bundle exec rake benchmark:all
|
|
47
|
-
|
|
48
|
-
# Run specific benchmark categories
|
|
49
|
-
bundle exec rake benchmark:success:simple
|
|
50
|
-
bundle exec rake benchmark:success:cpu_bound
|
|
51
|
-
bundle exec rake benchmark:success:io_bound
|
|
52
|
-
bundle exec rake benchmark:failure:simple
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
### Building and Releasing
|
|
56
|
-
```bash
|
|
57
|
-
# Build the gem
|
|
58
|
-
bundle exec rake build
|
|
59
|
-
|
|
60
|
-
# Install locally
|
|
61
|
-
bundle exec rake install
|
|
62
|
-
|
|
63
|
-
# Release to RubyGems (maintainers only)
|
|
64
|
-
bundle exec rake release
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
## Architecture
|
|
68
|
-
|
|
69
|
-
### Core Components
|
|
70
|
-
|
|
71
|
-
1. **Arbitrary** (`lib/pbt/arbitrary/`)
|
|
72
|
-
- Base classes and modules for generating random values
|
|
73
|
-
- Implements various arbitraries: integer, array, tuple, hash, etc.
|
|
74
|
-
- Each arbitrary knows how to generate values and shrink them
|
|
75
|
-
|
|
76
|
-
2. **Check** (`lib/pbt/check/`)
|
|
77
|
-
- `Property`: Defines properties to test
|
|
78
|
-
- `Runner`: Executes properties with different concurrency methods
|
|
79
|
-
- `Configuration`: Global and per-run configuration
|
|
80
|
-
- `Tosser`: Manages the generation and shrinking process
|
|
81
|
-
|
|
82
|
-
3. **Reporter** (`lib/pbt/reporter/`)
|
|
83
|
-
- Handles test result reporting
|
|
84
|
-
- Provides verbose mode for detailed output
|
|
85
|
-
|
|
86
|
-
### Key Design Patterns
|
|
87
|
-
|
|
88
|
-
- **Shrinking**: When a test fails, PBT attempts to find the minimal failing case by systematically reducing the input
|
|
89
|
-
- **Concurrency**: Supports both serial (`:none`) and parallel (`:ractor`) execution
|
|
90
|
-
- **Configuration**: Uses both global configuration and per-assertion overrides
|
|
91
|
-
|
|
92
|
-
### Testing Approach
|
|
93
|
-
|
|
94
|
-
The codebase uses RSpec for testing and follows these patterns:
|
|
95
|
-
- Unit tests for each arbitrary type in `spec/pbt/arbitrary/`
|
|
96
|
-
- Integration tests in `spec/e2e/`
|
|
97
|
-
- Property-based tests are used to test the library itself
|
|
98
|
-
|
|
99
|
-
### Important Notes
|
|
100
|
-
|
|
101
|
-
- Ruby 3.3+ is required
|
|
102
|
-
- When using `:ractor` worker, be aware of Ractor limitations (no shared state, limited object sharing)
|
|
103
|
-
- The gem uses Standard Ruby for code style (configured in `.standard.yml`)
|
|
104
|
-
- CI runs tests against Ruby 3.3, 3.4, 4.0, and 4.1.0-preview1
|
data/Rakefile
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "bundler/gem_tasks"
|
|
4
|
-
require "rspec/core/rake_task"
|
|
5
|
-
|
|
6
|
-
RSpec::Core::RakeTask.new(:spec)
|
|
7
|
-
|
|
8
|
-
require "standard/rake"
|
|
9
|
-
|
|
10
|
-
task default: %i[spec standard]
|
|
11
|
-
|
|
12
|
-
namespace :benchmark do
|
|
13
|
-
task all: ["success:simple", "success:cpu_bound", "success:io_bound", "failure:simple"]
|
|
14
|
-
|
|
15
|
-
namespace :success do
|
|
16
|
-
task :simple do
|
|
17
|
-
puts "### Benchmark success:simple"
|
|
18
|
-
puts
|
|
19
|
-
puts "This runs a script that does not do any IO or CPU bound work."
|
|
20
|
-
puts
|
|
21
|
-
ENV["RUBYOPT"] = "-W:no-experimental"
|
|
22
|
-
ENV["RUBY_MN_THREADS"] = "1"
|
|
23
|
-
puts "```"
|
|
24
|
-
sh "ruby", "benchmark/success_simple.rb"
|
|
25
|
-
puts "```"
|
|
26
|
-
puts
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
task :cpu_bound do
|
|
30
|
-
puts "### Benchmark success:cpu_bound"
|
|
31
|
-
puts
|
|
32
|
-
puts "This runs a script that does CPU bound work."
|
|
33
|
-
puts
|
|
34
|
-
ENV["RUBYOPT"] = "-W:no-experimental"
|
|
35
|
-
ENV["RUBY_MN_THREADS"] = "1"
|
|
36
|
-
puts "```"
|
|
37
|
-
sh "ruby", "benchmark/success_cpu_bound.rb"
|
|
38
|
-
puts "```"
|
|
39
|
-
puts
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
task :io_bound do
|
|
43
|
-
puts "### Benchmark success:io_bound"
|
|
44
|
-
puts
|
|
45
|
-
puts "This runs a script that does IO bound work."
|
|
46
|
-
puts
|
|
47
|
-
ENV["RUBYOPT"] = "-W:no-experimental"
|
|
48
|
-
ENV["RUBY_MN_THREADS"] = "1"
|
|
49
|
-
puts "```"
|
|
50
|
-
sh "ruby", "benchmark/success_io_bound.rb"
|
|
51
|
-
puts "```"
|
|
52
|
-
puts
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
namespace :failure do
|
|
57
|
-
task :simple do
|
|
58
|
-
puts "### Benchmark failure:simple"
|
|
59
|
-
puts
|
|
60
|
-
puts "This runs a script that fails and shrink happens."
|
|
61
|
-
puts
|
|
62
|
-
ENV["RUBYOPT"] = "-W:no-experimental"
|
|
63
|
-
ENV["RUBY_MN_THREADS"] = "1"
|
|
64
|
-
puts "```"
|
|
65
|
-
sh "ruby", "benchmark/failure_simple.rb"
|
|
66
|
-
puts "```"
|
|
67
|
-
puts
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
end
|
data/benchmark/FIBER.md
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
# Fiber predicate experiment
|
|
2
|
-
|
|
3
|
-
Run from the repository root:
|
|
4
|
-
|
|
5
|
-
```sh
|
|
6
|
-
bundle exec ruby benchmark/fiber_experiment.rb
|
|
7
|
-
NUM_RUNS=1000 bundle exec ruby benchmark/fiber_experiment.rb
|
|
8
|
-
```
|
|
9
|
-
|
|
10
|
-
This compares the normal sequential runner with the same runner wrapping each
|
|
11
|
-
predicate call in a newly created Fiber. Both use seed 42, the same number of
|
|
12
|
-
cases, and the same CPU / blocking-sleep predicates. The script checks successful
|
|
13
|
-
case counts and measures wall time with a monotonic clock. It installs no scheduler.
|
|
14
|
-
|
|
15
|
-
## Decision for v1
|
|
16
|
-
|
|
17
|
-
Keep `worker: :none` as the default and `:ractor` as the existing opt-in worker.
|
|
18
|
-
The bare-Fiber experiment is complete; it does not justify a new public worker.
|
|
19
|
-
A Fiber per case adds allocation/context-switching work, while blocking sleep
|
|
20
|
-
still blocks its thread. This is a baseline experiment, not a benchmark of a
|
|
21
|
-
scheduler-backed asynchronous worker or evidence against all Fiber designs.
|
|
22
|
-
|
|
23
|
-
A local smoke run on Ruby 4.0.9 (arm64 macOS), 100 cases, seed 42 produced:
|
|
24
|
-
|
|
25
|
-
```text
|
|
26
|
-
cpu sequential 0.001758 seconds
|
|
27
|
-
cpu fiber 0.002105 seconds
|
|
28
|
-
blocking_sleep sequential 0.126800 seconds
|
|
29
|
-
blocking_sleep fiber 0.128960 seconds
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
These short timings are illustrative and noisy, not performance guarantees.
|
|
33
|
-
Re-run on the target Ruby/platform with representative workloads before drawing
|
|
34
|
-
performance conclusions. CPU parallelism is not provided by these same-thread Fibers.
|
|
35
|
-
|
|
36
|
-
## Scheduler-backed TCP IO experiment
|
|
37
|
-
|
|
38
|
-
The separate experiment in `benchmark/fiber_scheduler/experiment.rb` uses Async's
|
|
39
|
-
scheduler. Async is an optional benchmark dependency, not a dependency of pbt:
|
|
40
|
-
|
|
41
|
-
```sh
|
|
42
|
-
BUNDLE_GEMFILE=benchmark/fiber_scheduler/Gemfile bundle install
|
|
43
|
-
BUNDLE_GEMFILE=benchmark/fiber_scheduler/Gemfile bundle exec rspec benchmark/fiber_scheduler/spec
|
|
44
|
-
BUNDLE_GEMFILE=benchmark/fiber_scheduler/Gemfile bundle exec ruby benchmark/fiber_scheduler/experiment.rb
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
Parameters are `NUM_RUNS` (100), `CONCURRENCY` (10), `IO_DELAY` (0.01 seconds),
|
|
48
|
-
and `REPEATS` (5; use an odd number). Each mode gets a separate warmup. The timer
|
|
49
|
-
includes client socket creation, connection, reads, property calls and scheduler
|
|
50
|
-
startup/cleanup, but excludes gem loading, input generation and server startup.
|
|
51
|
-
|
|
52
|
-
A dedicated server thread accepts loopback TCP connections and independently delays
|
|
53
|
-
each `ok` response by 10ms. Clients perform a real blocking `Socket#read` through
|
|
54
|
-
`TCPSocket`, which Async makes cooperative. The three modes use the same seed-42
|
|
55
|
-
integer inputs and identical protocol: serial requests, serial requests wrapped in
|
|
56
|
-
bare Fibers, and batches of at most 10 concurrent Async tasks. Batches wait for all
|
|
57
|
-
members before starting the next batch; this is not a continuously filled pool.
|
|
58
|
-
The server records its peak number of pending responses to verify that IO waits
|
|
59
|
-
actually overlap; timing thresholds are deliberately absent from tests.
|
|
60
|
-
|
|
61
|
-
Local measurement: Ruby 4.0.9, Async 2.46.0, arm64 macOS, 100 cases, seed 42,
|
|
62
|
-
10ms per response, maximum concurrency 10, 5 repetitions after warmup:
|
|
63
|
-
|
|
64
|
-
```text
|
|
65
|
-
mode elapsed seconds (all 5 samples) median seconds
|
|
66
|
-
sequential 1.232761 1.246074 1.233465 1.250332 1.226240 1.233465
|
|
67
|
-
bare Fiber 1.232813 1.231938 1.224650 1.241142 1.213324 1.231938
|
|
68
|
-
scheduler 0.124735 0.129789 0.130015 0.130826 0.128622 0.129789
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
Every sample completed 100 requests and produced the same input checksum, 47379.
|
|
72
|
-
Server peak pending responses were 1 for serial/bare Fiber and 10 for Scheduler
|
|
73
|
-
in every repetition. The Scheduler median was about 9.5 times faster than serial
|
|
74
|
-
for this intentionally wait-dominated workload. This demonstrates overlapping IO
|
|
75
|
-
waits, rather than CPU parallelism. It is not a general throughput guarantee;
|
|
76
|
-
loopback, synthetic delay, small payloads, batch size and machine load all matter.
|
|
77
|
-
|
|
78
|
-
The optional tests cover identical inputs and counts, bounded pending requests,
|
|
79
|
-
exception propagation, server-thread cleanup and refusing to replace an existing
|
|
80
|
-
caller-owned scheduler. Client and server sockets are closed in ensure blocks;
|
|
81
|
-
remaining batch tasks are stopped after failure. Async may log the expected failure
|
|
82
|
-
in the cleanup test. The benchmark owns its scheduler only when none is installed.
|
|
83
|
-
|
|
84
|
-
### Decision after both experiments
|
|
85
|
-
|
|
86
|
-
Keep the existing public workers for v1. The scheduled experiment calls generated
|
|
87
|
-
properties directly; it does not integrate with `Pbt.check`, its early-failure
|
|
88
|
-
ordering, cancellation policy, shrinking, reports or Ractor worker. Concurrent failure/shrink/report parity is outside this experiment.
|
|
89
|
-
|
|
90
|
-
Scheduler-backed execution can help IO-heavy properties, but adoption needs a
|
|
91
|
-
separate design for in-flight case cleanup, failure ordering, exception propagation,
|
|
92
|
-
shared fixture state, concurrency limits and scheduler ownership. A caller-installed
|
|
93
|
-
scheduler must not be replaced or closed by pbt. No public API or runtime dependency
|
|
94
|
-
is added by this experiment.
|
|
95
|
-
|
|
96
|
-
References: [Ruby Fiber scheduler interface](https://docs.ruby-lang.org/en/master/Fiber/Scheduler.html),
|
|
97
|
-
[Async](https://github.com/socketry/async).
|