Multiple ctx object provides:

  1. The ability to pass data between Test Stages
  2. Virtual Users with Identity, Context Awareness, and Memory
  3. A way to capture metrics
  4. An axios instance and fetch function with built-in metrics capture

This object is maintained by the system, is read-only, and is passed as an argument into each of the Test Stages, which makes it always available.

interface MxCtx {
  info: {
    vuId: number; // int, unique to each VU
    orgId: string; // uuid
    testId: string; // uuid
    testResultId: string; // uuid
    startedAt: number; // ms time since epoch
    startedBy: string;
    loopIteration: number; // int, starting at 0
  };

  globalInitData: object; // user-defined JSON serializable data returned from globalInit()

  vuInitData: any; // user-defined data of any type returned from vuInit()

  metric: (label: string, value: number, units?: string) => void; // use ctx.metric() to capture metrics

  util: {
    sleep: (ms: number) => Promise<void>; // helper function to sleep for a given number of milliseconds
  };

  axios: AxiosInstance; // Axios HTTP client instance with built-in Multiple metrics capture

  fetch: typeof fetch; // Node.js fetch function with built-in Multiple metrics capture
}

ctx.info

ctx.info represents general information about the test itself.

From within vuInit(), ctx.info.vuId is set individually in each VU at Test runtime, to a unique id which is assigned in sequence for each VU, starting from zero.

ctx.globalInitData

Use ctx.globalInitData to access data that is returned from globalInit().

ctx.vuInitData

Use ctx.vuInitData to access data that is returned from vuInit(), such as an auth token or database connection.

ctx.metric()

ctx.metric(label: string, value: number, units?: string) is used to capture custom metrics during a load test. All metrics captured are available on the Test Results page and in the Load Test Results export.

See Metrics to learn more about built-in and custom metrics.

Utility Functions

ctx.util.sleep()

Use await ctx.util.sleep(ms: number) to pause the VU for a given duration in milliseconds.