Skip to content

Instantly share code, notes, and snippets.

@branan
Last active March 18, 2016 17:18
Show Gist options
  • Save branan/4ab1ee5af3a9493baa0b to your computer and use it in GitHub Desktop.
Save branan/4ab1ee5af3a9493baa0b to your computer and use it in GitHub Desktop.
require 'puppet'
require 'benchmark'
def compare_no_warn(a, b)
a.to_s <=> b.to_s
end
def compare_deprecation_warning(a, b)
if (b.class != Symbol)
Puppet.deprecation_warning("deprecated")
end
a.to_s <=> b.to_s
end
def compare_warn_once(a, b)
if (b.class != Symbol)
Puppet.warn_once('deprecations', 'symbol_compare', 'deprecated')
end
a.to_s <=> b.to_s
end
def compare_check_strict(a, b)
if (b.class != Symbol)
Puppet.deprecation_warning("deprecated") if Puppet[:strict] == :warning
raise "deprecated" if Puppet[:strict] == :error
end
a.to_s <=> b.to_s
end
Puppet.initialize_settings
n = 1000000
Benchmark.bm(32) do |x|
x.report("simple symbol/symbol comparison") {
n.times do
begin
compare_no_warn(:foo, :foo)
rescue
end
end
}
x.report("simple symbol/string comparison") {
n.times do
begin
compare_no_warn(:foo, "foo")
rescue
end
end
}
x.report("warning symbol/symbol comparison") {
n.times do
begin
compare_deprecation_warning(:foo, :foo)
rescue
end
end
}
x.report("warning symbol/string comparison") {
n.times do
begin
compare_deprecation_warning(:foo, "foo")
rescue
end
end
}
x.report("warn once symbol/symbol compare") {
n.times do
begin
compare_warn_once(:foo, :bar)
rescue
end
end
}
x.report("warn once symbol/string compare") {
n.times do
begin
compare_warn_once(:foo, "foo")
rescue
end
end
}
x.report("strict symbol/symbol comparison") {
n.times do
begin
compare_check_strict(:foo, :foo)
rescue
end
end
}
x.report("strict symbol/string comparison") {
n.times do
begin
compare_check_strict(:foo, "foo")
rescue
end
end
}
Puppet.settings[:strict] = :error
x.report("strict symbol/string error") {
n.times do
begin
compare_check_strict(:foo, "foo")
rescue
end
end
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment