Skip to content

Instantly share code, notes, and snippets.

@jszmajda
Created February 10, 2011 22:26
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jszmajda/821485 to your computer and use it in GitHub Desktop.
Save jszmajda/821485 to your computer and use it in GitHub Desktop.
a benchmark to test Net::HTTP, HTTParty, Curb, and system curl
require 'rubygems'
require 'httparty'
require 'curb'
require 'net/http'
require 'benchmark'
include Benchmark
RUNS = 1000
url = 'http://localhost:4567/'
puts "Measuring GETs"
Benchmark.benchmark(" "*10 + CAPTION, 10, FMTSTR) do |x|
x.report("Net::HTTP") do
RUNS.times do
# NOTE: This works slightly faster without URI.parse, but I'm trying to make this a little more real-world.
Net::HTTP.get URI.parse(url)
end
end
x.report("HTTParty") do
RUNS.times do
HTTParty.get(url)
end
end
x.report("Curb") do
RUNS.times do
Curl::Easy.perform(url)
end
end
# NOTE: This situation might not be typical, but it is a lot faster.
x.report("Curb-reuse") do
c = Curl::Easy.new
RUNS.times do
c.url = url
c.perform
end
end
x.report("curl") do
RUNS.times do
`curl "#{url}" 2>/dev/null`
end
end
end
params = {'q' => 'test'}
puts "Measuring POSTs"
Benchmark.benchmark(" "*10 + CAPTION, 10, FMTSTR) do |x|
x.report("Net::HTTP") do
RUNS.times do
Net::HTTP.post_form URI.parse(url), params
end
end
x.report("HTTParty") do
RUNS.times do
HTTParty.post(url, :body => params)
end
end
x.report("Curb") do
curb_params = params.collect{|(k,v)| Curl::PostField.content(k, v) }
RUNS.times do
Curl::Easy.http_post(url, curb_params)
end
end
x.report("Curb-reuse") do
c = Curl::Easy.new
curb_params = params.collect{|(k,v)| Curl::PostField.content(k, v) }
RUNS.times do
c.url = url
c.http_post curb_params
end
end
x.report("curl") do
curl_params = params.collect{|(k,v)| "-F \"#{k}=#{v}\"" }.join(" ")
RUNS.times do
`curl -X POST -s #{curl_params} "#{url}" 2>/dev/null`
end
end
end
@AleksandrDarmeiko
Copy link

I have error:
test_http_clients.rb:12:in `

': uninitialized constant FMTSTR (NameError)

@kennuzzo
Copy link

kennuzzo commented Nov 9, 2013

in ruby 2 FMTSTR is replaced by FORMAT
the default value if nil is given is :

"%10.6u %10.6y %10.6t %10.6r\n"

you can check an example here:

http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html

and the format here:

http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark/Tms.html#method-i-format

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