File: c10k_spec.rb

package info (click to toggle)
ruby-async-io 1.34.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 424 kB
  • sloc: ruby: 3,103; makefile: 4
file content (138 lines) | stat: -rw-r--r-- 3,867 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# frozen_string_literal: true

# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require 'async/io'
require 'benchmark'
require 'open3'

# require 'ruby-prof'

RSpec.describe "c10k echo client/server", if: Process.respond_to?(:fork) do
	# macOS has a rediculously hard time to do this.
	# sudo sysctl -w net.inet.ip.portrange.first=10000
	# sudo sysctl -w net.inet.ip.portrange.hifirst=10000
	# Probably due to the use of select.
	
	let(:repeats) do
		if limit = Async::IO.file_descriptor_limit
			if limit > 1024*10
				10_000
			else
				[1, limit - 100].max
			end
		else
			10_000
		end
	end
	
	let(:server_address) {Async::IO::Address.tcp('0.0.0.0', 10101)}
	
	def echo_server(server_address)
		Async do |task|
			connections = []
			
			Async::IO::Socket.bind(server_address) do |server|
				server.listen(Socket::SOMAXCONN)
				
				while connections.size < repeats
					peer, address = server.accept
					connections << peer
				end
			end.wait
			
			Console.logger.info("Releasing #{connections.size} connections...")
			
			while connection = connections.pop
				connection.write(".")
				connection.close
			end
		end
	end
	
	def echo_client(server_address, data, responses)
		Async do |task|
			begin
				Async::IO::Socket.connect(server_address) do |peer|
					responses << peer.read(1)
				end
			rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EADDRINUSE
				Console.logger.warn(data, $!)
				# If the connection was refused, it means the server probably can't accept connections any faster than it currently is, so we simply retry.
				retry
			end
		end
	end
	
	def fork_server
		pid = fork do
			# profile = RubyProf::Profile.new(merge_fibers: true)
			# profile.start
			
			echo_server(server_address)
		# ensure
		# 	result = profile.stop
		# 	printer = RubyProf::FlatPrinter.new(result)
		# 	printer.print(STDOUT)
		end

		yield
	ensure
		Process.kill(:KILL, pid)
		Process.wait(pid)
	end
	
	around(:each) do |example|
		duration = Benchmark.realtime do
			example.run
		end
		
		example.reporter.message "Handled #{repeats} connections in #{duration.round(2)}s: #{(repeats/duration).round(2)}req/s"
	end
	
	it "should wait until all clients are connected" do
		fork_server do
			# profile = RubyProf::Profile.new(merge_fibers: true)
			# profile.start
			
			Async do |task|
				responses = []
				
				tasks = repeats.times.map do |i|
					# puts "Starting client #{i} on #{task}..." if (i % 1000) == 0
					
					echo_client(server_address, "Hello World #{i}", responses)
				end
				
				# task.reactor.print_hierarchy
				
				tasks.each(&:wait)
				
				expect(responses.size).to be repeats
			end
			
		# ensure
		# 	result = profile.stop
		# 	printer = RubyProf::FlatPrinter.new(result)
		# 	printer.print(STDOUT)
		end
	end
end