model 教程
本教程展示如何用共享类型描述一次基准测试运行:可在不同运行之间比较的环境快照、可以记录下来的协议、说明操作为何没有值的结果,以及从结果中得出的部署策略。每个示例都是一个完整的测试。
| 我想要 | 用法 |
|---|---|
| 描述一次运行所用的机器 | @model.EnvironmentSnapshot::new,或用 @env_detect.detect 填充它 |
| 判断两次运行能否比较 | @model.environment_compatible |
| 写下一次运行是如何测量的 | @model.RunProtocol::new 和 @model.protocol_identity |
| 为每次运行提供独立的 id | @model.run_identity(由运行器调用) |
| 说明一个操作为何没有值 | @model.ExecutionOutcome 的各个分支 |
| 把交叉点变成部署规则 | @model.CrossoverResult 和 @model.DeploymentPolicy |
快速上手
moon add Luna-Flow/mare_mark@0.4.2
import {
"Luna-Flow/mare_mark/model",
"Luna-Flow/mare_mark/runner",
}
只有运行 id 示例中的协议预设才需要 runner。
描述机器,并检查两次运行能否相互比较:
test "describe two runs" {
let semantic = @model.SemanticEnvironment::new(@model.ExecutionTarget::Native, "moonc 0.10.14", "release", "f64")
let performance = @model.PerformanceEnvironment::new(
"native", "AMD EPYC 7763", "default", 1, "monotonic", frequency_policy="performance",
)
let first = @model.EnvironmentSnapshot::new(
semantic, performance,
@model.ProvenanceEnvironment::new("linux", "ci-7", "2026-10-08T08:00:00Z", "a1b2c3", "nightly-101"),
)
let second = @model.EnvironmentSnapshot::new(
semantic, performance,
@model.ProvenanceEnvironment::new("linux", "ci-3", "2026-10-09T08:00:00Z", "d4e5f6", "nightly-102"),
)
inspect(@model.environment_compatible(first, second), content="true")
}
不同的主机、日期和修订版本无关紧要;声明的硬件、工具链和标志才重要。对于测试,或者对于你从别处描述的机器,手写快照是正确的选择;对于运行基准测试的机器,@env_detect.detect 会填写同样的三个部分(参见 env_detect 教程)。
日常任务
把协议与结果一起记录
test "a protocol and its identity" {
let protocol = @model.RunProtocol::new(
@model.ExperimentDesign::FixedDatasetRepeatedMeasurements,
3,
Some(5000.0),
@model.CalibrationProtocol::new(5000.0, 1, 10000, 250000.0, @model.BatchPolicy::PerImplementation),
1.0,
@model.OrderPolicy::BalancedBlocks(1UL),
@model.OutlierPolicy::ReportOnly,
@model.ValidationCoverage::EveryDataset,
3,
10,
)
inspect(@model.protocol_identity(protocol), content="mmkp_2:ee0e3d65a8dfe6b6")
let reseeded = @model.RunProtocol::new(
protocol.experiment_design,
protocol.warmup_iterations,
protocol.warmup_time_us,
protocol.calibration,
protocol.practical_delta_pct,
@model.OrderPolicy::BalancedBlocks(2UL),
protocol.outlier_policy,
protocol.validation_coverage,
protocol.exploratory_samples,
protocol.confirmatory_samples,
)
inspect(@model.protocol_identity(reseeded) == @model.protocol_identity(protocol), content="false")
}
该标识是每个协议字段的 64 位摘要,因此改变其中任何一个(这里只改变了区组顺序的种子)都会得到另一个键。它是键,而不是记录:运行器还会把完整的协议存入 JSONL 摘要,@event.protocol_from_json 可以从那里读回它。
为每次运行提供独立的 id
运行器用 run_identity 为一次运行命名:用例、协议标识、种子,以及环境来源信息中的运行 id 和时间戳。
test "run ids differ between runs" {
let protocol = @runner.ProtocolPreset::QuickCheck.validated().protocol
let monday = @model.ProvenanceEnvironment::new("linux", "ci-7", "2026-10-08T08:00:00Z", "a1b2c3", "nightly-101")
let tuesday = @model.ProvenanceEnvironment::new("linux", "ci-7", "2026-10-09T08:00:00Z", "a1b2c3", "nightly-102")
inspect(@model.run_identity("sum", protocol, 42UL, monday), content="sum|mmkp_2:a8272a0d9e84b872|42|nightly-101|2026-10-08T08:00:00Z")
inspect(@model.run_identity("sum", protocol, 42UL, monday) == @model.run_identity("sum", protocol, 42UL, tuesday), content="false")
}
保持来源信息中的运行 id 唯一(例如使用 @env_detect.new_run_id),一长串运行的运行 id 就永远不会冲突。
返回精确的结果
无法处理某个输入的实现应如实说明,而不是返回一个伪造的值:
fn checked_sqrt(x : Double) -> @model.OperationResult[Double, Unit] {
if x < 0.0 {
@model.OperationResult::new(Unsupported("negative input"), Some(()))
} else {
@model.OperationResult::completed(x.sqrt(), ())
}
}
test "unsupported is not wrong" {
let result = checked_sqrt(-4.0)
inspect(result.outcome.kind(), content="unsupported")
inspect(result.outcome.value_option() is None, content="true")
inspect(checked_sqrt(9.0).outcome.value_option() == Some(3.0), content="true")
}
运行器把 Unsupported 与失败分开计数,报告会在能力矩阵中列出它。
把交叉点转化为部署策略
test "piecewise deployment" {
let crossover : @model.CrossoverResult[Int] = @model.CrossoverResult::found(
@model.ScaleBoundary::new(64, 128), "piecewise-confirmed", ["A", "A", "B"],
)
guard crossover is Found(boundary, _, evidence) else { fail("no crossover") }
let policy : @model.DeploymentPolicy[Int, String] = Piecewise([
@model.Region::new(None, Some(boundary.below), "scalar", evidence),
@model.Region::new(Some(boundary.at_or_above), None, "blocked", evidence),
])
guard policy is Piecewise(regions) else { fail("not piecewise") }
inspect(regions.length(), content="2")
}
更进一步
- 手工构建
Observation、Validation和RunSummary值来测试你自己的接收器;event 教程就是这样做的。 - 对有文档记录的偏差(例如某个库按设计以不同方式舍入)使用
ExpectedDifference,使它们被计数而不是被隐藏。 - 在读取外来的 JSONL 之前检查
ArtifactVersion::V1.identifier()。
常见陷阱
- 含糊地描述环境。
"cpu"与其他任何"cpu"都相匹配;请写明型号和频率策略。 - 复用来源信息中的运行 id。
RunSummary.run_id包含它;用例、协议、种子、来源信息运行 id 和时间戳都相同的运行会共享同一个 id。 - 比较
mmkp_1键和mmkp_2键。 在标识覆盖每个字段之前写出的记录携带mmkp_1:…键;它们无法与新键比较。请改为比较记录的协议。 - 以为运行器会依据每个协议字段行事。
practical_delta_pct和outlier_policy不会改变测量的内容;报告在比较实现时应用它们。