Skip to content

Instantly share code, notes, and snippets.

@nevans
Created March 21, 2012 18:00
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 nevans/2150364 to your computer and use it in GitHub Desktop.
Save nevans/2150364 to your computer and use it in GitHub Desktop.
Why is EM.epoll slowing down my connections in other threads?
#!/usr/bin/env ruby
# copied from https://gist.github.com/939696, and
# edited to add redis (which is where I was experiencing the issue)
require 'rubygems'
require 'net/http'
require 'hiredis'
require "redis/connection/hiredis"
require 'redis'
require 'eventmachine'
require 'benchmark'
redis = Redis.new
bm = lambda do
http_get = 5.times.map do
"(%.4fs)" % [Benchmark.realtime{ Net::HTTP.get URI.parse('http://www.google.com') }]
end
puts "HTTP.get: #{http_get.join(" ")}"
redis_ping = 5.times.map do
"(%.4fs)" % [Benchmark.realtime{ redis.ping }]
end
puts "redis.ping: #{redis_ping.join(" ")}"
end
puts "### Before EM.run ###"
bm.call
puts "### EM.run #{ARGV[0] == "epoll" ? "with" : "without"} epoll ###"
Thread.new do
EM.epoll if ARGV[0] == "epoll"
EM.run do
puts "### EM started ###"
end
end
bm.call
EM.stop
puts "### EM stopped ###"
bm.call
### Before EM.run ###
HTTP.get: (0.1281s) (0.0965s) (0.0639s) (0.0956s) (0.0813s)
redis.ping: (0.0008s) (0.0002s) (0.0001s) (0.0001s) (0.0001s)
### EM.run with epoll ###
### EM started ###
HTTP.get: (0.2536s) (0.2070s) (0.2530s) (0.2028s) (0.2031s)
redis.ping: (0.0507s) (0.0003s) (0.0505s) (0.0002s) (0.0503s)
### EM stopped ###
HTTP.get: (0.0601s) (0.0778s) (0.0769s) (0.0938s) (0.0918s)
redis.ping: (0.0004s) (0.0001s) (0.0001s) (0.0001s) (0.0001s)
### Before EM.run ###
HTTP.get: (0.0561s) (0.0960s) (0.0815s) (0.0810s) (0.0600s)
redis.ping: (0.0009s) (0.0002s) (0.0003s) (0.0002s) (0.0001s)
### EM.run with epoll ###
### EM started ###
HTTP.get: (0.6179s) (0.6203s) (0.6190s) (0.6714s) (0.6235s)
redis.ping: (0.1006s) (0.1005s) (0.1005s) (0.1006s) (0.1006s)
### EM stopped ###
HTTP.get: (0.1120s) (0.1733s) (0.0920s) (0.0953s) (0.0818s)
redis.ping: (0.0003s) (0.0001s) (0.0001s) (0.0001s) (0.0001s)
### Before EM.run ###
HTTP.get: (0.3604s) (0.0806s) (0.0976s) (0.0842s) (0.0824s)
redis.ping: (0.0012s) (0.0003s) (0.0001s) (0.0001s) (0.0001s)
### EM.run without epoll ###
### EM started ###
HTTP.get: (0.0910s) (0.0995s) (0.1046s) (0.0782s) (0.0784s)
redis.ping: (0.0003s) (0.0001s) (0.0001s) (0.0001s) (0.0001s)
### EM stopped ###
HTTP.get: (0.0838s) (0.0948s) (0.0768s) (0.0676s) (0.0994s)
redis.ping: (0.0003s) (0.0001s) (0.0001s) (0.0001s) (0.0001s)
@nevans
Copy link
Author

nevans commented Aug 14, 2012

n.b. I tested this with ruby 1.9.3-p194, and the problem doesn't exist in that version anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment