Class: RSpec::Benchmark::TimingMatcher::Matcher Private

Inherits:
Object
  • Object
show all
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

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

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, **options)
  @threshold = threshold
  @samples   = options.fetch(:samples) {
                 RSpec::Benchmark.configuration.samples
               }
  @warmup    = options.fetch(:warmup) { 1 }
  @subprocess = options.fetch(:subprocess) {
                  RSpec::Benchmark.configuration.run_in_subprocess
                }
  @scale     = threshold.to_s.split(/\./).last.size
  @block     = nil
  @bench     = ::Benchmark::Perf
end

Instance Attribute Details

#thresholdObject (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

#actualObject

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

#descriptionObject

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.

Returns:

  • (Boolean)


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_messageObject

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 failure_message
  "expected block to #{description}, but #{positive_failure_reason}"
end

#failure_message_when_negatedObject

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 failure_message_when_negated
  "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.

Returns:

  • (Boolean)


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

#msObject

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_reasonObject

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

#nsObject

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_reasonObject

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

Parameters:

  • samples (Integer)

    the number of times to repeat the measurement



73
74
75
76
# File 'lib/rspec/benchmark/timing_matcher.rb', line 73

def sample(samples)
  @samples = samples
  self
end

#secsObject 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

Returns:

  • (True)


37
38
39
# File 'lib/rspec/benchmark/timing_matcher.rb', line 37

def supports_block_expectations?
  true
end

#timesObject

No-op, syntactic sugar.



80
81
82
# File 'lib/rspec/benchmark/timing_matcher.rb', line 80

def times
  self
end

#usObject

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

Parameters:

  • value (Numeric)

    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