From a31ecebf5059bd8bbddd7d0a1e4bf89e45028943 Mon Sep 17 00:00:00 2001 From: TheMeinerLP Date: Sat, 28 Nov 2026 18:13:25 +0100 Subject: [PATCH] fix(loadtest): let a run override a scenario threshold A plateau step passes only under the p95 limit of its scenario. Those limits stay provisional until the first measured run sets them, and changing one meant editing the shared file, which then changed every later run too. A run now takes --threshold = and uses that limit for its own scenario only, while the other limits keep their declared values. Input that would silently do the wrong thing is refused before any load is sent: a value that is not a positive whole number of milliseconds, an unknown scenario, deploy (its limit depends on the artifact size) and a scenario other than the one the run uses. --- .../contributing/run-the-load-tests.md | 3 + loadtest/lib/thresholds.test.ts | 75 +++++++++++++++++- loadtest/lib/thresholds.ts | 78 ++++++++++++++++++- loadtest/profiles/options.test.ts | 39 ++++++++++ loadtest/profiles/options.ts | 4 +- loadtest/runner/main.ts | 6 +- 6 files changed, 199 insertions(+), 6 deletions(-) diff --git a/docs/gitbook/contributing/run-the-load-tests.md b/docs/gitbook/contributing/run-the-load-tests.md index 2e0fdbc8a3..a2a89e296f 100644 --- a/docs/gitbook/contributing/run-the-load-tests.md +++ b/docs/gitbook/contributing/run-the-load-tests.md @@ -37,6 +37,7 @@ The options are: | `--size` | `N`, `T`, `S`, `M`, `L` | chart default resources, and no result record | | `--reference` | flag | off | | `--parallelism` | whole number of at least 1 | 1 | +| `--threshold` | `=`: a whole number of milliseconds for the scenario of the run, for example `resolve=200`. Given once. | the declared p95 limit of the scenario in `loadtest/lib/thresholds.ts` | The runner reads further settings from its environment: @@ -74,6 +75,8 @@ A plateau step passes when every minute of its hold meets all three limits: A step that breaks in its last minute does not pass. The limits are kept in `loadtest/lib/thresholds.ts`. Until the first measured run sets them, every limit is provisional. +A run can replace the p95 limit of its own scenario with `--threshold`, for example `--scenario resolve --threshold resolve=200` for a 200 ms limit. The other limits keep their declared values, and the flag is refused when it names another scenario than the run's. `deploy` cannot be overridden, because its limit depends on the artifact size. The override is passed to the scripts, so the breakpoint and the plateau verdicts both use it. + ## Read the maximum table The maximum sustainable rate is the rate of the highest step that passed for its whole hold. Read it with its p95, not on its own: a rate with a p95 close to the limit has little headroom. diff --git a/loadtest/lib/thresholds.test.ts b/loadtest/lib/thresholds.test.ts index d07716c7a6..8eadf2c2ae 100644 --- a/loadtest/lib/thresholds.test.ts +++ b/loadtest/lib/thresholds.test.ts @@ -16,7 +16,14 @@ import assert from 'node:assert/strict'; import { test } from 'node:test'; import { SCENARIOS } from './config.ts'; -import { MAX_ERROR_RATE, THRESHOLDS, thresholdFor } from './thresholds.ts'; +import { + MAX_ERROR_RATE, + THRESHOLDS, + overriddenP95Ms, + parseThresholdOverride, + thresholdFor, + thresholdOverrideVariables, +} from './thresholds.ts'; const DESIGN_SCENARIOS = [ 'resolve', @@ -58,3 +65,69 @@ test('the error rate limit of a passing step is one percent', () => { test('thresholdFor refuses a deploy without a size', () => { assert.throws(() => thresholdFor('deploy'), /deploy/); }); + +test('a threshold override names a scenario and a whole number of milliseconds', () => { + assert.deepEqual(parseThresholdOverride('resolve=200'), { scenario: 'resolve', p95Ms: 200 }); + assert.deepEqual(parseThresholdOverride('search-browse=800'), { scenario: 'search-browse', p95Ms: 800 }); +}); + +test('a threshold override refuses a value that is not a positive whole number of milliseconds', () => { + for (const value of ['resolve=0', 'resolve=-5', 'resolve=1.5', 'resolve=200ms', 'resolve=', 'resolve=abc']) { + assert.throws(() => parseThresholdOverride(value), /positive whole number of milliseconds/, value); + } +}); + +test('a threshold override refuses a scenario that does not exist, and names the known ones', () => { + assert.throws(() => parseThresholdOverride('spike=200'), /spike.*expected one of.*resolve/); +}); + +test('a threshold override refuses a text without the equals sign', () => { + assert.throws(() => parseThresholdOverride('resolve'), /scenario=milliseconds/); +}); + +test('a threshold override refuses deploy, whose threshold depends on the artifact size', () => { + assert.throws(() => parseThresholdOverride('deploy=200'), /deploy.*size/); +}); + +test('the run passes the override of its own scenario to the scripts', () => { + assert.deepEqual(thresholdOverrideVariables(['--scenario', 'resolve', '--threshold', 'resolve=200'], 'resolve'), { + LOADTEST_THRESHOLD_SCENARIO: 'resolve', + LOADTEST_THRESHOLD_P95_MS: '200', + }); +}); + +test('a run without a threshold flag passes no override', () => { + assert.deepEqual(thresholdOverrideVariables(['--scenario', 'resolve'], 'resolve'), {}); +}); + +test('a run refuses an override that names another scenario than its own', () => { + assert.throws( + () => thresholdOverrideVariables(['--threshold', 'resolve=200'], 'mix'), + /resolve.*mix/, + ); +}); + +test('a run refuses a second threshold flag instead of silently keeping one', () => { + assert.throws( + () => thresholdOverrideVariables(['--threshold', 'resolve=200', '--threshold', 'resolve=300'], 'resolve'), + /once/, + ); +}); + +test('a script uses the overridden p95 limit for the named scenario only', () => { + const env = { LOADTEST_THRESHOLD_SCENARIO: 'resolve', LOADTEST_THRESHOLD_P95_MS: '200' }; + + assert.equal(overriddenP95Ms(env, 'resolve', 150), 200); + assert.equal(overriddenP95Ms(env, 'mix', 500), 500); +}); + +test('a script keeps the declared p95 limit when no override is set', () => { + assert.equal(overriddenP95Ms({}, 'resolve', 150), 150); +}); + +test('a script refuses an override in its environment that is not a positive whole number', () => { + const env = { LOADTEST_THRESHOLD_SCENARIO: 'resolve', LOADTEST_THRESHOLD_P95_MS: '0' }; + + assert.throws(() => overriddenP95Ms(env, 'resolve', 150), /LOADTEST_THRESHOLD_P95_MS/); +}); + diff --git a/loadtest/lib/thresholds.ts b/loadtest/lib/thresholds.ts index 3cffc749b7..9c26bd55dd 100644 --- a/loadtest/lib/thresholds.ts +++ b/loadtest/lib/thresholds.ts @@ -15,10 +15,11 @@ */ // The thresholds of every scenario, declared in one place (design D3). The numbers are a starting point only: // every value is marked "to be tuned by the maintainer" and changes after the first reference runs. -// A run may override a value; the overrides are not part of this file. +// A run may override the p95 limit of its scenario with `--threshold =`. The runner passes the override to +// the scripts as two variables, and the override is never written into this file. import type { ArtifactSize } from './artifacts.ts'; -import type { Scenario } from './config.ts'; +import { SCENARIOS, type Scenario } from './config.ts'; /** A plateau step passes only while its error rate stays under this limit. Applies to every scenario. */ export const MAX_ERROR_RATE = 0.01; @@ -58,3 +59,76 @@ export function thresholdFor(scenario: Scenario, size?: ArtifactSize): ScenarioT } return entry; } + +/** The runner flag that overrides the p95 limit of the scenario of its run, as `--threshold resolve=200`. */ +export const THRESHOLD_FLAG = '--threshold'; +/** The script variables the runner sets for an override. A run has one scenario, so one pair is enough. */ +export const THRESHOLD_SCENARIO_VARIABLE = 'LOADTEST_THRESHOLD_SCENARIO'; +export const THRESHOLD_P95_VARIABLE = 'LOADTEST_THRESHOLD_P95_MS'; + +export interface ThresholdOverride { + readonly scenario: Scenario; + /** The p95 limit in milliseconds that replaces the declared one for the scenario. */ + readonly p95Ms: number; +} + +/** Reads one `scenario=milliseconds` value. A deploy has one limit per artifact size, so the override cannot name it. */ +export function parseThresholdOverride(text: string): ThresholdOverride { + const separator = text.indexOf('='); + if (separator === -1) { + throw new Error(`${THRESHOLD_FLAG} takes scenario=milliseconds, for example resolve=200, got "${text}"`); + } + const name = text.slice(0, separator); + const scenario = SCENARIOS.find((candidate) => candidate === name); + if (scenario === undefined) { + throw new Error(`unknown scenario "${name}" in ${THRESHOLD_FLAG}; expected one of: ${SCENARIOS.join(', ')}`); + } + if (scenario === 'deploy') { + throw new Error(`${THRESHOLD_FLAG} cannot name deploy: its limit depends on the artifact size, and the override does not name a size`); + } + return { scenario, p95Ms: positiveMilliseconds(text.slice(separator + 1), THRESHOLD_FLAG) }; +} + +/** + * The script variables of a run that carries a `--threshold` flag, empty when it carries none. The override must name the + * scenario of the run: a run has one scenario, and an override for another one would silently change nothing. + */ +export function thresholdOverrideVariables(argv: readonly string[], runScenario: string): Record { + const values = argv.flatMap((argument, index) => (argument === THRESHOLD_FLAG ? [argv[index + 1] ?? ''] : [])); + if (values.length === 0) { + return {}; + } + if (values.length > 1) { + throw new Error(`give ${THRESHOLD_FLAG} once: a run overrides the limit of its own scenario only`); + } + const override = parseThresholdOverride(values[0] ?? ''); + if (override.scenario !== runScenario) { + throw new Error( + `${THRESHOLD_FLAG} names ${override.scenario}, but this run uses ${runScenario}; give the scenario of the run`, + ); + } + return { + [THRESHOLD_SCENARIO_VARIABLE]: override.scenario, + [THRESHOLD_P95_VARIABLE]: String(override.p95Ms), + }; +} + +/** The p95 limit a script uses: the override when the environment names its scenario, else the declared limit. */ +export function overriddenP95Ms( + env: Readonly>, + scenario: string, + declaredMs: number, +): number { + if (env[THRESHOLD_SCENARIO_VARIABLE] !== scenario) { + return declaredMs; + } + return positiveMilliseconds(env[THRESHOLD_P95_VARIABLE] ?? '', THRESHOLD_P95_VARIABLE); +} + +function positiveMilliseconds(text: string, label: string): number { + if (!/^[1-9]\d*$/.test(text)) { + throw new Error(`${label} must be a positive whole number of milliseconds, got "${text}"`); + } + return Number(text); +} + diff --git a/loadtest/profiles/options.test.ts b/loadtest/profiles/options.test.ts index bebf3822a2..df4bce7894 100644 --- a/loadtest/profiles/options.test.ts +++ b/loadtest/profiles/options.test.ts @@ -90,3 +90,42 @@ test('the breakpoint profile runs the whole ramp when LOADTEST_ABORT_ON_FAIL is { threshold: 'p(95)<150', abortOnFail: false, delayAbortEval: '150s' }, ]); }); + +test('the plateau verdicts use the overridden p95 limit of the run scenario', () => { + const options = profileOptions( + { + LOADTEST_PROFILE: 'plateau', + LOADTEST_BREAKPOINT: '100', + LOADTEST_THRESHOLD_SCENARIO: 'resolve', + LOADTEST_THRESHOLD_P95_MS: '200', + }, + 'resolve', + 150, + ); + + assert.equal(options.scenarios?.['verdict-1']?.env?.LOADTEST_STEP_P95_MS, '200'); + assert.equal(options.scenarios?.['verdict-5']?.env?.LOADTEST_STEP_P95_MS, '200'); +}); + +test('the breakpoint aborts at the overridden p95 limit of the run scenario', () => { + const options = profileOptions( + { LOADTEST_PROFILE: 'breakpoint', LOADTEST_THRESHOLD_SCENARIO: 'resolve', LOADTEST_THRESHOLD_P95_MS: '200' }, + 'resolve', + 150, + ); + + assert.deepEqual(options.thresholds?.['http_req_duration{phase:ramp}'], [ + { threshold: 'p(95)<200', abortOnFail: true, delayAbortEval: '150s' }, + ]); +}); + +test('an override for another scenario leaves the declared p95 limit of this scenario', () => { + const options = profileOptions( + { LOADTEST_PROFILE: 'plateau', LOADTEST_BREAKPOINT: '100', LOADTEST_THRESHOLD_SCENARIO: 'resolve', LOADTEST_THRESHOLD_P95_MS: '200' }, + 'mix', + 500, + ); + + assert.equal(options.scenarios?.['verdict-1']?.env?.LOADTEST_STEP_P95_MS, '500'); +}); + diff --git a/loadtest/profiles/options.ts b/loadtest/profiles/options.ts index 2689090924..70a4709d5c 100644 --- a/loadtest/profiles/options.ts +++ b/loadtest/profiles/options.ts @@ -19,6 +19,7 @@ import type { Options } from 'k6/options'; import { PROFILES, type Profile } from '../lib/config.ts'; +import { overriddenP95Ms } from '../lib/thresholds.ts'; import { breakpointScenario } from './breakpoint.ts'; import { plateauScenarios, plateauSteps } from './plateau.ts'; import { soakScenario } from './soak.ts'; @@ -29,7 +30,8 @@ export type ScriptVariables = Readonly>; /** The function of the scenario module that each iteration runs. */ const EXEC = 'default'; -export function profileOptions(env: ScriptVariables, scenario: string, p95Ms: number): Options { +export function profileOptions(env: ScriptVariables, scenario: string, declaredP95Ms: number): Options { + const p95Ms = overriddenP95Ms(env, scenario, declaredP95Ms); switch (profileOf(env)) { case 'breakpoint': { const breakpoint = breakpointScenario({ diff --git a/loadtest/runner/main.ts b/loadtest/runner/main.ts index 339fb002d1..7c84cd5152 100644 --- a/loadtest/runner/main.ts +++ b/loadtest/runner/main.ts @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -// The runner entry point: `npm run load -- --topology --profile

--scenario [--size ] [--reference] [--parallelism n]`. +// The runner entry point: `npm run load -- --topology --profile

--scenario [--size ] [--reference] [--parallelism n] [--threshold =]`. // The Ingot image tag is read from INGOT_IMAGE_TAG, the remote write endpoint of the metrics store from INGOT_LOADTEST_RW_URL. A run with --size // writes its result record, which also needs INGOT_LOADTEST_CPU_MODEL, and reads its outcome from the query API of the metrics store (see // measure.ts, INGOT_LOADTEST_QUERY_URL). The namespace is fixed to the load test namespace; the TestRun and @@ -51,6 +51,7 @@ import { fileRecordWriter, recordStep } from './record.ts'; import { parseSizeOption, sizeHelmArguments } from './size.ts'; import { buildTestRun } from './testrun.ts'; import { runSteps, type RunnerPorts } from './steps.ts'; +import { thresholdOverrideVariables } from '../lib/thresholds.ts'; const exec = promisify(execFile); const loadtestRoot = join(dirname(fileURLToPath(import.meta.url)), '..'); @@ -96,7 +97,8 @@ async function main(argv: readonly string[]): Promise { } const ingotDigest = process.env.INGOT_IMAGE_DIGEST; const rwUrl = remoteWriteUrl(process.env); - const profileInputs = scriptVariables(process.env); + // A --threshold flag names the scenario of this run and replaces its p95 limit in the scripts (lib/thresholds.ts). + const profileInputs = { ...scriptVariables(process.env), ...thresholdOverrideVariables(argv, options.scenario) }; // Only a run with a reference size writes a record. Its inputs are checked now, not after a run of hours. const recording = size ? { queryUrl: metricsQueryUrl(process.env), cpuModel: cpuModel(process.env) } : undefined; const holdMinutes = Number(profileInputs.LOADTEST_HOLD_MINUTES ?? PLATEAU_DEFAULT_HOLD_MINUTES);