Skip to content

Instantly share code, notes, and snippets.

@hayduke19us
Created February 16, 2018 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hayduke19us/fa7967f5ef36e36b1d1fc59ab788fb0f to your computer and use it in GitHub Desktop.
Save hayduke19us/fa7967f5ef36e36b1d1fc59ab788fb0f to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
require 'benchmark/memory'
# For use within a spec. Three methods must be provided within the current
# namespace. #performance_object, #performance_a, and #performance_b.
# The object is the object with the methods to be measured.
# The performance a and b are the methods to be compared.
# The original is #performance_a.
#
# Example use
#
# let(:performance_object) { parasite_property }
# let(:performance_a) { :compare_restore_room_rates }
# let(:performance_b) { :restore_room_rates }
#
# it_behaves_like 'Performance'
class Performance
A = 'original'.freeze
B = 'new'.freeze
def initialize(object, a, b)
@object = object
@a = a
@b = b
end
def ips
Benchmark.ips do |x|
x.config time: 2, warmup: 5
x.report(A) { @object.send @a }
x.report(B) { @object.send @b }
x.compare!
end
end
def memory
Benchmark.memory do |x|
x.report(A) { @object.send @a }
x.report(B) { @object.send @b }
end
end
end
shared_examples_for 'Performance' do
let(:performance) { Performance.new performance_object, performance_a, performance_b }
it 'Iterations per second' do
performance.ips
end
it 'Memory and object consumption' do
performance.memory
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment