Skip to content

Instantly share code, notes, and snippets.

@olegykz
Created August 26, 2015 13:05
Show Gist options
  • Save olegykz/d78c2a702e140f1c1c71 to your computer and use it in GitHub Desktop.
Save olegykz/d78c2a702e140f1c1c71 to your computer and use it in GitHub Desktop.
Check gems memory consumption and load time
require 'benchmark'
def require_and_profile(gemname = nil)
unless gemname
puts "%-20s: %10s | %10s | %10s" % ['gem','increment','total','time']
return
end
# checked on procps-ng (ps) version 3.3.3 and 3.3.9
memory_usage = `ps -o rss= -p #{Process.pid}`.to_i / 1024.0
load_time = Benchmark.realtime { require gemname }
puts "%-20s: %10.2f | %10.2f | %10.2f" % [ gemname, (`ps -o rss= -p #{Process.pid}`.to_i / 1024.0 - memory_usage), (`ps -o rss= -p #{Process.pid}`.to_i / 1024.0), load_time.round(2)]
end
pattern = /^[^#]*gem[ ]*['"]([^,'"]*)['"][ ,~>0-9\.'"]*(:require[ => ]*['"]([^'"]*)['"][, ])?/
require_and_profile
File.open('Gemfile').each do |line|
if line.match(pattern)
if line.match(pattern)[3]
require_and_profile line.match(pattern)[3]
else
require_and_profile line.match(pattern)[1]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment