Created
November 15, 2009 22:05
-
-
Save nicck/235504 to your computer and use it in GitHub Desktop.
test eventmachine http get
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rubygems' | |
| require 'eventmachine' | |
| require 'benchmark' | |
| require 'net/http' | |
| require 'uri' | |
| CHECK_CONCURENCY = false | |
| REQUESTS_COUNT = 10 | |
| # if code run concurently => wired output; '0123456789-' if executed in one thread | |
| def check_concurency | |
| return unless CHECK_CONCURENCY | |
| ('0'..'9').each do |d| | |
| print d | |
| sleep rand(5)/10.0 | |
| end | |
| print '-' | |
| end | |
| count = REQUESTS_COUNT | |
| puts "\nthreads: " | |
| puts Benchmark.measure { | |
| thread_group = ThreadGroup.new | |
| count.times do |i| | |
| thread = Thread.new(i) do |i| | |
| time_start = Time.now | |
| Net::HTTP.get('ya.ru', '/') | |
| check_concurency | |
| puts "requ ##{i}: %.3f sec" % (Time.now - time_start) | |
| end | |
| thread_group.add(thread) | |
| end | |
| thread_group.list.each {|t| t.join } | |
| } | |
| count = REQUESTS_COUNT | |
| puts "\nevented http: " | |
| puts Benchmark.measure { | |
| EM.run do | |
| count.times do |i| | |
| time_start = Time.now | |
| http = EM::Protocols::HttpClient.request(:host=>"ya.ru", :port=>80, :request=>"/") | |
| http.callback do |response| | |
| # puts response[:status] | |
| puts "requ ##{i}: %.3f sec" % (Time.now - time_start) | |
| check_concurency | |
| count -= 1 | |
| EM.stop if count == 0 | |
| end | |
| end | |
| end | |
| } | |
| count = REQUESTS_COUNT | |
| puts "\nevented http2: " | |
| puts Benchmark.measure { | |
| EM.run do | |
| conn = EM::Protocols::HttpClient2.connect('ya.ru', 80) | |
| count.times do |i| | |
| time_start = Time.now | |
| req = conn.get('/') | |
| req.callback do |response| | |
| check_concurency | |
| puts "requ ##{i}: %.3f sec" % (Time.now - time_start) | |
| count -= 1 | |
| EM.stop if count == 0 | |
| end | |
| end | |
| end | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment