Skip to content

Commit 19dbe27

Browse files
authored
Zap performance improvements (#9293)
1 parent 38c565e commit 19dbe27

File tree

10 files changed

+110
-67
lines changed

10 files changed

+110
-67
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
CPU_COUNT=$(nproc)
4+
P=3000
5+
END=$(($P+$CPU_COUNT))
6+
CONF=""
7+
8+
while [ $P -lt $END ]; do
9+
CONF+="\t\tserver 127.0.0.1:$P;\n"
10+
let P=P+1
11+
done
12+
13+
sed -i "s|# replace|$CONF|g" nginx.conf

frameworks/Zig/zap/build.zig

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,13 @@ pub fn build(b: *std.Build) !void {
4040

4141
const zap_module = b.dependency("zap", dep_opts).module("zap");
4242
const pg_module = b.dependency("pg", dep_opts).module("pg");
43-
const dig_module = b.dependency("dig", dep_opts).module("dns");
4443

4544
try modules.put("zap", zap_module);
4645
try modules.put("pg", pg_module);
47-
try modules.put("dig", dig_module);
4846

4947
// // Expose this as a module that others can import
5048
exe.root_module.addImport("zap", zap_module);
5149
exe.root_module.addImport("pg", pg_module);
52-
exe.root_module.addImport("dig", dig_module);
5350

5451
exe.linkLibrary(zap.artifact("facil.io"));
5552

@@ -80,20 +77,4 @@ pub fn build(b: *std.Build) !void {
8077
// This will evaluate the `run` step rather than the default, which is "install".
8178
const run_step = b.step("run", "Run the app");
8279
run_step.dependOn(&run_cmd.step);
83-
84-
// Creates a step for unit testing. This only builds the test executable
85-
// but does not run it.
86-
const unit_tests = b.addTest(.{
87-
.root_source_file = b.path("src/main.zig"),
88-
.target = target,
89-
.optimize = optimize,
90-
});
91-
92-
const run_unit_tests = b.addRunArtifact(unit_tests);
93-
94-
// Similar to creating the run step earlier, this exposes a `test` step to
95-
// the `zig build --help` menu, providing a way for the user to request
96-
// running the unit tests.
97-
const test_step = b.step("test", "Run unit tests");
98-
test_step.dependOn(&run_unit_tests.step);
9980
}

frameworks/Zig/zap/build.zig.zon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@
99
},
1010
.pg = .{ .url = "https://github.com/karlseguin/pg.zig/archive/239a4468163a49d8c0d03285632eabe96003e9e2.tar.gz",
1111
.hash = "1220a1d7e51e2fa45e547c76a9e099c09d06e14b0b9bfc6baa89367f56f1ded399a0" },
12-
.dig = .{ .url = "https://github.com/lun-4/zigdig/archive/a54c85c26aa83c64ee81e3ee1818890be5cbed0b.tar.gz", .hash = "1220f078ab62d1328339504f9122dc4d241be30ada451628d78b8a3bf5bb9be1dcba" },
1312
} }

frameworks/Zig/zap/nginx.conf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error_log stderr;
2+
worker_processes auto;
3+
4+
events {
5+
worker_connections 65535;
6+
multi_accept off;
7+
}
8+
9+
http {
10+
default_type application/octet-stream;
11+
client_body_temp_path /tmp;
12+
access_log off;
13+
14+
sendfile on;
15+
tcp_nopush on;
16+
keepalive_requests 100000;
17+
keepalive_timeout 65;
18+
19+
upstream workers {
20+
# replace
21+
}
22+
23+
server {
24+
listen 8080;
25+
server_name tfb-server;
26+
27+
location / {
28+
proxy_http_version 1.1;
29+
proxy_set_header Connection "";
30+
proxy_pass http://workers;
31+
}
32+
}
33+
}

frameworks/Zig/zap/run.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

frameworks/Zig/zap/src/endpoints.zig

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,28 @@ pub const DbEndpoint = struct {
168168
}
169169
}
170170

171-
// std.debug.print("Attempting to return random: {}\n", .{random_number});
172-
173171
if (random_number == 0) {
174172
return;
175173
}
176174

177-
var conn = pool.acquire() catch return;
178-
defer conn.release();
179-
180-
const row_result = conn.row("SELECT id, randomNumber FROM World WHERE id = $1", .{random_number}) catch |err| {
175+
const json_to_send = getJson(pool, random_number) catch |err| {
181176
std.debug.print("Error querying database: {}\n", .{err});
182177
return;
183178
};
179+
180+
req.sendBody(json_to_send) catch return;
181+
182+
return;
183+
}
184+
185+
fn getJson(pool: *pg.Pool, random_number: u32) ![]const u8{
186+
var conn = try pool.acquire();
187+
defer conn.release();
188+
189+
const row_result = try conn.row("SELECT id, randomNumber FROM World WHERE id = $1", .{random_number});
190+
184191
var row = row_result.?;
192+
defer row.deinit() catch {};
185193

186194
const world = World{ .id = row.get(i32, 0), .randomNumber = row.get(i32, 1) };
187195

@@ -193,9 +201,7 @@ pub const DbEndpoint = struct {
193201
json_to_send = "null";
194202
}
195203

196-
req.sendBody(json_to_send) catch return;
197-
198-
return;
204+
return json_to_send;
199205
}
200206
};
201207

frameworks/Zig/zap/src/main.zig

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const std = @import("std");
2+
const builtin = @import("builtin");
23
const zap = @import("zap");
34
const pg = @import("pg");
45
const regex = @import("regex");
5-
const dns = @import("dns");
66
const pool = @import("pool.zig");
77

