aboutsummaryrefslogtreecommitdiff
path: root/misctools/machbench.zig
blob: 9c486f48ac3e3debb54cdecf0be046bee395c41f (plain) (blame)
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
// most of this file is copy pasted from other files in misctools
const std = @import("std");
const bun = @import("../src/global.zig");
const string = bun.string;
const Output = bun.Output;
const Global = bun.Global;
const Environment = bun.Environment;
const strings = bun.strings;
const MutableString = bun.MutableString;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const C = bun.C;
const clap = @import("../src/deps/zig-clap/clap.zig");
const AsyncIO = @import("io");

const URL = @import("../src/url.zig").URL;
const Headers = @import("http").Headers;
const Method = @import("../src/http/method.zig").Method;
const ColonListType = @import("../src/cli/colon_list_type.zig").ColonListType;
const HeadersTuple = ColonListType(string, noop_resolver);
const path_handler = @import("../src/resolver/resolve_path.zig");
const NetworkThread = @import("http").NetworkThread;
const HTTP = @import("http");
fn noop_resolver(in: string) !string {
    return in;
}

var waker: AsyncIO.Waker = undefined;

fn spamMe(count: usize) void {
    Output.Source.configureNamedThread("1");
    defer Output.flush();
    var timer = std.time.Timer.start() catch unreachable;

    var i: usize = 0;
    while (i < count) : (i += 1) {
        waker.wake() catch unreachable;
    }
    Output.prettyErrorln("[EVFILT_MACHPORT] Sent {any}", .{bun.fmt.fmtDuration(timer.read())});
}
const thread_count = 1;
pub fn machMain(runs: usize) anyerror!void {
    defer Output.flush();
    waker = try AsyncIO.Waker.init(bun.default_allocator);

    var args = try std.process.argsAlloc(bun.default_allocator);
    const count = std.fmt.parseInt(usize, args[args.len - 1], 10) catch 1024;
    var elapsed: u64 = 0;

    var remaining_runs: usize = runs;
    while (remaining_runs > 0) : (remaining_runs -= 1) {
        var threads: [thread_count]std.Thread = undefined;
        var j: usize = 0;
        while (j < thread_count) : (j += 1) {
            threads[j] = try std.Thread.spawn(.{}, spamMe, .{count});
        }

        var timer = try std.time.Timer.start();
        var i: usize = 0;
        while (i < count * thread_count) : (i += 1) {
            i += try waker.wait();
        }

        j = 0;
        while (j < thread_count) : (j += 1) {
            threads[j].join();
        }
        elapsed += timer.read();
    }

    Output.prettyErrorln("[EVFILT_MACHPORT] Recv {any}", .{bun.fmt.fmtDuration(elapsed)});
}
var user_waker: AsyncIO.UserFilterWaker = undefined;

fn spamMeUserFilter(count: usize) void {
    Output.Source.configureNamedThread("2");
    defer Output.flush();
    var timer = std.time.Timer.start() catch unreachable;
    var i: usize = 0;
    while (i < count * thread_count) : (i += 1) {
        user_waker.wake() catch unreachable;
    }

    Output.prettyErrorln("[EVFILT_USER]     Sent {any}", .{bun.fmt.fmtDuration(timer.read())});
}
pub fn userMain(runs: usize) anyerror!void {
    defer Output.flush();
    user_waker = try AsyncIO.UserFilterWaker.init(bun.default_allocator);

    var args = try std.process.argsAlloc(bun.default_allocator);
    const count = std.fmt.parseInt(usize, args[args.len - 1], 10) catch 1024;
    var remaining_runs = runs;
    var elapsed: u64 = 0;

    while (remaining_runs > 0) : (remaining_runs -= 1) {
        var threads: [thread_count]std.Thread = undefined;
        var j: usize = 0;
        while (j < thread_count) : (j += 1) {
            threads[j] = try std.Thread.spawn(.{}, spamMeUserFilter, .{count});
        }

        var timer = try std.time.Timer.start();
        var i: usize = 0;
        while (i < count) {
            i += try user_waker.wait();
        }

        j = 0;
        while (j < thread_count) : (j += 1) {
            threads[j].join();
        }
        elapsed += timer.read();
    }

    Output.prettyErrorln("[EVFILT_USER]     Recv {any}", .{bun.fmt.fmtDuration(elapsed)});
    Output.flush();
}

pub fn main() anyerror!void {
    var stdout_ = std.io.getStdOut();
    var stderr_ = std.io.getStdErr();
    var output_source = Output.Source.init(stdout_, stderr_);
    Output.Source.set(&output_source);

    var args = try std.process.argsAlloc(bun.default_allocator);
    const count = std.fmt.parseInt(usize, args[args.len - 1], 10) catch 1024;
    Output.prettyErrorln("For {d} messages and {d} threads:", .{ count, thread_count });
    Output.flush();
    defer Output.flush();
    const runs = if (std.os.getenv("RUNS")) |run_count| try std.fmt.parseInt(usize, run_count, 10) else 1;

    if (std.os.getenv("NO_MACH") == null)
        try machMain(runs);

    if (std.os.getenv("NO_USER") == null)
        try userMain(runs);
}