Skip to content

@bogdan /perf.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
require "benchmark"
load 'old_callbacks.rb'
load 'lib/active_support/callbacks.rb'
GC.disable
module Definition
def self.included(base)
base.class_eval do
define_callbacks :save
10.times do
set_callback :save, :before, :before_save1
set_callback :save, :after, :after_save1
set_callback :save, :around, :around_save1
end
def before_save1;nil; end
def after_save1; nil; end
def around_save1; nil; end
def save(key = nil)
if respond_to?(:__define_runner)
run_callbacks :save, key do
true
end
else
run_callbacks :save, key do
true
end
end
end
end
end
end
class OldClass
include ActiveSupport::OldCallbacks
end
class NewClass
include ActiveSupport::Callbacks
end
amount = 10
Benchmark.bmbm do |x|
x.report "New set_callback" do
amount.times do
Class.new do
include ActiveSupport::Callbacks
include Definition
end
end
end
x.report "Old set_callback" do
amount.times do
Class.new do
include ActiveSupport::OldCallbacks
include Definition
end
end
end
end
puts "*" * 120
old_klass = Class.new do
include ActiveSupport::OldCallbacks
end
new_klass = Class.new do
include ActiveSupport::Callbacks
end
amount = 100
Benchmark.bmbm do |x|
x.report "New define_callbacks" do
amount.times do |i|
old_klass.send(:define_callbacks, :"save#{i}")
end
end
x.report "Old define_callbacks" do
amount.times do |i|
new_klass.send(:define_callbacks, :"save#{i}")
end
end
end
puts "*" * 120
class OldClass
include ActiveSupport::OldCallbacks
include Definition
end
old = OldClass.new
class NewClass
include ActiveSupport::Callbacks
include Definition
end
new = NewClass.new
amount = 1000
Benchmark.bmbm do |x|
x.report "New run_callbacks" do
amount.times do
new.save
end
end
x.report "Old run_callbacks" do
amount.times do
old.save
end
end
end
puts "*" * 120
amount = 100
Benchmark.bmbm do |x|
x.report "New run_callbacks with key" do
amount.times do |index|
new.save "key#{index % 4}"
end
end
x.report "Old run_callbacks with key" do
amount.times do |index|
old.save "key#{index % 4}"
end
end
end
puts "*" * 120
amount = 100
Benchmark.bmbm do |x|
x.report "New skip_callback" do
klass = Class.new do
include ActiveSupport::Callbacks
include Definition
end
amount.times do |index|
klass.skip_callback :save, :before, :before_save1, :if => "false"
end
end
x.report "Old skip_callback" do
klass = Class.new do
include ActiveSupport::OldCallbacks
include Definition
end
amount.times do |index|
klass.skip_callback :save, :before, :before_save1, :if => "false"
end
end
end
GC.enable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.