Skip to main content

Write your own load driver

Speedscale replays recorded deliveries to a real broker on its own for Kafka, RabbitMQ, Google Pub/Sub and Redis; see Test a consumer. Write your own load driver when you need full control over what is sent and when, such as editing message bodies, sending to a broker the replay cannot reach, or running the load from your own test harness.

The approach has two steps: extract the recorded messages from a snapshot into a CSV file, then publish each row with a small producer.

Extract the messages​

speedctl extract data reads a snapshot, pulling it from Speedscale cloud if it is not on your machine, and writes one CSV row per recorded call. Give it one --path per column and turn off de-duplication, which otherwise keeps only one column of unique values:

speedctl extract data <snapshot-id> --dedup=false \
--path kafka.response.FetchResponse.topics.0.partitions.0.records.records.0.valueString \
--path ts \
--filter '(command IS "Fetch")'

The output names the directory it writes to, under ~/.speedscale/data/userdata. The CSV has a header row, then one row per call with each path's value and the call's RRPair UUID:

kafka.response.FetchResponse.topics.0.partitions.0.records.records.0.valueString,ts,RRPair UUID
"{""id"":1,""total"":10}",2026-01-15T10:30:45.123Z,44f7a2cc-2045-4fb6-9635-3da8aa7fa909
"{""id"":2,""total"":20}",2026-01-15T10:30:46.456Z,58f7a2cc-1135-4fa6-3433-ada5aa2fa161

A call that has no value for a path is left out. Add --include-errors to see why a path finds nothing, and --regex to keep only part of a value.

BrokerPath to the messageFilterThe value is
Kafka, and Azure Event Hubs through its Kafka endpointkafka.response.FetchResponse.topics.0.partitions.0.records.records.0.valueString(command IS "Fetch")Text, when the record value is UTF-8
RabbitMQ and Amazon MQAmqpV091.server.basic.deliver.body(command IS "Basic.Deliver")Base64
Amazon SQS, with the JSON protocol current AWS SDKs usehttp.req.bodyBase64.MessageBodyA filter on the queue's callsText
Amazon Kinesis Data Streamshttp.req.bodyBase64.DataA filter on the stream's callsBase64, as the Kinesis API sends it. An SDK that sends CBOR instead of JSON, as the AWS SDK for Java does by default, needs the whole body.
Amazon Data Firehosehttp.req.bodyBase64.Record.DataA filter on the delivery stream's callsBase64, as the Firehose API sends it
Amazon SNS, and anything else with a form or binary bodyhttp.req.bodyBase64A filter on the topic's callsBase64 of the whole request body, for your driver to parse

The paths name one field of one call. The Kafka path takes the first record of the first partition of each Fetch; change the indexes, or add more --path columns, for fetches that return several records. The HTTP paths read what your app sent, so a snapshot of a producer gives you its messages. speedctl extract data --help lists more examples.

Messages of protocols Speedscale does not decode, such as AMQP 1.0, the Pulsar binary protocol and OpenWire, are not in the recording, so there is nothing to extract for them.

Write the producer​

Using the language of your choice, and an AI assistant if you like, write a producer that:

  1. Reads the CSV and skips the header row.
  2. Connects to the broker.
  3. For each row, decodes the message body if it is base64.
  4. Optionally waits the time between this row's timestamp and the previous one, to keep the recorded pacing.
  5. Publishes the message.
  6. Flushes and closes the connection.

The loop is the same for every broker. Each version below takes a publish function, shown per broker below, and a flag to keep the recorded timing; without it, the driver sends as fast as the broker accepts.

package main

import (
"encoding/base64"
"encoding/csv"
"io"
"os"
"time"
)

// replay publishes each row of the CSV that speedctl extract data wrote
func replay(path string, isBase64, respectTiming bool, publish func([]byte) error) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()

r := csv.NewReader(f)
if _, err := r.Read(); err != nil { // skip the header row
return err
}
var last time.Time
for {
row, err := r.Read()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
body := []byte(row[0])
if isBase64 {
if body, err = base64.StdEncoding.DecodeString(row[0]); err != nil {
return err
}
}
if respectTiming && len(row) > 1 {
ts, err := time.Parse(time.RFC3339Nano, row[1])
if err != nil {
return err
}
if !last.IsZero() && ts.After(last) {
time.Sleep(ts.Sub(last))
}
last = ts
}
if err := publish(body); err != nil {
return err
}
}
}

