-
Notifications
You must be signed in to change notification settings - Fork 548
/
unix_sockets_vs_pipes.rb
executable file
·83 lines (76 loc) · 1.51 KB
/
unix_sockets_vs_pipes.rb
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
#!/usr/bin/env ruby
# Benchmarks raw Unix socket I/O performance versus pipe I/O performance.
require 'benchmark'
ITERATIONS = 150000
def start
if ARGV.empty?
benchmark(:unix_sockets)
benchmark(:pipes)
elsif ARGV.size == 1 && ARGV[0] == 'unix_sockets'
benchmark(:unix_sockets)
elsif ARGV.size == 1 && ARGV[0] == 'pipes'
benchmark(:pipes)
else
puts "Benchmarks raw Unix socket I/O performance versus pipe I/O performance."
puts "Usage: unix_sockets_vs_pipes.rb <unix_sockets|pipes>"
exit(1)
end
end
def benchmark(type)
if type == :unix_sockets
puts "Benchmarking Unix sockets..."
pid, reader, writer = setup_unix_sockets
else
puts "Benchmarking pipes..."
pid, reader, writer = setup_pipes
end
result = Benchmark.measure do
ITERATIONS.times do |i|
writer.write("hello world\n")
reader.readline
end
reader.close
if reader != writer
writer.close
end
Process.waitpid(pid)
end
puts "User/system/real time: #{result}"
printf "%.2f lines per second\n", ITERATIONS / result.real
end
def setup_unix_sockets
require 'socket'
a, b = UNIXSocket.pair
pid = fork do
a.close
child_main_loop(b, b)
end
b.close
return [pid, a, a]
end
def setup_pipes
a, b = IO.pipe
c, d = IO.pipe
pid = fork do
b.close
c.close
reader = a
writer = d
child_main_loop(reader, writer)
end
a.close
d.close
reader = c
writer = b
return [pid, reader, writer]
end
def child_main_loop(reader, writer)
begin
while true
x = reader.readline
writer.write(x)
end
rescue EOFError
end
end
start