public
Created

  • Download Gist
perf.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.