# Class PerformanceMonitor

Performance monitor for tracking operation metrics

Tracks duration of operations and provides statistical analysis. Useful for identifying performance regressions and bottlenecks.

#### Example

    const monitor = new PerformanceMonitor();

    // Record operation durations
    monitor.recordDuration('api_call', 150);
    monitor.recordDuration('api_call', 200);

    // Get statistics
    const stats = monitor.getStats('api_call');
    console.log(`p95: ${stats.p95}ms`);

- Defined in utils/performance.ts:88

##### Index

### Constructors

constructor

### Properties

maxSamples
metrics

### Methods

clear
clearOperation
getOperations
getStats
percentile
recordDuration

## Constructors

### constructor

- new PerformanceMonitor(maxSamples?: number): PerformanceMonitor

  Creates a new performance monitor.

  #### Parameters

  - maxSamples: number = 1000

    Maximum number of duration samples to retain per operation name. Older samples are evicted when the limit is reached (sliding window). Default: `1000`.

  #### Returns PerformanceMonitor

  #### Throws

  If `maxSamples` is not a positive finite integer

  #### Example

      const monitor = new PerformanceMonitor(500);

  #### Since

  0.8.0

  - Defined in utils/performance.ts:107

## Properties

### `Private` `Readonly` maxSamples

maxSamples: number

- Defined in utils/performance.ts:90

### `Private` `Readonly` metrics

metrics: Map<string, number[]> = ...

- Defined in utils/performance.ts:89

## Methods

### clear

- clear(): void

  Clears all recorded duration samples for every tracked operation.

  Useful for resetting state between test cases or at the start of a new monitoring window.

  #### Returns void

  #### Example

      // Reset metrics after each test
      afterEach(() => {
        monitor.clear();
      });

  #### Since

  0.8.0

  - Defined in utils/performance.ts:272

### clearOperation

- clearOperation(operation: string): void

  Clears recorded duration samples for a single operation.

  #### Parameters

  - operation: string

    Operation name whose samples should be discarded

  #### Returns void

  #### Example

      monitor.clearOperation('api_call');

  #### Since

  0.8.0

  - Defined in utils/performance.ts:288

### getOperations

- getOperations(): string[]

  Returns all operation names currently being tracked.

  #### Returns string[]

  Array of operation name strings, in insertion order

  #### Example

      const operations = monitor.getOperations();
      operations.forEach(op => {
        const stats = monitor.getStats(op);
        console.log(`${op}: p95=${stats?.p95}ms`);
      });

  #### Since

  0.8.0

  - Defined in utils/performance.ts:252

### getStats

- getStats(operation: string): PerformanceStats | null

  Returns aggregated performance statistics for a named operation.

  Calculates percentiles (p50 / p95 / p99), average, min, and max from the stored duration samples. Returns `null` when no samples have been recorded for the operation yet.

  #### Parameters

  - operation: string

    Operation name / identifier to query

  #### Returns PerformanceStats | null

  PerformanceStats object, or `null` if no data exists

  #### Throws

  If `operation` is not a string

  #### Example

      const stats = monitor.getStats('api_call');
      if (stats && stats.p95 > 1000) {
        console.warn('API calls are slow (p95 > 1s)');
      }

  #### Since

  0.8.0

  - Defined in utils/performance.ts:171

### `Private` percentile

- percentile(sorted: number[], p: number): number
  `Internal`

  Calculate percentile from sorted array

  Uses linear interpolation for more accurate percentile calculation

  #### Parameters

  - sorted: number[]

    Sorted array of numbers

  - p: number

    Percentile (0.0 to 1.0)

  #### Returns number

  Value at the specified percentile

  - Defined in utils/performance.ts:211

### recordDuration

- recordDuration(operation: string, durationMs: number): void

  Records a duration sample for the named operation.

  When the number of stored samples reaches `maxSamples`, the oldest entries are evicted in bulk (sliding window). Use getStats to retrieve aggregated statistics after recording samples.

  #### Parameters

  - operation: string

    Unique operation name / identifier (e.g., `'ep_api_call'`)

  - durationMs: number

    Observed duration in milliseconds (should be ≥ 0)

  #### Returns void

  #### Throws

  If `operation` is not a string

  #### Example

      const start = performance.now();
      await doWork();
      monitor.recordDuration('work', performance.now() - start);

  #### Since

  0.8.0

  - Defined in utils/performance.ts:134