Skip to content
Browse files

Fix last bits for headers.

  • Loading branch information...
1 parent da08bef commit 9f010a0444049e9da0daa55e0f9fc18be3f67c39 @i0rek i0rek committed
Showing with 95 additions and 3 deletions.
  1. +1 −1 lib/typhoeus/response.rb
  2. +2 −2 lib/typhoeus/response/header.rb
  3. +92 −0 test.rb
View
2 lib/typhoeus/response.rb
@@ -39,7 +39,7 @@ class Response
# @return [ Response ] The new response.
def initialize(options = {})
@options = options
- @headers = Header.new(options[:headers])
+ @headers = Header.new(options[:headers]) if options[:headers]
end
# Returns wether this request is mocked
View
4 lib/typhoeus/response/header.rb
@@ -27,7 +27,7 @@ def initialize(raw)
# @example Parse header.
# header.parse
def parse
- case raw
+ case @raw
when Hash
raw.each do |k, v|
process_pair(k, v)
@@ -77,7 +77,7 @@ def set_value(key, value, hash)
#
# @return [ String ] The raw header.
def raw
- @raw ||= ''
+ @raw || ''
end
end
end
View
92 test.rb
@@ -0,0 +1,92 @@
+require 'typhoeus'
+
+class Requests
+ attr_accessor :requests
+
+ def initialize
+ @requests = []
+ end
+
+ # add navbar to the request bundle
+ def add request
+ @requests.push request
+ request
+ end
+
+ # run multiple requests in parallel
+ def run
+ hydra = Typhoeus::Hydra.hydra
+ @requests.each{ |request| hydra.queue request }
+ hydra.run
+ end
+
+end
+
+
+Typhoeus.configure do |config|
+ config.verbose = false
+end
+
+def get_my_request url, forbid_reuse_val, &block
+ request = Typhoeus::Request.new(url, forbid_reuse: forbid_reuse_val)
+ request.on_complete { |response| yield response } if block
+ request
+end
+
+def self.memory_usage
+ `ps -o rss= -p #{Process.pid}`.to_i
+end
+
+requests_per_iteration_one = 2
+requests_per_iteration_two = 2
+single_requests_per_iteration = 2
+forbid_reuse_val = true
+memory_interval = 10
+j = 1
+
+1_000.times do
+ multi_requests_one = Requests::new
+ req1 = get_my_request "http://www.google.com/#q=stuff#{j}", forbid_reuse_val do |response|
+ puts "----------------------#{response.body.length} #{response.request.url}"
+ end
+ req2 = get_my_request "http://www.bing.com/search?q=stuff#{j}", forbid_reuse_val do |response|
+ puts "----------------------#{response.body.length} #{response.request.url}"
+ end
+ req3 = get_my_request "http://www.google.com/#q=sturf#{j*1000}", forbid_reuse_val do |response|
+ puts "----------------------#{response.body.length} #{response.request.url}"
+ end
+ req4 = get_my_request "http://www.bing.com/search?q=sturf#{j*1000}", forbid_reuse_val do |response|
+ puts "----------------------#{response.body.length} #{response.request.url}"
+ end
+ multi_requests_one.add(req1)
+ multi_requests_one.add(req2)
+ multi_requests_one.add(req3)
+ multi_requests_one.add(req4)
+ puts "running multi_requests_one #{j}"
+ multi_requests_one.run
+
+ multi_requests_two = Requests::new
+ req1 = get_my_request"http://search.aol.com/aol/search?q=stuff#{j}", forbid_reuse_val do |response|
+ puts "----------------------#{response.body.length} #{response.request.url}"
+ end
+ req2 = get_my_request"http://www.yelp.com/search?find_desc=Thai-#{j}", forbid_reuse_val do |response|
+ puts "----------------------#{response.body.length} #{response.request.url}"
+ end
+ multi_requests_two.add(req1)
+ multi_requests_two.add(req2)
+ puts "running multi_requests_two #{j}"
+ multi_requests_two.run
+
+ single_requests_per_iteration.times do |k|
+ url = "http://www.google.com/#q=stiff#{j+k}"
+ request = get_my_request url, forbid_reuse_val do |response|
+ puts "----------------------#{response.body.length} #{response.request.url}"
+ end
+ puts "running single_requests #{j} #{k}"
+ request.run
+ end
+ iteration = (j+=single_requests_per_iteration)/single_requests_per_iteration
+ kilos = memory_usage if (iteration % memory_interval) == 0
+ puts "#{iteration}- Memory usage: #{kilos/1024} MB - #{kilos} KB | PID: #{Process.pid}" if (iteration % memory_interval) == 0
+ GC.start
+end

0 comments on commit 9f010a0

Please sign in to comment.
Something went wrong with that request. Please try again.