Class: RSpec::Benchmark::ComplexityMatcher::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/benchmark/complexity_matcher.rb

Overview

Implements the ‘perform`

Instance Method Summary collapse

Constructor Details

#initialize(fit_type, **options) ⇒ Matcher

Returns a new instance of Matcher.



12
13
14
15
16
17
18
19
20
21
# File 'lib/rspec/benchmark/complexity_matcher.rb', line 12

def initialize(fit_type, **options)
  @fit_type  = fit_type
  @threshold = options.fetch(:threshold) {
                RSpec::Benchmark.configuration.fit_quality }
  @repeat    = options.fetch(:repeat) {
                RSpec::Benchmark.configuration.samples }
  @start     = 8
  @limit     = 8 << 10
  @ratio     = 8
end

Instance Method Details

#actualObject



79
80
81
# File 'lib/rspec/benchmark/complexity_matcher.rb', line 79

def actual
  @trend
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.



90
91
92
# File 'lib/rspec/benchmark/complexity_matcher.rb', line 90

def description
  "perform #{@fit_type}"
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.



95
96
97
# File 'lib/rspec/benchmark/complexity_matcher.rb', line 95

def failure_message
  "expected block to #{description}, but #{failure_reason}"
end

#failure_message_when_negatedObject



99
100
101
# File 'lib/rspec/benchmark/complexity_matcher.rb', line 99

def failure_message_when_negated
  "expected block not to #{description}, but #{failure_reason}"
end

#failure_reasonObject



103
104
105
# File 'lib/rspec/benchmark/complexity_matcher.rb', line 103

def failure_reason
  "performed #{actual}"
end

#in_range(start, limit = (not_set = true)) ⇒ Object

Specify range of inputs



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rspec/benchmark/complexity_matcher.rb', line 50

def in_range(start, limit = (not_set = true))
  case start
  when Array
    @start, *, @limit = *start
    @ratio = start[1] / start[0]
  when Numeric
    @start, @limit = start, limit
  else
    raise ArgumentError,
        "Wrong range argument '#{start}', it expects an array or numeric start value."
  end
  self
end

#matcher_nameObject



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

def matcher_name
  "perform_#{@fit_type}"
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
45
# File 'lib/rspec/benchmark/complexity_matcher.rb', line 39

def matches?(block)
  range = ::Benchmark::Trend.range(@start, @limit, ratio: @ratio)
  @trend, trends = ::Benchmark::Trend.infer_trend(range, repeat: @repeat, &block)
  threshold = trends[@trend][:residual]

  @trend == @fit_type && threshold >= @threshold
end

#ratio(ratio) ⇒ Object



69
70
71
72
# File 'lib/rspec/benchmark/complexity_matcher.rb', line 69

def ratio(ratio)
  @ratio = ratio
  self
end

#sample(repeat) ⇒ Object



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

def sample(repeat)
  @repeat = repeat
  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)


28
29
30
# File 'lib/rspec/benchmark/complexity_matcher.rb', line 28

def supports_block_expectations?
  true
end

#threshold(threshold) ⇒ Object



64
65
66
67
# File 'lib/rspec/benchmark/complexity_matcher.rb', line 64

def threshold(threshold)
  @threshold = threshold
  self
end

#timesObject

No-op, syntactic sugar.



85
86
87
# File 'lib/rspec/benchmark/complexity_matcher.rb', line 85

def times
  self
end