diff --git a/.gitignore b/.gitignore index 75aff9a..79876e7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .venv ZipponDB data +benchmark engine engine.o zig-out diff --git a/build.zig b/build.zig index 42b163c..5afce63 100644 --- a/build.zig +++ b/build.zig @@ -25,7 +25,8 @@ pub fn build(b: *std.Build) void { const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); - // All tests + // Test + // ----------------------------------------------- const tests1 = b.addTest(.{ .root_source_file = b.path("src/stuffs/UUIDFileIndex.zig"), .target = target, @@ -93,4 +94,22 @@ pub fn build(b: *std.Build) void { test_step.dependOn(&run_tests4.step); test_step.dependOn(&run_tests5.step); test_step.dependOn(&run_tests6.step); + + // Benchmark + // ----------------------------------------------- + const benchmark = b.addExecutable(.{ + .name = "benchmark", + .root_source_file = b.path("src/benchmark.zig"), + .target = target, + .optimize = optimize, + }); + benchmark.root_module.addImport("dtype", b.createModule(.{ .root_source_file = b.path("lib/types/out.zig") })); + benchmark.root_module.addImport("ZipponData", b.createModule(.{ .root_source_file = b.path("lib/zid.zig") })); + b.installArtifact(benchmark); + + const run_benchmark = b.addRunArtifact(benchmark); + run_benchmark.step.dependOn(b.getInstallStep()); + + const benchmark_step = b.step("benchmark", "Run benchmarks"); + benchmark_step.dependOn(&run_benchmark.step); } diff --git a/src/benchmark.zig b/src/benchmark.zig new file mode 100644 index 0000000..4aac737 --- /dev/null +++ b/src/benchmark.zig @@ -0,0 +1,77 @@ +const std = @import("std"); +const DBEngine = @import("main.zig").DBEngine; +const dtype = @import("dtype"); +const ziqlTokenizer = @import("tokenizers/ziql.zig").Tokenizer; +const ziqlToken = @import("tokenizers/ziql.zig").Token; +const ziqlParser = @import("ziqlParser.zig").Parser; + +pub const std_options = .{ + .logFn = myLog, +}; + +pub fn myLog( + comptime message_level: std.log.Level, + comptime scope: @Type(.EnumLiteral), + comptime format: []const u8, + args: anytype, +) void { + if (true) return; + _ = message_level; + _ = scope; + _ = format; + _ = args; +} + +pub fn main() !void { + var line_buffer: [1024 * 1024]u8 = undefined; + // Initialize your DBEngine here + var db_engine = DBEngine.init("benchmark", "example.zipponschema"); + defer db_engine.deinit(); + + // Define your benchmark queries + const populate = [_][]const u8{ + "ADD User (name = 'Bob', email='bob@email.com', age=55, scores=[ 1 ], best_friend=none, friends=none, bday=2000/01/01, a_time=12:04, last_order=2000/01/01-12:45)", + }; + + // Run benchmarks + const populate_start_time = std.time.nanoTimestamp(); + for (populate) |query| { + const null_term_query_str = try std.fmt.bufPrintZ(&line_buffer, "{s}", .{query}); + for (0..1) |_| { + var toker = ziqlTokenizer.init(null_term_query_str); + var parser = ziqlParser.init(&toker, &db_engine.file_engine, &db_engine.schema_engine); + try parser.parse(); + } + } + + const populate_end_time = std.time.nanoTimestamp(); + const populate_duration = @as(f64, @floatFromInt(populate_end_time - populate_start_time)) / 1e9; + + std.debug.print("Populate duration: {d:.6} seconds\n\n", .{populate_duration}); + + // Define your benchmark queries + const queries = [_][]const u8{ + "GRAB User {}", + "GRAB User [1] {}", + "GRAB User [name] {}", + "GRAB User {name = 'Adrien'}", + "GRAB User {age > 30}", + }; + + // Run benchmarks + std.debug.print("Running benchmarks...\n", .{}); + for (queries) |query| { + const start_time = std.time.nanoTimestamp(); + + // Execute the query here + const null_term_query_str = try std.fmt.bufPrintZ(&line_buffer, "{s}", .{query}); + var toker = ziqlTokenizer.init(null_term_query_str); + var parser = ziqlParser.init(&toker, &db_engine.file_engine, &db_engine.schema_engine); + try parser.parse(); + + const end_time = std.time.nanoTimestamp(); + const duration = @as(f64, @floatFromInt(end_time - start_time)) / 1e9; + + std.debug.print("Query: {s}\nDuration: {d:.6} seconds\n\n", .{ query, duration }); + } +} diff --git a/src/main.zig b/src/main.zig index 9de3593..dbe6c52 100644 --- a/src/main.zig +++ b/src/main.zig @@ -56,6 +56,8 @@ pub fn myLog( comptime format: []const u8, args: anytype, ) void { + if (true) return; + const level_txt = comptime message_level.asText(); const prefix = if (scope == .default) " - " else "(" ++ @tagName(scope) ++ ") - "; diff --git a/src/stuffs/utils.zig b/src/stuffs/utils.zig index 6b0d9d4..67b13ed 100644 --- a/src/stuffs/utils.zig +++ b/src/stuffs/utils.zig @@ -45,7 +45,7 @@ const stdout = std.io.getStdOut().writer(); // Maybe create a struct for that pub fn send(comptime format: []const u8, args: anytype) void { - if (false) return; + if (true) return; stdout.print(format, args) catch |err| { log.err("Can't send: {any}", .{err});