Skip to content

Instantly share code, notes, and snippets.

@glennpratt
Created January 14, 2014 20:42
Show Gist options
  • Select an option

  • Save glennpratt/8425302 to your computer and use it in GitHub Desktop.

Select an option

Save glennpratt/8425302 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'pp'
class App
def initialize
$realm_match = Regexp.new(ENV['CLOUDAPI_DISABLE_REALM_MATCH']) rescue false
@@realm_match = Regexp.new(ENV['CLOUDAPI_DISABLE_REALM_MATCH']) rescue false
@realm_match = Regexp.new(ENV['CLOUDAPI_DISABLE_REALM_MATCH']) rescue false
$_match = nil
@@_match = nil
end
def wolanin_match?(realm)
ENV['CLOUDAPI_DISABLE_REALM_MATCH'] && /#{ENV['CLOUDAPI_DISABLE_REALM_MATCH']}/ =~ realm
end
# Actually, this raises when not set.
#def smith_match?(realm)
# !!(Regexp.new(ENV['CLOUDAPI_DISABLE_REALM_MATCH']) =~ realm)
#end
def pratt_match?(realm)
!ENV['CLOUDAPI_DISABLE_REALM_MATCH'].nil? && !!(Regexp.new(ENV['CLOUDAPI_DISABLE_REALM_MATCH']) =~ realm)
end
def const_match?(realm)
CLOUDAPI_DISABLE_REALM_MATCH =~ realm
end
def const_bool_match?(realm)
!!(CLOUDAPI_DISABLE_REALM_MATCH =~ realm)
end
def global_cache_match?(realm)
if $_match.nil?
$_match = Regexp.new(ENV['CLOUDAPI_DISABLE_REALM_MATCH']) rescue false
end
$_match =~ realm
end
def class_cache_match?(realm)
if @@_match.nil?
@@_match = Regexp.new(ENV['CLOUDAPI_DISABLE_REALM_MATCH']) rescue false
end
@@_match =~ realm
end
def instance_cache_match?(realm)
if @_match.nil?
@_match = Regexp.new(ENV['CLOUDAPI_DISABLE_REALM_MATCH']) rescue false
end
@_match =~ realm
end
def global_startup_match?(realm)
$realm_match =~ realm
end
def class_startup_match?(realm)
@@realm_match =~ realm
end
def instance_startup_match?(realm)
@realm_match =~ realm
end
end
def test(app, realm, match)
methods = app.methods.grep(/match\?/)
methods.each do |method_symbol|
method = app.method(method_symbol)
result = method.call(realm)
if match
puts "#{method_symbol} expected #{match} got #{result.inspect}" unless result
else
puts "#{method_symbol} expected #{match} got #{result.inspect}" if result
end
end
end
def benchmark(app, realm, iterations = 100000)
Benchmark.bm(25) do |bm|
bm.report('wolanin') do
iterations.times do
app.wolanin_match?(realm)
end
end
# bm.report('smith') do
# iterations.times do
# app.smith_match?(realm)
# end
# end
bm.report('pratt') do
iterations.times do
app.pratt_match?(realm)
end
end
bm.report('const_match') do
iterations.times do
app.const_match?(realm)
end
end
bm.report('const_bool_match') do
iterations.times do
app.const_bool_match?(realm)
end
end
bm.report('global_cache_match') do
iterations.times do
app.global_cache_match?(realm)
end
end
bm.report('class_cache_match') do
iterations.times do
app.class_cache_match?(realm)
end
end
bm.report('instance_cache_match') do
iterations.times do
app.instance_cache_match?(realm)
end
end
bm.report('global_startup_match') do
iterations.times do
app.global_startup_match?(realm)
end
end
bm.report('class_startup_match') do
iterations.times do
app.class_startup_match?(realm)
end
end
bm.report('instance_startup_match') do
iterations.times do
app.instance_startup_match?(realm)
end
end
end
end
puts " --- ENV unset, no match"
app = App.new
CLOUDAPI_DISABLE_REALM_MATCH = Regexp.new(ENV['CLOUDAPI_DISABLE_REALM_MATCH']) rescue false
test(app, 'devcloud', false)
benchmark(app, 'devcloud')
puts
puts " --- ENV set, matches"
ENV['CLOUDAPI_DISABLE_REALM_MATCH'] = 'devcloud'
app = App.new
# Re-assignment generates a warning, don't care.
$VERBOSE = nil
CLOUDAPI_DISABLE_REALM_MATCH = Regexp.new(ENV['CLOUDAPI_DISABLE_REALM_MATCH']) rescue false
test(app, 'devcloud', true)
benchmark(app, 'devcloud')
puts
puts " --- ENV set, no match"
app = App.new
test(app, 'network', false)
benchmark(app, 'network')
@glennpratt
Copy link
Author

|ruby-2.0.0-p353| $ ruby benchmark.rb 
 --- ENV unset, no match
                                user     system      total        real
wolanin                     0.210000   0.000000   0.210000 (  0.204374)
pratt                       0.220000   0.000000   0.220000 (  0.226874)
const_match                 0.090000   0.000000   0.090000 (  0.089160)
const_bool_match            0.110000   0.000000   0.110000 (  0.109304)
global_cache_match          0.140000   0.000000   0.140000 (  0.132800)
class_cache_match           0.210000   0.000000   0.210000 (  0.220921)
instance_cache_match        0.150000   0.000000   0.150000 (  0.141810)
global_startup_match        0.090000   0.000000   0.090000 (  0.090267)
class_startup_match         0.130000   0.000000   0.130000 (  0.130027)
instance_startup_match      0.090000   0.000000   0.090000 (  0.093800)

 --- ENV set, matches
                                user     system      total        real
wolanin                     4.530000   0.020000   4.550000 (  4.570830)
pratt                       4.220000   0.010000   4.230000 (  4.258453)
const_match                 0.600000   0.000000   0.600000 (  0.600909)
const_bool_match            0.700000   0.010000   0.710000 (  0.722437)
global_cache_match          0.710000   0.000000   0.710000 (  0.720152)
class_cache_match           0.830000   0.000000   0.830000 (  0.832805)
instance_cache_match        0.680000   0.010000   0.690000 (  0.686846)
global_startup_match        0.630000   0.000000   0.630000 (  0.630173)
class_startup_match         0.660000   0.000000   0.660000 (  0.670518)
instance_startup_match      0.590000   0.000000   0.590000 (  0.591996)

 --- ENV set, no match
                                user     system      total        real
wolanin                     4.530000   0.020000   4.550000 (  4.611864)
pratt                       3.730000   0.010000   3.740000 (  3.764850)
const_match                 0.290000   0.000000   0.290000 (  0.284955)
const_bool_match            0.300000   0.000000   0.300000 (  0.301784)
global_cache_match          0.330000   0.000000   0.330000 (  0.337714)
class_cache_match           0.410000   0.000000   0.410000 (  0.410488)
instance_cache_match        0.360000   0.000000   0.360000 (  0.360238)
global_startup_match        0.280000   0.000000   0.280000 (  0.291208)
class_startup_match         0.320000   0.000000   0.320000 (  0.328929)
instance_startup_match      0.290000   0.000000   0.290000 (  0.290356)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment