Skip to content

Instantly share code, notes, and snippets.

@deniscapeto
Last active February 4, 2024 14:56
Show Gist options
  • Save deniscapeto/d05544347dd903636e71ee74ea0d9950 to your computer and use it in GitHub Desktop.
Save deniscapeto/d05544347dd903636e71ee74ea0d9950 to your computer and use it in GitHub Desktop.
K6 load testing using CSV file
import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js';
import { SharedArray } from "k6/data";
import http from 'k6/http';
import { check } from 'k6';
const csvData = new SharedArray("another data name", function() {
return papaparse.parse(open('./data.csv'), { header: true }).data;
});
const BASE_URL = 'https://www.mywebsiteundertest.com.br'
export const options = {
thresholds: {
http_req_failed: ['rate<0.01'], // http errors should be less than 1%
http_req_duration: ['p(50)<200'], // 50% of requests should be below 200ms
http_req_duration: ['p(95)<900'], // 95% of requests should be below 200ms
},
vus: 30,
duration: '30s',
}
export default function () {
let my_id = getRandonId()
let url = `${BASE_URL}/my_resource/${my_id}`
let params = {
headers: {
'Accept': 'application/json'
},
};
let res = http.get(url, params);
console.log(`${res.status} - ${url}`);
check(res, {
'status is 200': (r) => r.status === 200,
});
}
function getRandonId(){
let randIndex = getRandomInt(0, csvData.length);
return csvData[randIndex].my_file_header_name;
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment