Skip to content

Commit e444027

Browse files
committed
Benchmark experimentation.
1 parent 710920c commit e444027

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

benchmark/server/compiled.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <sys/types.h>
44
#include <sys/socket.h>
55
#include <netinet/in.h>
6+
#include <arpa/inet.h>
67
#include <unistd.h>
78
#include <string.h>
89

@@ -23,7 +24,11 @@ int main (int argc, char *argv[]) {
2324

2425
server.sin_family = AF_INET;
2526
server.sin_port = htons(port);
26-
server.sin_addr.s_addr = htonl(INADDR_ANY);
27+
// server.sin_addr.s_addr = htonl(INADDR_ANY);
28+
if (inet_pton(AF_INET, "127.0.0.1", &server.sin_addr) <= 0) {
29+
perror("inet_pton");
30+
exit(EXIT_FAILURE);
31+
}
2732

2833
int opt_val = 1;
2934
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt_val, sizeof opt_val);
@@ -45,7 +50,8 @@ int main (int argc, char *argv[]) {
4550

4651
if (client_fd < 0) on_error("Could not establish new connection\n");
4752

48-
recv(client_fd, buf, BUFFER_SIZE, 0);
53+
// Removing this line entirely gives us the best performance, but obviously we are just blasting out a response without considering the request. Setting it to MSG_DONTWAIT gives us the second best performance, but we are still reading the request but probably not all of it.
54+
recv(client_fd, buf, BUFFER_SIZE, MSG_DONTWAIT);
4955
send(client_fd, response, response_size, 0);
5056

5157
close(client_fd);

benchmark/server/config.ru

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# frozen_string_literal: true
2+
3+
run lambda{|env| [204, {}, []]}

benchmark/server/loop.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
# Copyright, 2021-2025, by Samuel Williams.
66

77
require "socket"
8+
require "time"
89

10+
# Assuming request per connection:
911
RESPONSE = "HTTP/1.1 204 No Content\r\nConnection: close\r\n\r\n"
1012

1113
port = Integer(ARGV.pop || 9090)
@@ -14,9 +16,14 @@
1416
loop do
1517
peer, address = server.accept
1618

17-
while (peer.recv(1024) rescue nil)
18-
peer.send(RESPONSE, 0)
19-
end
19+
# This is by far the fastest path, clocking in at around 80,000 requests per second in a single thread.
20+
peer.recv(1024) rescue nil
21+
peer.send(RESPONSE, 0)
22+
23+
# This drops us to about 1/4 the performance due to the overhead of blocking operations.
24+
# while (peer.recv(1024) rescue nil)
25+
# peer.send(RESPONSE, 0)
26+
# end
2027

2128
peer.close
2229
end

0 commit comments

Comments
 (0)