Skip to content

Commit d4eab64

Browse files
committed
Adding Zinc framework
1 parent db30328 commit d4eab64

File tree

8 files changed

+281
-0
lines changed

8 files changed

+281
-0
lines changed

frameworks/Zig/zinc/.gitignore

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

frameworks/Zig/zinc/README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Congratulations!
2+
3+
You have successfully built a new test in the suite!
4+
5+
There are some remaining tasks to do before you are ready to open a pull request, however.
6+
7+
## Next Steps
8+
9+
1. Gather your source code.
10+
11+
You will need to ensure that your source code is beneath this directory. The most common solution is to include a `src` directory and place your source code there.
12+
13+
2. Edit `benchmark_config.json`
14+
15+
You will need alter `benchmark_config.json` to have the appropriate end-points and port specified.
16+
17+
3. Create `zinc.dockerfile`
18+
19+
This is the dockerfile that is built into a docker image and run when a benchmark test is run. Specifically, this file tells the suite how to build and start your test application.
20+
21+
You can create multiple implementations and they will all conform to `[name in benchmark_config.json].dockerfile`. For example, the `default` implementation in `benchmark_config.json` will be `zinc.dockerfile`, but if you wanted to make another implementation that did only the database tests for MySQL, you could make `zinc-mysql.dockerfile` and have an entry in your `benchmark_config.json` for `zinc-mysql`.
22+
23+
4. Test your application
24+
25+
$ tfb --mode verify --test zinc
26+
27+
This will run the suite in `verify` mode for your test. This means that no benchmarks will be captured and we will test that we can hit your implementation end-points specified by `benchmark_config.json` and that the response is correct.
28+
29+
Once you are able to successfully run your test through our suite in this way **and** your test passes our validation, you may move on to the next step.
30+
31+
5. Add your test to `.github/workflows/build.yml`
32+
33+
Edit `.github/workflows/build.yml` to ensure that Github Actions will automatically run our verification tests against your new test. This file is kept in alphabetical order, so find where `TESTDIR=Zig/zinc` should be inserted under `env > matrix` and put it there.
34+
35+
6. Fix this `README.md` and open a pull request
36+
37+
Starting on line 49 is your actual `README.md` that will sit with your test implementation. Update all the dummy values to their correct values so that when people visit your test in our Github repository, they will be greated with information on how your test implementation works and where to look for useful source code.
38+
39+
After you have the real `README.md` file in place, delete everything above line 59 and you are ready to open a pull request.
40+
41+
Thanks and Cheers!
42+
43+
44+
45+
46+
47+
48+
49+
# Zinc Benchmarking Test
50+
51+
### Test Type Implementation Source Code
52+
53+
* [JSON](Relative/Path/To/Your/Source/File)
54+
* [PLAINTEXT](Relative/Path/To/Your/Source/File)
55+
* [DB](Relative/Path/To/Your/Source/File)
56+
* [QUERY](Relative/Path/To/Your/Source/File)
57+
* [CACHED QUERY](Relative/Path/To/Your/Source/File)
58+
* [UPDATE](Relative/Path/To/Your/Source/File)
59+
* [FORTUNES](Relative/Path/To/Your/Source/File)
60+
61+
## Important Libraries
62+
The tests were run with:
63+
* [Software](https://www.example1.com/)
64+
* [Example](http://www.example2.com/)
65+
66+
## Test URLs
67+
### JSON
68+
69+
http://localhost:8080/json
70+
71+
### PLAINTEXT
72+
73+
http://localhost:8080/plaintext
74+
75+
### DB
76+
77+
http://localhost:8080/db
78+
79+
### QUERY
80+
81+
http://localhost:8080/query?queries=
82+
83+
### CACHED QUERY
84+
85+
http://localhost:8080/cached_query?queries=
86+
87+
### UPDATE
88+
89+
http://localhost:8080/update?queries=
90+
91+
### FORTUNES
92+
93+
http://localhost:8080/fortunes
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"framework": "zinc",
3+
"tests": [
4+
{
5+
"default": {
6+
"json_url": "/json",
7+
"plaintext_url": "/plaintext",
8+
"port": 8080,
9+
"approach": "Stripped",
10+
"classification": "Fullstack",
11+
"database": "None",
12+
"framework": "Zinc",
13+
"language": "Zig",
14+
"flavor": "None",
15+
"orm": "None",
16+
"platform": "None",
17+
"webserver": "None",
18+
"os": "Linux",
19+
"database_os": "Linux",
20+
"display_name": "Zinc",
21+
"notes": "",
22+
"versus": "None"
23+
}
24+
}
25+
]
26+
}

frameworks/Zig/zinc/build.zig

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const std = @import("std");
2+
3+
// Although this function looks imperative, note that its job is to
4+
// declaratively construct a build graph that will be executed by an external
5+
// runner.
6+
pub fn build(b: *std.Build) void {
7+
// Standard target options allows the person running `zig build` to choose
8+
// what target to build for. Here we do not override the defaults, which
9+
// means any target is allowed, and the default is native. Other options
10+
// for restricting supported target set are available.
11+
const target = b.standardTargetOptions(.{});
12+
13+
// Standard optimization options allow the person running `zig build` to select
14+
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
15+
// set a preferred release mode, allowing the user to decide how to optimize.
16+
const optimize = b.standardOptimizeOption(.{});
17+
18+
const exe = b.addExecutable(.{
19+
.name = "zinc",
20+
.root_source_file = b.path("src/main.zig"),
21+
.target = target,
22+
.optimize = optimize,
23+
});
24+
25+
const zinc = b.dependency("zinc", .{
26+
.target = target,
27+
.optimize = optimize,
28+
});
29+
exe.root_module.addImport("zinc", zinc.module("zinc"));
30+
31+
const datetime = b.dependency("zig-datetime", .{
32+
.target = target,
33+
.optimize = optimize,
34+
});
35+
exe.root_module.addImport("datetime", datetime.module("zig-datetime"));
36+
37+
// This declares intent for the executable to be installed into the
38+
// standard location when the user invokes the "install" step (the default
39+
// step when running `zig build`).
40+
b.installArtifact(exe);
41+
42+
// This *creates* a Run step in the build graph, to be executed when another
43+
// step is evaluated that depends on it. The next line below will establish
44+
// such a dependency.
45+
const run_cmd = b.addRunArtifact(exe);
46+
47+
// By making the run step depend on the install step, it will be run from the
48+
// installation directory rather than directly from within the cache directory.
49+
// This is not necessary, however, if the application depends on other installed
50+
// files, this ensures they will be present and in the expected location.
51+
run_cmd.step.dependOn(b.getInstallStep());
52+
53+
// This allows the user to pass arguments to the application in the build
54+
// command itself, like this: `zig build run -- arg1 arg2 etc`
55+
if (b.args) |args| {
56+
run_cmd.addArgs(args);
57+
}
58+
59+
// This creates a build step. It will be visible in the `zig build --help` menu,
60+
// and can be selected like this: `zig build run`
61+
// This will evaluate the `run` step rather than the default, which is "install".
62+
const run_step = b.step("run", "Run the app");
63+
run_step.dependOn(&run_cmd.step);
64+
65+
const exe_unit_tests = b.addTest(.{
66+
.root_source_file = b.path("src/main.zig"),
67+
.target = target,
68+
.optimize = optimize,
69+
});
70+
71+
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
72+
73+
// Similar to creating the run step earlier, this exposes a `test` step to
74+
// the `zig build --help` menu, providing a way for the user to request
75+
// running the unit tests.
76+
const test_step = b.step("test", "Run unit tests");
77+
test_step.dependOn(&run_exe_unit_tests.step);
78+
}

frameworks/Zig/zinc/build.zig.zon

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.{
2+
.name = "zinc",
3+
.version = "0.1.0",
4+
.dependencies = .{
5+
.zinc = .{
6+
.url = "https://github.com/zon-dev/zinc/archive/refs/heads/main.zip",
7+
.hash = "1220bbd81a53f8268d970dfbf92268c36a25b50978dd8e7dcbf551e69b2c03677e33",
8+
},
9+
.@"zig-datetime" = .{
10+
.url = "https://github.com/frmdstryr/zig-datetime/archive/refs/heads/master.zip",
11+
.hash = "122077215ce36e125a490e59ec1748ffd4f6ba00d4d14f7308978e5360711d72d77f",
12+
},
13+
},
14+
.paths = .{
15+
"build.zig",
16+
"build.zig.zon",
17+
"src",
18+
},
19+
}

frameworks/Zig/zinc/run.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
echo "Waiting for Zinc framework to start..."
2+
3+
zinc

frameworks/Zig/zinc/src/main.zig

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const std = @import("std");
2+
const zinc = @import("zinc");
3+
const Datetime = @import("datetime").datetime.Datetime;
4+
5+
pub fn main() !void {
6+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
7+
const allocator = gpa.allocator();
8+
9+
var z = try zinc.init(.{ .port = 8080, .allocator = allocator, .num_threads = 255 });
10+
11+
var router = z.getRouter();
12+
try router.use(&.{setupHeader});
13+
14+
try router.get("/json", json);
15+
try router.get("/plaintext", plaintext);
16+
17+
try z.run();
18+
}
19+
20+
fn plaintext(ctx: *zinc.Context) anyerror!void {
21+
try ctx.text("Hello, World!", .{});
22+
}
23+
24+
fn json(ctx: *zinc.Context) anyerror!void {
25+
try ctx.json(.{ .message = "Hello, World!" }, .{});
26+
}
27+
28+
fn setupHeader(ctx: *zinc.Context) anyerror!void {
29+
try ctx.setHeader("Server", "Zinc");
30+
// try ctx.setHeader("date", "Sun Sep 22 10:01:11 CEST 2024");
31+
// In UTC
32+
const now = Datetime.now();
33+
const now_str = try now.formatHttp(ctx.allocator);
34+
// defer ctx.allocator.free(now_str);
35+
// std.debug.warn("The time is now: {}\n", .{now_str});
36+
// The time is now: Fri, 20 Dec 2019 22:03:02 UTC
37+
try ctx.setHeader("date", now_str);
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM fedora:40 AS build
2+
3+
WORKDIR /zinc
4+
5+
COPY src src
6+
COPY run.sh run.sh
7+
8+
COPY build.zig.zon build.zig.zon
9+
COPY build.zig build.zig
10+
11+
RUN dnf install -y zig
12+
RUN zig version
13+
# RUN zig build -Doptimize=ReleaseFast
14+
RUN zig build
15+
RUN cp /zinc/zig-out/bin/zinc /usr/local/bin
16+
17+
EXPOSE 8080
18+
ARG BENCHMARK_ENV
19+
ARG TFB_TEST_DATABASE
20+
ARG TFB_TEST_NAME
21+
22+
CMD ["sh", "run.sh"]

0 commit comments

Comments
 (0)