synthtraffic Get started
Docs menu

Build a scenario

Collections

An order with three line items is one event containing a nested list—not four generators. Use an array expression to build those repeated items inside the event.

Build an order and its items

This scenario creates two orders. Each order should contain three generated items and totals calculated from them:

{
  "orderId": "ORD-000",
  "items": [
    {"lineNumber": 1, "product": "keyboard", "unitPrice": 25, "quantity": 2, "lineTotal": 50},
    {"lineNumber": 2, "product": "mouse", "unitPrice": 15, "quantity": 1, "lineTotal": 15},
    {"lineNumber": 3, "product": "monitor", "unitPrice": 200, "quantity": 3, "lineTotal": 600}
  ],
  "itemCount": 3,
  "subtotal": 665,
  "averageUnitPrice": 80
}

Line items with a subtotal

YAML order-line-items.yaml
generators:
  - name: orders
    config:
      maxEvents: 2
    value:
      orderId: =seq(format=ORD-%03d)
      items:
        =array:
          count: 3
          of:
            lineNumber: =seq(start=1)
            product: =cycle(keyboard, mouse, monitor)
            unitPrice: =cycle(25, 15, 200)
            quantity: =cycle(2, 1, 3)
            lineTotal: =multiply($unitPrice, $quantity)
      itemCount: =count($items)
      subtotal: =sum($items.lineTotal)
      averageUnitPrice: =avg($items.unitPrice)

Press Try it and expand an order. Its payload keeps the nested structure shown above.

Read the array block

The line items: =array: means “set the items field to a generated array.” The indented block below it has two important settings:

  • count decides how many items to create.
  • of describes the fields in each item.

count can also be an expression, such as =int(1, 5), when different orders should contain different numbers of items.

Use fields from the same item

Inside of, $unitPrice and $quantity refer to fields on the item currently being built:

lineTotal: =multiply($unitPrice, $quantity)

Synthtraffic calculates the dependencies in the right order, so lineTotal uses the matching price and quantity from that line item.

Calculate values from the full list

After items is complete, parent fields can inspect it:

itemCount: =count($items)
subtotal: =sum($items.lineTotal)
averageUnitPrice: =avg($items.unitPrice)

$items.lineTotal means “the lineTotal from every object in items.” count, sum, and avg then reduce those values to one result. An average of an empty list is null.

This lesson focuses on generating a nested list. Collection expressions covers range, merge, selectKeys, omitKeys, and the full function details.

Run this file

Save the example as order-line-items.yaml.

sample prints a short preview to your terminal. It does not wait for real-time pacing, and it never opens Kafka, PostgreSQL, or other destinations.

synthtraffic sample order-line-items.yaml --events 2 --seed 42

Next: send events through a connection.