Closed thelinuxlich closed 3 years ago
I don't know if Crystal build-in HTTP::Client
module supports this. Keep in mind crest
is just a wrapper around HTTP::Client
How to test keep-alive is working on the client end?
Got it.
The Crest::Resource
object allows to use a persistent connection.
Using new connection for each HTTP request:
start_time = Time.utc
url = "https://httpbin.org/get"
50.times do
response = Crest.get(url)
json = JSON.parse(response.body)
puts json["origin"]
end
puts "--- #{Time.utc - start_time} seconds ---"
Using persistent connection for all HTTP requests:
start_time = Time.utc
client = Crest::Resource.new("https://httpbin.org")
path = "/get"
50.times do
response = client.get(path)
json = JSON.parse(response.body)
puts json["origin"]
end
puts "--- #{Time.utc - start_time} seconds ---"
I ran both scripts five times and the result was very interesting.
Average time with keep-alive/persistent connections: around 8 seconds Average time with new connections: around 30 seconds
If it has, how do we use it?