go-loadgen is a log load generator tool meant for testing the logging infrastructure. It is capable of producing structured or unstructured/structure logs with random alphanumeric strings and can also playback any input file at a specified rate.
go-loadgen requires Go 1.26 or newer. It is tested with the two supported Go releases.
go install github.com/intuit/go-loadgen@latest
ln -s "$(go env GOPATH)/bin/go-loadgen" /usr/local/bin/loadgen
loadgen --version
//Build command
go build -o loadgen
//Run the tests
go test -race ./...
docker build -t go-loadgen .
docker run --rm go-loadgen random-strings --duration 5 --lines-per-second 5 \
--line-count 1 --line-length 100 --multi-line-percent 0 --output stdout
Use the help command to find all available commands and flags.
$ ./loadgen --help
loadgen is a tool which generates test-data (synthetic/replay) at a controlled rate to test logging infrastructure.
Usage:
loadgen [COMMANDS] [flags]
loadgen [command]
Available Commands:
completion Generate the autocompletion script for the specified shell
help Help about any command
random-strings test data is generated by producing random alphanumeric strings.
replay-file test data is generated by replaying a file.
Flags:
--config string -config /path/to/source/jsonfile (default "cfg")
--custom-timestamp-format string Optional custom timestamp format.The default format is 2006-01-02T15:04:05.000-07:00
-x, --disable-timestamp use --disable-timestamp false will disable timestamp injection while replaying logs. When this flag is not used. Do not modify this parameter for auto-generated random alphanumberic logs.Disabling timestamps is typically suitable while replaying a log-file with pre-existing timestamps.
-d, --duration int Duration of the job in seconds
--enable-config use config file for configuration (default false)
-r, --enable-log-rotation by default log rotation is off, use this flag to turn it on. Example --enable-log-rotation will turn log rotation on
--enable-metrics true, enables prometheus metrics on http://<localhost>:<port>/metric HTTP endpoint for line emit rates and byte count. default is off.
--file-count int number of output files (up to 30). Lines are spread round-robin over the files. (default 1)
-h, --help help for loadgen
-s, --lines-per-second int target lines/second (default 1000)
--log-rotation-max-size-megabytes int Rotate after N MegaBytes. example 100. (default 100)
-p, --metrics-server-port string prometheus metrics server port. default is port 8080. (default "8080")
-o, --output string file path of output log file ex: /var/tmp/test.log
--result-log string Path of the results log where metrics such as total_lines_generated will be log.
--tags string Custom hardcoded tags in the form of K=V
-t, --threads int number of threads generating lines concurrently. --lines-per-second is the combined rate of all threads. With more than one thread, lines from different threads are interleaved in the output. Combine with --file-count for higher throughput. (default 1)
-v, --version version for loadgen
Example-1: To start a test with random-strings in the log file Most of the local flags to this command is mandatory.
The command generates 5 lines/second, with each line of length 1KiloBytes (1000 ASCII characters) and log entry spanning not more than 1 line (line-count) per event (no line breaks). The command produces a log file which grows at a rate of 5 KiloBytes/second and runs for 5 seconds. The overall size of the file after the test completes will be equal to 5 KB*5 seconds = 25KB
./loadgen random-strings --file-count 1 \
--duration 5 \
--enable-log-rotation \
--line-count 1 \
--lines-per-second 5 \
--multi-line-percent 0 \
--line-length 1000 \
--output /var/log/test-alphanumeric.log
Example-2: To start a test with random-strings in the log file with 100% of the log entries containing multi-line.
The flag --line-count and --multi-line-percent determines the number of line-breaks in the multi-line log entry. The resulting log file will be written at a byte rate of line-length*line-count*lines-per-second = 1000*5*5 = 25000 bytes/second. 100% of the log entries in the resulting log file contains line-break character at the end of each line, with each line spanning 1000 characters in length
./loadgen random-strings --file-count 1 \
--duration 5 \
--enable-log-rotation \
--line-count 5 \
--lines-per-second 5 \
--multi-line-percent 100 \
--line-length 1000 \
--output /var/log/test-alphanumeric.log
Example-3: To output log lines to "stdout" instead of a log file, simply declar '--output stdout' parameter. Refer to the example below. Generating logs to stdout is especially useful to simulate logging performance for application pod/containers deployed in Kubernetes.
./loadgen random-strings --file-count 1 \
--duration 5 \
--line-count 1 \
--lines-per-second 5 \
--multi-line-percent 0 \
--line-length 1000 \
--output stdout
To start a log replay test with a specified input file.
Note: Local flags such as duration and lines-per-second is required.
./loadgen replay-file \
--input-file-path /var/log/sample.log \
--output test.log\
--lines-per-second 1 \
--duration 5
You can also specify the number of times a file should be replayed using the --replay-count flag.
You can disable timestamp injection if your replay reference log files already have timestamp using --disable-timestamp boolean flag
If you use the
--replay-countflag, then the flag--durationcannot be used, since the former flag becomes the test termination criteria, and you cannot simultaneously use two different termination criteria for the test.
In the below example, the test will complete after replaying the file "5" times.
Example:
./loadgen replay-file \
--input-file-path /var/log/sample.log \
--output test.log\
--lines-per-second 1 \
--replay-count 5
go-loadgen can introduce custom fields using the tags flag. Provide tags in the form of K=V and those tags will be included in the log event. This particularly useful when metadata such as testId and/or line count should be present in the logs for backend validation.
./loadgen replay-file \
--input-file-path /var/log/sample.log \
--output test.log\
--enable-log-rotation \
--log-rotation-max-size-megabytes 10 \
--lines-per-second 1 \
--file-count 3 \
--duration 5 \
--tags "lineCount=1 testId=1"
By default go-loadgen generates lines on a single thread. Use --threads to generate lines concurrently, for instance to reach higher rates, to keep up with slow storage, or to write to several files at the same time:
./loadgen replay-file \
--input-file-path /var/log/sample.log \
--output test.log \
--lines-per-second 200000 \
--threads 4 \
--file-count 4 \
--duration 60
--lines-per-secondis the combined rate of all the threads; it is split evenly between them.- Each thread writes to the output files round-robin. Writes to a single file happen one at a time, so use
--file-count(at least as many files as threads) for the highest throughput. - Lines are never split or mixed up, and multi-line events (such as stack traces) are always written in one piece. With more than one thread however, events written by different threads are interleaved, so the output is no longer in the order of the input file.
--replay-countis still the number of times the whole input file is replayed, whatever the number of threads, andtotal_lines_generatedin the result log counts the lines written by all threads.
go-loadgen can generate test results log file in the specified path. --result-log is the persistent flag which enables that. Use it as shown below.
It generates a file with "total_lines_generated=x" entry, which can be used as a way to assert whether the generated load is equal to the load received on the backend.
./loadgen replay-file \
--input-file-path /var/log/sample.log \
--output test.log\
--enable-log-rotation \
--log-rotation-max-size-megabytes 10 \
--lines-per-second 1 \
--file-count 1 \
--duration 5 \
--result-log /var/tmp/result.log"
For certain tests, it is critical to generate multiple log files along with log-rotation and backup file retention policies. Here is how you can achieve that using go-loadgen:
./loadgen replay-file \
--input-file-path /var/log/sample.log \
--output test.log\
--enable-log-rotation \
--log-rotation-max-size-megabytes 10 \
--lines-per-second 1 \
--file-count 3 \
--duration 5
In the above CLI command :
- The argument
--enable-log-rotationenables log-rotation - The argument pair
--log-rotation-max-size-megabytes 10specifies how large a file can grow before the file is rotated with a new one. In this example the limit is 10MB. - The argument pair
--file-count 3determines how many log files the test has to create and update simultenously.
go-loadgen currently allows for a maximum of 3 backup files after which the oldest file will be deleted.
Log rotation default settings
| Property | Default | Comments |
|---|---|---|
| MaxFileSize | 100MB | Maximum size the file is allowed to grow, set with --log-rotation-max-size-megabytes |
| MaxAge | 1 Day | After MaxAge exceeds the file is rotated automatically even if size is within the max limit. |
| MaxBackups | 3 | number of rotated files. |
| File Compression | Disabled |
- Customizing log timestamp
Use
2006-01-02T15:04:05.000-07:00with the --custom-timestamp-format flag to provide custom timestamp format. Example:
./loadgen ... --custom-timestamp-format 2006/01/02T15-04-05 ...
The above custom timestamp will override the default timestamp in both alphanumeric random logs and replay logs depending on which mode the test is executed.
To start a test with prometheus metrics exposed to http://localhost:<port>/metrics endpoint, use the "enable-metrics" global flag
Note: the default prometheus listener port is "8080" unless specified using the --metrics-server-port <port> flag
./loadgen replay-file \
--input-file-path /var/log/sample.log \
--output test.log\
--lines-per-second 1 \
--duration 5 \
--enable-metrics \
--metrics-server-port 8081
Example output using curl:
$ curl http://localhost:8081/metrics
# HELP total_bytes_processed Total bytes processed
# TYPE total_bytes_processed counter
total_bytes_processed 2.23982e+07
# HELP total_events_processed Total log events generated by the tool.
