Skip to content

Commit dff8d0c

Browse files
committed
Add client request benchmark/comparison.
1 parent 4033130 commit dff8d0c

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

benchmark/async/http/client.rb

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# frozen_string_literal: true
2+
3+
require "sus/fixtures/async/http"
4+
require "sus/fixtures/benchmark"
5+
require "net/http"
6+
require "uri"
7+
8+
describe Async::HTTP::Client do
9+
include Sus::Fixtures::Async::HTTP::ServerContext
10+
include Sus::Fixtures::Benchmark
11+
12+
let(:count) {100}
13+
14+
RESPONSE_DATA = "x" * 1024 * 1024 * 2 # 2MB
15+
16+
def app
17+
Protocol::HTTP::Middleware.for do |request|
18+
# sleep 0.001 # Simulate some work.
19+
20+
Protocol::HTTP::Response[
21+
200,
22+
{
23+
"content-type" => "text/plain",
24+
"cache-control" => "no-cache, no-store",
25+
},
26+
RESPONSE_DATA
27+
]
28+
end
29+
end
30+
31+
with Thread do
32+
measure Net::HTTP do |repeats|
33+
uri = URI(self.bound_url)
34+
repeats.times do
35+
threads = []
36+
37+
count.times do
38+
threads << Thread.new do
39+
http = Net::HTTP.new(uri.host, uri.port)
40+
41+
response = http.get("/")
42+
response.body.length
43+
ensure
44+
http.finish rescue nil
45+
end
46+
end
47+
48+
threads.each(&:join)
49+
end
50+
end
51+
end
52+
53+
with Async do
54+
measure Net::HTTP do |repeats|
55+
uri = URI(self.bound_url)
56+
repeats.times do
57+
tasks = []
58+
59+
count.times do
60+
tasks << Async do
61+
http = Net::HTTP.new(uri.host, uri.port)
62+
63+
response = http.get("/")
64+
65+
ensure
66+
http.finish rescue nil
67+
end
68+
end
69+
70+
results = tasks.map(&:wait)
71+
end
72+
end
73+
74+
measure Async::HTTP do |repeats|
75+
repeats.times do
76+
tasks = []
77+
78+
count.times do
79+
tasks << Async do
80+
response = client.get("/")
81+
body = response.read
82+
body.length
83+
end
84+
end
85+
86+
results = tasks.map(&:wait)
87+
end
88+
end
89+
end
90+
end

0 commit comments

Comments
 (0)