rrichards
5/29/2009 - 3:03 AM

gemweight.rb

require 'rubygems'
require 'benchmark'

gem = ARGV.shift.strip rescue ''
if gem == ''
        STDERR.puts "usage: #{$0} [gem]"
        exit 1
end

`free`
if $? != 0
        STDERR.puts "This program only works on Linux (or other unixes with the \"free\" command)."
        STDERR.puts "If you can figure out a way to get this to work on OS X, please send me a patch!"
        exit 1
end

def free_mem
        `free -b`.match(/Mem:\s*\d+\s*(\d+)/)[1].to_i
end

KB = 1024
MB = 1024 * KB
GB = 1024 * MB

def bytes(amount)
        return nil if amount.nil?
        return "#{amount} bytes" if amount < KB
        return "#{(amount / KB).round}kb" if amount < MB
        return "#{(amount / MB).round}MB" if amount < GB
        return "#{(amount / GB).round}GB"
end

before = free_mem
time = Benchmark.measure { require gem }.real
after = free_mem

used = after - before

time = sprintf("%0.02f", time)

puts "#{gem} uses #{bytes(used)} and takes #{time}s to load"