This document provides a comprehensive guide to the k6 load testing configuration used in the Stroppy project. The k6 configuration provides a flexible way to run load tests against your database.
The k6 configuration script is a TypeScript file that defines how load tests should be executed. It integrates with the Stroppy benchmarking framework to generate and execute database queries under load.
The script reads configuration from environment variables, primarily from the context
environment variable which contains a serialized StepContext object.
| Parameter | Environment Variable | Description | Default |
|---|---|---|---|
| Setup Timeout | STEP_RUN_CONTEXT.config.k6Executor.k6SetupTimeout |
Maximum time allowed for test setup | 1 second |
| Step Rate | STEP_RUN_CONTEXT.config.k6Executor.k6Rate |
Number of iterations per second | 1 |
| Step Duration | STEP_RUN_CONTEXT.config.k6Executor.k6Duration |
How long the test should run (in seconds) | 1 second |
| Pre-allocated VUs | STEP_RUN_CONTEXT.config.k6Executor.k6Vus |
Initial number of virtual users | 1 |
| Max VUs | STEP_RUN_CONTEXT.config.k6Executor.k6MaxVus |
Maximum number of virtual users | 1 |
| Error Threshold | Hardcoded | Percentage of errors that will abort the test | 50% |
The test follows this lifecycle:
- Setup: Initializes the test environment and generates the query queue
- Execution: Runs the actual load test
- Teardown: Cleans up resources
- Summary: Generates test results and metrics
The instance object is an instance of the Xk6Instance golang type provided by xk6 plugin,
which is responsible for executing golang functions from k6.ts. You can't customize it.
type Serialized<T extends any> = string; // protojson serialization of T, need for type safety
interface Xk6Instance {
setup(config: Serialized<StepContext>): Error | undefined; // call to initialize resources and setup drivers
generateQueue(): Serialized<DriverQueriesList>;// call to generate the query queue from the StepContext
runQuery(query: Serialized<DriverQuery>): Error | undefined; // call to execute a query
teardown(): Error | undefined; // call to clean up resources
}
const instance: Xk6Instance = newstess.new(); // Initialize Xk6InstanceThe STEP_RUN_CONTEXT variable is a serialized StepContext object,
which is passed to the setup function of the instance object.
In StepContext could be found all the information about the test such a RunConfig, BenchmarkDescriptor and
StepDescriptor.
const err = instance.setup(StepContext.toJsonString(STEP_RUN_CONTEXT));
if (err !== undefined) {
throw err;
}The example script includes a set of metrics to track the performance of the test. You could customize it as you need in your custom script.
const setupTimeCounter = new Counter("setup_time") // Time taken to initialize the test
const respTimeTrend = new Trend("resp_time"); // Response time trend
const requestCounter = new Counter("total_requests"); // Request count
const errorCounter = new Counter("total_errors"); // Error countExample script uses the handleSummary function to generate a summary report after test completion, the handleSummary
function generates a detailed report including:
- Test configuration details
- Duration of all test stages
- Total requests processed
- Error count and rate
- Requests per second (RPS) metrics
- Response time statistics (min, max, avg, percentiles)
interface CounterMeter {
values: { count: number, rate: number }
}
interface TrendMeter {
values: {
avg: number
min: number
med: number
max: number
p90: number
p95: number
}
}
class Summary {
setup_data: Context
metrics: {
data_received: CounterMeter
total_requests: CounterMeter
resp_time: TrendMeter
iteration_duration: TrendMeter
dropped_iterations?: CounterMeter
iterations: CounterMeter
total_errors: CounterMeter
setup_time: CounterMeter
data_sent: CounterMeter
}
state: {
isStdOutTTY: boolean
isStdErrTTY: boolean
testRunDurationMs: number
}
}
// Summary function, that will create summary file with metrics.
export function handleSummary(summaryData: Summary) {
};- Start Small: Begin with a low number of VUs and gradually increase.
- Monitor Resources: Keep an eye on system resources during tests.
- Set Realistic Durations: Ensure test durations are long enough to get meaningful results.
- Analyze Results: Pay attention to both success rates and response times.
- Error Handling: Implement proper error handling for your specific use case.
- Setup failures
- Query execution errors
- Timeout handling
- Error threshold violations (test will abort if error rate exceeds threshold)
- Test Failing During Setup: Check the setup timeout value and increase if necessary.
- High Error Rates: Verify your database connection settings and query parameters.
- Performance Issues: Monitor system resources and adjust VU count accordingly.
- Unexpected Behavior: Check the k6 logs and console output for detailed error messages.