Skip to content

Instantly share code, notes, and snippets.

@gravis
Created July 3, 2010 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gravis/462623 to your computer and use it in GitHub Desktop.
Save gravis/462623 to your computer and use it in GitHub Desktop.
#! /usr/bin/rackup
#\ -w -p 8765
map '/' do
run Proc.new {|env|
req = Rack::Request.new(env)
if req.params['PARAM1'] and req.params['PARAM2'] and req.params['PARAM3'] and req.params['PARAM4'] and req.params['PARAM5'] and env['HTTP_HEADER1'] and env['HTTP_HEADER2']
[200, {"Content-Type" => "text/html"}, ["Data Sent"]]
else
puts req.params
[400, {"Content-Type" => "text/html"}, ["Bad request"]]
end
}
end
#! /usr/bin/env ruby
require 'rubygems'
require 'typhoeus'
multi = Typhoeus::Multi.new
failures = 0
2000.times.each do
req = Typhoeus::Easy.new
req.url = "http://localhost:8765"
req.method = :post
req.headers = {'HEADER1' => '1', 'HEADER2' => '1'}
req.params = {:PARAM1 => "something to send", :PARAM2 => '1', :PARAM3 => '1', :PARAM4 => '1', :PARAM5 => '1'}
req.on_failure do |response|
puts "Error #{response.inspect}"
failures += 1
end
req.set_headers
multi.add req
end
multi.perform
puts "Failed : #{failures}"
# Output :
#
# $ ruby typh.rb
# Error #<Typhoeus::Easy:0x7ffe74dde558>
# Error #<Typhoeus::Easy:0x7ffe74ddd838>
# Error #<Typhoeus::Easy:0x7ffe74ddcaa0>
# [...]
# Error #<Typhoeus::Easy:0x7ffe750d1a08>
# Failed : 1775
# $
#
#
# => More than 75% of the total requests are failing
# => With 1000 requests, ~500 are failing
#! /usr/bin/env ruby
require 'rubygems'
require 'typhoeus'
hydra = Typhoeus::Hydra.new(:max_concurrency => 20)
failures = 0
2000.times.each do
req = Typhoeus::Request.new("http://localhost:8765", :method => :post, :params => {:PARAM1 => "something to send", :PARAM2 => '1', :PARAM3 => '1', :PARAM4 => '1', :PARAM5 => '1'}, :headers => {'HEADER1' => '1', 'HEADER2' => '1'})
req.on_complete do |response|
if !response.success?
puts "Error #{response.body}"
failures += 1
end
end
hydra.queue req
end
hydra.run
puts "Failed : #{failures}"
# Output :
#
# $ ruby typhoeus_test.rb
# Error Bad request
# Error Bad request
# Error Bad request
# Error Bad request
# Error Bad request
# Error Bad request
# Error Bad request
# Error Bad request
# Error Bad request
# Error Bad request
# Error Bad request
# Failed : 11
# $
# $ ruby typhoeus_test.rb
# Error Bad request
# Error Bad request
# Error Bad request
# Error Bad request
# Error Bad request
# Failed : 5
#
# [...]
# (on rack side :)
#
# [...]
# 127.0.0.1 - - [03/Jul/2010 17:06:40] "POST / HTTP/1.1" 200 - 0.0007
# 127.0.0.1 - - [03/Jul/2010 17:06:40] "POST / HTTP/1.1" 200 - 0.0007
# PARAM1something to sendPARAM21@???1PARAM51
# 127.0.0.1 - - [03/Jul/2010 17:06:40] "POST / HTTP/1.1" 400 - 0.0271
# PARAM1something to sendPARAM21?@?1PARAM51
# 127.0.0.1 - - [03/Jul/2010 17:06:40] "POST / HTTP/1.1" 400 - 0.0009
# PARAM1something to sendPARAM21?v?1PARAM51
# 127.0.0.1 - - [03/Jul/2010 17:06:40] "POST / HTTP/1.1" 400 - 0.0008
# 0?j91PARAM1something to sendPARAM21PARAM51
# 127.0.0.1 - - [03/Jul/2010 17:06:40] "POST / HTTP/1.1" 400 - 0.0008
# 127.0.0.1 - - [03/Jul/2010 17:06:40] "POST / HTTP/1.1" 200 - 0.0008
# [...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment