Class: RSpec::Benchmark::TimingMatcher::Matcher Private
- Inherits:
-
Object
- Object
- RSpec::Benchmark::TimingMatcher::Matcher
- Includes:
- RSpec::Benchmark
- Defined in:
- lib/rspec/benchmark/timing_matcher.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Implements the ‘perform_under` matcher
Constant Summary
Constants included from RSpec::Benchmark
Instance Attribute Summary collapse
- #threshold ⇒ Object readonly private
Instance Method Summary collapse
- #actual ⇒ Object private
- #description ⇒ Object private
- #does_not_match?(block) ⇒ Boolean private
- #failure_message ⇒ Object private
- #failure_message_when_negated ⇒ Object private
-
#initialize(threshold, **options) ⇒ Matcher
constructor
private
A new instance of Matcher.
- #matches?(block) ⇒ Boolean private
-
#ms ⇒ Object
Tell this matcher to convert threshold to ms.
- #negative_failure_reason ⇒ Object private
-
#ns ⇒ Object
Tell this matcher to convert threshold to ns.
- #positive_failure_reason ⇒ Object private
-
#sample(samples) ⇒ Object
How many times to repeat measurement.
- #secs ⇒ Object (also: #sec) private
-
#supports_block_expectations? ⇒ True
private
Indicates this matcher matches against a block.
-
#times ⇒ Object
No-op, syntactic sugar.
-
#us ⇒ Object
Tell this matcher to convert threshold to us.
-
#warmup(value) ⇒ Object
The time before measurements are taken.
Methods included from RSpec::Benchmark
configure, reset_configuration
Constructor Details
#initialize(threshold, **options) ⇒ Matcher
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Matcher.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 18 def initialize(threshold, **) @threshold = threshold @samples = .fetch(:samples) { RSpec::Benchmark.configuration.samples } @warmup = .fetch(:warmup) { 1 } @subprocess = .fetch(:subprocess) { RSpec::Benchmark.configuration.run_in_subprocess } @scale = threshold.to_s.split(/\./).last.size @block = nil @bench = ::Benchmark::Perf end |
Instance Attribute Details
#threshold ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 16 def threshold @threshold end |
Instance Method Details
#actual ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
122 123 124 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 122 def actual "#{Formatter.format_time(@average)} (± #{Formatter.format_time(@stddev)})" end |
#description ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
118 119 120 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 118 def description "perform under #{Formatter.format_time(@threshold)}" end |
#does_not_match?(block) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
52 53 54 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 52 def does_not_match?(block) !matches?(block) && block.is_a?(Proc) end |
#failure_message ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
110 111 112 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 110 def "expected block to #{description}, but #{positive_failure_reason}" end |
#failure_message_when_negated ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
114 115 116 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 114 def "expected block to not #{description}, but #{negative_failure_reason}" end |
#matches?(block) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
44 45 46 47 48 49 50 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 44 def matches?(block) @block = block return false unless block.is_a?(Proc) @average, @stddev = @bench.cpu(repeat: @samples, warmup: @warmup, subprocess: @subprocess, &block) @average <= @threshold end |
#ms ⇒ Object
Tell this matcher to convert threshold to ms
91 92 93 94 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 91 def ms @threshold /= 1e3 self end |
#negative_failure_reason ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
131 132 133 134 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 131 def negative_failure_reason return "was not a block" unless @block.is_a?(Proc) "performed #{actual} under" end |
#ns ⇒ Object
Tell this matcher to convert threshold to ns
105 106 107 108 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 105 def ns @threshold /= 1e9 self end |
#positive_failure_reason ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
126 127 128 129 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 126 def positive_failure_reason return "was not a block" unless @block.is_a?(Proc) "performed above #{actual} " end |
#sample(samples) ⇒ Object
How many times to repeat measurement
73 74 75 76 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 73 def sample(samples) @samples = samples self end |
#secs ⇒ Object Also known as: sec
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
84 85 86 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 84 def secs self end |
#supports_block_expectations? ⇒ True
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Indicates this matcher matches against a block
37 38 39 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 37 def supports_block_expectations? true end |
#times ⇒ Object
No-op, syntactic sugar.
80 81 82 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 80 def times self end |
#us ⇒ Object
Tell this matcher to convert threshold to us
98 99 100 101 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 98 def us @threshold /= 1e6 self end |
#warmup(value) ⇒ Object
The time before measurements are taken
62 63 64 65 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 62 def warmup(value) @warmup = value self end |