simplebench.reporters.reporter.protocols moduleπ
Protocols for the Reporter class and its mixins.
- class simplebench.reporters.reporter.protocols.ReporterProtocol(*args, **kwargs)[source]π
Bases:
ProtocolProtocol for the Reporter class and its mixins.
- add_boolean_flags_to_argparse(
- parser: ArgumentParser,
- choice: Choice,
Add boolean flags for a
Choiceto anArgumentParser.This method adds a
--<choice.flag>flag to the parser, which acts as a boolean switch to enable the choice.- Parameters:
parser (
ArgumentParser) β TheArgumentParserto add the flags to.
- add_choice(
- choice: Choice,
Add a
Choiceto the reporterβs choices.- Parameters:
- Raises:
SimpleBenchTypeError β If the provided choice is not a
Choiceinstance.SimpleBenchValueError β If the choiceβs sections, targets, or formats are not supported by the reporter.
- add_flags_to_argparse(
- parser: ArgumentParser,
Add command-line flags for the reporterβs choices to an
ArgumentParser.This method iterates through the reporterβs choices and adds the appropriate command-line flags for each choice to the provided
ArgumentParser.- Parameters:
parser (
ArgumentParser) β TheArgumentParserto add the flags to.
- add_list_of_targets_flags_to_argparse(
- parser: ArgumentParser,
- choice: Choice,
Add a list of target flags for a
Choiceto anArgumentParser.This method adds a
--<choice.flag>-targetsflag to the parser, allowing users to specify a comma-separated list of output targets for the choice.- Parameters:
parser (
ArgumentParser) β TheArgumentParserto add the flags to.
- property choices: Choicesπ
The
Choicesfor the reporter.The
Choicesinstance contains one or moreChoiceinstances, each representing a specific combination of sections, targets, and formats, command line flags, and descriptions.This property allows access to the reporterβs choices for generating reports and customizing report output and available options.
- property config: ReporterConfigπ
The configuration object for the reporter.
- dispatch_to_targets(
- *,
- output: str | bytes | Text | Table,
- log_metadata: ReportLogMetadata,
- filename_base: str,
- args: Namespace,
- choice: Choice,
- case: Case,
- section: Section,
- path: Path | None = None,
- session: Session | None = None,
- callback: ReporterCallback | None = None,
Deliver the rendered output to the specified targets.
This method orchestrates sending the generated report content to the various output targets (console, filesystem, callback) based on the selected options.
- Parameters:
output (str | bytes |
Text|Table) β The rendered report content.log_metadata (ReportLogMetadata) β The metadata for the report log.
filename_base (str) β The base name for the output file.
args (
Namespace) β The parsed command-line arguments.path (
Path| None, optional) β The output path for filesystem targets. Defaults toNone.session (
Session| None, optional) β TheSessioninstance for the report. Defaults toNone.callback (
ReporterCallback| None, optional) β A callback function for callback targets. Defaults toNone.
- static find_options_by_type(
- options: Iterable[ReporterOptions] | None,
- cls: type[T],
Retrieve an instance of type
cls(if present) from a collection ofReporterOptions.This is used to extract reporter specific options from an iterable container of generic
ReporterOptionssuch as those associated with aChoiceorCase.For example, a
CSVReportermay define aCSVReporterOptionsclass that extendsReporterOptionsand use this method to extract theCSVReporterOptionsinstance from the options iterable:options = Reporter.find_options_by_type(case.options, CSVReporterOptions)
- Parameters:
options (Iterable[
ReporterOptions]) β An iterable ofReporterOptionsinstances.cls (type[T]) β The specific subclass type of
ReporterOptionsto find.
- Returns:
The instance of the class
clsif found, otherwiseNone.- Return type:
T | None
- get_all_stats_values( ) list[float][source]π
Gathers all primary statistical values for a given section across multiple results.
It collects mean, median, minimum, maximum, 5th percentile, and 95th percentile, from each
Resultsinstance for the specified section.This method is useful in determining appropriate scaling factors or units for reporting by analyzing the range of values across all results.
Adjusted standard deviation is not included in this collection because it can be
NaNfor results with insufficient data points, or orders of magnitude different from the other statistics, which can skew scaling calculations.
- get_base_unit_for_section(
- section: Section,
Return the base unit for the specified section.
- classmethod get_default_options() ReporterOptions[source]π
Get the default options for the reporter.
Returns the default options set via
set_default_options()if set, otherwise returns the built-in hardcoded default options fromget_hardcoded_default_options().- Returns:
The default options.
- Return type:
- classmethod get_hardcoded_default_options() ReporterOptions[source]π
Get the built-in hardcoded default options for the reporter.
This abstract method must be implemented by all
Reportersubclasses. It defines the base class default options for a reporter and must be available in allReportersubclasses.It returns the hardcoded default
ReporterOptionssub-class instance specific to the reporter.Sub-classes must implement the following class variables:
_OPTIONS_TYPE: ClassVar[type[MyOptions]] = MyOptions _OPTIONS_KWARGS: ClassVar[dict[str, Any]] = {β¦}
or the method will raise an exception.
- Returns:
The built-in hardcoded default
ReporterOptionsinstance.- Raises:
SimpleBenchNotImplementedError β If required class variables are not implemented or are of incorrect types.
- get_prioritized_default_targets(
- choice: Choice,
Get the prioritized default targets from the choice or reporter defaults.
- get_prioritized_file_append(
- choice: Choice,
Get the prioritized file append flag from the choice or reporter.
- get_prioritized_file_suffix(
- choice: Choice,
Get the prioritized file suffix from the choice or reporter.
- get_prioritized_file_unique(
- choice: Choice,
Get the prioritized file unique flag from the choice or reporter.
- get_prioritized_options( ) ReporterOptions[source]π
Get the prioritized
ReporterOptionsfor the given case and choice.- Parameters:
- Returns:
The prioritized options.
- Return type:
- get_prioritized_subdir(
- choice: Choice,
Get the prioritized subdirectory from the choice or reporter defaults.
- render(
- *,
- case: Case,
- section: Section,
- options: ReporterOptions,
Render the report for a specific case and section.
This abstract method must be implemented by all
Reportersubclasses. It is responsible for generating the actual report content for a given case and section, based on the provided options.The output can be a string, bytes, or a Rich object (
TextorTable).- Parameters:
case (
Case) β TheCaseinstance containing the benchmark results.section (
Section) β The specificSectionof the results to render.options (
ReporterOptions) β The reporter-specificReporterOptionsfor rendering.
- Returns:
The rendered report content.
- Return type:
- Raises:
NotImplementedError β If the method is not implemented in a subclass.
- render_by_case(
- *,
- renderer: ReportRenderer,
- log_metadata: ReportLogMetadata,
- args: Namespace,
- case: Case,
- choice: Choice,
- path: Path | None = None,
- session: Session | None = None,
- callback: ReporterCallback | None = None,
Render a single report for the entire case.
This method is suitable for reporters that generate a single, consolidated output for all sections of the benchmark results.
- Parameters:
renderer (
ReportRenderer) β A callable that takes a case, section (which will beNone), and options, and returns the rendered output.log_metadata (ReportLogMetadata) β The metadata for the report log.
args (
Namespace) β The parsed command-line arguments.path (
Path| None, optional) β The output path for filesystem targets. Defaults toNone.session (
Session| None, optional) β TheSessioninstance for the report. Defaults toNone.callback (
ReporterCallback| None, optional) β A callback function for callback targets. Defaults toNone.
- render_by_section(
- *,
- renderer: ReportRenderer,
- log_metadata: ReportLogMetadata,
- args: Namespace,
- case: Case,
- choice: Choice,
- path: Path | None = None,
- session: Session | None = None,
- callback: ReporterCallback | None = None,
Render a report by iterating through each section specified in the choice.
This method is suitable for reporters that generate a separate output for each section of the benchmark results (e.g., OPS, TIMING, MEMORY).
- Parameters:
renderer (
ReportRenderer) β A callable that takes a case, section, and options, and returns the rendered output.log_metadata (ReportLogMetadata) β The metadata for the report log.
args (
Namespace) β The parsed command-line arguments.path (
Path| None, optional) β The output path for filesystem targets. Defaults toNone.session (
Session| None, optional) β TheSessioninstance for the report. Defaults toNone.callback (
ReporterCallback| None, optional) β A callback function for callback targets. Defaults toNone.
- report(
- *,
- args: Namespace,
- log_metadata: ReportLogMetadata,
- case: Case,
- choice: Choice,
- path: Path | None = None,
- session: Session | None = None,
- callback: ReporterCallback | None = None,
Generate a report based on the benchmark results.
This method performs validation and then calls the subclassβs
run_report()method.- Parameters:
args (
Namespace) β The parsed command-line arguments.log_metadata (ReportLogMetadata) β The metadata for the report log.
case (
Case) β TheCaseinstance containing benchmark results.choice (
Choice) β TheChoiceinstance specifying the report configuration.path (
Path| None, optional) β The path to the directory where the report can be saved if needed. Leave asNoneif not saving to the filesystem. Defaults toNone.session (
Session| None, optional) β TheSessioninstance containing benchmark results. Defaults toNone.callback (
ReporterCallback| None, optional) β A callback function for additional processing of the report. Defaults toNone.
- run_report(
- *,
- args: Namespace,
- log_metadata: ReportLogMetadata,
- case: Case,
- choice: Choice,
- path: Path | None = None,
- session: Session | None = None,
- callback: ReporterCallback | None = None,
Orchestration hook for report generation.
This method is the primary customization point for controlling how a report is generated. It is called by the public
report()method after all inputs have been validated.The default implementation calls
render_by_section(), which is suitable for most reporters. Subclasses can override this method to provide alternative orchestration, such as callingrender_by_case()for reports that are generated once per case.Note
This is also the correct place to implement custom logic for non-standard targets like
CUSTOM.- Parameters:
args (
Namespace) β The parsed command-line arguments.log_metadata (ReportLogMetadata) β The metadata for the report log.
case (
Case) β TheCaseinstance representing the benchmarked code.choice (
Choice) β TheChoiceinstance specifying the report configuration.path (
Path| None, optional) β The path to the directory where report files will be saved. Defaults toNone.session (
Session| None, optional) β TheSessioninstance containing benchmark results. Defaults toNone.callback (
ReporterCallback| None, optional) β A callback function for additional processing. Defaults toNone.
- select_targets_from_args( ) set[Target][source]π
Select output targets based on command-line arguments and
Choiceconfiguration.This method determines the final set of output targets for a given choice by prioritizing command-line arguments over the choiceβs default targets.
- Parameters:
- Returns:
A set of the selected output targets.
- Return type:
set[
Target]
- classmethod set_default_options(
- options: ReporterOptions | None = None,
Set the default options for the reporter.
- Parameters:
options (
ReporterOptionsor None, optional) β The options to set as the default, defaults to None
- supported_formats() frozenset[Format][source]π
The set of supported
Formatfor the reporter.This is the set of
Formatthat the reporter can output in.Defined
Choicecan only includeFormatthat are declared in this set.
- supported_sections() frozenset[Section][source]π
The set of supported
Sectionfor the reporter.This is the set of
Sectionthat the reporter can include in its reports.Defined
Choicecan only includeSectionthat are declared in this set.
- supported_targets() frozenset[Target][source]π
The set of supported
Targetfor the reporter.This is the set of
Targetthat the reporter can output to.Defined
Choicecan only includeTargetthat are declared in this set.
- target_callback(
- callback: ReporterCallback | None,
- case: Case,
- section: Section,
- output_format: Format,
- output: str | bytes | Text | Table,
Send report data to a callback function.
- Parameters:
- target_console( ) None[source]π
Output report data to the console.
This method uses the console associated with the
Sessionto print the report output.
- target_filesystem(
- *,
- log_metadata: ReportLogMetadata,
- path: Path | None,
- subdir: str,
- filename: str,
- output: str | bytes | Text | Table,
- unique: bool,
- append: bool,
Write report data to the filesystem.
This method handles the logic for writing report output to a file, including creating directories, handling unique filenames, and appending to existing files.
- Parameters:
log_metadata (ReportLogMetadata) β The metadata for the report log.
path (
Path| None) β The base output path.subdir (str) β The subdirectory within the base path to write to.
filename (str) β The name of the file to write.
output (str | bytes |
Text|Table) β The content to write to the file.unique (bool) β Whether to generate a unique filename if the file already exists.
append (bool) β Whether to append to the file if it already exists.