Performance class provides static methods for performance metrics analysis.

Features:

  • Get aggregated performance statistics by strategy
  • Generate markdown reports with bottleneck analysis
  • Save reports to disk
  • Clear accumulated metrics
import { Performance, listenPerformance } from "backtest-kit";

// Subscribe to performance events
listenPerformance((event) => {
console.log(`${event.metricType}: ${event.duration.toFixed(2)}ms`);
});

// Run backtest...

// Get aggregated statistics
const stats = await Performance.getData("my-strategy");
console.log("Total time:", stats.totalDuration);
console.log("Slowest operations:", Object.values(stats.metricStats)
.sort((a, b) => b.avgDuration - a.avgDuration)
.slice(0, 5));

// Generate and save report
await Performance.dump("my-strategy");

Constructors

Methods

Constructors

Methods

  • Saves performance report to disk.

    Creates directory if it doesn't exist. Default path: ./dump/performance/{strategyName}.md

    Parameters

    • strategyName: string

      Strategy name to save report for

    • Optionalpath: string

      Optional custom directory path

    Returns Promise<void>

    // Save to default path: ./dump/performance/my-strategy.md
    await Performance.dump("my-strategy");

    // Save to custom path: ./reports/perf/my-strategy.md
    await Performance.dump("my-strategy", "./reports/perf");
  • Gets aggregated performance statistics for a symbol-strategy pair.

    Returns detailed metrics grouped by operation type:

    • Count, total duration, average, min, max
    • Standard deviation for volatility
    • Percentiles (median, P95, P99) for outlier detection

    Parameters

    • symbol: string

      Trading pair symbol

    • strategyName: string

      Strategy name to analyze

    Returns Promise<PerformanceStatistics>

    Performance statistics with aggregated metrics

    const stats = await Performance.getData("BTCUSDT", "my-strategy");

    // Find slowest operation type
    const slowest = Object.values(stats.metricStats)
    .sort((a, b) => b.avgDuration - a.avgDuration)[0];
    console.log(`Slowest: ${slowest.metricType} (${slowest.avgDuration.toFixed(2)}ms avg)`);

    // Check for outliers
    for (const metric of Object.values(stats.metricStats)) {
    if (metric.p99 > metric.avgDuration * 5) {
    console.warn(`High variance in ${metric.metricType}: P99=${metric.p99}ms, Avg=${metric.avgDuration}ms`);
    }
    }
  • Generates markdown report with performance analysis.

    Report includes:

    • Time distribution across operation types
    • Detailed metrics table with statistics
    • Percentile analysis for bottleneck detection

    Parameters

    • symbol: string

      Trading pair symbol

    • strategyName: string

      Strategy name to generate report for

    Returns Promise<string>

    Markdown formatted report string

    const markdown = await Performance.getReport("BTCUSDT", "my-strategy");
    console.log(markdown);

    // Or save to file
    import fs from "fs/promises";
    await fs.writeFile("performance-report.md", markdown);