Skip to content

Commit 308ec7a

Browse files
committed
Add category "thread"
`./run_benchmarks --category thread` now runs all benchmarks from benchmarks.yml with the category of "thread". Added a benchmark with this new category for benchmarking how long it takes when there's mixed IO-bound and heavy CPU-bound work.
1 parent 81bb0f6 commit 308ec7a

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

benchmarks.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ throw:
235235
single_file: true
236236
ractor: true
237237

238+
#
239+
# Thread benchmarks
240+
#
241+
threads_io_with_cpu:
242+
desc: Benchmark to test how long I/O takes to complete when there's a mix of IO-bound and CPU-bound threads.
243+
category: thread
244+
single_file: true
245+
238246
#
239247
# Ractor scaling benchmarks
240248
#

benchmarks/threads_io_with_cpu.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
require_relative '../harness/loader'
2+
3+
require "socket"
4+
num_cpu_threads = 1
5+
num_io_threads = 1
6+
7+
server_started = false
8+
9+
def start_tcp_server(&started)
10+
Thread.new do
11+
server = TCPServer.new('localhost', 0) # random open port
12+
started.call(server.local_address.ip_port)
13+
14+
loop do
15+
client = server.accept
16+
client.close
17+
# Don't get request or generate response, it makes the benchmark take too long
18+
end
19+
end
20+
end
21+
22+
def open_tcp_connection(host, port)
23+
TCPSocket.open(host, port) { }
24+
end
25+
26+
def fib(n)
27+
return n if n <= 1
28+
fib(n - 1) + fib(n - 2)
29+
end
30+
31+
server_started = false
32+
port = nil
33+
start_tcp_server do |server_port|
34+
server_started = true
35+
port = server_port
36+
end
37+
loop until server_started
38+
39+
io_requests_threshold = 5 # per thread
40+
41+
run_benchmark(5) do
42+
io_threads = num_io_threads.times.map do
43+
Thread.new do
44+
io_requests_made = 0
45+
loop do
46+
open_tcp_connection("localhost", port)
47+
io_requests_made += 1
48+
if io_requests_made >= io_requests_threshold
49+
break
50+
end
51+
end
52+
end
53+
end
54+
55+
stop_looping = false
56+
57+
cpu_threads = num_cpu_threads.times.map do
58+
Thread.new do
59+
loop do
60+
fib(30)
61+
break if stop_looping
62+
end
63+
end
64+
end
65+
66+
io_threads.each(&:join)
67+
stop_looping = true
68+
cpu_threads.join
69+
end

lib/argument_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def parse(argv)
6868
args.out_override = v
6969
end
7070

71-
opts.on("--category=headline,other,micro,ractor", "when given, only benchmarks with specified categories will run") do |v|
71+
opts.on("--category=headline,other,micro,thread,ractor", "when given, only benchmarks with specified categories will run") do |v|
7272
args.categories += v.split(",")
7373
if args.categories == ["ractor"]
7474
args.harness = "harness-ractor"

0 commit comments

Comments
 (0)