Created
October 5, 2017 21:52
-
-
Save NickLaMuro/a1acfee84f8c58d8c55d72af6a4a1f9f to your computer and use it in GitHub Desktop.
Benchmarks getting azure private images
This file contains 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
# | |
# This script has been adapted from a similar script by Dave Gynn (@dgynn): | |
# | |
# - https://gist.github.com/dgynn/bd54e970f8942ce2ca532e4adc3fd30a | |
# | |
# And the functionality has been heavily modified and made configurable from a | |
# script by Daniel Berger (@djberg96) | |
# | |
# - https://gist.github.com/djberg96/809562e6e7868966a22032935ee6e5e8 | |
# | |
# With updates to allow different forms of profiling and performance | |
# improvements around the original script, and for profiling only subsets of | |
# the data. Also makes use of `VCR` to reduce variations of results between | |
# runs. | |
# | |
# Requires ruby and a semi recent version of bundler (> 1.10) | |
# | |
# Git is optional, but the script will have to be updated in the inline gemfile | |
# to use the non-git version of the `memory_profiler` gem (see comments) | |
# | |
# | |
# | |
# This can be run against a local version of Azure::Armrest or the latest gem | |
# release for comparison | |
# | |
# Run at least once with --install-gems to ensure all required Gems are installed | |
# | |
# ruby ./azure_private_images_benchmark.rb --install-gems | |
# | |
# If you wish to change gems (based on options, see below), you must re-run | |
# with --install-gems | |
# | |
# | |
# | |
# To run against local code | |
# | |
# ruby ./azure_private_images_benchmark.rb --local | |
# | |
# To run against latest release | |
# | |
# ruby ./azure_private_images_benchmark.rb | |
# | |
begin | |
require "bundler/inline" | |
require "optparse" | |
rescue LoadError => e | |
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler" | |
raise e | |
end | |
### Option Parsing ### | |
options = { :install_gems => false, :local => false, :sa_samples => 10 } | |
ARGV.options do |opts| | |
opts.banner = "Usage: #{File.basename $PROGRAM_NAME} [options]" | |
opts.on("--install-gems", "Bundle install the gems. Run with this flag once.") do |install| | |
options[:install_gems] = install | |
end | |
opts.on("--local", "Run against local code rather than installed gem") do |local| | |
options[:local] = local | |
end | |
opts.on("--silent-bundler", "Don't display bundler output") do | |
require "bundler" | |
options[:ui] = Bundler::UI::Silent.new | |
end | |
opts.on("--profile", "Profile a sample of the storage accounts") do | |
options[:profile] = true | |
options[:profile_full] = false | |
end | |
opts.on("--samples=SAMPLES", "Number of storage account samples to use") do |samples| | |
options[:sa_samples] = samples.to_i | |
end | |
opts.on("--profile-full", "Profile all of the storage accounts (could take time)") do | |
options[:profile] = false | |
options[:profile_full] = true | |
end | |
# Can't use this with ---profile or --profile_full | |
# Not currently implemented | |
opts.on("--mem-over-time", "Capture memory over time (requires fork)") do | |
options[:mem_over_time] = true | |
end | |
opts.on("-h", "--help", "Show this help") { puts opts; exit } | |
opts.parse! | |
end | |
### Dependency Handling ### | |
bundler_keys = [:ui] | |
bundler_opts = options.select {|k,v| bundler_keys.include?(k) && v } | |
gemfile(options[:install_gems], bundler_opts) do | |
source "https://rubygems.org" | |
if options[:local] | |
gem "azure-armrest", :path => "./" | |
else | |
gem "azure-armrest" | |
end | |
gem "vcr" | |
gem "webmock" | |
# gem "memory_profiler" | |
# | |
# This version has some unreleased improvements in place that very much | |
# improve the string analysis of memory_profiler when dealing with | |
# c-extensions. | |
gem 'memory_profiler', :github => 'dgynn/memory_profiler', :branch => 'reduced-string-memory' | |
gem "sys-proctable", :require => false | |
end | |
require "benchmark" | |
require "pathname" | |
### Azure::Armrest configuration ### | |
# | |
# Put your real credentials in a file in tmp/azure_credentials.cfg with the | |
# following format (extra spacing optional, but at least 1 is needed): | |
# | |
# tenant_id: [TENANT_ID_HERE] | |
# subscription_id: [SUBSCRIPTION_ID_HERE] | |
# client_id: [CLIENT_ID_HERE] | |
# client_key: [CLIENT_KEY_HERE] | |
# | |
az_keys = {} | |
File.foreach('tmp/azure_credentials.cfg') do |line| | |
key, val = line.split(/: +/) | |
next if key.nil? | |
az_keys[key] = val.strip | |
end | |
conf = Azure::Armrest::Configuration.new(az_keys) | |
### VCR Configuration ### | |
VCR.configure do |config| | |
config.cassette_library_dir = Pathname.new(Dir.pwd).join('tmp', 'vcr_cassettes').to_s | |
config.hook_into :webmock | |
end | |
images = nil | |
### Main Script ### | |
VCR.use_cassette("azure_private_images#{'_old2' if options[:local]}") do | |
sas = Azure::Armrest::StorageAccountService.new(conf) | |
images = [] | |
if options[:profile] | |
list_all_opts = { :location => 'eastus' } | |
list_all_opts[:skip_accessors_definition] = true if options[:local] | |
MemoryProfiler.report { | |
storage_accounts = sas.list_all(list_all_opts) | |
images = sas.send(:get_private_images, storage_accounts[0,options[:sa_samples]]) | |
}.pretty_print :to_file => "tmp/performance_reports/azure_#{options[:sa_samples]}_sa_image_list_#{options[:local] ? "local" : "master"}.report" | |
elsif options[:profile_full] | |
MemoryProfiler.report { | |
images = sas.list_all_private_images(:location => 'eastus') | |
}.pretty_print :to_file => "tmp/performance_reports/azure_image_list_#{options[:local] ? "local" : "master"}.report" | |
else | |
images = sas.list_all_private_images(:location => 'eastus') | |
end | |
end | |
require 'bigdecimal' | |
puts "Mem: #{((1024 * BigDecimal.new(`ps -o rss= -p #{Process.pid}`))/BigDecimal.new(1_048_576)).to_f}MiB" | |
images # Hold on to the reference so the mem result is as accurate as possible | |
# puts images.count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment