Skip to content

Instantly share code, notes, and snippets.

@mattetti
Created May 11, 2009 07:35
Show Gist options
  • Save mattetti/109902 to your computer and use it in GitHub Desktop.
Save mattetti/109902 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'json'
require 'typhoeus'
require 'net/http'
class CouchDB
include Typhoeus
remote_defaults :on_success => lambda {|response| JSON.parse(response.body)},
:on_failure => lambda {|response| puts "error code: #{response.code}"},
:base_uri => "http://127.0.0.1:5984/couch-test-db"
define_remote_method :doc, :path => '/document-id'
end
tstart_time = Time.now
100.times do
p CouchDB.doc.to_s.to_f
end
puts "sleep 45s"
sleep 45
100.times do
p CouchDB.doc.to_s.to_f
end
ttime = Time.now - tstart_time
nstart_time = Time.now
def connection
Net::HTTP.new("127.0.0.1", 5984)
end
def request
@request ||= Proc.new { @http.request(Net::HTTP::Get.new("/couch-test-db/document-id")) }
end
@net = connection
@http = @net.start
def get_doc
begin
res = request.call
rescue Errno::ECONNRESET
p "rescue"
@net = connection
@http = @net.start
res = request.call
end
p JSON.parse(res.body).to_s.to_f
end
100.times do
get_doc
end
puts 'sleep 45s'
# forcing a connection error rescue
sleep 45
100.times do
get_doc
end
etime = Time.now - nstart_time
p "typhoeus total time: #{ttime}"
p "net/http total time: #{etime}"
p "net/http was #{etime - ttime}s faster/slower"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment