Skip to content

Instantly share code, notes, and snippets.

@delano
Forked from careo/threads_vs_em.rb
Created February 4, 2009 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delano/58384 to your computer and use it in GitHub Desktop.
Save delano/58384 to your computer and use it in GitHub Desktop.
require 'uri'
require 'benchmark'
require 'net/http'
require 'rubygems'
require 'eventmachine'
require 'dnsruby'
# github.com/careo/em-http-request/
require '/Users/djensen/Projects/em-http-request/lib/em-http'
sources = %w{
http://gusmueller.com/blog/rss.xml
http://feeds.feedburner.com/HacketyOrg
http://www.hawkwings.net/feed/atom/
http://blog.haxe.org/rss
http://feeds.feedburner.com/heroku
http://heronsperch.blogspot.com/feeds/posts/default
http://highscalability.com/rss.xml
http://blog.kovyrin.net/feed/atom/
http://www.hrheroblogs.com/?feed=rss2
http://www.avibryant.com/atom.xml
http://feeds.feedburner.com/nicksieger
http://headius.blogspot.com/feeds/posts/default
http://ola-bini.blogspot.com/feeds/posts/default
http://feeds.feedburner.com/The_Erlang_Fix
http://www.wired.com/rss/index.xml
http://ulf.wiger.net/weblog/feed/atom/
http://ungeni.us/feeds/ungenius.xml
http://blogs.unity3d.com/feed/
http://unity3d.com/news.rss
http://blog.omnigroup.com/feed/
http://www.lukas-renggli.ch/blog?view=PBEntriesRssView
http://steve-yegge.blogspot.com/feeds/posts/default
http://spawnlink.com/feed/
http://ninh.nl/blog/feed/atom/
http://ungeni.us/feeds/ungenius.xml
http://blogs.unity3d.com/feed/
http://unity3d.com/news.rss
http://blog.omnigroup.com/feed/
http://www.lukas-renggli.ch/blog?view=PBEntriesRssView
}.collect { |s| URI.parse(s) }
Benchmark.bm(20) do |x|
x.report("em-http-request") do
EM.run {
@gets = []
source = sources.pop
sources.each do |source|
EM.next_tick {
http = EventMachine::HttpRequest.new(source.to_s).get
@gets.push http
http.callback {
@gets.delete http
http.response
EM.stop if @gets.empty?
}
}
end
}
end
x.report("no threads") do
sources.each do |source|
begin
Net::HTTP.new(source.host,80).get2(source.path) do |resp,data|
data
end
rescue
end
end
end
x.report("green threads") do
sources.each do |source|
Thread.new {
begin
Net::HTTP.new(source.host,80).get2(source.path) do |resp,data|
data
end
rescue
end
}.join
end
end
end
#x = Time.now
#dict1 = {}
#sources.each { |src|
# topics.each { |topic|
# dict1[[src,topic]] = world_bound_task(src,topic)
# }
# }
#print "No threads took #{Time.now-x} seconds\n"
#
#x = Time.now
#dict2 = {}
#sources.collect { |src|
# topics.collect { |topic|
# Thread.new {
# dict2[[src,topic]] = world_bound_task(src,topic)
# }
# }
# }.each { |t| t.join }
#print "With (green) threads took #{Time.now-x} seconds\n"
#
#
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment