-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.zig
148 lines (135 loc) · 5.89 KB
/
test.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
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
const std = @import("std");
const testing = std.testing;
const sdk = @import("extism");
const Plugin = sdk.Plugin;
const CurrentPlugin = sdk.CurrentPlugin;
const Function = sdk.Function;
const manifest = sdk.manifest;
export fn hello_world(plugin_ptr: ?*sdk.c.ExtismCurrentPlugin, inputs: [*c]const sdk.c.ExtismVal, n_inputs: u64, outputs: [*c]sdk.c.ExtismVal, n_outputs: u64, user_data: ?*anyopaque) callconv(.C) void {
std.debug.print("Hello from Zig!\n", .{});
const str_ud = @as([*:0]const u8, @ptrCast(user_data orelse unreachable));
std.debug.print("User data: {s}\n", .{str_ud});
var input_slice = inputs[0..n_inputs];
var output_slice = outputs[0..n_outputs];
var curr_plugin = CurrentPlugin.getCurrentPlugin(plugin_ptr orelse unreachable);
const input = curr_plugin.inputBytes(&input_slice[0]);
std.debug.print("input: {s}\n", .{input});
if (curr_plugin.hostContext()) |ctx_ptr| {
const ctx: *u64 = @alignCast(@ptrCast(ctx_ptr));
std.debug.print("Host context={}\n", .{ctx.*});
}
output_slice[0] = input_slice[0];
}
const wasmfile_manifest = manifest.WasmFile{ .path = "wasm/code-functions.wasm" };
const man = .{ .wasm = &[_]manifest.Wasm{.{ .wasm_file = wasmfile_manifest }} };
test "Single threaded tests" {
var wasm_start = try std.time.Timer.start();
_ = sdk.setLogFile("test.log", .Debug);
var f = Function.init(
"hello_world",
&[_]sdk.c.ExtismValType{sdk.c.ExtismValType_I64},
&[_]sdk.c.ExtismValType{sdk.c.ExtismValType_I64},
&hello_world,
@constCast(@as(*const anyopaque, @ptrCast("user data"))),
);
defer f.deinit();
var plugin = try Plugin.initFromManifest(testing.allocator, man, &[_]Function{f}, true);
defer plugin.deinit();
std.debug.print("\nregister loaded plugin: {}\n", .{std.fmt.fmtDuration(wasm_start.read())});
const repeat = 1182;
const input = "aeiouAEIOU____________________________________&smtms_y?" ** repeat;
var data = try plugin.call("count_vowels", input);
try testing.expectEqualStrings("{\"count\": 11820}", data);
std.debug.print("register plugin + function call: {}, sent input size: {} bytes\n", .{ std.fmt.fmtDuration(wasm_start.read()), input.len });
var ctx: u64 = 12345;
data = try plugin.callWithContext("count_vowels", input, @ptrCast(&ctx));
try testing.expectEqualStrings("{\"count\": 11820}", data);
std.debug.print("--------------\n", .{});
var i: usize = 0;
var wasm_elapsed: u64 = 0;
while (i < 100) : (i += 1) {
var call_start = try std.time.Timer.start();
_ = try plugin.call("count_vowels", input);
wasm_elapsed += call_start.read();
}
const wasm_avg = wasm_elapsed / i;
i = 0;
var native_elapsed: u64 = 0;
var native_count: u32 = 0;
while (i < 100) : (i += 1) {
var native_start = try std.time.Timer.start();
for (input) |char| {
switch (char) {
'A', 'I', 'E', 'O', 'U', 'a', 'e', 'i', 'o', 'u' => native_count += 1,
else => {},
}
}
native_elapsed += native_start.read();
}
const native_avg = native_elapsed / i;
std.debug.print("native function call (avg, N = {}): {}\n", .{ i, std.fmt.fmtDuration(native_avg) });
std.debug.print("wasm function call (avg, N = {}): {}\n", .{ i, std.fmt.fmtDuration(wasm_avg) });
}
test "Multi threaded tests" {
const S = struct {
fn _test() !void {
var f = Function.init(
"hello_world",
&[_]sdk.c.ExtismValType{sdk.c.ExtismValType_I64},
&[_]sdk.c.ExtismValType{sdk.c.ExtismValType_I64},
&hello_world,
@constCast(@as(*const anyopaque, @ptrCast("user data"))),
);
defer f.deinit();
var plugin = try Plugin.initFromManifest(testing.allocator, man, &[_]Function{f}, true);
defer plugin.deinit();
const output = try plugin.call("count_vowels", "this is a test");
std.debug.print("{s}\n", .{output});
}
};
const t1 = try std.Thread.spawn(.{}, S._test, .{});
const t2 = try std.Thread.spawn(.{}, S._test, .{});
t1.join();
t2.join();
_ = sdk.setLogFile("test.log", .Debug);
var f = Function.init(
"hello_world",
&[_]sdk.c.ExtismValType{sdk.c.ExtismValType_I64},
&[_]sdk.c.ExtismValType{sdk.c.ExtismValType_I64},
&hello_world,
@constCast(@as(*const anyopaque, @ptrCast("user data"))),
);
defer f.deinit();
var plugin = try Plugin.initFromManifest(testing.allocator, man, &[_]Function{f}, true);
defer plugin.deinit();
const output = try plugin.call("count_vowels", "this is a test");
std.debug.print("{s}\n", .{output});
}
test "Plugin Cancellation" {
const loop_manifest = manifest.WasmFile{ .path = "wasm/loop.wasm" };
const loop_man = .{ .wasm = &[_]manifest.Wasm{.{ .wasm_file = loop_manifest }} };
_ = sdk.setLogFile("test.log", .Debug);
var f = Function.init(
"hello_world",
&[_]sdk.c.ExtismValType{sdk.c.ExtismValType_I64},
&[_]sdk.c.ExtismValType{sdk.c.ExtismValType_I64},
&hello_world,
@constCast(@as(*const anyopaque, @ptrCast("user data"))),
);
defer f.deinit();
var plugin = try Plugin.initFromManifest(testing.allocator, loop_man, &[_]Function{f}, true);
defer plugin.deinit();
var handle = plugin.cancelHandle();
const S = struct {
fn _test(h: *sdk.CancelHandle) void {
std.time.sleep(1 * std.time.ns_per_s);
_ = h.cancel();
}
};
_ = try std.Thread.spawn(.{}, S._test, .{&handle});
var call_start = try std.time.Timer.start();
const output = plugin.call("infinite_loop", "abc123");
const call_end = call_start.read();
try std.testing.expectError(error.PluginCallFailed, output);
std.debug.print("Cancelled plugin ran for {}\n", .{std.fmt.fmtDuration(call_end)});
}