tiny.profiling.schema
Defined in tiny.profiling.
API (7)
Actions
Public operations.
Types and contracts
Public types and contracts.
Values and defaults
Public values and defaults.
Source
Source: src/profiling/root.zig:42
zig
pub const schema = @import("schema.zig");Source: src/profiling/schema.zig
zig
const std = @import("std");const json = @import("json.zig");pub const metric_schema = "tiny.profiling.metric/v1";pub const Family = enum { timing, memory, allocation, unknown, pub fn name(self: Family) []const u8 { return @tagName(self); }};pub const Classification = struct { family: Family, strict: bool,};pub fn classify(row: std.json.ObjectMap, source_kind: []const u8) Classification { if (json.string(row.get("schema"))) |schema_name| { if (std.mem.eql(u8, schema_name, metric_schema)) { if (strictTiming(row)) return .{ .family = .timing, .strict = true }; if (strictMemory(row)) return .{ .family = .memory, .strict = true }; return .{ .family = .unknown, .strict = true }; } } if (legacyTiming(row)) return .{ .family = .timing, .strict = false }; if (legacyMemory(row)) return .{ .family = .memory, .strict = false }; if (std.mem.eql(u8, source_kind, "allocations") and allocationEvent(row)) return .{ .family = .allocation, .strict = false }; return .{ .family = .unknown, .strict = false };}pub fn isTimingMetric(row: std.json.ObjectMap) bool { if (json.string(row.get("schema"))) |schema_name| { if (std.mem.eql(u8, schema_name, metric_schema)) return strictTiming(row); } return legacyTiming(row);}pub fn isMemoryMetric(row: std.json.ObjectMap, source_kind: []const u8) bool { if (std.mem.eql(u8, source_kind, "allocations") and allocationEvent(row)) return true; if (json.string(row.get("schema"))) |schema_name| { if (std.mem.eql(u8, schema_name, metric_schema)) return strictMemory(row) or legacyMemory(row); } return legacyMemory(row);}fn strictTiming(row: std.json.ObjectMap) bool { if (!familyIs(row, "timing")) return false; if (!unitIs(row, "ns")) return false; if (!hasIdentity(row)) return false; return hasTimingEvidence(row);}fn strictMemory(row: std.json.ObjectMap) bool { if (!familyIs(row, "memory")) return false; if (!(unitIs(row, "bytes") or unitIs(row, "count"))) return false; if (!hasIdentity(row)) return false; return hasMemoryEvidence(row);}fn legacyTiming(row: std.json.ObjectMap) bool { if (eventIs(row, "bench_end") and hasSampleNs(row)) return true; if (kindIs(row, "summary") and hasSummaryTiming(row)) return true; return false;}fn legacyMemory(row: std.json.ObjectMap) bool { if (eventIs(row, "bench_end") and (hasNumber(row, "alloc_bytes") or hasNumber(row, "alloc_count"))) return true; if (kindIs(row, "summary") and summaryIs(row, "memory") and hasMemoryEvidence(row)) return true; return false;}fn allocationEvent(row: std.json.ObjectMap) bool { const kind = json.string(row.get("kind")) orelse return false; return std.mem.eql(u8, kind, "alloc") or std.mem.eql(u8, kind, "free") or std.mem.eql(u8, kind, "resize") or std.mem.eql(u8, kind, "remap") or std.mem.eql(u8, kind, "allocator");}fn familyIs(row: std.json.ObjectMap, expected: []const u8) bool { return if (json.string(row.get("family"))) |family| std.mem.eql(u8, family, expected) else false;}fn unitIs(row: std.json.ObjectMap, expected: []const u8) bool { return if (json.string(row.get("unit"))) |unit| std.mem.eql(u8, unit, expected) else false;}fn eventIs(row: std.json.ObjectMap, expected: []const u8) bool { return if (json.string(row.get("event"))) |event| std.mem.eql(u8, event, expected) else false;}fn kindIs(row: std.json.ObjectMap, expected: []const u8) bool { return if (json.string(row.get("kind"))) |kind| std.mem.eql(u8, kind, expected) else false;}fn summaryIs(row: std.json.ObjectMap, expected: []const u8) bool { return if (json.string(row.get("summary"))) |summary| std.mem.eql(u8, summary, expected) else false;}fn hasIdentity(row: std.json.ObjectMap) bool { return hasString(row, "name") or hasString(row, "id") or hasString(row, "phase") or hasString(row, "pass") or hasString(row, "analysis") or hasString(row, "metric");}fn hasTimingEvidence(row: std.json.ObjectMap) bool { return hasSampleNs(row) or hasSummaryTiming(row) or hasNumber(row, "ns") or hasNumber(row, "duration_ns") or hasNumber(row, "wall_ns");}fn hasSummaryTiming(row: std.json.ObjectMap) bool { return hasNumber(row, "mean_ns") or hasNumber(row, "median_ns") or hasNumber(row, "p95_ns") or hasNumber(row, "p99_ns");}fn hasMemoryEvidence(row: std.json.ObjectMap) bool { return hasNumber(row, "value") or hasNumber(row, "mean") or hasNumber(row, "median") or hasNumber(row, "alloc_bytes") or hasNumber(row, "alloc_count") or hasNumber(row, "allocated_bytes") or hasNumber(row, "retained_bytes") or hasNumber(row, "high_water_live_bytes");}fn hasSampleNs(row: std.json.ObjectMap) bool { return if (row.get("sample_ns")) |value| json.array(value) catch null != null else false;}fn hasString(row: std.json.ObjectMap, field: []const u8) bool { return if (json.string(row.get(field))) |value| value.len != 0 else false;}fn hasNumber(row: std.json.ObjectMap, field: []const u8) bool { return json.asF64(row.get(field)) != null;}test "profiling schema classifies strict timing rows" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const value = try std.json.parseFromSliceLeaky(std.json.Value, arena.allocator(), \\{"schema":"tiny.profiling.metric/v1","family":"timing","metric":"duration_ns","unit":"ns","name":"x","median_ns":12} , .{}); const classification = classify(try json.object(value), "bench_jsonl"); try std.testing.expectEqual(Family.timing, classification.family); try std.testing.expect(classification.strict);}test "profiling schema classifies memtrace allocation rows" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const value = try std.json.parseFromSliceLeaky(std.json.Value, arena.allocator(), \\{"v":1,"kind":"alloc","allocation_id":1,"len":64} , .{}); const classification = classify(try json.object(value), "allocations"); try std.testing.expectEqual(Family.allocation, classification.family); try std.testing.expect(!classification.strict);}test "profiling schema exposes overlapping timing and memory rows" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const value = try std.json.parseFromSliceLeaky(std.json.Value, arena.allocator(), \\{"schema":"tiny.profiling.metric/v1","event":"bench_end","family":"timing","metric":"duration_ns","unit":"ns","name":"x","sample_ns":[10,20],"alloc_count":2,"alloc_bytes":64} , .{}); const row = try json.object(value); try std.testing.expect(isTimingMetric(row)); try std.testing.expect(isMemoryMetric(row, "bench_jsonl")); try std.testing.expectEqual(Family.timing, classify(row, "bench_jsonl").family);}Audit
| Definitions | 8 |
|---|---|
| Public names | 8 |
| Members | 6 |
| Version | 26.7.0 |
| Revision | daab053ee433 |