tiny.profiling.experiment.receipt
Defined in experiment.
API (4)
Actions
Public operations.
Types and contracts
Public types and contracts.
Source
Source: src/profiling/experiment/receipt.zig
zig
const std = @import("std");const capture = @import("capture");const pretty = @import("pretty");const sys = @import("sys");const profiling = @import("../root.zig");const acquire = @import("acquire.zig");const model = @import("model.zig");const variant = @import("variant.zig");const environment = profiling.environment;const execute = profiling.execute;const fingerprint = profiling.fingerprint;const host = profiling.host;const scenario = profiling.scenario;const pretty_json = pretty.json;pub const Assessment = struct { verdict: model.Verdict, support: model.Support, reason: []const u8, baseline_mean: ?f64 = null, candidate_mean: ?f64 = null, percent_change: ?f64 = null, interval: ?model.Interval = null,};pub const Document = struct { run_id: []const u8, artifact_root: []const u8, plan_path: []const u8, plan_file: fingerprint.File, plan: model.Plan, started_unix_ns: i128, finished_unix_ns: i128, host_environment: environment.Host, host_state_start: host.state.Snapshot, baseline: variant.Prepared, candidate: variant.Prepared, setup: [2]?acquire.CommandResult, calibration: ?acquire.PhaseOutcome, sample_plan: ?model.SamplePlan, evaluation: ?acquire.PhaseOutcome, assessment: Assessment,};pub fn writeFile( allocator: std.mem.Allocator, path: []const u8, document: Document,) !void { const pending = try std.fmt.allocPrint( allocator, "{s}.pending", .{path}, ); defer sys.fs.deleteFile(pending) catch {}; var file = try sys.fs.createFile(pending, .{ .truncate = true }); var open = true; defer if (open) file.close(sys.fs.debugIo()); var buffer: [64 * 1024]u8 = undefined; var writer = file.writer(sys.fs.debugIo(), &buffer); var out = pretty_json.Writer.init(&writer.interface, .minified); try writeJson(&out, document); try writer.interface.writeByte('\n'); try writer.interface.flush(); file.close(sys.fs.debugIo()); open = false; try sys.fs.rename(pending, path);}pub fn writeJson( out: *pretty_json.Writer, document: Document,) !void { try out.beginObject(); try field(out, "schema", model.receipt_schema); try field(out, "run_id", document.run_id); try field(out, "artifact_root", document.artifact_root); try field(out, "started_unix_ns", document.started_unix_ns); try field(out, "finished_unix_ns", document.finished_unix_ns); try writePlanSource(out, document); try writeMethod(out, document.plan); try writeEnvironment(out, document); try out.objectField("plan"); try writePlan(out, document.plan); try out.objectField("variants"); try out.beginObject(); try out.objectField("baseline"); try writePrepared(out, document.baseline); try out.objectField("candidate"); try writePrepared(out, document.candidate); try out.endObject(); try out.objectField("setup"); try writeSetup(out, document.setup); try out.objectField("calibration"); try writeOptionalPhase(out, document.calibration, true); try out.objectField("sample_plan"); try out.write(document.sample_plan); try out.objectField("evaluation"); try writeOptionalPhase(out, document.evaluation, false); try out.objectField("assessment"); try writeAssessment(out, document.assessment); try out.endObject();}fn writePlanSource( out: *pretty_json.Writer, document: Document,) !void { const hex = document.plan_file.hex(); try out.objectField("plan_source"); try out.beginObject(); try field(out, "path", document.plan_path); try field(out, "bytes", document.plan_file.bytes); try field(out, "sha256", hex[0..]); try out.endObject();}fn writeMethod( out: *pretty_json.Writer, plan: model.Plan,) !void { try out.objectField("method"); try out.beginObject(); try field(out, "design", "discarded_calibration_then_fixed_evaluation"); try field(out, "pairing", "matched_baseline_candidate_process_samples"); try field(out, "ordering", "balanced_seeded_within_pair_interleaving"); try field(out, "stopping", "evaluation_pair_count_fixed_before_evaluation"); try field( out, "planning_assumption", "independent paired log-ratios with approximate normal sample planning", ); try field( out, "evidence", "deterministic_percentile_bootstrap_paired_mean_percent_change", ); try field(out, "confidence", model.confidence); try field(out, "power", model.power); try field(out, "bootstrap_iterations", capture.compare.effect.bootstrap_iterations); try field(out, "practical_effect_percent", plan.practical_effect_percent); try out.endObject();}fn writeEnvironment( out: *pretty_json.Writer, document: Document,) !void { try out.objectField("environment"); try out.beginObject(); try out.objectField("host"); try environment.writeJson(out, document.host_environment); try out.objectField("host_state_start"); try host.state.writeJson(out, document.host_state_start); try out.endObject();}fn writePlan(out: *pretty_json.Writer, plan: model.Plan) !void { try out.beginObject(); try field(out, "schema", model.schema); try field(out, "name", plan.name); try out.objectField("scenario"); try writeScenario(out, plan.scenario); try out.objectField("baseline"); try writeVariantSource(out, plan.baseline); try out.objectField("candidate"); try writeVariantSource(out, plan.candidate); try field(out, "metric", @tagName(plan.metric)); try field(out, "metric_unit", plan.metric.unit()); try field(out, "practical_effect_percent", plan.practical_effect_percent); try field(out, "seed", plan.seed); try out.objectField("design"); try out.write(plan.design); try out.endObject();}fn writeScenario( out: *pretty_json.Writer, definition: scenario.Definition,) !void { try out.beginObject(); try field(out, "schema", scenario.schema); try field(out, "name", definition.name); switch (definition.input) { .catalog => |input| { try field(out, "input_kind", "catalog"); try field(out, "workload", input.workload); try out.objectField("args"); try out.write(input.args); }, .command => |argv| { try field(out, "input_kind", "command"); try out.objectField("argv"); try out.write(argv); }, } try field(out, "cwd", definition.cwd); try out.objectField("env"); try out.write(definition.environment); try out.objectField("setup"); try out.write(definition.setup); try out.objectField("reset"); try out.write(definition.reset); try out.objectField("oracle"); try out.write(definition.oracle); try out.endObject();}fn writeVariantSource( out: *pretty_json.Writer, source: model.Variant,) !void { try out.beginObject(); switch (source) { .binary => |path| { try field(out, "kind", "binary"); try field(out, "path", path); }, .git_ref => |git_ref| { try field(out, "kind", "git_ref"); try field(out, "ref", git_ref.name); try out.objectField("build"); try out.write(git_ref.build); }, } try out.endObject();}fn writePrepared( out: *pretty_json.Writer, prepared: variant.Prepared,) !void { const file_hex = prepared.file.hex(); try out.beginObject(); try field(out, "side", @tagName(prepared.side)); try out.objectField("source"); try writeVariantSource(out, prepared.source); try field(out, "resolved_ref", prepared.resolved_ref); try field(out, "build_command", prepared.build_command); try field(out, "source_root", prepared.root); try field(out, "default_cwd", prepared.default_cwd); try field(out, "archived_binary_path", prepared.archived_binary_path); try out.objectField("binary_fingerprint"); try out.beginObject(); try field(out, "bytes", prepared.file.bytes); try field(out, "sha256", file_hex[0..]); try out.endObject(); try out.objectField("code_layout"); if (prepared.code_layout) |layout| { const layout_hex = layout.hex(); try out.beginObject(); try field(out, "schema", capture.code_layout.schema); try field(out, "sha256", layout_hex[0..]); try field(out, "executable_sections", layout.executable_sections); try field(out, "executable_bytes", layout.executable_bytes); try field(out, "first_address", layout.first_address); try field(out, "last_address_end", layout.last_address_end); try out.endObject(); } else { try out.write(null); } try out.endObject();}fn writeSetup( out: *pretty_json.Writer, setup: [2]?acquire.CommandResult,) !void { try out.beginObject(); try out.objectField("baseline"); try writeOptionalCommand(out, setup[0]); try out.objectField("candidate"); try writeOptionalCommand(out, setup[1]); try out.endObject();}fn writeOptionalCommand( out: *pretty_json.Writer, command: ?acquire.CommandResult,) !void { if (command) |value| { try writeCommand(out, value); } else { try out.write(null); }}fn writeCommand( out: *pretty_json.Writer, command: acquire.CommandResult,) !void { try out.beginObject(); try field(out, "command", command.command); try field(out, "cwd", command.cwd); try field(out, "stdout_path", command.stdout_path); try field(out, "stderr_path", command.stderr_path); try out.objectField("execution"); try writeExecution(out, command.execution); try out.endObject();}fn writeOptionalPhase( out: *pretty_json.Writer, outcome: ?acquire.PhaseOutcome, discarded: bool,) !void { if (outcome) |value| { try writePhase(out, value, discarded); } else { try out.write(null); }}fn writePhase( out: *pretty_json.Writer, outcome: acquire.PhaseOutcome, discarded: bool,) !void { try out.beginObject(); try field(out, "discarded_from_evaluation", discarded); try field(out, "support", @tagName(outcome.support)); try field(out, "scheduled_pairs", outcome.orders.len); try field(out, "completed_pairs", outcome.baseline_values.len); try out.objectField("orders"); try out.write(outcome.orders); try out.objectField("baseline_values"); try out.write(outcome.baseline_values); try out.objectField("candidate_values"); try out.write(outcome.candidate_values); try out.objectField("observations"); try out.beginArray(); for (outcome.observations) |observation| { try writeObservation(out, observation); } try out.endArray(); try out.endObject();}fn writeObservation( out: *pretty_json.Writer, observation: acquire.Observation,) !void { try out.beginObject(); try field(out, "sequence", observation.sequence); try field(out, "phase", @tagName(observation.phase)); try field(out, "pair_index", observation.pair_index); try field(out, "position_in_pair", observation.position_in_pair); try field(out, "pair_order", @tagName(observation.pair_order)); try field(out, "side", @tagName(observation.side)); try field(out, "command", observation.command); try field(out, "cwd", observation.cwd); try out.objectField("reset"); try writeOptionalCommand(out, observation.reset); try field(out, "stdout_path", observation.stdout_path); try field(out, "stderr_path", observation.stderr_path); try out.objectField("execution"); try writeExecution(out, observation.execution); try field(out, "metric_value", observation.metric_value); try out.objectField("oracle"); try writeOracle(out, observation.oracle); try out.objectField("host_state"); try writeHostWindow(out, observation.host_state); try field(out, "support", @tagName(observation.support)); try out.endObject();}fn writeExecution( out: *pretty_json.Writer, execution: execute.Result,) !void { try out.beginObject(); inline for (.{ "pid", "exit_code", "wall_ns", "resource_usage_source", "maxrss_kib", "user_s", "system_s", "minor_page_faults", "major_page_faults", "voluntary_context_switches", "involuntary_context_switches", }) |name| { try out.objectField(name); try out.write(@field(execution, name)); } try out.endObject();}fn writeOracle( out: *pretty_json.Writer, result: @import("oracle.zig").Result,) !void { const stdout_hex = result.stdout_sha256; const stderr_hex = result.stderr_sha256; try out.beginObject(); try field(out, "passed", result.passed); try field(out, "exit_code_matches", result.exit_code_matches); try field(out, "stdout_sha256", stdout_hex[0..]); try field(out, "stderr_sha256", stderr_hex[0..]); try field(out, "stdout_matches", result.stdout_matches); try field(out, "stderr_matches", result.stderr_matches); try out.objectField("files"); try out.beginArray(); for (result.files) |file| { try out.beginObject(); try field(out, "path", file.path); try field(out, "expected_sha256", file.expected_sha256); try out.objectField("actual_sha256"); if (file.actual_sha256) |actual| { try out.write(actual[0..]); } else { try out.write(null); } try field(out, "passed", file.passed); try out.endObject(); } try out.endArray(); try out.endObject();}fn writeHostWindow( out: *pretty_json.Writer, window: host.state.Window,) !void { try out.beginObject(); try field(out, "placement", "immediately_before_and_after_timed_child"); try field(out, "condition", @tagName(window.condition)); try out.objectField("before"); try host.state.writeJson(out, window.before); try out.objectField("after"); try host.state.writeJson(out, window.after); try out.endObject();}fn writeAssessment( out: *pretty_json.Writer, assessment: Assessment,) !void { try out.beginObject(); try field(out, "verdict", @tagName(assessment.verdict)); try field(out, "support", @tagName(assessment.support)); try field(out, "reason", assessment.reason); try field(out, "baseline_mean", assessment.baseline_mean); try field(out, "candidate_mean", assessment.candidate_mean); try field(out, "percent_change", assessment.percent_change); try out.objectField("confidence_interval_percent"); if (assessment.interval) |interval| { try out.beginObject(); try field(out, "low", interval.low_percent); try field(out, "high", interval.high_percent); try out.endObject(); } else { try out.write(null); } try out.endObject();}fn field( out: *pretty_json.Writer, name: []const u8, value: anytype,) !void { try out.objectField(name); try out.write(value);}test "profiling experiment receipt preserves typed scenario policy" { var buffer = std.Io.Writer.Allocating.init(std.testing.allocator); defer buffer.deinit(); var out = pretty_json.Writer.init(&buffer.writer, .minified); try writeScenario(&out, .{ .name = "startup", .input = .{ .command = &.{ "{binary}", "--version" } }, .environment = &.{.{ .name = "MODE", .value = "{side}" }}, .reset = .{ .argv = &.{"/bin/true"} }, .oracle = .{ .exit_code = 0 }, }); try std.testing.expect( std.mem.indexOf(u8, buffer.written(), "\"input_kind\":\"command\"") != null, ); try std.testing.expect( std.mem.indexOf(u8, buffer.written(), "\"reset\"") != null, ); try std.testing.expect( std.mem.indexOf(u8, buffer.written(), "\"oracle\"") != null, );}test "profiling experiment receipt retains child process identity" { var buffer = std.Io.Writer.Allocating.init(std.testing.allocator); defer buffer.deinit(); var out = pretty_json.Writer.init(&buffer.writer, .minified); try writeExecution(&out, .{ .pid = 4242, .exit_code = 0, .wall_ns = 10, }); try std.testing.expect( std.mem.indexOf(u8, buffer.written(), "\"pid\":4242") != null, );}Source: src/profiling/experiment/root.zig:6
zig
pub const receipt = @import("receipt.zig");Complete call list for experiment.receipt.writeJson
9 direct calls.
src.profiling.experiment.receipt.field[function] — private; no exact target atsrc/profiling/experiment/receipt.zig:462in nearest public ownertiny.profiling.experiment.receiptsrc.profiling.experiment.receipt.writeAssessment[function] — private; no exact target atsrc/profiling/experiment/receipt.zig:439in nearest public ownertiny.profiling.experiment.receiptsrc.profiling.experiment.receipt.writeEnvironment[function] — private; no exact target atsrc/profiling/experiment/receipt.zig:147in nearest public ownertiny.profiling.experiment.receiptsrc.profiling.experiment.receipt.writeMethod[function] — private; no exact target atsrc/profiling/experiment/receipt.zig:120in nearest public ownertiny.profiling.experiment.receiptsrc.profiling.experiment.receipt.writeOptionalPhase[function] — private; no exact target atsrc/profiling/experiment/receipt.zig:304in nearest public ownertiny.profiling.experiment.receiptsrc.profiling.experiment.receipt.writePlan[function] — private; no exact target atsrc/profiling/experiment/receipt.zig:160in nearest public ownertiny.profiling.experiment.receiptsrc.profiling.experiment.receipt.writePlanSource[function] — private; no exact target atsrc/profiling/experiment/receipt.zig:107in nearest public ownertiny.profiling.experiment.receiptsrc.profiling.experiment.receipt.writePrepared[function] — private; no exact target atsrc/profiling/experiment/receipt.zig:231in nearest public ownertiny.profiling.experiment.receiptsrc.profiling.experiment.receipt.writeSetup[function] — private; no exact target atsrc/profiling/experiment/receipt.zig:267in nearest public ownertiny.profiling.experiment.receipt
Audit
| Definitions | 5 |
|---|---|
| Public names | 5 |
| Members | 23 |
| Version | 26.7.0 |
| Revision | daab053ee433 |