Latency Assertion Handler (assertions/latency.ts)

promptfoo · src/assertions/latency.ts · typescript · MIT
analytics, testing, lang:typescript, tag:performance, tag:validation

Validates that system or API response times meet defined latency thresholds. Use this within automated testing suites to enforce performance requirements.

// Source: https://github.com/promptfoo/promptfoo | License: MIT | Copyright Promptfoo 2025
// Provided via justclaw.it.com; keep this notice with the code.
import type { AssertionParams, GradingResult } from '../types/index';

export const handleLatency = ({
  assertion,
  latencyMs,
  inverse,
}: AssertionParams): GradingResult => {
  if (assertion.threshold === undefined) {
    throw new Error('Latency assertion must have a threshold in milliseconds');
  }
  if (latencyMs === undefined) {
    throw new Error(
      'Latency assertion does not support cached results. Rerun the eval with --no-cache',
    );
  }
  const pass = latencyMs <= assertion.threshold !== inverse;
  return {
    pass,
    score: pass ? 1 : 0,
    reason: pass
      ? 'Assertion passed'
      : `Latency ${latencyMs}

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key