synthtraffic Get started
Docs menu

Build a scenario

vars and const

The product generator now needs two kinds of shared values: one catalog id for the whole run, and one product id reused inside each product event. Writing the expressions twice would calculate two different results.

Use const and vars to calculate a value once, give it a name, and reuse it with $name.

The result we want

The catalog id should stay fixed. The product id should change, but the id inside productUrl must match it:

{"catalogId":"CAT-00","productId":"PROD-00","productName":"Wireless headphones","productUrl":"/products/PROD-00"}
{"catalogId":"CAT-00","productId":"PROD-01","productName":"Mechanical keyboard","productUrl":"/products/PROD-01"}

Reuse calculated values

Products from one catalog

YAML catalog-products.yaml
generators:
  - name: products
    config:
      maxEvents: 3
    const:
      catalogId: =seq(format=CAT-%02d)
    vars:
      productId: =seq(format=PROD-%02d)
    value:
      catalogId: $catalogId
      productId: $productId
      productName: =cycle(Wireless headphones, Mechanical keyboard, Studio microphone)
      productUrl: =concat(/products/, $productId)

Press Try it and compare the three events.

  • const.catalogId is calculated once when the generator starts, so every product uses CAT-00.
  • vars.productId is calculated again for every event, producing PROD-00, PROD-01, and PROD-02.
  • $catalogId and $productId copy those calculated values into value.
  • =concat(/products/, $productId) uses the current event’s product id in the URL.

Helpers are not output fields

Only fields under value appear in the event. Names under const and vars are available while the event is built, but stay hidden unless value copies them.

If you remove productId: $productId from value, productUrl still works because $productId remains available. The standalone productId field simply disappears from the output.

Choose the right lifetime

  • Use const for one value shared by the generator run, such as a catalog, batch, or tenant id.
  • Use vars for a value shared only inside one event, such as an id used in both a key and payload.

Placing the same expression in two fields does not share its result. Name it once when the fields must agree.

Next: control timestamps and pacing.