Skip to content

Performance Pipeline

The Performance Pipeline measures how long commands and queries take to execute and logs a warning when their execution time exceeds the configured threshold.

You enable it by passing withPerformance() to XenoAppBuilder.addPipeline().

The pipeline supports two levels of configuration:

  • thresholdMs: global threshold used for all intents.
  • intentThresholdMs: optional thresholds for specific intents.

When an intent has a specific threshold configured, that value takes precedence over the global threshold.

Add the pipeline when configuring your application:

import { XenoAppBuilder, withPerformance } from '@xeno-js/core'
const app = new XenoAppBuilder()
.addPipeline(withPerformance())
.build()

withPerformance() adds the Performance Pipeline to the common pipeline behaviors used by commands and queries.

Use the thresholdMs option to define the default threshold for commands and queries.

import { XenoAppBuilder, withPerformance } from '@xeno-js/core'
const app = new XenoAppBuilder()
.addPipeline(withPerformance((options) => {
options.thresholdMs = 500
}))
.build()

The default value is:

500

The value is expressed in milliseconds.

The global threshold is used whenever the current request intent does not have a specific threshold configured.

Use intentThresholdMs as a registry of per-intent threshold overrides. Each key must match the request’s intent; each value is an object containing a thresholdMs property.

An intent-specific threshold takes precedence over the global thresholdMs option.

import { XenoAppBuilder, withPerformance } from '@xeno-js/core'
const app = new XenoAppBuilder()
.addPipeline(withPerformance((options) => {
options.thresholdMs = 500
options.intentThresholdMs = {
CREATE_USER_HANDLER: { thresholdMs: 300 },
}
}))
.build()

In this example:

  • all requests use 500ms by default;
  • requests with intent CREATE_USER_HANDLER use 300ms;
  • the specific CREATE_USER_HANDLER threshold overrides the global threshold.

This is useful when different application operations have different expected execution times.

You can configure multiple intents independently in the intentThresholdMs registry:

import { XenoAppBuilder, withPerformance } from '@xeno-js/core'
const app = new XenoAppBuilder()
.addPipeline(withPerformance((options) => {
options.thresholdMs = 500
options.intentThresholdMs = {
CREATE_USER_HANDLER: { thresholdMs: 300 },
UPDATE_USER_HANDLER: { thresholdMs: 400 },
DELETE_USER_HANDLER: { thresholdMs: 250 },
GET_USER_HANDLER: { thresholdMs: 100 },
}
}))
.build()

The effective threshold for a request is selected in this order:

  1. options.intentThresholdMs[request.intent].thresholdMs
  2. options.thresholdMs
  3. the default global threshold of 500ms

For example, with:

options.thresholdMs = 500
options.intentThresholdMs = {
CREATE_USER_HANDLER: { thresholdMs: 300 },
}

the behavior is:

Intent Effective threshold
CREATE_USER_HANDLER 300ms
UPDATE_USER_HANDLER 500ms
DELETE_USER_HANDLER 500ms
Any unconfigured intent 500ms

An intent-specific threshold is therefore an override, not an additional threshold.

You can keep the global threshold and override only the operations that require different limits:

import { XenoAppBuilder, withPerformance } from '@xeno-js/core'
const app = new XenoAppBuilder()
.addPipeline(withPerformance((options) => {
options.thresholdMs = 1000
options.intentThresholdMs = {
CREATE_USER_HANDLER: { thresholdMs: 300 },
CHECKOUT_HANDLER: { thresholdMs: 800 },
}
}))
.build()

Here:

  • CREATE_USER_HANDLER uses 300ms;
  • CHECKOUT_HANDLER uses 800ms;
  • every other intent uses 1000ms.

When execution exceeds the effective threshold, the pipeline logs a warning:

Performance warning: CREATE_USER_HANDLER took 342.17ms

The warning contains:

  • the request intent;
  • the measured execution time in milliseconds.

A warning is logged only when the execution time is greater than the effective threshold.

For example, with a threshold of 300ms:

  • 299ms → no warning;
  • 300ms → no warning;
  • 301ms → warning.

The global thresholdMs must be greater than zero. Intent-specific thresholdMs values must be positive integers.

For example, this configuration is invalid:

import { XenoAppBuilder, withPerformance } from '@xeno-js/core'
const app = new XenoAppBuilder()
.addPipeline(withPerformance((options) => {
options.thresholdMs = 0
options.intentThresholdMs = {
CREATE_USER_HANDLER: { thresholdMs: -100 },
}
}))
.build()

The global threshold rejects negative values and zero, but is not checked for integer precision. The pipeline validates each intent-specific threshold as a positive integer.

Each configured intent threshold is validated independently.

The following example configures a global threshold together with different thresholds for selected handlers:

import { XenoAppBuilder, withPerformance } from '@xeno-js/core'
const app = new XenoAppBuilder()
.addPipeline(withPerformance((options) => {
// Fallback threshold for all intents.
options.thresholdMs = 500
// Overrides for selected intents.
options.intentThresholdMs = {
CREATE_USER_HANDLER: { thresholdMs: 300 },
UPDATE_USER_HANDLER: { thresholdMs: 400 },
CHECKOUT_HANDLER: { thresholdMs: 1000 },
}
}))
.build()

The resulting behavior is:

CREATE_USER_HANDLER → 300ms
UPDATE_USER_HANDLER → 400ms
CHECKOUT_HANDLER → 1000ms
Everything else → 500ms

The Performance Pipeline applies to both commands and queries executed through the configured CQRS pipeline.

The intent-specific configuration works the same way for both:

withPerformance((options) => {
options.intentThresholdMs = {
CREATE_USER_HANDLER: { thresholdMs: 300 },
GET_USER_HANDLER: { thresholdMs: 100 },
}
})

The intent identifies the operation being executed, regardless of whether it is a command or query.

Check that the registry key in intentThresholdMs exactly matches the request intent, and that its value contains thresholdMs:

options.intentThresholdMs = {
CREATE_USER_HANDLER: { thresholdMs: 300 },
}

The pipeline compares the key directly with request.intent.

If the intent does not match, Xeno.JS falls back to:

options.thresholdMs

I expected a warning at exactly the threshold

Section titled “I expected a warning at exactly the threshold”

The pipeline logs a warning only when execution time is greater than the threshold.

With:

options.thresholdMs = 500

an execution time of exactly 500ms does not generate a warning.

Check that the global value is greater than zero. Intent-specific values must be positive integers.

Valid:

300
500
1000

Invalid global values:

0
-100

An intent-specific value such as 250.5 is also invalid because intent thresholds must be integers.


Xeno.JS is an MIT-licensed open source project. It can grow thanks to the support of these awesome people. If you’d like to join them, please read more at support section