Created
January 14, 2014 20:42
-
-
Save glennpratt/8425302 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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') |
Author
glennpratt
commented
Jan 14, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment