Skip to content

Instantly share code, notes, and snippets.

@hayduke19us
Created September 8, 2017 19:56
Show Gist options
  • Save hayduke19us/9975c87cd46cc1edfa48d52d4e261197 to your computer and use it in GitHub Desktop.
Save hayduke19us/9975c87cd46cc1edfa48d52d4e261197 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