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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
require 'benchmark'
require 'socket'
require 'fileutils'
DEPS = %w[rugged linguist]
N = 100
FORK_SOCKET = 'fork.socket'
PRE_FORK_SOCKET = 'pre-fork.socket'
THREAD_SOCKET = 'thread.socket'
OID_REGEX = /^[a-f0-9]{40}$/
class ForkServer < Struct.new(:socket_path)
def run
DEPS.each { |dep| require(dep) }
# Every request creates a child process which becomes a zombie when it's
# done. It's our job to reap them.
Thread.new do
loop do
Process.wait
rescue
sleep 0.1
end
end
FileUtils.rm_f(socket_path)
l = Socket.unix_server_socket(socket_path)
loop do
conn, _addrinfo = l.accept
fork do
l.close
repo = Rugged::Repository.new('.')
conn.write(repo.head.target.oid)
end
conn.close
end
end
end
class ThreadServer < Struct.new(:socket_path)
def run
DEPS.each { |dep| require(dep) }
FileUtils.rm_f(socket_path)
l = Socket.unix_server_socket(socket_path)
loop do
conn, _addrinfo = l.accept
Thread.new do
repo = Rugged::Repository.new('.')
conn.write(repo.head.target.oid)
repo.close # simulate cleanup, needed in multi-threaded process
conn.close
end
end
end
end
class PreForkServer < Struct.new(:socket_path)
WORKERS = 2
REUSE = 10
def run
DEPS.each { |dep| require(dep) }
FileUtils.rm_f(socket_path)
listener = Socket.unix_server_socket(socket_path)
WORKERS.times.map { fork { handle(listener, Process.pid) } }
loop do
Process.wait
fork { handle(listener, Process.pid) }
end
end
def handle(listener, ppid)
can_exit = false
mu = Mutex.new
Thread.new do
loop do
sleep 1
begin
Process.kill(0, ppid)
rescue
mu.synchronize { exit if can_exit }
end
end
end
REUSE.times do
mu.synchronize { can_exit = true }
conn, _addrinfo = listener.accept
mu.synchronize { can_exit = false }
repo = Rugged::Repository.new('.')
conn.write(repo.head.target.oid)
conn.close
end
end
end
def main
pids = [
fork { ForkServer.new(FORK_SOCKET).run },
fork { ThreadServer.new(THREAD_SOCKET).run },
fork { PreForkServer.new(PRE_FORK_SOCKET).run },
]
[FORK_SOCKET, THREAD_SOCKET, PRE_FORK_SOCKET].each do |sock|
try_connect(sock)
end
Benchmark.bm(15) do |x|
n_spawn = N / 10
x.report("pre-fork (#{N}):") { N.times { benchmark_socket(PRE_FORK_SOCKET) } }
x.report("fork (#{N}):") { N.times { benchmark_socket(FORK_SOCKET) } }
x.report("thread (#{N}):") { N.times { benchmark_socket(THREAD_SOCKET) } }
x.report("spawn (#{n_spawn}):") { n_spawn.times { benchmark_spawn } }
end
ensure
pids.each { |pid| Process.kill('KILL', pid) }
end
def benchmark_spawn
cmd = ['ruby', *DEPS.map { |d| "-r#{d}" }, '-e', <<~SCRIPT
repo = Rugged::Repository.new('.')
print repo.head.target.oid
SCRIPT
]
out = IO.popen(cmd, 'r', &:read)
raise 'command failed' unless $?.success?
raise "bad output #{out.inspect}" unless OID_REGEX =~ out
end
def benchmark_socket(socket)
Socket.unix(socket) do |conn|
out = conn.read
raise "bad output #{out.inspect}" unless OID_REGEX =~ out
end
end
def try_connect(sock)
500.times do
begin
UNIXSocket.new(sock).read
return
rescue Errno::ENOENT, Errno::ECONNREFUSED
sleep 0.1
end
end
UNIXSocket.new(sock).read
end
main
|