Skip to content

Instantly share code, notes, and snippets.

@databus23
Last active August 29, 2015 13:58
Show Gist options
  • Save databus23/10022514 to your computer and use it in GitHub Desktop.
Save databus23/10022514 to your computer and use it in GitHub Desktop.
benchmark ohai plugins
#!/usr/bin/env ruby
require 'optparse'
require 'benchmark'
require 'ohai'
options = {}
rest = OptionParser.new do |opts|
opts.on("-d", "--disabled-plugins [PLUGIN_NAMES]", "Disable ohai plugins by name") do |plugin_names|
Ohai::Config[:disabled_plugins] = plugin_names.split(/\s*,\s*/)
end
opts.on("-p", "--plugin-dir [PLUGIN_PATH]", "Additional ohai plugin path") do |plugin_path|
Ohai::Config[:plugin_path] << plugin_path
end
end.parse(ARGV)
if rest[0] == 'load_order'
class OhaiSystem < Ohai::System
attr_accessor :plugin_load_order
def require_plugin name
super
@plugin_load_order = [] unless @plugin_load_order
@plugin_load_order << name
end
end
ohai = OhaiSystem.new()
ohai.all_plugins
puts ohai.plugin_load_order.uniq
else
puts "Determining plugin load order in a child process..."
plugins = %x{ruby #{$0} #{ARGV.join(' ')} load_order}.split("\n")
puts "Benchmarking plugins in determined order..."
ohai = Ohai::System.new
load_times = {}
plugins.each do |plugin_name|
puts "Loading #{plugin_name}"
dur = Benchmark.realtime do
ohai.require_plugin plugin_name
end
load_times[plugin_name] = dur
end
puts
puts "==================== Results ==================="
printf " %-30s: %.4f seconds\n", "total time", load_times.values.reduce(:+)
puts
load_times.sort_by {|_,value|value}.reverse.each {|plugin_name, secs| printf " %-30s: %.4f seconds\n", plugin_name, secs}
end
#!/usr/bin/env ruby
require 'optparse'
require 'benchmark'
require 'ohai'
$load_times = {}
module Ohai
module DSL
class Plugin
class VersionVII
alias :old_run_plugin :run_plugin
def run_plugin
dur = Benchmark.realtime do
old_run_plugin
end
raise "Plugin #{name} alread benchmarked" if $load_times[name]
$load_times[name] = dur
end
end
end
end
end
Ohai::System.new.all_plugins
puts
puts "==================== Results ==================="
printf " %-30s: %.4f seconds\n", "total time", $load_times.values.reduce(:+)
puts
$load_times.sort_by {|_,value|value}.reverse.each {|plugin_name, secs| printf " %-30s: %.4f seconds\n", plugin_name, secs}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment