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

format_time

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



116
117
118
# File 'lib/rspec/benchmark/timing_matcher.rb', line 116

def actual
  "#{format_time(@average)}#{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.



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.

Returns:

  • (Boolean)


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_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.



104
105
106
# File 'lib/rspec/benchmark/timing_matcher.rb', line 104

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.



108
109
110
# File 'lib/rspec/benchmark/timing_matcher.rb', line 108

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)


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

#msObject

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_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.



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

#nsObject

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_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.



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

Parameters:

  • samples (Integer)

    the number of times to repeat the measurement



67
68
69
70
# File 'lib/rspec/benchmark/timing_matcher.rb', line 67

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.



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

Returns:

  • (True)


32
33
34
# File 'lib/rspec/benchmark/timing_matcher.rb', line 32

def supports_block_expectations?
  true
end

#timesObject

No-op, syntactic sugar.



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

def times
  self
end

#usObject

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

Parameters:

  • value (Numeric)

    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