simplebench.reporters.json.reporter.reporter moduleπ
Reporter for benchmark results using JSON files.
- class simplebench.reporters.json.reporter.reporter.JSONReporter(
- config: JSONConfig | None = None,
Bases:
ReporterClass for outputting benchmark results to JSON files.
It supports reporting statistics for various sections, either separately or together, to the filesystem, via a callback function, or to the console in JSON format.
The JSON files are tagged with metadata comments including the case title, description, and units for clarity.
Defined command-line flags:
--json: {filesystem, console, callback}(default=filesystem) Outputs statistical results to JSON.--json-data: {filesystem, console, callback}(default=filesystem) Outputs results to JSON with full data.
Example usage:
program.py --json # Outputs results to JSON files in the filesystem (default). program.py --json filesystem # Outputs results to JSON files in the filesystem. program.py --json console # Outputs results to the console in JSON format. program.py --json callback # Outputs results via a callback function in JSON format. program.py --json filesystem console # Outputs results to both JSON files and the console.
- Variables:
name (str) β The unique identifying name of the reporter.
description (str) β A brief description of the reporter.
choices (Choices) β A collection of
Choicesinstances defining the reporter instance, CLI flags,Choicename, supportedSectionobjects, supported outputTargetobjects, and supported outputFormatobjects for the reporter.targets (set[Target]) β The supported output targets for the reporter.
formats (set[Format]) β The supported output formats for the reporter.
Initialize the JSONReporter.
Note
The exception documentation below refers to validation of subclass configuration class variables
_OPTIONS_TYPEand_OPTIONS_KWARGS. These must be correctly defined in any subclass ofJSONReporterto ensure proper functionality.- Parameters:
config (JSONConfig | None) β An optional configuration object to override default reporter settings. If not provided, default settings will be used.
- Raises:
SimpleBenchTypeError β If the subclass configuration types are invalid.
SimpleBenchValueError β If the subclass configuration values are invalid.
- render(
- *,
- case: Case,
- section: Section,
- options: ReporterOptions,
Convert the Case data for all sections to a JSON string.
Machine info is included in the JSON output under the βmetadataβ key.
- Parameters:
case β The
Caseinstance holding the benchmarked code statistics.section β The
Sectionto render (ignored, all sections are included).options β The
JSONOptionsinstance specifying rendering options orNoneif not provided. (JSONOptionsis a subclass ofReporterOptions.)
- Returns:
The JSON string representation of the
Casedata.
- run_report(
- *,
- args: Namespace,
- log_metadata: ReportLogMetadata,
- case: Case,
- choice: Choice,
- path: Path | None = None,
- session: Session | None = None,
- callback: ReporterCallback | None = None,
Output the benchmark results to a file as tagged JSON if available.
This method is called by the base classβs
report()method after validation. The base class handles validation of the arguments, so subclasses can assume the arguments are valid without a large amount of boilerplate code. The base class also handles lazy loading of the reporter classes, so subclasses can assume any required imports are available.The
run_report()methodβs main responsibilities are to select the appropriate output method (render_by_case()in this case) based on the provided arguments and to pass the actual rendering method to be used (therender()method in this case). The rendering method must conform with theReportRendererprotocol.- Parameters:
args β The parsed command-line arguments.
log_metadata β The
ReportLogMetadatainstance containing metadata about the report being generated.case β The
Caseinstance representing the benchmarked code.choice β The
Choiceinstance specifying the report configuration.path β The path to the directory where the JSON file(s) will be saved.
reports_log_path β The path to the reports log file.
session β The
Sessioninstance containing benchmark results.callback β A callback function for additional processing of the report. The function should accept two arguments: the
Caseinstance and the JSON data as a string. Leave asNoneif no callback is needed.