Skip to content

Instantly share code, notes, and snippets.

@jerodsanto
Forked from adamwiggins/gemweight.rb
Created January 13, 2009 13:20
Show Gist options
  • Select an option

  • Save jerodsanto/46442 to your computer and use it in GitHub Desktop.

Select an option

Save jerodsanto/46442 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Jan 13, 2009.
    41 changes: 41 additions & 0 deletions gemweight.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    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"