Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Created August 12, 2012 09:08
Show Gist options
  • Save francescoagati/3330766 to your computer and use it in GitHub Desktop.
Save francescoagati/3330766 to your computer and use it in GitHub Desktop.
celluloid-io and faraday
require 'celluloid/io'
require 'faraday'
require 'benchmark'
class HttpClient
include Celluloid::IO
attr_accessor :conn
def initialize()
@conn = Faraday.new(:url => 'http://localhost:8888') do |faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :logger # log requests to STDOUT
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
end
def get_data
conn.get '/MAMP'
end
end
conn= Faraday.new(:url => 'http://localhost:8888') do |faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :logger # log requests to STDOUT
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
rt1=Benchmark.measure {
(0..100).map do |n|
conn.get('/MAMP').body
end
}
rt2=Benchmark.measure do
pool = HttpClient.pool(size: 30)
(0..100).to_a.map { |n| pool.future(:get_data) }.map {|f| f.value.body }
end
p rt1
p rt2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment