Class: RSpec::Benchmark::IterationMatcher::Matcher Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/benchmark/iteration_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_at_least` matcher

Instance Method Summary collapse

Constructor Details

#initialize(iterations, **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
# File 'lib/rspec/benchmark/iteration_matcher.rb', line 12

def initialize(iterations, **options)
  @iterations = iterations
  @time       = options.fetch(:time) { 0.2 }
  @warmup     = options.fetch(:warmup) { 0.1 }
  @format     = options.fetch(:format) {
                  RSpec::Benchmark.configuration.format }
  @bench      = ::Benchmark::Perf
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.



88
89
90
91
# File 'lib/rspec/benchmark/iteration_matcher.rb', line 88

def actual
  avg = human_format? ? Formatter.format_unit(@average) : @average
  "%s (± %d%%) i/s" % [avg, (@stddev / @average.to_f) * 100]
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.



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

def description
  iters = human_format? ? Formatter.format_unit(@iterations) : @iterations
  "perform at least #{iters} i/s"
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.



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

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.



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

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

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

Check if human format should be used

Returns:

  • (Boolean)


26
27
28
# File 'lib/rspec/benchmark/iteration_matcher.rb', line 26

def human_format?
  @format == :human
end

#ipsObject

Sugar syntax for iterations per second



71
72
73
# File 'lib/rspec/benchmark/iteration_matcher.rb', line 71

def ips
  self
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)


42
43
44
45
# File 'lib/rspec/benchmark/iteration_matcher.rb', line 42

def matches?(block)
  @average, @stddev, = @bench.ips(time: @time, warmup: @warmup, &block)
  @iterations <= (@average + 3 * @stddev)
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.



97
98
99
# File 'lib/rspec/benchmark/iteration_matcher.rb', line 97

def negative_failure_reason
  "performed #{actual}"
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.



93
94
95
# File 'lib/rspec/benchmark/iteration_matcher.rb', line 93

def positive_failure_reason
  "performed only #{actual}"
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)


35
36
37
# File 'lib/rspec/benchmark/iteration_matcher.rb', line 35

def supports_block_expectations?
  true
end

#warmup(value) ⇒ Object

The time before measurements are taken

Parameters:

  • value (Numeric)

    the time before measurements are taken



53
54
55
56
# File 'lib/rspec/benchmark/iteration_matcher.rb', line 53

def warmup(value)
  @warmup = value
  self
end

#within(value) ⇒ Object

Time to measure iteration for

Parameters:

  • value (Numeric)

    the time to take measurements for



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

def within(value)
  @time = value
  self
end