Class: RSpec::Benchmark::ComparisonMatcher::Matcher Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/benchmark/comparison_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_faster_than` and `perform_slower_than` matchers

Instance Method Summary collapse

Constructor Details

#initialize(expected, comparison_type, **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.



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

def initialize(expected, comparison_type, **options)
  check_comparison(comparison_type)
  @expected = expected
  @comparison_type = comparison_type
  @count      = 1
  @count_type = :at_least
  @time       = options.fetch(:time) { 0.2 }
  @warmup     = options.fetch(:warmup) { 0.1 }
  @bench      = ::Benchmark::Perf::Iteration
end

Instance Method Details

#at_least(n) ⇒ Object

Specify the minimum number of times a block is faster/slower than other.



83
84
85
86
# File 'lib/rspec/benchmark/comparison_matcher.rb', line 83

def at_least(n)
  set_expected_times_count(:at_least, n)
  self
end

#at_most(n) ⇒ Object

Specify the maximum number of times a block is faster/slower than another.



91
92
93
94
# File 'lib/rspec/benchmark/comparison_matcher.rb', line 91

def at_most(n)
  set_expected_times_count(:at_most, n)
  self
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.



141
142
143
144
145
146
147
148
# File 'lib/rspec/benchmark/comparison_matcher.rb', line 141

def description
  if @count == 1
    "perform #{@comparison_type} than comparison block"
  else
    "perform #{@comparison_type} than comparison block " \
    "by #{@count_type} #{@count} times"
  end
end

#exactly(n) ⇒ Object

Specify exact number of times a block is faster/slower than another.



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

def exactly(n)
  set_expected_times_count(:exactly, n)
  self
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.



131
132
133
# File 'lib/rspec/benchmark/comparison_matcher.rb', line 131

def failure_message
  "expected given block to #{description}, but #{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.



136
137
138
# File 'lib/rspec/benchmark/comparison_matcher.rb', line 136

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

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



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rspec/benchmark/comparison_matcher.rb', line 151

def failure_reason
  return "was not a block" unless @actual.is_a?(Proc)

  if @ratio < 1
    "performed slower by #{format("%.2f", (@ratio**-1))} times"
  elsif @ratio > 1
    "performed faster by #{format("%.2f", @ratio)} times"
  else
    "performed by the same time"
  end
end

#matcher_nameObject

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.



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

def matcher_name
  "perform_#{@comparison_type}_than"
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
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rspec/benchmark/comparison_matcher.rb', line 39

def matches?(block)
  @actual = block
  return false unless @actual.is_a?(Proc)

  @expected_ips, @expected_stdev, = @bench.run(time: @time, warmup: @warmup, &@expected)
  @actual_ips, @actual_stdev, = @bench.run(time: @time, warmup: @warmup, &@actual)

  @ratio = @actual_ips / @expected_ips.to_f

  case @count_type
  when :at_most
    at_most_comparison
  when :exactly
    exact_comparison
  else
    default_comparison
  end
end

#onceObject

Specify that the code runs faster/slower exactly once.



106
107
108
109
# File 'lib/rspec/benchmark/comparison_matcher.rb', line 106

def once
  exactly(1)
  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/comparison_matcher.rb', line 28

def supports_block_expectations?
  true
end

#thriceObject

Specify that the code runs faster/slower exactly thrice.



119
120
121
122
# File 'lib/rspec/benchmark/comparison_matcher.rb', line 119

def thrice
  exactly(3)
  self
end

#timesObject

No-op, syntactic sugar.



126
127
128
# File 'lib/rspec/benchmark/comparison_matcher.rb', line 126

def times
  self
end

#twiceObject

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.

Specify that the code runs faster/slower exactly twice.



112
113
114
115
# File 'lib/rspec/benchmark/comparison_matcher.rb', line 112

def twice
  exactly(2)
  self
end

#warmup(value) ⇒ Object

The time before measurements are taken

Parameters:

  • value (Numeric)

    the time before measurements are taken



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

def warmup(value)
  @warmup = value
  self
end

#within(value) ⇒ Object

Time to measure iteration for

Parameters:

  • value (Numeric)

    the time to take measurements for



75
76
77
78
# File 'lib/rspec/benchmark/comparison_matcher.rb', line 75

def within(value)
  @time = value
  self
end