Publish to your broker​

Each snippet connects to the broker and defines the publish function the loop calls. Replace the names in angle brackets with your own, and call replay with the CSV path.

Kafka and Azure Event Hubs​

// github.com/confluentinc/confluent-kafka-go/v2/kafka
p, err := kafka.NewProducer(&kafka.ConfigMap{"bootstrap.servers": "<broker>:9092"})
if err != nil {
return err
}
defer p.Close()
topic := "<topic>"
err = replay("out.csv", false, true, func(body []byte) error {
return p.Produce(&kafka.Message{
TopicPartition: kafka.TopicPartition{Topic: &topic, Partition: kafka.PartitionAny},
Value: body,
}, nil)
})
p.Flush(15_000)

For Azure Event Hubs, connect to <namespace>.servicebus.windows.net:9093 with security.protocol=SASL_SSL, sasl.mechanism=PLAIN, the username $ConnectionString and the namespace's connection string, from Shared access policies in the Azure portal, as the password. The event hub's name is the topic.

RabbitMQ and Amazon MQ​

The recorded body is base64, so pass true for the base64 flag. Publish to the exchange and routing key the consumer reads through; an empty exchange with the queue's name as the routing key sends straight to that queue. For Amazon MQ, use an amqps:// URL on port 5671.

// github.com/rabbitmq/amqp091-go
conn, err := amqp.Dial("amqp://<user>:<password>@<broker>:5672/")
if err != nil {
return err
}
defer conn.Close()
ch, err := conn.Channel()
if err != nil {
return err
}
err = replay("out.csv", true, true, func(body []byte) error {
return ch.PublishWithContext(context.Background(), "<exchange>", "<routing-key>", false, false,
amqp.Publishing{Body: body})
})

Amazon SQS​

// github.com/aws/aws-sdk-go-v2/config and service/sqs
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
return err
}
client := sqs.NewFromConfig(cfg)
err = replay("out.csv", false, true, func(body []byte) error {
_, err := client.SendMessage(context.Background(), &sqs.SendMessageInput{
QueueUrl: aws.String("<queue-url>"),
MessageBody: aws.String(string(body)),
})
return err
})

Amazon SNS​

SNS Publish calls are form-encoded, so extract the whole body and take its Message parameter. SNS delivers each message to every subscription on the topic.

// github.com/aws/aws-sdk-go-v2/config and service/sns
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
return err
}
client := sns.NewFromConfig(cfg)
err = replay("out.csv", true, true, func(body []byte) error {
form, err := url.ParseQuery(string(body))
if err != nil {
return err
}
_, err = client.Publish(context.Background(), &sns.PublishInput{
TopicArn: aws.String("<topic-arn>"),
Message: aws.String(form.Get("Message")),
})
return err
})

Amazon Kinesis Data Streams​

The partition key decides which shard a record goes to. Use one key to keep the records in order, or vary it to spread the load across shards.

// github.com/aws/aws-sdk-go-v2/config and service/kinesis
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
return err
}
client := kinesis.NewFromConfig(cfg)
err = replay("out.csv", true, true, func(body []byte) error {
_, err := client.PutRecord(context.Background(), &kinesis.PutRecordInput{
StreamName: aws.String("<stream>"),
PartitionKey: aws.String("<partition-key>"),
Data: body,
})
return err
})

Amazon Data Firehose​

Firehose buffers records and delivers them to their destination by its own buffer size and interval, so the destination sees them later than you sent them.

// github.com/aws/aws-sdk-go-v2/config, service/firehose and service/firehose/types
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
return err
}
client := firehose.NewFromConfig(cfg)
err = replay("out.csv", true, true, func(body []byte) error {
_, err := client.PutRecord(context.Background(), &firehose.PutRecordInput{
DeliveryStreamName: aws.String("<delivery-stream>"),
Record: &types.Record{Data: body},
})
return err
})

The AWS SDKs read credentials and the region from the usual places: environment variables, the shared credentials file or an IAM role.

Run it​

Point the driver at a disposable broker, start the consumer under test, and run the driver. Without the timing flag it sends as fast as the broker accepts, for maximum throughput; with it, the gaps between messages match the recording. Run several copies at once to multiply the load.

A complete RabbitMQ example app with a driver like this is in the Speedscale demo repository.

Loading search...