Skip to content

Instantly share code, notes, and snippets.

@NickLaMuro
Last active August 8, 2017 02:00
Embed
What would you like to do?
ManageIQ perf_capture_realtime profiling script
#!/usr/bin/env ruby
require "optparse"
options = { :loops => 5 }
OptionParser.new do |opts|
opts.banner = "usage: #{File.basename $PROGRAM_NAME, '.rb'} [options]"
opts.on("-D", "--[no-]debug", "Toggle debug mode (includes verbose)") do |val|
options[:verbose] = val
options[:debug] = val
end
opts.on("-V", "--[no-]verbose", "Toggle verbose mode") do |val|
options[:verbose] = val
end
opts.on("-l=NUM", "--loops=NUM", "Number of loops to measure (default: 5)") do |val|
options[:loops] = val.to_i
end
opts.on("-n=NAME", "--name=NAME", "The name of the test suite (defaults to git branch)") do |val|
options[:name] = val
end
opts.on("-s", "--[no-]skip-metrics", "Skip testing") do |val|
options[:skip_metrics] = val
end
opts.on("-h", "--help", "Displays this help") do
puts opts
exit
end
end.parse!(ARGV)
##### Remove warnings #####
#
# There are actually a lot more warnings caused than the one below because the
# variable isn't initialized, but most of them happen within the Rails
# initialization, so pushing down setting the $VERBOSE and $DEBUG down to right
# before the non-dryrun captures prevents most of them from showing up.
$_miq_worker_current_msg = nil
##### Initialize Rails App #####
require File.expand_path('../../config/environment', __FILE__)
##### Stub the ManageIQ::Providers::Vmware::InfraManager #####
#
# Use the spec tools from the manageiq-providers-vmware gem to use some fake
# data that is used in the tests, and was captured from a running vsphere.
# Allows us to test everything that would be done in a normal run, without
# needing a running vmware env.
ManageIQ::Providers::Vmware::InfraManager
gem_dir = Bundler.locked_gems.specs.select { |g| g.name == "manageiq-providers-vmware" }.first.gem_dir
require File.join(gem_dir, "spec", "tools", "vim_data", "vim_data_test_helper")
module ManageIQ::Providers
class Vmware::InfraManager < InfraManager
def connect
FakeMiqVimHandle.new
end
def disconnect
true
end
end
end
##### Setup Memory Profiler and Transactional Fixtures
#
# Use Rails' built in mechanics for running everything within a transaction so
# that we can re-run this code multiple times with the exact same results.
require "memory_profiler"
require "#{::Rails.root}/spec/support/factory_girl_helper"
class PerfCaptureRunner
include FileUtils
include ActiveRecord::TestFixtures
self.fixture_path = "#{::Rails.root}/spec/fixtures"
BASE_DIR = ::Rails.root.join "tmp", "perf_realtime_capture_mem_profs"
def self.filename report_id
"report_%03d.txt" % report_id
end
def initialize run_id = 0, run_suite_timestamp = Time.now.to_i, skip_metrics = false
@run_id = run_id
@run_suite = run_suite_timestamp.to_s
@skip_metrics = skip_metrics
end
def run
setup_fixtures
create_ems
create_vm
perf_capture
ensure
teardown_fixtures
end
def create_ems
@ems = FactoryGirl.create(:ems_vmware, :zone => MiqServer.my_server.zone)
end
def create_vm
@vm = FactoryGirl.create(:vm_perf, :ext_management_system => @ems)
end
def perf_capture
if @skip_metrics
@vm.perf_capture_realtime
else
report_dir = BASE_DIR.join @run_suite.to_s
report_file = report_dir.join self.class.filename(@run_id)
mkdir_p report_dir.to_s
MemoryProfiler.report do
@vm.perf_capture_realtime
end.pretty_print :to_file => report_file.to_s
end
end
# no-op for some active_support B.S.
def method_name
""
end
end
# Dry run an extra run since it will be autoloading a bunch of things, skewing
# the metrics and slowing down the report
PerfCaptureRunner.new(nil, nil, :skip_metrics => true).run
$VERBOSE = options[:verbose]
$DEBUG = options[:debug]
name = options[:name] || `git rev-parse --abbrev-ref HEAD`.strip
(1..options[:loops]).to_a.each do |i|
puts name.inspect if $VERBOSE
PerfCaptureRunner.new(i, name, options[:skip_metrics]).run
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment