|
| 1 | +const std = @import("std"); |
| 2 | +const ast = @import("../selector/ast.zig"); |
| 3 | +const ParseOptions = @import("../html/document.zig").ParseOptions; |
| 4 | + |
| 5 | +pub const QueryInstrumentationKind = enum(u8) { |
| 6 | + one_runtime, |
| 7 | + one_compiled, |
| 8 | + all_runtime, |
| 9 | + all_compiled, |
| 10 | +}; |
| 11 | + |
| 12 | +pub const ParseInstrumentationStats = struct { |
| 13 | + elapsed_ns: u64, |
| 14 | + input_len: usize, |
| 15 | + node_count: usize, |
| 16 | +}; |
| 17 | + |
| 18 | +pub const QueryInstrumentationStats = struct { |
| 19 | + elapsed_ns: u64, |
| 20 | + selector_len: usize, |
| 21 | + kind: QueryInstrumentationKind, |
| 22 | + matched: ?bool = null, |
| 23 | +}; |
| 24 | + |
| 25 | +fn elapsedNs(start: i128, finish: i128) u64 { |
| 26 | + if (finish <= start) return 0; |
| 27 | + return @intCast(finish - start); |
| 28 | +} |
| 29 | + |
| 30 | +fn HookDeclType(comptime H: type) type { |
| 31 | + return switch (@typeInfo(H)) { |
| 32 | + .pointer => |p| p.child, |
| 33 | + else => H, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +fn matchedFromValue(value: anytype) ?bool { |
| 38 | + return switch (@typeInfo(@TypeOf(value))) { |
| 39 | + .optional => value != null, |
| 40 | + else => true, |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +pub fn parseWithHooks(doc: anytype, input: []u8, comptime opts: ParseOptions, hooks: anytype) !void { |
| 45 | + if (comptime @hasDecl(HookDeclType(@TypeOf(hooks)), "onParseStart")) { |
| 46 | + hooks.onParseStart(input.len); |
| 47 | + } |
| 48 | + |
| 49 | + const start = std.time.nanoTimestamp(); |
| 50 | + try doc.parse(input, opts); |
| 51 | + const stats: ParseInstrumentationStats = .{ |
| 52 | + .elapsed_ns = elapsedNs(start, std.time.nanoTimestamp()), |
| 53 | + .input_len = input.len, |
| 54 | + .node_count = doc.nodes.items.len, |
| 55 | + }; |
| 56 | + |
| 57 | + if (comptime @hasDecl(HookDeclType(@TypeOf(hooks)), "onParseEnd")) { |
| 58 | + hooks.onParseEnd(stats); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +pub fn queryOneRuntimeWithHooks(doc: anytype, selector: []const u8, hooks: anytype) @TypeOf(doc.queryOneRuntime(selector)) { |
| 63 | + if (comptime @hasDecl(HookDeclType(@TypeOf(hooks)), "onQueryStart")) { |
| 64 | + hooks.onQueryStart(.one_runtime, selector.len); |
| 65 | + } |
| 66 | + |
| 67 | + const start = std.time.nanoTimestamp(); |
| 68 | + const out = doc.queryOneRuntime(selector); |
| 69 | + if (out) |value| { |
| 70 | + if (comptime @hasDecl(HookDeclType(@TypeOf(hooks)), "onQueryEnd")) { |
| 71 | + hooks.onQueryEnd(QueryInstrumentationStats{ |
| 72 | + .elapsed_ns = elapsedNs(start, std.time.nanoTimestamp()), |
| 73 | + .selector_len = selector.len, |
| 74 | + .kind = .one_runtime, |
| 75 | + .matched = matchedFromValue(value), |
| 76 | + }); |
| 77 | + } |
| 78 | + return value; |
| 79 | + } else |err| { |
| 80 | + if (comptime @hasDecl(HookDeclType(@TypeOf(hooks)), "onQueryEnd")) { |
| 81 | + hooks.onQueryEnd(QueryInstrumentationStats{ |
| 82 | + .elapsed_ns = elapsedNs(start, std.time.nanoTimestamp()), |
| 83 | + .selector_len = selector.len, |
| 84 | + .kind = .one_runtime, |
| 85 | + .matched = null, |
| 86 | + }); |
| 87 | + } |
| 88 | + return err; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +pub fn queryOneCompiledWithHooks(doc: anytype, sel: *const ast.Selector, hooks: anytype) @TypeOf(doc.queryOneCompiled(sel)) { |
| 93 | + if (comptime @hasDecl(HookDeclType(@TypeOf(hooks)), "onQueryStart")) { |
| 94 | + hooks.onQueryStart(.one_compiled, sel.source.len); |
| 95 | + } |
| 96 | + |
| 97 | + const start = std.time.nanoTimestamp(); |
| 98 | + const value = doc.queryOneCompiled(sel); |
| 99 | + if (comptime @hasDecl(HookDeclType(@TypeOf(hooks)), "onQueryEnd")) { |
| 100 | + hooks.onQueryEnd(QueryInstrumentationStats{ |
| 101 | + .elapsed_ns = elapsedNs(start, std.time.nanoTimestamp()), |
| 102 | + .selector_len = sel.source.len, |
| 103 | + .kind = .one_compiled, |
| 104 | + .matched = matchedFromValue(value), |
| 105 | + }); |
| 106 | + } |
| 107 | + return value; |
| 108 | +} |
| 109 | + |
| 110 | +pub fn queryAllRuntimeWithHooks(doc: anytype, selector: []const u8, hooks: anytype) @TypeOf(doc.queryAllRuntime(selector)) { |
| 111 | + if (comptime @hasDecl(HookDeclType(@TypeOf(hooks)), "onQueryStart")) { |
| 112 | + hooks.onQueryStart(.all_runtime, selector.len); |
| 113 | + } |
| 114 | + |
| 115 | + const start = std.time.nanoTimestamp(); |
| 116 | + const out = doc.queryAllRuntime(selector); |
| 117 | + if (out) |iter| { |
| 118 | + if (comptime @hasDecl(HookDeclType(@TypeOf(hooks)), "onQueryEnd")) { |
| 119 | + hooks.onQueryEnd(QueryInstrumentationStats{ |
| 120 | + .elapsed_ns = elapsedNs(start, std.time.nanoTimestamp()), |
| 121 | + .selector_len = selector.len, |
| 122 | + .kind = .all_runtime, |
| 123 | + .matched = null, |
| 124 | + }); |
| 125 | + } |
| 126 | + return iter; |
| 127 | + } else |err| { |
| 128 | + if (comptime @hasDecl(HookDeclType(@TypeOf(hooks)), "onQueryEnd")) { |
| 129 | + hooks.onQueryEnd(QueryInstrumentationStats{ |
| 130 | + .elapsed_ns = elapsedNs(start, std.time.nanoTimestamp()), |
| 131 | + .selector_len = selector.len, |
| 132 | + .kind = .all_runtime, |
| 133 | + .matched = null, |
| 134 | + }); |
| 135 | + } |
| 136 | + return err; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +pub fn queryAllCompiledWithHooks(doc: anytype, sel: *const ast.Selector, hooks: anytype) @TypeOf(doc.queryAllCompiled(sel)) { |
| 141 | + if (comptime @hasDecl(HookDeclType(@TypeOf(hooks)), "onQueryStart")) { |
| 142 | + hooks.onQueryStart(.all_compiled, sel.source.len); |
| 143 | + } |
| 144 | + |
| 145 | + const start = std.time.nanoTimestamp(); |
| 146 | + const iter = doc.queryAllCompiled(sel); |
| 147 | + if (comptime @hasDecl(HookDeclType(@TypeOf(hooks)), "onQueryEnd")) { |
| 148 | + hooks.onQueryEnd(QueryInstrumentationStats{ |
| 149 | + .elapsed_ns = elapsedNs(start, std.time.nanoTimestamp()), |
| 150 | + .selector_len = sel.source.len, |
| 151 | + .kind = .all_compiled, |
| 152 | + .matched = null, |
| 153 | + }); |
| 154 | + } |
| 155 | + return iter; |
| 156 | +} |
0 commit comments