Skip to content

Commit 20e3c17

Browse files
authored
[Zig] Add ZZZ framework (TechEmpower#9715)
* feat(zig) add zzz * Update benchmark_config.json * feat(zig/zzz): add date header * fix(main.zig) build error * fix(main.zig) build error 2 * Dupa date * Alloc date * Fix build error * Update main.zig * Fix date * Update README.md * Fix build error 3
1 parent 0e74035 commit 20e3c17

File tree

7 files changed

+198
-0
lines changed

7 files changed

+198
-0
lines changed

frameworks/Zig/zzz/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.zig-out
2+
.zig-cache

frameworks/Zig/zzz/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
# [ZZZ](https://github.com/tardy-org/zzz) - Web Franework.
3+
4+
## Description
5+
6+
ZZZ is a framework for writing performant and reliable networked services in Zig. It supports both HTTP and HTTPS.
7+
8+
## Test URLs
9+
10+
### Test 1: JSON Encoding
11+
12+
http://localhost:8080/json
13+
14+
### Test 2: Plaintext
15+
16+
http://localhost:8080/plaintext
17+
18+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"framework": "zzz",
3+
"tests": [{
4+
"default": {
5+
"json_url": "/json",
6+
"plaintext_url": "/plaintext",
7+
"port": 8080,
8+
"approach": "Realistic",
9+
"classification": "Micro",
10+
"database": "None",
11+
"framework": "zzz",
12+
"language": "Zig",
13+
"flavor": "None",
14+
"orm": "None",
15+
"platform": "None",
16+
"webserver": "None",
17+
"os": "Linux",
18+
"database_os": "Linux",
19+
"display_name": "ZZZ (Zig)",
20+
"notes": "",
21+
"versus": ""
22+
}
23+
}]
24+
}

frameworks/Zig/zzz/build.zig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
const root_source_file = b.path("src/main.zig");
7+
8+
const zzz = b.dependency("zzz", .{
9+
.target = target,
10+
.optimize = optimize,
11+
});
12+
13+
const exe = b.addExecutable(.{
14+
.name = "zzz",
15+
.target = target,
16+
.optimize = optimize,
17+
.root_source_file = root_source_file,
18+
.strip = true,
19+
});
20+
exe.root_module.addImport("zzz", zzz.module("zzz"));
21+
b.installArtifact(exe);
22+
}

frameworks/Zig/zzz/build.zig.zon

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.{
2+
.name = .zzz_bench,
3+
.version = "0.0.0",
4+
.fingerprint = 0xab2ef1c28b2bcc91,
5+
.minimum_zig_version = "0.14.0",
6+
.dependencies = .{
7+
.zzz = .{
8+
.url = "git+https://github.com/tardy-org/zzz#90cc62494644e7234efd85ab1df5d65440f9eead",
9+
.hash = "zzz-0.3.0-4HoaJqpQAgDgcImt8cC2cpT59J25JNDynMDt4qLaXDYK",
10+
},
11+
},
12+
.paths = .{
13+
"src/",
14+
"build.zig",
15+
"build.zig.zon",
16+
},
17+
}

frameworks/Zig/zzz/src/main.zig

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const std = @import("std");
2+
3+
const zzz = @import("zzz");
4+
const http = zzz.HTTP;
5+
6+
const tardy = zzz.tardy;
7+
const Tardy = tardy.Tardy(.auto);
8+
const Runtime = tardy.Runtime;
9+
const Socket = tardy.Socket;
10+
11+
const Server = http.Server;
12+
const Router = http.Router;
13+
const Context = http.Context;
14+
const Route = http.Route;
15+
const Respond = http.Respond;
16+
17+
const Message = struct { message: []const u8 };
18+
var date: [29]u8 = undefined;
19+
20+
pub fn main() !void {
21+
const host: []const u8 = "0.0.0.0";
22+
const port: u16 = 8080;
23+
24+
const date_thread = try std.Thread.spawn(.{}, struct {
25+
fn a() !void {
26+
while (true) {
27+
var d = http.Date.init(std.time.timestamp());
28+
const http_date = d.to_http_date();
29+
_ = try http_date.into_buf(date[0..]);
30+
std.time.sleep(std.time.ns_per_ms * 985);
31+
}
32+
}
33+
}.a, .{});
34+
35+
date_thread.detach();
36+
37+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
38+
defer if (gpa.deinit() == .leak) {
39+
@panic("Memory leak has occurred!");
40+
};
41+
42+
const allocator = gpa.allocator();
43+
44+
var t = try Tardy.init(allocator, .{
45+
.threading = .all,
46+
});
47+
defer t.deinit();
48+
49+
var router = try Router.init(allocator, &.{
50+
Route.init("/plaintext").get({}, home_handler).layer(),
51+
Route.init("/json").get({}, json_handler).layer(),
52+
}, .{});
53+
defer router.deinit(allocator);
54+
55+
var socket = try Socket.init(.{ .tcp = .{ .host = host, .port = port } });
56+
defer socket.close_blocking();
57+
try socket.bind();
58+
try socket.listen(4096);
59+
60+
const EntryParams = struct {
61+
router: *const Router,
62+
socket: Socket,
63+
};
64+
65+
try t.entry(
66+
EntryParams{ .router = &router, .socket = socket },
67+
struct {
68+
fn entry(rt: *Runtime, p: EntryParams) !void {
69+
var server = Server.init(.{
70+
.capture_count_max = 0,
71+
});
72+
try server.serve(rt, p.router, .{ .normal = p.socket });
73+
}
74+
}.entry,
75+
);
76+
}
77+
78+
pub fn home_handler(ctx: *const Context, _: void) !Respond {
79+
try ctx.response.headers.put("Date", try ctx.allocator.dupe(u8, date[0..]));
80+
return ctx.response.apply(.{
81+
.mime = http.Mime.TEXT,
82+
.body = "Hello, World!",
83+
.status = .OK,
84+
});
85+
}
86+
87+
pub fn json_handler(ctx: *const Context, _: void) !Respond {
88+
try ctx.response.headers.put("Date", try ctx.allocator.dupe(u8, date[0..]));
89+
return ctx.response.apply(.{
90+
.mime = http.Mime.JSON,
91+
.body = try std.json.stringifyAlloc(ctx.allocator, Message{ .message = "Hello, World!" }, .{}),
92+
.status = .OK,
93+
});
94+
}

frameworks/Zig/zzz/zzz.dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM debian:12.9
2+
3+
WORKDIR /app
4+
5+
COPY src src
6+
COPY build.zig.zon build.zig.zon
7+
COPY build.zig build.zig
8+
9+
ARG ZIG_VER=0.14.0
10+
11+
RUN apt-get update && apt-get install -y curl xz-utils ca-certificates
12+
13+
RUN curl https://ziglang.org/download/${ZIG_VER}/zig-linux-$(uname -m)-${ZIG_VER}.tar.xz -o zig-linux.tar.xz && \
14+
tar xf zig-linux.tar.xz && \
15+
mv zig-linux-$(uname -m)-${ZIG_VER}/ /opt/zig
16+
17+
RUN /opt/zig/zig build -Doptimize=ReleaseFast
18+
19+
EXPOSE 8080
20+
21+
CMD ["zig-out/bin/zzz"]

0 commit comments

Comments
 (0)