tmm1 (owner)

Fork Of

Forks

Revisions

gist: 111491 Download_button fork
public
Description:
net/http vs fibered em-http
Public Clone URL: git://gist.github.com/111491.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
require 'net/http'
require 'uri'
 
url = URI.parse 'http://localhost:5984/test/test'
req = Net::HTTP::Get.new(url.path)
start = Time.now
 
100.times do
  http = Net::HTTP.start(url.host, 5984)
  http.request(req)
end
 
p Time.now - start
 
# 2nd fastest
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
require 'net/http'
require 'uri'
 
url = URI.parse 'http://localhost:5984/test/test'
req = Net::HTTP::Get.new(url.path)
start = Time.now
http = Net::HTTP.start(url.host, 5984)
 
 
100.times do
  http.request(req)
end
 
p Time.now - start
 
# fastest
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
require 'eventmachine'
require 'em-http'
require 'fiber'
 
def async_fetch(url)
  f = Fiber.current
  http = EventMachine::HttpRequest.new(url).get :timeout => 10
  http.callback { f.resume(http) }
  http.errback { f.resume(http) }
 
  return Fiber.yield
end
 
start = Time.now
 
EventMachine.run do
  n = 0
  100.times do
    Fiber.new{
      data = async_fetch('http://localhost:5984/test/test')
      EventMachine.stop if (n+=1) == 100
    }.resume
  end
end
 
p Time.now - start
 
# slowest