benchmark Module (stx.benchmark)

scitex-benchmark — performance benchmarking + monitoring + profiling (standalone).

This module provides tools for benchmarking and monitoring the performance of SciTeX functions.

scitex.benchmark.benchmark_function(func, args=(), kwargs=None, iterations=10, warmup=2, input_size=None, measure_memory=False)[source]

Benchmark a single function.

Parameters:
  • func (Callable) – Function to benchmark

  • args (tuple) – Arguments to pass to function

  • kwargs (dict) – Keyword arguments to pass to function

  • iterations (int) – Number of benchmark iterations

  • warmup (int) – Number of warmup iterations

  • input_size (str, optional) – Description of input size

  • measure_memory (bool) – Whether to measure memory usage

Returns:

Benchmark results

Return type:

BenchmarkResult

scitex.benchmark.benchmark_module(module_name, pattern='test_*')[source]

Create a benchmark suite for all matching functions in a module.

Parameters:
  • module_name (str) – Name of module to benchmark

  • pattern (str) – Pattern to match function names

Returns:

Suite containing all matching benchmarks

Return type:

BenchmarkSuite

class scitex.benchmark.BenchmarkResult(function_name, module, mean_time, std_time, min_time, max_time, iterations, input_size=None, memory_usage=None, notes=None)[source]

Bases: object

Results from a benchmark run.

function_name: str
module: str
mean_time: float
std_time: float
min_time: float
max_time: float
iterations: int
input_size: str | None = None
memory_usage: float | None = None
notes: str | None = None
to_dict()[source]

Convert to dictionary for easy serialization.

class scitex.benchmark.BenchmarkSuite(name)[source]

Bases: object

Collection of benchmarks for a module or set of functions.

add_benchmark(func, test_data_generator, name=None, sizes=None)[source]

Add a benchmark to the suite.

run(iterations=10, verbose=True)[source]

Run all benchmarks in the suite.

Return type:

DataFrame

save_results(path)[source]

Save benchmark results to CSV.

compare_with_baseline(baseline_path)[source]

Compare current results with baseline.

Return type:

DataFrame

scitex.benchmark.run_all_benchmarks(output_dir='./benchmark_results')[source]

Run all pre-defined benchmark suites.

Parameters:

output_dir (str) – Directory to save results

Returns:

Dictionary mapping suite names to results

Return type:

dict

scitex.benchmark.compare_implementations(implementations, test_data_generator, iterations=10, sizes=None)[source]

Compare multiple implementations of the same functionality.

Parameters:
  • implementations (dict) – Dictionary mapping implementation names to functions

  • test_data_generator (callable) – Function that returns (args, kwargs) for testing

  • iterations (int) – Number of iterations per implementation

  • sizes (list, optional) – List of input sizes to test

Returns:

Comparison results

Return type:

pd.DataFrame

scitex.benchmark.profile_function(func)[source]

Decorator to profile a function using the global profiler.

Example

>>> @profile_function
... def my_function(x):
...     return sum(range(x))
Return type:

Callable

scitex.benchmark.profile_module(module_name, pattern='*')[source]

Profile all matching functions in a module.

Parameters:
  • module_name (str) – Name of module to profile

  • pattern (str) – Pattern to match function names

Returns:

Profiling results

Return type:

dict

scitex.benchmark.get_profile_report()[source]

Get profiling report from global profiler.

Return type:

Dict[str, Any]

class scitex.benchmark.PerformanceMonitor(max_history=1000)[source]

Bases: object

Monitor performance metrics for SciTeX functions.

Example

>>> monitor = PerformanceMonitor()
>>> monitor.start()
>>> # Your code here
>>> stats = monitor.get_stats()
start()[source]

Start monitoring.

stop()[source]

Stop monitoring.

record_metric(metric)[source]

Record a performance metric.

add_alert_callback(callback)[source]

Add a callback for performance alerts.

get_stats(function=None)[source]

Get performance statistics.

Parameters:

function (str, optional) – Specific function to get stats for

Returns:

Performance statistics

Return type:

dict

get_recent_metrics(n=100)[source]

Get n most recent metrics.

Return type:

List[PerformanceMetric]

save_metrics(path)[source]

Save metrics to file.

load_metrics(path)[source]

Load metrics from file.

clear()[source]

Clear all metrics.

scitex.benchmark.track_performance(func)[source]

Decorator to track function performance.

Example

>>> @track_performance
... def my_function(x):
...     return x ** 2
Return type:

Callable

scitex.benchmark.get_performance_stats(function=None)[source]

Get performance statistics from global monitor.

Return type:

Dict[str, Any]