-
Notifications
You must be signed in to change notification settings - Fork 8
/
example_udp_benchmark.zig
60 lines (42 loc) · 1.44 KB
/
example_udp_benchmark.zig
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
const std = @import("std");
const pike = @import("pike.zig");
const os = std.os;
const net = std.net;
pub fn main() !void {
try pike.init();
defer pike.deinit();
const notifier = try pike.Notifier.init();
defer notifier.deinit();
var stopped = false;
var server_frame = async runBenchmarkServer(¬ifier, &stopped);
var client_frame = async runBenchmarkClient(¬ifier, &stopped);
while (!stopped) {
try notifier.poll(10_000);
}
try nosuspend await server_frame;
try nosuspend await client_frame;
}
fn runBenchmarkServer(notifier: *const pike.Notifier, stopped: *bool) !void {
defer stopped.* = true;
const address = try net.Address.parseIp("127.0.0.1", 9000);
var socket = try pike.Socket.init(os.AF.INET, os.SOCK.DGRAM, 0, 0);
defer socket.deinit();
try socket.registerTo(notifier);
try socket.set(.reuse_address, true);
try socket.bind(address);
var buf: [1400]u8 = undefined;
while (true) {
_ = try socket.recvFrom(&buf, 0, null);
}
}
fn runBenchmarkClient(notifier: *const pike.Notifier, stopped: *bool) !void {
defer stopped.* = true;
const address = try net.Address.parseIp("127.0.0.1", 9000);
var socket = try pike.Socket.init(os.AF.INET, os.SOCK.DGRAM, 0, 0);
defer socket.deinit();
try socket.registerTo(notifier);
var buf: [1400]u8 = undefined;
while (true) {
_ = try socket.sendTo(&buf, 0, address);
}
}