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
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 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 18 def initialize(threshold, **) @threshold = threshold @samples = .fetch(:samples) { 1 } @warmup = .fetch(:warmup) { 1 } @scale = threshold.to_s.split(/\./).last.size @block = nil @bench = ::Benchmark::Perf::ExecutionTime 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.
116 117 118 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 116 def actual "#{format_time(@average)} (± #{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.
112 113 114 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 112 def description "perform under #{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.
46 47 48 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 46 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.
104 105 106 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 104 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.
108 109 110 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 108 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.
39 40 41 42 43 44 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 39 def matches?(block) @block = block return false unless block.is_a?(Proc) @average, @stddev = @bench.run(repeat: @samples, warmup: @warmup, &block) @average <= @threshold end |
#ms ⇒ Object
Tell this matcher to convert threshold to ms
85 86 87 88 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 85 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.
125 126 127 128 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 125 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
99 100 101 102 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 99 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.
120 121 122 123 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 120 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
67 68 69 70 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 67 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.
78 79 80 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 78 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
32 33 34 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 32 def supports_block_expectations? true end |
#times ⇒ Object
No-op, syntactic sugar.
74 75 76 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 74 def times self end |
#us ⇒ Object
Tell this matcher to convert threshold to us
92 93 94 95 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 92 def us @threshold /= 1e6 self end |
#warmup(value) ⇒ Object
The time before measurements are taken
56 57 58 59 |
# File 'lib/rspec/benchmark/timing_matcher.rb', line 56 def warmup(value) @warmup = value self end |