ac-bench
How-to guides

How-to guide

Write a custom reporter

Turn a run's events into your own output — here a file with one line per gate verdict — with defineReporterPlugin, and select it by id.

Before you start. A suite with a gate that runs with measure -c bench.config.ts.

A reporter is a plugin with an id and a createSink function. The sink receives the run’s events as they happen; the reporter picks the ones it renders. Everything that belongs to the host — the output streams, the terminal, the files — comes in the ReporterContext, so a reporter imports no node: module of its own.

1. Install the plugin API

Terminal
npm install --save-dev @ac-bench/core @ac-kit/app-report

@ac-kit/app-report provides the event and sink types.

2. Write the reporter

This one collects the gate verdicts and, when the run ends, writes one line per verdict to bench-results.verdicts.txt:

verdict-lines.ts
import { defineReporterPlugin, type MeasureData } from "@ac-bench/core/plugin";
import type { ISink, ReportEvent } from "@ac-kit/app-report";

export const verdictLines = defineReporterPlugin({
  id: "verdict-lines",
  createSink: (context): ISink<MeasureData> => {
    const lines: string[] = [];
    return {
      write(event: ReportEvent<MeasureData>) {
        if (event.kind === "data" && event.data.kind === "gate-verdict") {
          const { id, runtime, verdict, message } = event.data.verdict;
          lines.push(`${verdict} ${id} (${runtime}): ${message}\n`);
        }
      },
      flush() {},
      async close() {
        const file = context.openOutput(`${context.defaultOutput}.verdicts.txt`);
        const writer = file.getWriter();
        for (const line of lines) await writer.write(line);
        await writer.close();
      },
    };
  },
});
  • defineReporterPlugin brands the object, so measure recognises it among the plugins.
  • context.openOutput(name) opens a file relative to the working directory.
  • context.defaultOutput is the run’s base name, --output, bench-results by default.

3. List it, then select it

Listing a reporter in plugins configures it; reporters (or --reporter) selects it by id:

bench.config.ts
import { defineConfig } from "@ac-bench/cli";
import durationPlugin from "@ac-bench/measure-duration/plugin";

import { verdictLines } from "./verdict-lines.ts";

export default defineConfig({
  plugins: [durationPlugin, verdictLines],
  reporters: ["table", "verdict-lines"],
});
Terminalexit 0
npx measure -c bench.config.tscat bench-results.verdicts.txtpass ratio:for loop/reduce (node): for loop/reduce = 0.0778 [0.0744, 0.0814] (95 % interval), below 1.1

Variations

context.stdout is the process’s output stream. The table reporter holds it for the whole run, so a reporter that prints there replaces table in reporters rather than joining it. It releases the stream’s writer in close and never closes the stream itself.

Options

A reporter that takes options is a factory: it receives them, then returns defineReporterPlugin({ … }). The options never cross a process boundary, so they need no schema.

Render the case results

event.data.kind === "case-result" carries one case’s result, opaque to the reporter. context.registry resolves its measure id to the measure plugin that can describe it; the built-in reporters are written this way.