File: client.rb

package info (click to toggle)
ruby-async-http 0.94.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 916 kB
  • sloc: ruby: 5,224; javascript: 40; makefile: 4
file content (93 lines) | stat: -rw-r--r-- 1,683 bytes parent folder | download
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
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2025, by Samuel Williams.

require "sus/fixtures/async/http"
require "sus/fixtures/benchmark"
require "net/http"
require "uri"

describe Async::HTTP::Client do
	include Sus::Fixtures::Async::HTTP::ServerContext
	include Sus::Fixtures::Benchmark
	
	let(:count) {100}
	
	RESPONSE_DATA = "x" * 1024 * 1024 * 2 # 2MB
	
	def app
		Protocol::HTTP::Middleware.for do |request|
			# sleep 0.001 # Simulate some work.
			
			Protocol::HTTP::Response[
				200,
				{
					"content-type" => "text/plain",
					"cache-control" => "no-cache, no-store",
				},
				RESPONSE_DATA
			]
		end
	end
	
	with Thread do
		measure Net::HTTP do |repeats|
			uri = URI(self.bound_url)
			repeats.times do
				threads = []
				
				count.times do
					threads << Thread.new do
						http = Net::HTTP.new(uri.host, uri.port)
						
						response = http.get("/")
						response.body.length
					ensure
						http.finish rescue nil
					end
				end
				
				threads.each(&:join)
			end
		end
	end
	
	with Async do
		measure Net::HTTP do |repeats|
			uri = URI(self.bound_url)
			repeats.times do
				tasks = []
				
				count.times do
					tasks << Async do
						http = Net::HTTP.new(uri.host, uri.port)
						
						response = http.get("/")
						
					ensure
						http.finish rescue nil
					end
				end
				
				results = tasks.map(&:wait)
			end
		end
		
		measure Async::HTTP do |repeats|
			repeats.times do
				tasks = []
				
				count.times do
					tasks << Async do
						response = client.get("/")
						body = response.read
						body.length
					end
				end
				
				results = tasks.map(&:wait)
			end
		end
	end
end