Skip to content

Instantly share code, notes, and snippets.

@mruzicka
Created August 16, 2013 15:00
Show Gist options
  • Save mruzicka/6250656 to your computer and use it in GitHub Desktop.
Save mruzicka/6250656 to your computer and use it in GitHub Desktop.
Performance test script
list = [
{ :path => 'path_C', :name => 'vm_x' },
{ :path => 'path_a', :name => 'vm_y' },
{ :path => 'path_b', :name => 'vm_z' },
{ :path => 'path_d', :name => 'vm_b' },
{ :path => 'path_D', :name => 'vm_a' },
]
def build_output_michal(list)
result_map = {}
list.each do |vm|
full_path = "#{vm[:path]}/#{vm[:name]}"
result_map[full_path] = "#{full_path}\n" <<
" powerstate: #{vm[:power_state]}\n" <<
" name: #{vm[:name]}\n" <<
" hostname: #{vm[:hostname] || '--------'}\n" <<
" instanceid: #{vm[:instance_uuid]}\n" <<
" ipaddress: #{vm[:ipaddress] || '---.---.---.---'}\n" <<
" template: #{vm[:is_a_template]}\n"
end
result_map.keys.sort { |l, r| l.downcase <=> r.downcase }.map { |k| result_map[k] }.join("\n")
end
def build_output_michal_optimized(list)
result_map = {}
list.each do |vm|
result_map["#{vm[:path]}/#{vm[:name]}"] = vm
end
memo = ''
result_map.keys.sort { |l, r| l.downcase <=> r.downcase }.each do |full_path|
vm = result_map[full_path]
# Add a blank line if this isn't the first record
memo << "\n" unless memo.empty?
# Store the next record string and return the memo value
memo << full_path << "\n" <<
" powerstate: #{vm[:power_state]}\n" <<
" name: #{vm[:name]}\n" <<
" hostname: #{vm[:hostname] || '--------'}\n" <<
" instanceid: #{vm[:instance_uuid]}\n" <<
" ipaddress: #{vm[:ipaddress] || '---.---.---.---'}\n" <<
" template: #{vm[:is_a_template]}\n"
end
memo
end
def build_output_michal_new(list)
list = list.map do |vm|
full_path = "#{vm[:path]}/#{vm[:name]}"
{
:full_path => full_path,
:sort_key => full_path.downcase,
:vm => vm,
}
end
memo = ''
list.sort { |l, r| l[:sort_key] <=> r[:sort_key] }.each do |m|
vm = m[:vm]
# Add a blank line if this isn't the first record
memo << "\n" unless memo.empty?
# Store the next record string and return the memo value
memo <<
"#{m[:full_path]}\n" <<
" powerstate: #{vm[:power_state]}\n" <<
" name: #{vm[:name]}\n" <<
" hostname: #{vm[:hostname] || '--------'}\n" <<
" instanceid: #{vm[:instance_uuid]}\n" <<
" ipaddress: #{vm[:ipaddress] || '---.---.---.---'}\n" <<
" template: #{vm[:is_a_template]}\n"
end
memo
end
def build_output_jill(list)
list.sort { |l, r|
# Sort by the full path + name in alphabetical order
"#{l[:path]}/#{l[:name]}".downcase <=> "#{r[:path]}/#{r[:name]}".downcase
}.inject(String.new) { |memo, vm|
# Add a blank line if this isn't the first record
memo << "\n" if memo == ""
# Store the next record string and return the memo value
memo <<
"#{vm[:path]}/#{vm[:name]}\n" <<
" powerstate: #{vm[:power_state]}\n" <<
" name: #{vm[:name]}\n" <<
" hostname: #{vm[:hostname] || '--------'}\n" <<
" instanceid: #{vm[:instance_uuid]}\n" <<
" ipaddress: #{vm[:ipaddress] || '---.---.---.---'}\n" <<
" template: #{vm[:is_a_template]}\n"
}
end
test_proc = method("build_output_#{ARGV[0]}".to_sym)
puts test_proc.call(list)
for i in 1 .. 1000000
test_proc.call(list)
end
@mruzicka
Copy link
Author

use the script like this:
ruby perf.rb michal_optimized
ruby perf.rb jill
etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment