Skip to content

Instantly share code, notes, and snippets.

@jsuchal
Forked from karmi/tire_http_clients_benchmark.rb
Created December 31, 2011 01:04
Show Gist options
  • Save jsuchal/1542321 to your computer and use it in GitHub Desktop.
Save jsuchal/1542321 to your computer and use it in GitHub Desktop.
Benchmark Tire gem with RestClient, Curb and Thrift Clients
================================================================================
["RestClient", 5.152005974]
--------------------------------------------------------------------------------
["Curb", 2.073422755]
--------------------------------------------------------------------------------
["Thrift", 3.357725494]
--------------------------------------------------------------------------------
ruby-prof
87.42% 1.14% 6.79 0.09 0.00 6.70 7806 Tire::Search::Search#perform 72
3.80 0.01 0.00 3.79 2602/2602 <Class::Tire::HTTP::Client::RestClient>#get 73
1.75 0.01 0.00 1.74 2602/2602 Tire::HTTP::Client::Thrift#get 73
0.47 0.02 0.00 0.45 2602/2602 <Class::Tire::HTTP::Client::Curb>#get
module Tire
module HTTP
module Client
class Thrift
def initialize(host = 'localhost', port = 9500)
socket = ::Thrift::Socket.new(host, port)
transport = ::Thrift::BufferedTransport.new(socket)
protocol = ::Thrift::BinaryProtocolAccelerated.new(transport)
@client = ElasticSearch::Thrift::Rest::Client.new(protocol)
transport.open # TODO close?
end
def get(url, data = nil)
if data
execute(ElasticSearch::Thrift::Method::POST, url, data)
else
execute(ElasticSearch::Thrift::Method::GET, url)
end
end
def post(url, data)
execute(ElasticSearch::Thrift::Method::POST, url, data)
end
def put(url, data)
execute(ElasticSearch::Thrift::Method::PUT, url, data)
end
def delete(url)
execute(ElasticSearch::Thrift::Method::DELETE, url)
end
def head(url)
execute(ElasticSearch::Thrift::Method::HEAD, url)
end
private
def execute(method, uri, body = nil)
uri.gsub!('http://localhost:9200', '')
request = ElasticSearch::Thrift::RestRequest.new(:method => method, :uri => uri, :body => body || '')
response = @client.execute(request)
Response.new response.body, response.status
end
end
end
end
end
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
require 'rubygems'
require 'yajl/json_gem'
require 'benchmark'
require 'tire'
# ===============================
# Require the Curb implementation
#
require 'curb'
require 'tire/http/clients/curb'
# ===============================
# ===============================
# Require the Thrift implementation
#
require 'thrift'
require 'tire/http/clients/thrift'
# ===============================
extend Tire::DSL
content = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
documents = (1..1000).map { |i| { :id => i, :title => "Document #{i}", :content => content } }
curb_elapsed = restclient_elapsed = thrift_elapsed = 0
thrift_elapsed = Benchmark.realtime do
configure do
# logger STDOUT
client Tire::HTTP::Client::Thrift.new
end
index 'import-thrift' do
delete
create
import documents, nil, :per_page => 100
refresh
end
s = search('import-thrift') { query { all } }
s = search('import-thrift') { query { string 'hey' } }
100.times do
('a'..'z').each do |letter|
search('import-thrift') { query { string letter } }
end
end
# p s.results
end
curb_elapsed = Benchmark.realtime do
configure do
# logger STDOUT
client Tire::HTTP::Client::Curb
end
index 'import-curb' do
delete
create
import documents, nil, :per_page => 100
refresh
end
s = search('import-curb') { query { all } }
s = search('import-curb') { query { string 'hey' } }
100.times do
('a'..'z').each do |letter|
search('import-curb') { query { string letter } }
end
end
# p s.results
end
restclient_elapsed = Benchmark.realtime do
configure do
# logger STDOUT
client Tire::HTTP::Client::RestClient
end
index 'import-restclient' do
delete
create
import documents, nil, :per_page => 100
refresh
end
s = search('import-restclient') { query { all } }
s = search('import-restclient') { query { string 'hey' } }
100.times do
('a'..'z').each do |letter|
search('import-restclient') { query { string letter } }
end
end
# p s.results
end
puts '='*80
p ["RestClient", restclient_elapsed]
puts '-'*80, ""
p ["Curb", curb_elapsed]
puts '-'*80, ""
p ["Thrift", thrift_elapsed]
puts '-'*80, ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment