Created
June 3, 2022 06:42
-
-
Save ioquatix/d7bc41e5c2adcc9c32525440b7cec718 to your computer and use it in GitHub Desktop.
Small stress test of async-http server (unimportant), client and barrier with timeout.
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
#!/usr/bin/env ruby | |
require 'async/container' | |
require 'async/barrier' | |
require 'async/http' | |
require 'async/io/shared_endpoint' | |
Console.logger.debug! | |
endpoint = Async::HTTP::Endpoint.parse('http://127.0.0.1:9294') | |
# A fake server which has a random short delay before responding. | |
bound_endpoint = Sync{Async::IO::SharedEndpoint.bound(endpoint)} | |
app = lambda do |request| | |
Async::Task.current.sleep(rand*0.5) | |
Protocol::HTTP::Response[200, {}, ["Hello World"]] | |
end | |
container = Async::Container::Threaded.new | |
container.run do | |
server = Async::HTTP::Server.new(app, bound_endpoint, protocol: endpoint.protocol, scheme: endpoint.scheme) | |
Async do | |
server.run | |
end | |
end | |
# A client which performs batches of 32 requests in a fan-out style using discrete clients: | |
container.run do | |
Async do |task| | |
while true | |
barrier = Async::Barrier.new | |
responses = [] | |
32.times do | |
barrier.async do | |
client = Async::HTTP::Client.new(endpoint) | |
begin | |
response = client.get("/") | |
responses << response.read | |
ensure | |
response&.close | |
end | |
ensure | |
client.close | |
end | |
end | |
task.with_timeout(0.4) do | |
barrier.wait | |
rescue Async::TimeoutError | |
barrier.stop | |
end | |
Console.logger.info(task, "responses: #{responses.count}") | |
end | |
end | |
end | |
# Wait for the experiment to finish: | |
container.wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment