Skip to content

Instantly share code, notes, and snippets.

@mstoykov
Created August 6, 2020 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstoykov/f4a7c42ce91abaec3326b654bf4484a2 to your computer and use it in GitHub Desktop.
Save mstoykov/f4a7c42ce91abaec3326b654bf4484a2 to your computer and use it in GitHub Desktop.
import http from 'k6/http';
import {check, group, sleep, fail} from 'k6';
export let options = {
scenarios:{
"vus_100": {
executor: "ramping-vus",
stages: [
{ target: 100, duration: '1m' },
{ target: 100, duration: '3m' },
],
},
"arrival_rate_100": {
executor: "ramping-arrival-rate",
stages: [
{ target: 120, duration: '1m' },
{ target: 120, duration: '3m' },
],
preallocatedVus: 100,
startTime:"5m",
},
"arrival_rate_200": {
executor: "ramping-arrival-rate",
stages: [
{ target: 120, duration: '1m' },
{ target: 120, duration: '3m' },
],
preallocatedVus: 200,
startTime:"10m",
},
"arrival_rate_300": {
executor: "ramping-arrival-rate",
stages: [
{ target: 120, duration: '1m' },
{ target: 120, duration: '3m' },
],
preallocatedVus: 300,
startTime:"15m",
},
"vus_70": {
executor: "ramping-vus",
stages: [
{ target: 70, duration: '1m' },
{ target: 0, duration: '3m' },
],
startTime: "20m",
},
},
thresholds: {
'http_req_duration': ['p(95)<500', 'p(99)<1500'],
'http_req_duration{name:PublicCrocs}': ['avg<400'],
'http_req_duration{name:Create}': ['avg<600', 'max<1000'],
},
};
function randomString(length) {
const charset = 'abcdefghijklmnopqrstuvwxyz';
let res = '';
while (length--) res += charset[Math.random() * charset.length | 0];
return res;
}
const USERNAME = `${randomString(10)}@example.com`; // Set your own email or `${randomString(10)}@example.com`;
const PASSWORD = 'superCroc2019';
const BASE_URL = 'https://test-api.k6.io';
export function setup() {
// register a new user and authenticate via a Bearer token.
let res = http.post(`${BASE_URL}/user/register/`, {
first_name: 'Crocodile',
last_name: 'Owner',
username: USERNAME,
password: PASSWORD,
});
check(res, { 'created user': (r) => r.status === 201 });
let loginRes = http.post(`${BASE_URL}/auth/token/login/`, {
username: USERNAME,
password: PASSWORD
});
let authToken = loginRes.json('access');
check(authToken, { 'logged in successfully': () => authToken !== '', });
return authToken;
}
export default (authToken) => {
const requestConfigWithTag = tag => ({
headers: {
Authorization: `Bearer ${authToken}`
},
tags: Object.assign({}, {
name: 'PrivateCrocs'
}, tag)
});
group('Public endpoints', () => {
// call some public endpoints in a batch
let responses = http.batch([
['GET', `${BASE_URL}/public/crocodiles/1/`, null, {tags: {name: 'PublicCrocs'}}],
['GET', `${BASE_URL}/public/crocodiles/2/`, null, {tags: {name: 'PublicCrocs'}}],
['GET', `${BASE_URL}/public/crocodiles/3/`, null, {tags: {name: 'PublicCrocs'}}],
['GET', `${BASE_URL}/public/crocodiles/4/`, null, {tags: {name: 'PublicCrocs'}}],
]);
const ages = Object.values(responses).map(res => res.json('age'));
// Functional test: check that all the public crocodiles are older than 5
check(ages, {
'Crocs are older than 5 years of age': Math.min(...ages) > 5
});
});
group('Create and modify crocs', () => {
let URL = `${BASE_URL}/my/crocodiles/`;
group('Create crocs', () => {
const payload = {
name: `Name ${randomString(10)}`,
sex: 'M',
date_of_birth: '2001-01-01',
};
const res = http.post(URL, payload, requestConfigWithTag({ name: 'Create' }));
if (check(res, { 'Croc created correctly': (r) => r.status === 201 })) {
URL = `${URL}${res.json('id')}/`;
} else {
console.log(`Unable to create a Croc ${res.status} ${res.body}`);
return
}
});
group('Update croc', () => {
const payload = { name: 'New name' };
const res = http.patch(URL, payload, requestConfigWithTag({ name: 'Update' }));
const isSuccessfulUpdate = check(res, {
'Update worked': () => res.status === 200,
'Updated name is correct': () => res.json('name') === 'New name',
});
if (!isSuccessfulUpdate) {
console.log(`Unable to update the croc ${res.status} ${res.body}`);
return
}
});
const delRes = http.del(URL, null, requestConfigWithTag({ name: 'Delete' }));
const isSuccessfulDelete = check(null, {
'Croc was deleted correctly': () => delRes.status === 204,
});
if (!isSuccessfulDelete) {
console.log(`Croc was not deleted properly`);
return
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment