Skip to content

Instantly share code, notes, and snippets.

@gr33n7007h
Created June 14, 2015 21:38
Show Gist options
  • Save gr33n7007h/87229ea3124214617d5d to your computer and use it in GitHub Desktop.
Save gr33n7007h/87229ea3124214617d5d to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'net/http'
require 'thread'
class GemCalc
ServiceUri = "https://rubygems.org/api/v1/gems/"
def initialize
@gems = `gem list --remote`.gsub(/\(.+\)/, '').split("\n").map(&:strip)
@bytes = []
@threads = 32
@qpool = Queue.new
@mpool = Mutex.new
end
def get_bytes(gem)
res = Net::HTTP.get_response(URI("#{ServiceUri}#{gem}.yaml"))
if res.is_a? Net::HTTPOK
gem_link = res.body.match(/gem_uri:\s(.+)/)[1]
res = Net::HTTP.get_response(URI(gem_link))
if res.is_a? Net::HTTPOK
res['content-length'].to_i
elsif res.is_a? Net::HTTPFound
res = Net::HTTP.get_response(URI(res['location']))
puts res['content-length'].to_i
res['content-length'].to_i
end
end
rescue
end
def voodoo
puts "-" * 70
puts "Sit back, this will take a long time"
puts "-" * 70
@gems.each {|gem| @qpool << gem }
@threads.times.map do
Thread.new do
while !@qpool.empty?
gem_in_pool = @qpool.pop(true)
bytes = get_bytes(gem_in_pool)
@mpool.synchronize do
@bytes << bytes
end
end
end
end.map(&:join)
total = @bytes.compact.inject(0, :+)
puts "Total bytes: #{total}"
end
end
obj = GemCalc.new.voodoo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment