simplebench.reporters.json packageπ
JSON Reporter public API.
Purpose is to provide a JSON reporter for the simplebench.reporters package. This package provides functionality for generating JSON reports from benchmark results.
Public APIπ
JSONConfig: Configuration class for the JSON reporter.JSONOptions: Options class for the JSON reporter.JSONReporter: The JSON reporter class.
- class simplebench.reporters.json.JSONConfig(
- *,
- name: str | None = None,
- description: str | None = None,
- sections: Iterable[Section] | None = None,
- targets: Iterable[Target] | None = None,
- default_targets: Iterable[Target] | None = None,
- formats: Iterable[Format] | None = None,
- choices: ChoicesConf | None = None,
- file_suffix: str | None = None,
- file_unique: bool | None = None,
- file_append: bool | None = None,
- subdir: str | None = None,
Bases:
ReporterConfigConfiguration for a JSONReporter.
This class inherits from
ReporterConfigand provides a type-safe, discoverable interface for overriding the default settings of aJSONReporter.Initialize the JSONReporter configuration.
Accepts keyword arguments to override any of the default configurations. All arguments are optional. If not provided, the default value for JSONReporter will be used.
- class simplebench.reporters.json.JSONOptions(*, full_data: bool = False)[source]π
Bases:
ReporterOptionsClass for holding JSON reporter specific options in a Choice or Case.
This class provides additional configuration options specific to the JSON reporter. It is accessed via the
optionsattribute of aChoiceorCaseinstance.- Variables:
full_data (bool) β Whether to include full data in the JSON output.
Initialize JSONChoiceOptions with default targets and subdirectory.
- Parameters:
full_data (bool) β Whether to include full data in the JSON output. Defaults to
False.
- class simplebench.reporters.json.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.