aboutsummaryrefslogtreecommitdiff
path: root/src/output.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-11-28 18:30:49 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-11-28 18:30:49 -0800
commit77d0828179ae21f86abead5f5c58a9c7d357053c (patch)
tree7d9b5ecae07d995c63a87bfb11aa132a6dd8b4b9 /src/output.zig
parenteccd854dbc50444a3665b31f170ab94451f3cd04 (diff)
downloadbun-77d0828179ae21f86abead5f5c58a9c7d357053c.tar.gz
bun-77d0828179ae21f86abead5f5c58a9c7d357053c.tar.zst
bun-77d0828179ae21f86abead5f5c58a9c7d357053c.zip
[internal] Add debug timer
Diffstat (limited to 'src/output.zig')
-rw-r--r--src/output.zig30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/output.zig b/src/output.zig
index d6ba81a3b..55cd16677 100644
--- a/src/output.zig
+++ b/src/output.zig
@@ -617,3 +617,33 @@ pub fn printError(comptime fmt: string, args: anytype) void {
std.fmt.format(source.error_stream.writer(), fmt, args) catch {};
}
}
+
+pub const DebugTimer = struct {
+ timer: @import("global.zig").DebugOnly(std.time.Timer) = undefined,
+
+ pub fn start() DebugTimer {
+ if (comptime Environment.isDebug) {
+ return DebugTimer{
+ .timer = std.time.Timer.start() catch unreachable,
+ };
+ } else {
+ return .{};
+ }
+ }
+
+ pub const WriteError = error{};
+
+ pub fn format(self: DebugTimer, comptime _: []const u8, opts: std.fmt.FormatOptions, writer_: anytype) WriteError!void {
+ if (comptime Environment.isDebug) {
+ var timer = self.timer;
+ var _opts = opts;
+ _opts.precision = 3;
+ std.fmt.formatFloatDecimal(
+ @floatCast(f64, @intToFloat(f128, timer.read()) / std.time.ns_per_ms),
+ _opts,
+ writer_,
+ ) catch unreachable;
+ writer_.writeAll("ms") catch unreachable;
+ }
+ }
+};