-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuccesses.rb
134 lines (117 loc) · 3.1 KB
/
successes.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# http://www.ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html
# https://github.com/dbalatero/typhoeus
# http://codesnippets.joyent.com/posts/show/868
require "net/http"
require "typhoeus"
# from http://www.skorks.com/2010/03/timing-ruby-code-it-is-easy-with-benchmark/
def time_method(method=nil, *args)
beginning_time = Time.now
if block_given?
yield
else
self.send(method, args)
end
end_time = Time.now
puts "\nTime elapsed #{(end_time - beginning_time)} seconds"
end
# use a server that won't mind 4000 requests in < 1 minute
# like the sintra server here -- ruby -rubygems server.rb
random_uri = URI('http://localhost:4567/with_random_delay')
no_random_uri = URI('http://localhost:4567/with_no_random_delay')
timeout = 5 # seconds
requests = 400
puts "Net::HTTP"
# net/http
time_method do
requests.times do
request = Net::HTTP::Get.new(no_random_uri.path)
http = Net::HTTP.new(no_random_uri.host, no_random_uri.port)
http.read_timeout = timeout
response = http.start do |web|
web.request(request)
end
if response.is_a?(Net::HTTPSuccess)
print "."
else
print "#"
end
end
end
puts "\n\nTyphoeus (like net/http)"
time_method do
requests.times do
if Typhoeus::Request.get(no_random_uri.to_s).success?
print "."
else
print "#"
end
end
end
puts "\n\nTyphoeus 1 thread"
time_method do
hydra = Typhoeus::Hydra.new(:max_concurrency => 1)
hydra.disable_memoization
requests.times do
request = Typhoeus::Request.new(no_random_uri.to_s, :timeout => timeout*1000)
request.on_complete do |response|
if response.success?
print "."
else
print "#"
end
end
hydra.queue(request)
end
hydra.run # this is a blocking call
end
puts "\n\nTyphoeus, up to 20 threads (no random delay)"
time_method do
hydra = Typhoeus::Hydra.new(:max_concurrency => 20)
hydra.disable_memoization
requests.times do
request = Typhoeus::Request.new(no_random_uri.to_s, :timeout => timeout*1000)
request.on_complete do |response|
if response.success?
print "."
else
print "#"
end
end
hydra.queue(request)
end
hydra.run # this is a blocking call
end
puts "\n\nTyphoeus, up to 20 threads (random delay)"
time_method do
hydra = Typhoeus::Hydra.new(:max_concurrency => 20)
hydra.disable_memoization
requests.times do
request = Typhoeus::Request.new(random_uri.to_s, :timeout => timeout*1000)
request.on_complete do |response|
if response.success?
print "."
else
print "#"
end
end
hydra.queue(request)
end
hydra.run # this is a blocking call
end
puts "\n\nTyphoeus, up to 20 threads (random delay, memoization)"
time_method do
hydra = Typhoeus::Hydra.new(:max_concurrency => 20)
# hydra.disable_memoization
requests.times do
request = Typhoeus::Request.new(random_uri.to_s, :timeout => timeout*1000)
request.on_complete do |response|
if response.success?
print "."
else
print "#"
end
end
hydra.queue(request)
end
hydra.run # this is a blocking call
end