88
const endpoints = @import("endpoints.zig");
@@ -23,6 +23,24 @@ pub fn main() !void {
2323

2424
const allocator = tsa.allocator();
2525

26+
var zap_port: []u8 = undefined;
27+
var arg_string = try std.fmt.allocPrint(allocator, "{s}", .{"0"});
28+
defer allocator.free(arg_string);
29+
30+
var args = try std.process.argsWithAllocator(allocator);
31+
defer args.deinit();
32+
while (args.next()) |arg| {
33+
arg_string = try std.fmt.allocPrint(allocator, "{s}", .{arg});
34+
35+
zap_port = arg_string; // use arg
36+
}
37+
38+
var port = try std.fmt.parseInt(u16, zap_port, 0);
39+
40+
if (port == 0) {
41+
port = 3000;
42+
}
43+
2644
var pg_pool = try pool.initPool(allocator);
2745
defer pg_pool.deinit();
2846

@@ -68,7 +86,7 @@ pub fn main() !void {
6886
var listener = try zap.Middleware.Listener(middleware.Context).init(
6987
.{
7088
.on_request = null, // must be null
71-
.port = 3000,
89+
.port = port,
7290
.log = false,
7391
.max_clients = 100000,
7492
},
@@ -78,13 +96,15 @@ pub fn main() !void {
7896
);
7997
try listener.listen();
8098

81-
const cpuCount = @as(i16, @intCast(std.Thread.getCpuCount() catch 1));
99+
//const cpuCount = @as(i16, @intCast(std.Thread.getCpuCount() catch 1));
100+
//const workers = if (builtin.mode == .Debug) 1 else cpuCount;
101+
const threads = 128;
82102

83-
std.debug.print("Listening on 0.0.0.0:3000 on {d} threads\n", .{cpuCount});
103+
std.debug.print("Listening at 0.0.0.0:{d} on {d} threads\n", .{port, threads});
84104

85105
// start worker threads
86106
zap.start(.{
87-
.threads = 16 * cpuCount,
107+
.threads = threads,
88108
.workers = 1,
89109
});
90110
}

frameworks/Zig/zap/src/pool.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const Regex = regex.Regex;
1010

1111
pub fn initPool(allocator: Allocator) !*pg.Pool {
1212
const info = try parsePostgresConnStr(allocator);
13-
std.debug.print("Connection: {s}:{s}@{s}:{d}/{s}\n", .{ info.username, info.password, info.hostname, info.port, info.database });
13+
//std.debug.print("Connection: {s}:{s}@{s}:{d}/{s}\n", .{ info.username, info.password, info.hostname, info.port, info.database });
1414

1515
const pg_pool = try Pool.init(allocator, .{
1616
.size = 28,
@@ -60,7 +60,7 @@ fn addressAsString(address: std.net.Address) ![]const u8 {
6060

6161
fn parsePostgresConnStr(allocator: Allocator) !ConnectionInfo {
6262
const pg_port = try getEnvVar(allocator, "PG_PORT", "5432");
63-
std.debug.print("tfb port {s}\n", .{pg_port});
63+
// std.debug.print("tfb port {s}\n", .{pg_port});
6464
var port = try std.fmt.parseInt(u16, pg_port, 0);
6565

6666
if (port == 0) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
CPU_COUNT=$(nproc)
4+
P=3000
5+
END=$(($P+$CPU_COUNT))
6+
7+
while [ $P -lt $END ]; do
8+
zap $P &
9+
let P=P+1
10+
done

frameworks/Zig/zap/zap.dockerfile

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,22 @@ ENV PG_HOST=tfb-database
99
ENV PG_PORT=5432
1010

1111
COPY src src
12-
COPY run.sh run.sh
13-
1412
COPY build.zig.zon build.zig.zon
1513
COPY build.zig build.zig
14+
COPY start-servers.sh start-servers.sh
15+
COPY build-nginx-conf.sh build-nginx-conf.sh
16+
COPY nginx.conf nginx.conf
1617

17-
RUN dnf install -y zig
18-
RUN zig version
19-
# RUN zig build -Doptimize=ReleaseFast
20-
RUN zig build
21-
RUN cp /zap/zig-out/bin/zap /usr/local/bin
22-
23-
EXPOSE 3000
24-
25-
CMD ["sh", "run.sh"]
26-
27-
# FROM alpine:3.19
18+
RUN chmod +x start-servers.sh
19+
RUN chmod +x build-nginx-conf.sh
2820

29-
# WORKDIR /zap
21+
RUN ./build-nginx-conf.sh
3022

31-
# ENV PG_USER=benchmarkdbuser
32-
# ENV PG_PASS=benchmarkdbpass
33-
# ENV PG_DB=hello_world
34-
# ENV PG_HOST=tfb-database
35-
# ENV PG_PORT=5432
36-
37-
# RUN apk update
38-
# RUN apk add libc6-compat
39-
40-
# COPY run.sh run.sh
41-
42-
# COPY --from=build /zap/zig-out/bin/zap /usr/local/bin
23+
RUN dnf install -y zig nginx
24+
RUN zig version
25+
RUN zig build -Doptimize=ReleaseFast
26+
RUN cp /zap/zig-out/bin/zap /usr/local/bin
4327

44-
# EXPOSE 3000
28+
EXPOSE 8080
4529

46-
# CMD ["sh", "run.sh"]
30+
CMD ./start-servers.sh && nginx -c /zap/nginx.conf -g "daemon off;"

0 commit comments

Comments
 (0)