Harness perf tdd

From Intense-Visions/harness-engineering · View interactive version

Harness perf tdd is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Red-Green-Refactor with performance assertions. Every feature gets a correctness test AND a benchmark. No optimization without measurement.

How to use this skill

Skill file

Harness Perf TDD

Red-Green-Refactor with performance assertions. Every feature gets a correctness test AND a benchmark. No optimization without measurement.

When to Use

Process

Iron Law

No production code exists without both a failing test AND a failing benchmark that demanded its creation.

If you find yourself writing production code before both the test and the benchmark exist, STOP. Write the test. Write the benchmark. Then implement.


Phase 1: RED — Write Failing Test + Benchmark

  1. Write the correctness test following the same process as harness-tdd Phase 1 (RED):
  1. Write a .bench.ts benchmark file alongside the test file:

```typescript

import { bench, describe } from 'vitest';

import { processData } from './handler';

describe('processData benchmarks', () => {

bench('processData with small input', () => {

processData(smallInput);

});

bench('processData with large input', () => {

processData(largeInput);

});

});

```

  1. Run the test — observe failure. The function is not implemented yet, so the test should fail with "not defined" or "not a function."
  1. Run the benchmark — observe failure or no baseline. This establishes that the benchmark exists and will track performance once the implementation lands.

Phase 2: GREEN — Pass Test and Benchmark

  1. Write the minimum implementation to make the correctness test pass. Do not optimize yet. The goal is correctness first.
  1. Run the test — observe pass. If it fails, fix the implementation until it passes.
  1. Run the benchmark -- capture initial results and apply thresholds:

When the spec defines a performance requirement (e.g., "< 50ms"):

When the spec is vague or silent on performance:

When no baseline exists (new code):

  1. If the performance assertion fails, you have two options:

Phase 3: REFACTOR — Optimize While Green

This phase is optional. Enter it when:

  1. Profile the implementation if the benchmark result is far from the requirement. Use the benchmark output to identify the bottleneck.
  1. Refactor for performance — consider:
  1. After each change, run both checks:
  1. Stop when the benchmark meets the performance requirement, or when further optimization yields diminishing returns (< 1% improvement per change).
  1. Do not gold-plate. If the requirement is "< 100ms" and you are at 40ms, stop. Move on.

Phase 4: VALIDATE — Harness Checks

  1. Run harness check-perf to verify no Tier 1 or Tier 2 violations were introduced by the implementation:
  1. Run harness validate to verify overall project health:
  1. Update baselines if this is a new benchmark:

```bash

harness perf baselines update

```

This persists the current benchmark results so future runs can detect regressions.

  1. Commit with a descriptive message that mentions both the feature and its performance characteristics:

```

feat(parser): add streaming JSON parser (<50ms for 1MB payloads)

```


Benchmark File Convention

Benchmark files are co-located with their source files, using the .bench.ts extension:

| Source File | Benchmark File |

| ----------------------------- | ----------------------------------- |

| src/parser/handler.ts | src/parser/handler.bench.ts |

| src/api/resolver.ts | src/api/resolver.bench.ts |

| packages/core/src/engine.ts | packages/core/src/engine.bench.ts |

Each benchmark file should:


Harness Integration

Success Criteria

Examples

Example: Implementing a Performance-Critical Parser

Phase 1: RED

// src/parser/json-stream.test.ts
it('parses 1MB JSON in under 50ms', () => {
  const result = parseStream(largeMbPayload);
  expect(result).toEqual(expectedOutput);
});

// src/parser/json-stream.bench.ts
bench('parseStream 1MB', () => {
  parseStream(largeMbPayload);
});

Run test: FAIL (parseStream not defined). Run benchmark: FAIL (no implementation).

Phase 2: GREEN

// src/parser/json-stream.ts
export function parseStream(input: string): ParsedResult {
  return JSON.parse(input); // simplest correct implementation
}

Run test: PASS. Run benchmark: 38ms average (meets <50ms requirement).

Phase 3: REFACTOR — skipped (38ms already meets 50ms target).

Phase 4: VALIDATE

harness check-perf — no violations
harness validate — passes
harness perf baselines update — baseline saved
git commit -m "feat(parser): add streaming JSON parser (<50ms for 1MB payloads)"

Example: Optimizing an Existing Hot Path

Phase 1: RED — test and benchmark already exist from initial implementation.

Phase 3: REFACTOR

Before: resolveImports 12ms (requirement: <5ms)
Change: switch from recursive descent to iterative with stack
After:  resolveImports 3.8ms
Test: still passing

Phase 4: VALIDATE

harness check-perf — complexity reduced from 12 to 8 (improvement)
harness perf baselines update — new baseline saved

Rationalizations to Reject

| Rationalization | Reality |

| --------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

| "The correctness test is green, I'll add the benchmark later when we know performance is an issue." | The benchmark is not optional — it is the mechanism that defines "performance issue." Without a baseline captured at implementation time, you have nothing to compare against when a regression appears months later. Later never comes. |

| "I'll skip the REFACTOR phase since the spec doesn't mention performance requirements." | The spec not mentioning a requirement means there is no user-facing SLO, not that performance is irrelevant. The benchmark still captures the baseline that future work must not regress from. Phase 3 is optional; the benchmark file is not. |

| "The benchmark results vary too much between runs to be meaningful — I'll just omit it." | Variance is a signal, not a reason to skip. High variance means the benchmark needs warmup iterations, more samples, or isolation from I/O. Fix the benchmark, do not delete it. An absent benchmark offers zero protection against regressions. |

| "This function is only called during startup, so its performance doesn't matter at runtime." | Startup performance determines deployment speed, lambda cold-start latency, and test suite duration. "Not in the hot path at runtime" does not mean performance is free to ignore. Measure it so the baseline exists if startup behavior changes. |

| "We already have an integration test that covers this — writing a separate benchmark would be redundant." | Integration tests verify correctness under realistic conditions. Benchmarks measure isolated performance with precise input control. An integration test that passes in 2 seconds tells you nothing about whether the function itself takes 1ms or 800ms. |

Gates

Escalation

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.