-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.rb
More file actions
96 lines (81 loc) · 2.64 KB
/
benchmark.rb
File metadata and controls
96 lines (81 loc) · 2.64 KB
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
require 'benchmark'
require 'json'
require 'typhoeus'
require 'pry'
numtimes = 10 # How many reads and writes?
# =================== POST Benchmark ====================
hydra = Typhoeus::Hydra.hydra # A hundred serpents is better than one
total = 0 # Total time (updates after each request)
url = "http://0.0.0.0:9292/orders"
uuids = [] # UUIDs of newly created records
puts '=' * url.length
puts url
(1..numtimes.to_i).each do |n|
# Compose the request
request = Typhoeus::Request.new(
url,
method: :post,
body: {"item_name" => "Item #{n}", "quantity" => n, "price" => n * 10}
)
# Request callbacks
request.on_complete do |response|
if response.success?
# hell yeah
puts "POST #{url} - #{n}: #{(response.total_time - total).round(4)}s"
total = response.total_time
order = JSON.parse(response.body)
uuids.push(order['uuid'])
elsif response.timed_out?
# aw hell no
puts "Got a time out"
elsif response.code == 0
# Could not get an http response, something's wrong.
puts "Could not get an HTTP response, message was: #{response.return_message}"
else
# Received a non-successful http response.
puts "HTTP request failed: #{response.code.to_s}"
end
end
# Queue the request
hydra.queue(request);
# binding.pry
end
hydra.run # Release the Hydra!
# binding.pry
puts '=' * url.length
puts "Average for #{url} : #{(total / numtimes.to_i).round(4)}s or #{1 / (total / numtimes.to_i).round(4)} reqs/sec."
puts "\n"
# =================== GET Benchmark ====================
hydra = Typhoeus::Hydra.hydra # A hundred serpents is better than one
total = 0 # Reset the total time counter
(uuids).each do |n|
# Compose the request
request = Typhoeus::Request.new(
url + '/' + n,
method: :get
)
# Request callbacks
request.on_complete do |response|
if response.success?
# hell yeah
order = JSON.parse(response.body)
puts "GET #{url} - #{order['uuid']}: #{(response.total_time - total).round(4)}s"
total = response.total_time
elsif response.timed_out?
# aw hell no
puts "Got a time out"
elsif response.code == 0
# Could not get an http response, something's wrong.
puts "Could not get an HTTP response, message was: #{response.return_message}"
else
# Received a non-successful http response.
puts "HTTP request failed: #{response.code.to_s}"
end
end
# Queue the request
hydra.queue(request);
end
hydra.run # Release the Hydra!
puts '=' * url.length
puts "Average for #{url} : #{(total / numtimes.to_i).round(4)}s or #{1 / (total / numtimes.to_i).round(4)} reqs/sec."
puts "\n"