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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
require 'rspec/core'
require 'rspec/expectations'
# switches between these implementations - https://github.com/rspec/rspec-core/pull/1858/files
# benchmark requested in this PR - https://github.com/rspec/rspec-core/pull/1858
#
# I ran these from lib root by adding "gem 'benchmark-ips'" to ../Gemfile-custom
# then ran `bundle install --standalone --binstubs bundle/bin`
# then ran `ruby --disable-gems -I lib -I "$PWD/bundle" -r bundler/setup -S benchmarks/threadsafe_let_block.rb`
# The old, non-thread safe implementation, imported from the `main` branch and pared down.
module OriginalNonThreadSafeMemoizedHelpers
def __memoized
@__memoized ||= {}
end
module ClassMethods
def let(name, &block)
# We have to pass the block directly to `define_method` to
# allow it to use method constructs like `super` and `return`.
raise "#let or #subject called without a block" if block.nil?
OriginalNonThreadSafeMemoizedHelpers.module_for(self).__send__(:define_method, name, &block)
# Apply the memoization. The method has been defined in an ancestor
# module so we can use `super` here to get the value.
if block.arity == 1
define_method(name) { __memoized.fetch(name) { |k| __memoized[k] = super(RSpec.current_example, &nil) } }
else
define_method(name) { __memoized.fetch(name) { |k| __memoized[k] = super(&nil) } }
end
end
end
def self.module_for(example_group)
get_constant_or_yield(example_group, :LetDefinitions) do
mod = Module.new do
include Module.new {
example_group.const_set(:NamedSubjectPreventSuper, self)
}
end
example_group.const_set(:LetDefinitions, mod)
mod
end
end
# @private
def self.define_helpers_on(example_group)
example_group.__send__(:include, module_for(example_group))
end
def self.get_constant_or_yield(example_group, name)
if example_group.const_defined?(name, (check_ancestors = false))
example_group.const_get(name, check_ancestors)
else
yield
end
end
end
class HostBase
# wires the implementation
# adds `let(:name) { nil }`
# returns `Class.new(self) { let(:name) { super() } }`
def self.prepare_using(memoized_helpers, options={})
include memoized_helpers
extend memoized_helpers::ClassMethods
memoized_helpers.define_helpers_on(self)
define_method(:initialize, &options[:initialize]) if options[:initialize]
let(:name) { nil }
verify_memoizes memoized_helpers, options[:verify]
Class.new(self) do
memoized_helpers.define_helpers_on(self)
let(:name) { super() }
end
end
def self.verify_memoizes(memoized_helpers, additional_verification)
# Since we're using custom code, ensure it actually memoizes as we expect...
counter_class = Class.new(self) do
include RSpec::Matchers
memoized_helpers.define_helpers_on(self)
counter = 0
let(:count) { counter += 1 }
end
extend RSpec::Matchers
instance_1 = counter_class.new
expect(instance_1.count).to eq(1)
expect(instance_1.count).to eq(1)
instance_2 = counter_class.new
expect(instance_2.count).to eq(2)
expect(instance_2.count).to eq(2)
instance_3 = counter_class.new
instance_3.instance_eval &additional_verification if additional_verification
end
end
class OriginalNonThreadSafeHost < HostBase
Subclass = prepare_using OriginalNonThreadSafeMemoizedHelpers
end
class ThreadSafeHost < HostBase
Subclass = prepare_using RSpec::Core::MemoizedHelpers,
:initialize => lambda { |*| @__memoized = ThreadsafeMemoized.new },
:verify => lambda { |*| expect(__memoized).to be_a_kind_of RSpec::Core::MemoizedHelpers::ThreadsafeMemoized }
end
class ConfigNonThreadSafeHost < HostBase
Subclass = prepare_using RSpec::Core::MemoizedHelpers,
:initialize => lambda { |*| @__memoized = NonThreadSafeMemoized.new },
:verify => lambda { |*| expect(__memoized).to be_a_kind_of RSpec::Core::MemoizedHelpers::NonThreadSafeMemoized }
end
def title(title)
hr = "#" * (title.length + 6)
blank = "# #{' ' * title.length} #"
[hr, blank, "# #{title} #", blank, hr]
end
require 'benchmark/ips'
puts title "versions"
puts "RUBY_VERSION #{RUBY_VERSION}"
puts "RUBY_PLATFORM #{RUBY_PLATFORM}"
puts "RUBY_ENGINE #{RUBY_ENGINE}"
puts "ruby -v #{`ruby -v`}"
puts "Benchmark::IPS::VERSION #{Benchmark::IPS::VERSION}"
puts "rspec-core SHA #{`git log --pretty=format:%H -1`}"
puts
puts title "1 call to let -- each sets the value"
Benchmark.ips do |x|
x.report("non-threadsafe (original)") { OriginalNonThreadSafeHost.new.name }
x.report("non-threadsafe (config) ") { ConfigNonThreadSafeHost.new.name }
x.report("threadsafe ") { ThreadSafeHost.new.name }
x.compare!
end
puts title "10 calls to let -- 9 will find memoized value"
Benchmark.ips do |x|
x.report("non-threadsafe (original)") do
i = OriginalNonThreadSafeHost.new
i.name; i.name; i.name; i.name; i.name
i.name; i.name; i.name; i.name; i.name
end
x.report("non-threadsafe (config) ") do
i = ConfigNonThreadSafeHost.new
i.name; i.name; i.name; i.name; i.name
i.name; i.name; i.name; i.name; i.name
end
x.report("threadsafe ") do
i = ThreadSafeHost.new
i.name; i.name; i.name; i.name; i.name
i.name; i.name; i.name; i.name; i.name
end
x.compare!
end
puts title "1 call to let which invokes super"
Benchmark.ips do |x|
x.report("non-threadsafe (original)") { OriginalNonThreadSafeHost::Subclass.new.name }
x.report("non-threadsafe (config) ") { ConfigNonThreadSafeHost::Subclass.new.name }
x.report("threadsafe ") { ThreadSafeHost::Subclass.new.name }
x.compare!
end
puts title "10 calls to let which invokes super"
Benchmark.ips do |x|
x.report("non-threadsafe (original)") do
i = OriginalNonThreadSafeHost::Subclass.new
i.name; i.name; i.name; i.name; i.name
i.name; i.name; i.name; i.name; i.name
end
x.report("non-threadsafe (config) ") do
i = ConfigNonThreadSafeHost::Subclass.new
i.name; i.name; i.name; i.name; i.name
i.name; i.name; i.name; i.name; i.name
end
x.report("threadsafe ") do
i = ThreadSafeHost::Subclass.new
i.name; i.name; i.name; i.name; i.name
i.name; i.name; i.name; i.name; i.name
end
x.compare!
end
__END__
##############
# #
# versions #
# #
##############
RUBY_VERSION 2.2.0
RUBY_PLATFORM x86_64-darwin13
RUBY_ENGINE ruby
ruby -v ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin13]
Benchmark::IPS::VERSION 2.1.1
rspec-core SHA 1ee7a8d8cde6ba2dd13d35e90e824e8e5ba7db76
##########################################
# #
# 1 call to let -- each sets the value #
# #
##########################################
Calculating -------------------------------------
non-threadsafe (original)
53.722k i/100ms
non-threadsafe (config)
44.998k i/100ms
threadsafe
26.123k i/100ms
-------------------------------------------------
non-threadsafe (original)
830.988k (± 6.3%) i/s - 4.190M
non-threadsafe (config)
665.662k (± 6.7%) i/s - 3.330M
threadsafe
323.575k (± 5.6%) i/s - 1.620M
Comparison:
non-threadsafe (original): 830988.5 i/s
non-threadsafe (config) : 665661.9 i/s - 1.25x slower
threadsafe : 323574.9 i/s - 2.57x slower
###################################################
# #
# 10 calls to let -- 9 will find memoized value #
# #
###################################################
Calculating -------------------------------------
non-threadsafe (original)
28.724k i/100ms
non-threadsafe (config)
25.357k i/100ms
threadsafe
18.349k i/100ms
-------------------------------------------------
non-threadsafe (original)
346.302k (± 6.1%) i/s - 1.752M
non-threadsafe (config)
309.970k (± 5.4%) i/s - 1.547M
threadsafe
208.946k (± 5.2%) i/s - 1.046M
Comparison:
non-threadsafe (original): 346302.0 i/s
non-threadsafe (config) : 309970.2 i/s - 1.12x slower
threadsafe : 208946.3 i/s - 1.66x slower
#######################################
# #
# 1 call to let which invokes super #
# #
#######################################
Calculating -------------------------------------
non-threadsafe (original)
42.458k i/100ms
non-threadsafe (config)
37.367k i/100ms
threadsafe
21.088k i/100ms
-------------------------------------------------
non-threadsafe (original)
591.906k (± 6.3%) i/s - 2.972M
non-threadsafe (config)
511.295k (± 4.7%) i/s - 2.578M
threadsafe
246.080k (± 5.8%) i/s - 1.244M
Comparison:
non-threadsafe (original): 591906.3 i/s
non-threadsafe (config) : 511295.0 i/s - 1.16x slower
threadsafe : 246079.6 i/s - 2.41x slower
#########################################
# #
# 10 calls to let which invokes super #
# #
#########################################
Calculating -------------------------------------
non-threadsafe (original)
24.282k i/100ms
non-threadsafe (config)
22.762k i/100ms
threadsafe
14.685k i/100ms
-------------------------------------------------
non-threadsafe (original)
297.423k (± 5.0%) i/s - 1.505M
non-threadsafe (config)
264.046k (± 5.6%) i/s - 1.320M
threadsafe
170.853k (± 4.7%) i/s - 866.415k
Comparison:
non-threadsafe (original): 297422.6 i/s
non-threadsafe (config) : 264045.8 i/s - 1.13x slower
threadsafe : 170853.1 i/s - 1.74x slower
|