Compare commits

..

2 commits
main ... zbench

Author SHA1 Message Date
4feb810d93
wip: zbench 2024-10-24 21:03:45 +02:00
8484063764
wip: zBench not ready for 0.13.0 2024-06-11 22:23:01 +02:00
4 changed files with 47 additions and 3 deletions

View file

@ -7,6 +7,9 @@ pub fn build(b: *std.Build) void {
const clap_dep = b.dependency("clap", .{ .target = target, .optimize = optimize });
const clap = clap_dep.module("clap");
const opts = .{ .target = target, .optimize = optimize };
const zbench_module = b.dependency("zbench", opts).module("zbench");
var opt = b.addOptions();
opt.addOption([]const u8, "version", "0.0.2");
const exe = b.addExecutable(.{
@ -17,6 +20,8 @@ pub fn build(b: *std.Build) void {
});
exe.root_module.addImport("build_info", opt.createModule());
exe.root_module.addImport("clap", clap);
exe.root_module.addImport("zbench", zbench_module);
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());

View file

@ -8,5 +8,9 @@
.url = "https://github.com/Hejsil/zig-clap/archive/master/latest.tar.gz",
.hash = "1220c900c70daf3e715fad6f266ec14b1d0f5e6c2d3f34b32142f60306cb9b5e5f05",
},
.zbench = .{
.url = "https://github.com/hendriknielaender/zbench/archive/0.13.0/latest.tar.gz",
.hash = "1220b7219ed9b700aaac2a5f84af3c2b3a7c8f714f701511912434525f41735f950b",
},
},
}

View file

@ -1,12 +1,14 @@
const std = @import("std");
const clap = @import("clap");
const build_info = @import("build_info");
// const build_info = @import("build_info");
const base64 = std.base64;
const debug = std.debug;
const io = std.io;
const zbench = @import("zbench");
pub fn main() !void {
const params = comptime clap.parseParamsComptime(
\\-b, --benchmark Benchmark
\\-h, --help Display this help and exit.
\\-d, --decode Decode base64 to string.
\\-e, --encode Encode string to base64.
@ -32,9 +34,17 @@ pub fn main() !void {
const stdout = std.io.getStdOut();
if (res.args.benchmark != 0) {
var bench = zbench.Benchmark.init(allocator, .{});
defer bench.deinit();
try bench.add("decode", bench_decode, .{});
try bench.run(io.getStdOut().writer());
}
if (res.args.help != 0) {
usage();
try clap.help(std.io.getStdErr().writer(), clap.Help, &params, .{});
try clap.help(io.getStdErr().writer(), clap.Help, &params, .{});
return;
}
@ -115,7 +125,8 @@ fn usage() void {
}
fn version() void {
std.debug.print("b64 - {s}\n", .{build_info.version});
// std.debug.print("b64 - {s}\n", .{build_info.version});
std.debug.print("b64 - {s}\n", .{"0.3.0"});
}
fn read_from_stream(allocator: std.mem.Allocator, stream: std.fs.File) !std.ArrayList(u8) {
@ -161,6 +172,20 @@ fn decode(allocator: std.mem.Allocator, b64string: []const u8) ![]u8 {
return decodedString;
}
fn bench_decode(allocator: std.mem.Allocator) void {
const base64String = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA==";
try decode(allocator, base64String);
}
test "bench decode" {
var bench = try zbench.Benchmark.init(std.testing.allocator, .{});
defer bench.deinit();
try bench.add("bench decode", bench_decode, .{});
try bench.run(std.io.getStdout().Writer());
}
test "base64_decode() returns decoded string" {
const test_allocator = std.testing.allocator;
const base64String = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA==";

10
src/root.zig Normal file
View file

@ -0,0 +1,10 @@
const std = @import("std");
const testing = std.testing;
export fn add(a: i32, b: i32) i32 {
return a + b;
}
test "basic add functionality" {
try testing.expect(add(3, 7) == 10);
}