czwartek, 25 września 2008

Why EC2 command line API-tools sucks...

Every time cron daemon run ruby scripts witch contain command line EC2 API-tools load grow up and take a lot resources. In last days decide to rewrite it. For some configure files need to check often what IP's got working www backends or database backends. Got for that class in ruby:

machine.rb:

require 'resolv'

class Machine

IMAGES = {
'www' => ['ami-WWWWWWWW'],
'db' => ['ami-DBDBDBDB'],
'kolektor' => ['ami-POOLPOOL'],
}

MACHINES = {
'www' => [],
'db' => [],
'kolektor' => [],
}

def self.getips
output=`ec2-describe-instances`
output.split("\n").each do |line|
IMAGES.each do |type, amis|
amis.each do |ami|
if line.match('\s' + ami + '\s.*?running')
ip = Resolv.getaddress(line.split[3])
MACHINES[type] << ip
end
end
end
end
MACHINES
end
end


Easy for use at other scripts like that:

getwww.rb:

#!/usr/bin/ruby

require 'machines.rb'

puts Machine.getips['www']


But that was slow and take too much resources. Discover project amazon-ec2.rubyforge.org and rewrite that class.

machines_new.rb:

require 'resolv'
require 'rubygems'
require 'EC2'

class Machine

ACCESS_KEY_ID = 'your_access_key_id'
SECRET_ACCESS_KEY = 'your_secret_access_key'


IMAGES = {
'www' => ['ami-WWWWWWWW'],
'db' => ['ami-DBDBDBDB'],
'kolektor' => ['ami-POOLPOOL'],
}

MACHINES = {
'www' => [],
'db' => [],
'kolektor' => [],
}

def self.getips
ec2 = EC2::Base.new(:access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)
ec2.describe_instances.reservationSet.item.each do |reservation|
IMAGES.each do |type, amis|
amis.each do |ami|
reservation.instancesSet.item.each do |item|
if item.imageId.match(ami)
ip = Resolv.getaddress(item.dnsName)
MACHINES[type] << ip
end
end
end
end
end
MACHINES
end
end


How fast could be use non-JAVA EC2 API-tools ?

With old class:

# time for i in `seq 1 20`; do ./getwww.rb ; done
real 3m58.211s
user 0m48.870s
sys 0m2.650s

With new class:

# time for i in `seq 1 20`; do ./getwww.rb ; done
real 0m20.234s
user 0m6.570s
sys 0m0.870s

Only 11.5x times faster ! And doesn't take 80mb mem, just 10mem for 1 sec.

Brak komentarzy: