Class: RSpec::Benchmark::AllocationMatcher::Matcher Private

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

Instance Method Summary collapse

Constructor Details

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

def initialize(objects, **options)
  @objects = objects
  @retained_objects = nil
  @warmup = options.fetch(:warmup) { 1 }
  @bench  = ::Benchmark::Malloc
  @count_type = :objects
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.



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rspec/benchmark/allocation_matcher.rb', line 127

def actual
  if @count_type == :memory
    "#{objects_to_s(@actual)} bytes"
  else
    desc = ["#{objects_to_s(@actual)} #{pluralize_objects(@actual)}"]
    if @retained_objects
      desc << " and retained #{objects_to_s(@actual_retained)}"
    end
    desc.join
  end
end

#and_retain(objects) ⇒ Object

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.



66
67
68
69
# File 'lib/rspec/benchmark/allocation_matcher.rb', line 66

def and_retain(objects)
  @retained_objects = objects
  self
end

#count_objects(objects) ⇒ Object

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
113
114
115
116
# File 'lib/rspec/benchmark/allocation_matcher.rb', line 110

def count_objects(objects)
  if @count_type == :memory
    "#{objects_to_s(objects)} #{objects == 1 ? "byte" : "bytes"}"
  else
    "#{objects_to_s(objects)} #{pluralize_objects(objects)}"
  end
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.



102
103
104
105
106
107
108
# File 'lib/rspec/benchmark/allocation_matcher.rb', line 102

def description
  desc = ["perform allocation of #{count_objects(@objects)}"]
  if @retained_objects
    desc << " and retain #{count_objects(@retained_objects)}"
  end
  desc.join
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.



94
95
96
# File 'lib/rspec/benchmark/allocation_matcher.rb', line 94

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.



98
99
100
# File 'lib/rspec/benchmark/allocation_matcher.rb', line 98

def failure_message_when_negated
  "expected block not to #{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)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rspec/benchmark/allocation_matcher.rb', line 32

def matches?(block)
  @block = block
  alloc_stats = @bench.trace(&block)
  @actual = nil
  @actual_retained = nil

  case @objects
  when Hash
    case @count_type
    when :memory
      @actual = alloc_stats.allocated.count_memory
    else
      @actual = alloc_stats.allocated.count_objects
    end
    @objects.all? do |name, count|
      @actual[name] <= count
    end
  when Numeric
    case @count_type
    when :memory
      @actual = alloc_stats.allocated.total_memory
      @actual_retained = alloc_stats.retained.total_memory
    else
      @actual = alloc_stats.allocated.total_objects
      @actual_retained = alloc_stats.retained.total_objects
    end
    result = @actual <= @objects
    result &= @actual_retained <= @retained_objects if @retained_objects
    result
  else
    raise ArgumentError, "'#{@objects}' is not a recognized argument"
  end
end

#memoryObject Also known as: bytes

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/allocation_matcher.rb', line 88

def memory
  @count_type = :memory
  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.



123
124
125
# File 'lib/rspec/benchmark/allocation_matcher.rb', line 123

def negative_failure_reason
  "allocated #{actual}"
end

#objectsObject Also known as: object

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.



82
83
84
85
# File 'lib/rspec/benchmark/allocation_matcher.rb', line 82

def objects
  @count_type = :objects
  self
end

#objects_to_s(value) ⇒ Object

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
# File 'lib/rspec/benchmark/allocation_matcher.rb', line 151

def objects_to_s(value)
  if value.respond_to?(:to_hash)
    value
      .sort_by { |k, v| k.to_s }
      .map { |key, val| "#{val} #{key}" if @objects.keys.include?(key) }
      .compact.join(" and ")
  else
    value
  end
end

#pluralize_objects(value) ⇒ Object

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.



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rspec/benchmark/allocation_matcher.rb', line 139

def pluralize_objects(value)
  if value.respond_to?(:to_hash)
    if value.keys.size == 1 && value.values.reduce(&:+) == 1
      "object"
    else
      "objects"
    end
  else
    value == 1 ? "object" : "objects"
  end
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.



118
119
120
121
# File 'lib/rspec/benchmark/allocation_matcher.rb', line 118

def positive_failure_reason
  return "was not a block" unless @block.is_a?(Proc)
  "allocated #{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)


25
26
27
# File 'lib/rspec/benchmark/allocation_matcher.rb', line 25

def supports_block_expectations?
  true
end

#warmup(value) ⇒ Object

The time before measurements are taken

Parameters:

  • value (Numeric)

    the time before measurements are taken



77
78
79
80
# File 'lib/rspec/benchmark/allocation_matcher.rb', line 77

def warmup(value)
  @warmup = value
  self
end