Skip to content

Instantly share code, notes, and snippets.

@kristoff-it
Created May 9, 2020 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristoff-it/3bcd22f04823875a49b0b46766b5efe8 to your computer and use it in GitHub Desktop.
Save kristoff-it/3bcd22f04823875a49b0b46766b5efe8 to your computer and use it in GitHub Desktop.
const std = @import("std");
const time = std.time;
const Timer = time.Timer;
const net = std.net;
const fs = std.fs;
pub const io_mode = .evented;
const NUM_CLIENTS = 5;
const MSG_SIZE = 4096 * 512;
pub fn client(port: u16) !u8 {
var socket: fs.File = undefined;
try net.tcpConnectToAddress(&socket, try net.Address.parseIp("127.0.0.1", port));
const socket_in_stream = socket.inStream();
var rcv_buf: [MSG_SIZE]u8 = undefined;
_ = try socket_in_stream.readAll(&rcv_buf);
var result: u8 = 0;
for (rcv_buf) |b| result +%= b;
return result;
}
pub fn startServer(server: *net.StreamServer, message: []const u8) !void {
var i: usize = 0;
while (i < NUM_CLIENTS) : (i += 1) {
var conn: net.StreamServer.Connection = undefined;
try server.accept(&conn);
try conn.file.writeAll(message);
conn.file.close();
}
}
pub fn main() !void {
var server = net.StreamServer.init(.{});
defer server.deinit();
const message = try std.heap.page_allocator.alloc(u8, MSG_SIZE);
for (message) |*b| {
b.* = 'z';
}
try server.listen(net.Address.parseIp("127.0.0.1", 0) catch unreachable);
var serverFrame = async startServer(&server, message);
std.debug.warn("server listenting at {} fd={}\n", .{ server.listen_address.in.port, server.sockfd });
var clientFrames = try std.heap.page_allocator.alloc(@Frame(client), NUM_CLIENTS);
var timer = try Timer.start();
const start = timer.lap();
var final_hash: u8 = 0;
{
// Timed block
for (clientFrames) |*c| {
c.* = async client(server.listen_address.getPort());
}
for (clientFrames) |*c| {
final_hash +%= try await c;
}
}
const end = timer.read();
const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
const throughput = @floatToInt(u64, MSG_SIZE * NUM_CLIENTS / elapsed_s);
std.debug.warn(
\\time elapsed = {:.2}s
\\throughput = {Bi:.2}/second
\\hash = {}
\\
, .{ elapsed_s, throughput, final_hash });
try await serverFrame;
server.close();
}
const std = @import("std");
const time = std.time;
const Timer = time.Timer;
const net = std.net;
const fs = std.fs;
pub const io_mode = .evented;
const NUM_CLIENTS = 5;
const MSG_SIZE = 4096 * 512;
pub fn client(port: u16) !u8 {
const socket = try net.tcpConnectToAddress(try net.Address.parseIp("127.0.0.1", port));
const socket_in_stream = socket.inStream();
var rcv_buf: [MSG_SIZE]u8 = undefined;
_ = try socket_in_stream.readAll(&rcv_buf);
var result: u8 = 0;
for (rcv_buf) |b| result +%= b;
return result;
}
pub fn startServer(server: *net.StreamServer, message: []const u8) !void {
var i: usize = 0;
while (i < NUM_CLIENTS) : (i += 1) {
var conn = try server.accept();
try conn.file.writeAll(message);
conn.file.close();
}
}
pub fn main() !void {
var server = net.StreamServer.init(.{});
defer server.deinit();
const message = try std.heap.page_allocator.alloc(u8, MSG_SIZE);
for (message) |*b| {
b.* = 'z';
}
try server.listen(net.Address.parseIp("127.0.0.1", 0) catch unreachable);
var serverFrame = async startServer(&server, message);
std.debug.warn("server listenting at {}\n", .{server.listen_address.in.port});
var clientFrames = try std.heap.page_allocator.alloc(@Frame(client), NUM_CLIENTS);
var timer = try Timer.start();
const start = timer.lap();
var final_hash: u8 = 0;
{
// Timed block
for (clientFrames) |*c| {
c.* = async client(server.listen_address.getPort());
}
for (clientFrames) |*c| {
final_hash +%= try await c;
}
}
const end = timer.read();
const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
const throughput = @floatToInt(u64, MSG_SIZE * NUM_CLIENTS / elapsed_s);
std.debug.warn(
\\time elapsed = {:.2}s
\\throughput = {Bi:.2}/second
\\hash = {}
\\
, .{ elapsed_s, throughput, final_hash });
try await serverFrame;
server.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment