-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.zig
More file actions
218 lines (189 loc) · 7.87 KB
/
scripts.zig
File metadata and controls
218 lines (189 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const std = @import("std");
const builtin = @import("builtin");
const RootReadmeBenchCompareStartMarker = "<!-- BENCH_COMPARE:START -->";
const RootReadmeBenchCompareEndMarker = "<!-- BENCH_COMPARE:END -->";
const BenchmarkReadmeBenchCompareStartMarker = "<!-- BENCH_COMPARE:START -->";
const BenchmarkReadmeBenchCompareEndMarker = "<!-- BENCH_COMPARE:END -->";
const BenchmarkResultsRelDir = "benchmark/results";
const BenchmarkLatestJsonRelPath = "benchmark/results/latest.json";
const BenchmarkLatestMdRelPath = "benchmark/results/latest.md";
pub fn envInt(env: *const std.process.Environ.Map, name: []const u8, default: usize) usize {
const v = env.get(name) orelse return default;
return std.fmt.parseInt(usize, v, 10) catch default;
}
fn runChecked(io: std.Io, argv: []const []const u8, cwd: ?[]const u8, env_map: ?*const std.process.Environ.Map) !void {
const cwd_opt: std.process.Child.Cwd = if (cwd) |p| .{ .path = p } else .inherit;
var child = try std.process.spawn(io, .{
.argv = argv,
.cwd = cwd_opt,
.environ_map = env_map,
.stdin = .ignore,
.stdout = .inherit,
.stderr = .inherit,
});
const term = try child.wait(io);
switch (term) {
.exited => |code| if (code != 0) return error.ProcessFailed,
else => return error.ProcessFailed,
}
}
fn dirExists(io: std.Io, path: []const u8) bool {
const dir = std.Io.Dir.openDirAbsolute(io, path, .{}) catch return false;
dir.close(io);
return true;
}
fn ensureClone(io: std.Io, cwd: []const u8, dir: []const u8, url: []const u8) !void {
if (dirExists(io, dir)) return;
try runChecked(io, &.{ "git", "clone", "--depth", "1", url, dir }, cwd, null);
}
pub fn buildUwsServer(
io: std.Io,
allocator: std.mem.Allocator,
root: []const u8,
uws_dir: []const u8,
environ: std.process.Environ,
) ![]u8 {
try ensureClone(io, root, uws_dir, "https://github.com/uNetworking/uWebSockets");
const usockets_dir = try std.fs.path.join(allocator, &.{ root, ".zig-cache", "uSockets-bench" });
defer allocator.free(usockets_dir);
try ensureClone(io, root, usockets_dir, "https://github.com/uNetworking/uSockets");
const tmp_dir = try std.fs.path.join(allocator, &.{ root, ".zig-cache", "tmp" });
defer allocator.free(tmp_dir);
try runChecked(io, &.{ "mkdir", "-p", tmp_dir }, root, null);
var env = try std.process.Environ.createMap(environ, allocator);
defer env.deinit();
try env.put("TMPDIR", tmp_dir);
try env.put("TMP", tmp_dir);
try env.put("TEMP", tmp_dir);
try runChecked(io, &.{ "make", "WITH_ZLIB=0" }, usockets_dir, &env);
const out_path = try std.fs.path.join(allocator, &.{ root, "zig-out", "bin", "uws-bench-server" });
const src_path = try std.fs.path.join(allocator, &.{ root, "benchmark", "uws_server.cpp" });
const include1 = try std.fs.path.join(allocator, &.{ uws_dir, "src" });
defer allocator.free(include1);
const include2 = try std.fs.path.join(allocator, &.{ usockets_dir, "src" });
defer allocator.free(include2);
const usockets_lib = try std.fs.path.join(allocator, &.{ usockets_dir, "uSockets.a" });
defer allocator.free(usockets_lib);
const include1_arg = try std.fmt.allocPrint(allocator, "-I{s}", .{include1});
defer allocator.free(include1_arg);
const include2_arg = try std.fmt.allocPrint(allocator, "-I{s}", .{include2});
defer allocator.free(include2_arg);
const out_arg = try std.fmt.allocPrint(allocator, "-o{s}", .{out_path});
defer allocator.free(out_arg);
try runChecked(io, &.{
"g++",
"-O3",
"-march=native",
"-flto=auto",
"-std=c++2b",
"-DUWS_NO_ZLIB",
include1_arg,
include2_arg,
src_path,
usockets_lib,
"-pthread",
out_arg,
}, root, &env);
return out_path;
}
fn readFileMaybe(io: std.Io, allocator: std.mem.Allocator, path: []const u8) !?[]u8 {
const file = std.Io.Dir.openFileAbsolute(io, path, .{}) catch |err| switch (err) {
error.FileNotFound => return null,
else => return err,
};
defer file.close(io);
const stat = try file.stat(io);
if (stat.size > std.math.maxInt(usize)) return error.FileTooLarge;
const len: usize = @intCast(stat.size);
const out = try allocator.alloc(u8, len);
errdefer allocator.free(out);
var buffer: [4096]u8 = undefined;
var reader = file.reader(io, &buffer);
try reader.interface.readSliceAll(out);
return out;
}
fn writeFileIfChanged(io: std.Io, allocator: std.mem.Allocator, path: []const u8, bytes: []const u8) !bool {
const existing = try readFileMaybe(io, allocator, path);
if (existing) |buf| {
defer allocator.free(buf);
if (std.mem.eql(u8, buf, bytes)) return false;
}
const file = try std.Io.Dir.createFileAbsolute(io, path, .{ .truncate = true });
defer file.close(io);
var buf: [4096]u8 = undefined;
var writer = file.writer(io, &buf);
try writer.interface.writeAll(bytes);
try writer.interface.flush();
return true;
}
fn updateReadmeSectionAtPath(
io: std.Io,
allocator: std.mem.Allocator,
path: []const u8,
start_marker: []const u8,
end_marker: []const u8,
replacement: []const u8,
) !void {
const readme = try readFileMaybe(io, allocator, path) orelse return error.FileNotFound;
defer allocator.free(readme);
const start = std.mem.indexOf(u8, readme, start_marker) orelse return error.ReadmeBenchMarkersMissing;
const after_start = start + start_marker.len;
const end = std.mem.indexOfPos(u8, readme, after_start, end_marker) orelse return error.ReadmeBenchMarkersMissing;
var out: std.ArrayList(u8) = .empty;
defer out.deinit(allocator);
try out.appendSlice(allocator, readme[0..after_start]);
try out.appendSlice(allocator, "\n\n");
try out.appendSlice(allocator, replacement);
if (replacement.len == 0 or replacement[replacement.len - 1] != '\n') try out.append(allocator, '\n');
if (readme[end - 1] != '\n') try out.append(allocator, '\n');
try out.appendSlice(allocator, readme[end..]);
_ = try writeFileIfChanged(io, allocator, path, out.items);
}
fn syncReadmes(
io: std.Io,
allocator: std.mem.Allocator,
root: []const u8,
root_replacement: []const u8,
benchmark_replacement: []const u8,
) !void {
const root_readme_path = try std.fs.path.join(allocator, &.{ root, "README.md" });
defer allocator.free(root_readme_path);
try updateReadmeSectionAtPath(
io,
allocator,
root_readme_path,
RootReadmeBenchCompareStartMarker,
RootReadmeBenchCompareEndMarker,
root_replacement,
);
const benchmark_readme_path = try std.fs.path.join(allocator, &.{ root, "benchmark", "README.md" });
defer allocator.free(benchmark_readme_path);
try updateReadmeSectionAtPath(
io,
allocator,
benchmark_readme_path,
BenchmarkReadmeBenchCompareStartMarker,
BenchmarkReadmeBenchCompareEndMarker,
benchmark_replacement,
);
}
pub fn writeCompareArtifactsAndSyncReadme(
io: std.Io,
allocator: std.mem.Allocator,
root: []const u8,
root_summary_md: []const u8,
benchmark_summary_md: []const u8,
full_md: []const u8,
json: []const u8,
) !void {
const results_dir = try std.fs.path.join(allocator, &.{ root, BenchmarkResultsRelDir });
defer allocator.free(results_dir);
try std.Io.Dir.createDirPath(.cwd(), io, results_dir);
const json_path = try std.fs.path.join(allocator, &.{ root, BenchmarkLatestJsonRelPath });
defer allocator.free(json_path);
_ = try writeFileIfChanged(io, allocator, json_path, json);
const md_path = try std.fs.path.join(allocator, &.{ root, BenchmarkLatestMdRelPath });
defer allocator.free(md_path);
_ = try writeFileIfChanged(io, allocator, md_path, full_md);
try syncReadmes(io, allocator, root, root_summary_md, benchmark_summary_md);
}