pbt 0.8.0 → 1.0.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 (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +29 -0
  3. data/README.md +70 -109
  4. data/lib/pbt/arbitrary/arbitrary.rb +23 -0
  5. data/lib/pbt/arbitrary/arbitrary_methods.rb +46 -16
  6. data/lib/pbt/arbitrary/bind_arbitrary.rb +54 -0
  7. data/lib/pbt/arbitrary/choose_arbitrary.rb +82 -5
  8. data/lib/pbt/arbitrary/constant.rb +42 -12
  9. data/lib/pbt/arbitrary/constant_from_arbitrary.rb +3 -3
  10. data/lib/pbt/arbitrary/float_arbitrary.rb +239 -17
  11. data/lib/pbt/arbitrary/frequency_arbitrary.rb +1 -1
  12. data/lib/pbt/arbitrary/integer_arbitrary.rb +31 -14
  13. data/lib/pbt/arbitrary/map_arbitrary.rb +4 -1
  14. data/lib/pbt/arbitrary/sample.rb +35 -0
  15. data/lib/pbt/check/configuration.rb +1 -1
  16. data/lib/pbt/check/property.rb +5 -0
  17. data/lib/pbt/check/runner_iterator.rb +38 -3
  18. data/lib/pbt/check/runner_methods.rb +70 -13
  19. data/lib/pbt/reporter/run_details_reporter.rb +39 -6
  20. data/lib/pbt/stateful/command.rb +220 -0
  21. data/lib/pbt/stateful/errors.rb +58 -0
  22. data/lib/pbt/stateful/property.rb +276 -280
  23. data/lib/pbt/stateful/shrinker.rb +318 -0
  24. data/lib/pbt/stateful/spec.rb +299 -0
  25. data/lib/pbt/stateful/types.rb +308 -0
  26. data/lib/pbt/version.rb +1 -1
  27. data/lib/pbt.rb +34 -19
  28. data/sig/manifest.yaml +2 -0
  29. data/sig/pbt/arbitrary.rbs +150 -0
  30. data/sig/pbt/check.rbs +74 -0
  31. data/sig/pbt/reporter.rbs +44 -0
  32. data/sig/pbt/stateful.rbs +244 -0
  33. data/sig/pbt.rbs +40 -1
  34. metadata +18 -23
  35. data/.rspec +0 -3
  36. data/.standard.yml +0 -3
  37. data/CLAUDE.md +0 -104
  38. data/Rakefile +0 -70
  39. data/benchmark/FIBER.md +0 -97
  40. data/benchmark/README.md +0 -103
  41. data/benchmark/example.txt +0 -299
  42. data/benchmark/failure_simple.rb +0 -29
  43. data/benchmark/fiber_experiment.rb +0 -39
  44. data/benchmark/fiber_scheduler/Gemfile +0 -7
  45. data/benchmark/fiber_scheduler/experiment.rb +0 -131
  46. data/benchmark/fiber_scheduler/spec/experiment_spec.rb +0 -38
  47. data/benchmark/success_cpu_bound.rb +0 -35
  48. data/benchmark/success_io_bound.rb +0 -28
  49. data/benchmark/success_simple.rb +0 -24
  50. data/docs/plans/fiber-experiment.md +0 -12
  51. data/docs/plans/generator-statistics.md +0 -16
  52. data/docs/plans/stateful-pbt-redesign.md +0 -415
@@ -0,0 +1,244 @@
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
+ # Internal helper module (not part of the stable SemVer public API).
107
+ module Resolver
108
+ def self.arbitrary?: (untyped obj) -> bool
109
+ def self.deep_dup: (untyped obj, ?Hash[untyped, untyped] cache) -> untyped
110
+ def self.resolve: (untyped obj, Hash[Integer, untyped] env, ?detach: bool, ?cache: Hash[untyped, untyped]) -> untyped
111
+ def self.collect_ref_ids: (untyped obj, ?Set[Integer] ids, ?Hash[untyped, bool] visited) -> Set[Integer]
112
+ def self.referenced_step_ids: (Array[Step] steps) -> Set[Integer]
113
+ def self.format_value: (untyped obj, ?Hash[untyped, bool] visited) -> String
114
+ def self.deep_freeze: (untyped obj) -> untyped
115
+ def self.invoke_with_arity: (untyped callable, *untyped args) -> untyped
116
+ end
117
+
118
+ class Command
119
+ attr_reader name: Symbol
120
+ attr_reader weight: Integer
121
+
122
+ def initialize: (
123
+ name: Symbol,
124
+ weight: Integer,
125
+ guard_block: Proc?,
126
+ arguments_source: untyped,
127
+ precondition_block: Proc?,
128
+ next_state_block: Proc?,
129
+ run_block: Proc,
130
+ verify_block: Proc?
131
+ ) -> void
132
+
133
+ def guard?: (untyped state) -> bool
134
+ def state_dependent_arguments?: () -> bool
135
+ def arguments_for: (untyped state) -> Arbitrary::_Arbitrary[untyped]
136
+ def precondition?: (untyped state, untyped args) -> bool
137
+ def next_state: (untyped state, untyped args, Ref ref) -> untyped
138
+ def run!: (untyped sut, untyped concrete_args) -> untyped
139
+ def verify!: (Context context) -> void
140
+ end
141
+
142
+ class CommandBuilder
143
+ def initialize: (name: Symbol, weight: Integer) -> void
144
+ def guard: () { (untyped state) -> boolish } -> void
145
+ def arguments: (Arbitrary::_Arbitrary[untyped] arb) -> void
146
+ | (**Arbitrary::_Arbitrary[untyped] kwargs) -> void
147
+ | () { (untyped state) -> Arbitrary::_Arbitrary[untyped] } -> void
148
+ def precondition: () { (untyped state, untyped args) -> boolish } -> void
149
+ def next_state: () { (untyped state, untyped args, Ref ref) -> untyped } -> void
150
+ def run: () { (untyped sut, untyped args) -> untyped } -> void
151
+ def verify: () { (Context ctx) -> untyped } -> void
152
+ def build: () -> Command
153
+ end
154
+
155
+ class Invariant < Data
156
+ attr_reader name: Symbol | String | nil
157
+ attr_reader block: Proc
158
+
159
+ def initialize: (name: Symbol | String | nil, block: Proc) -> void
160
+ def with: (?name: Symbol | String | nil, ?block: Proc) -> Invariant
161
+ end
162
+
163
+ class Spec
164
+ attr_reader commands: Array[Command]
165
+ attr_reader invariants: Array[Invariant]
166
+
167
+ def initialize: (
168
+ initial_state_kind: :constant | :block | :arbitrary,
169
+ initial_state_source: untyped,
170
+ lifecycle: Proc,
171
+ commands: Array[Command],
172
+ invariants: Array[Invariant]
173
+ ) -> void
174
+
175
+ def arbitrary_initial_state?: () -> bool
176
+ def sample_initial_state: (Random rng) -> [untyped, Arbitrary::Sample[untyped]?]
177
+ def command_by_name: (Symbol name) -> Command
178
+ def with_sut: (untyped initial_state) { (untyped sut) -> untyped } -> void
179
+ end
180
+
181
+ class SpecBuilder
182
+ def initialize: () -> void
183
+ def initial_state: (untyped value_or_arb) -> void
184
+ | () { () -> untyped } -> void
185
+ def sut: () { (untyped initial_state) -> untyped } -> void
186
+ def teardown: () { (untyped sut) -> untyped } -> void
187
+ def around: () { (untyped initial_state, ^(untyped) -> void run) -> untyped } -> void
188
+ def command: (Symbol name, ?weight: Integer) { (CommandBuilder) -> untyped } -> void
189
+ def invariant: (?Symbol | String | nil name) { (untyped state, untyped sut) -> untyped } -> void
190
+ def build: () -> Spec
191
+ end
192
+
193
+ # Internal shrinking classes (not part of the stable SemVer public API).
194
+ class ShrinkSession
195
+ attr_reader attempts: Integer
196
+ attr_reader max_attempts: Integer
197
+
198
+ def initialize: (max_attempts: Integer) -> void
199
+ def exhausted?: () -> bool
200
+ def seen?: (Run run) -> bool
201
+ def mark_seen: (Run run) -> void
202
+ def consume_attempt!: () -> Integer
203
+ end
204
+
205
+ class RunSample < Arbitrary::Sample[Run]
206
+ attr_reader initial_state_sample: Arbitrary::Sample[untyped]?
207
+ attr_reader step_samples: Hash[Integer, Arbitrary::Sample[untyped]]
208
+ attr_reader session: ShrinkSession
209
+
210
+ def initialize: (
211
+ Run run,
212
+ initial_state_sample: Arbitrary::Sample[untyped]?,
213
+ step_samples: Hash[Integer, Arbitrary::Sample[untyped]],
214
+ session: ShrinkSession
215
+ ) -> void
216
+
217
+ def run: () -> Run
218
+ end
219
+
220
+ class Shrinker
221
+ def initialize: (Spec spec) -> void
222
+ def shrink: (RunSample run_sample, failure: Exception?) -> Enumerator[RunSample, untyped]
223
+ def valid_run?: (Run run, ?original_states: Hash[Integer, untyped]?) -> bool
224
+ end
225
+
226
+ class Property
227
+ attr_reader spec: Spec
228
+ attr_reader max_steps: Integer
229
+ attr_reader min_steps: Integer
230
+ attr_reader max_shrink_attempts: Integer
231
+
232
+ def initialize: (spec: Spec, ?max_steps: Integer, ?min_steps: Integer, ?max_shrink_attempts: Integer) -> void
233
+ def stateful?: () -> true
234
+ def generate: (?Random rng) -> Run
235
+ def generate_for_check: (?Random rng) -> RunSample
236
+ def run: (Run run) -> void
237
+ def replay: (Run run) -> void
238
+ def same_failure?: (Exception initial_err, Exception candidate_err) -> bool
239
+ def shrink_case: (Check::Case test_case) -> Enumerator[RunSample, untyped]
240
+ def shrink: (Run run) -> Enumerator[Run, untyped]
241
+ def run_in_ractor: (untyped run) -> bot
242
+ end
243
+ end
244
+ end
data/sig/pbt.rbs CHANGED
@@ -1,4 +1,43 @@
1
1
  module Pbt
2
2
  VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
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.8.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ohbarye
@@ -15,32 +15,15 @@ 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
43
25
  - lib/pbt/arbitrary/array_arbitrary.rb
26
+ - lib/pbt/arbitrary/bind_arbitrary.rb
44
27
  - lib/pbt/arbitrary/choose_arbitrary.rb
45
28
  - lib/pbt/arbitrary/constant.rb
46
29
  - lib/pbt/arbitrary/constant_arbitrary.rb
@@ -63,17 +46,29 @@ files:
63
46
  - lib/pbt/reporter/run_details.rb
64
47
  - lib/pbt/reporter/run_details_reporter.rb
65
48
  - lib/pbt/reporter/run_execution.rb
49
+ - lib/pbt/stateful/command.rb
50
+ - lib/pbt/stateful/errors.rb
66
51
  - lib/pbt/stateful/property.rb
52
+ - lib/pbt/stateful/shrinker.rb
53
+ - lib/pbt/stateful/spec.rb
54
+ - lib/pbt/stateful/types.rb
67
55
  - lib/pbt/statistics.rb
68
56
  - lib/pbt/version.rb
57
+ - sig/manifest.yaml
69
58
  - sig/pbt.rbs
59
+ - sig/pbt/arbitrary.rbs
60
+ - sig/pbt/check.rbs
61
+ - sig/pbt/reporter.rbs
62
+ - sig/pbt/stateful.rbs
70
63
  homepage: https://github.com/ohbarye/pbt
71
64
  licenses:
72
65
  - MIT
73
66
  metadata:
74
- homepage_uri: https://github.com/ohbarye/pbt
75
67
  source_code_uri: https://github.com/ohbarye/pbt
76
- changelog_uri: https://github.com/ohbarye/pbt/releases
68
+ changelog_uri: https://github.com/ohbarye/pbt/blob/main/CHANGELOG.md
69
+ bug_tracker_uri: https://github.com/ohbarye/pbt/issues
70
+ documentation_uri: https://www.rubydoc.info/gems/pbt
71
+ rubygems_mfa_required: 'true'
77
72
  rdoc_options: []
78
73
  require_paths:
79
74
  - lib
@@ -90,6 +85,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
85
  requirements: []
91
86
  rubygems_version: 4.0.20
92
87
  specification_version: 4
93
- summary: Property-Based Testing tool for Ruby, utilizing Ractor for parallelizing
94
- test cases.
88
+ summary: Property-Based Testing for Ruby with shrinking, stateful testing, and optional
89
+ Ractor parallelism.
95
90
  test_files: []
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/.standard.yml DELETED
@@ -1,3 +0,0 @@
1
- # For available configuration options, see:
2
- # https://github.com/standardrb/standard
3
- ruby_version: 3.3
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).