import grpc from ‘k6/net/grpc’;
import { check, sleep } from ‘k6’;
import { Counter } from ‘k6/metrics’;
export let errorCounter = new Counter(‘errors’);
const client = new grpc.Client();
client.load([‘…/proto’], ‘service.proto’);
let payloads = ; // Declare payloads globally for collection during iterations
export async function setup() {
const awsConfig = await configureAWS(region, awsAccessKeyId, awsSecretAccessKey, awsSessionToken);
const s3Client = new S3Client(awsConfig);
// Fetch data from S3
const fetchData = await s3Client.getObject(bucketName, objectKey);
const jsonData = JSON.parse(fetchData.data);
return { jsonData };
}
export let options = {
vus: 1, // Number of Virtual Users
iterations: 50, // Number of iterations equal to the number of payload records
};
export default function (data) {
const { jsonData } = data;
// Get the current iteration index
let index = __ITER;
// Get the payload for the current iteration
const payload = jsonData[index];
if (__ITER === 0) {
client.connect(__ENV.BASE_URL + ':' + __ENV.PORT, {
plaintext: true
});
}
const response = client.invoke('ts.service.services.v1.Service/SportGetScore', payload);
// Collect the payload locally
payloads.push({ request: payload, response: response });
// Log errors
if (response.error !== null) {
errorCounter.add(1);
console.log("Error payload: ", payload);
}
// Optional sleep to control the rate of requests
sleep(2);
}
export function teardown() {
// This function will be called once after all VUs complete
return { payloads: payloads };
}
export function handleSummary(data) {
// Access the returned data from teardown
const teardownData = data.teardown || {};
return {
'summary.json': JSON.stringify({
metrics: data,
payloads: teardownData.payloads || [], // Include collected payloads in the summary
}, null, 2),
stdout: JSON.stringify(teardownData.payloads || [], null, 2), // Show the collected payloads in the console output
};
}