rics.performance#

Performance testing utility.

Functions

run_multivariate_test(candidate_method, ...)

Run performance tests for multiple candidate methods on collections of test data.

format_perf_counter(start[, end])

Format performance counter output.

plot_run(run_results[, x, unit])

Plot the results of a test run.

to_dataframe(run_results)

Create a DataFrame from performance run output.

Classes

MultiCaseTimer(candidate_method, test_data)

Performance testing implementation for multiple candidates and data sets.

class MultiCaseTimer(candidate_method: Union[Callable[[Any], None], Collection[Callable[[Any], None]], Dict[str, Callable[[Any], None]]], test_data: Union[Any, Dict[str, Any]], sanity_check: bool = True)[source]#

Bases: object

Performance testing implementation for multiple candidates and data sets.

Parameters
  • candidate_method – A single method, collection of method or a dict {label: function} of candidates.

  • test_data – A single datum or a dict {label: data} to evaluate candidates on.

  • sanity_check – If True, verify total expected runtime.

EXPECTED_RUNTIME_WARNING_LIMIT = 10800#

Warn of long runtimes above this limit (3 hours).

run(time_per_candidate: float = 6.0, repeat: int = 5, number: Optional[int] = None, max_expected_runtime: int = 604800) Dict[str, Dict[str, List[float]]][source]#

Run for all cases.

Note that the test case variant data isn’t included in the expected runtime computation, so increasing the amount of test data variants (at initialization) will reduce the amount of times each candidate is evaluated.

Parameters
  • time_per_candidate – Desired runtime for each repetition per candidate label. Ignored if number is set.

  • repeat – Number of times to repeat for all candidates per data label.

  • number – Number of times to execute each test case, per repetition. Compute based on per_case_time_allocation if None.

  • max_expected_runtime – Maximum expected runtime to allow before throwing an exception. Disabled if `None`. Default is one week (604 800 seconds).

Examples

If repeat=5 and time_per_candidate=3 for an instance with and 2 candidates, the total runtime will be approximately 5 * 3 * 2 = 30 seconds.

Returns

A dict run_results on the form {candidate_label: {data_label: [runtime..]}}.

Raises

ValueError – If the total expected runtime exceeds max_expected_runtime.

Notes

  • Precomputed runtime is inaccurate for functions where a single call are longer than time_per_candidate.

  • By default, this function reports averages of all runs (repetition), as opposed to the built-in timeit which reports only the best result (in non-verbose mode).

See also

The timeit.Timer class which this implementation depends on.

run_multivariate_test(candidate_method: Union[Callable[[Any], None], Collection[Callable[[Any], None]], Dict[str, Callable[[Any], None]]], test_data: Union[Any, Dict[str, Any]], time_per_candidate: float = 6.0, plot: bool = True, **figure_kwargs: Any) DataFrame[source]#

Run performance tests for multiple candidate methods on collections of test data.

This is a convenience method which combines MultiCaseTimer.run(), to_dataframe() and – if plotting is enabled – plot_run(). For full functionally these methods should be use directly.

Parameters
  • candidate_method – Candidate methods to evaluate.

  • test_data – Test data to evaluate.

  • time_per_candidate – Desired runtime for each repetition per candidate label.

  • plot – If True, plot a figure using plot_run().

  • **figure_kwargs – Keyword arguments for the barplot. Ignored if plot=False.

Returns

A long-format DataFrame of results.

Raises

ModuleNotFoundError – If Seaborn isn’t installed and plot=True.

format_perf_counter(start: float, end: Optional[float] = None) str[source]#

Format performance counter output.

This function formats performance counter output based on the time elapsed. For t < 60 sec, accuracy is increased whereas for durations above one minute a more user-friendly formatting is used.

Parameters
  • start – Start time.

  • end – End time. Set to now if not given.

Returns

A formatted performance counter time.

Examples

>>> from rics.performance import format_perf_counter
>>> format_perf_counter(0, 3131)  # 3131 seconds is about 52 minutes.
'0:52:11'
>>> format_perf_counter(0, 0.154)
'0.154 sec'
plot_run(run_results: Union[Dict[str, Dict[str, List[float]]], DataFrame], x: Optional[Literal['candidate', 'data']] = None, unit: Literal['s', 'ms', 'μs', 'us'] = 'ms', **figure_kwargs: Any) None[source]#

Plot the results of a test run.

Parameters
  • run_results – Output of rics.performance.MultiCaseTimer.run().

  • x – The value to plot on the X-axis. Default=derive.

  • unit – Time unit to plot on the Y-axis.

  • **figure_kwargs – Keyword arguments for the barplot.

Raises
to_dataframe(run_results: Dict[str, Dict[str, List[float]]]) DataFrame[source]#

Create a DataFrame from performance run output.

Parameters

run_results – Output from rics.performance.MultiCaseTimer.run().

Returns

The run_result input wrapped in a DataFrame.