Skip to content

Commit

Permalink
Tidy up K6 examples
Browse files Browse the repository at this point in the history
  • Loading branch information
TimCsaky committed Apr 21, 2023
1 parent 67a14e3 commit 0703347
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 30 deletions.
14 changes: 14 additions & 0 deletions k6/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Load testing with K6

[K6](https://k6.io/docs/) is a load testing tool.
Using the K6 command line interface, you can run the scripts found in this directory to test the performance of COMS API features.

Note: It is important to not run load tests against production environments. Always check with your server administrators before load testing in a shared server environment.

## Prerequesites

The simple test scripts (for example: [createObject.js](createObject.js) can be updated with actual values specific to your envionment (for example: your COMS api url, authorization token and bucket ID) or could also pass these values using parameters of the K6 command used to trigger the test. See more K6 details on how [Environment Variables](https://k6.io/docs/using-k6/environment-variables/) work.

### Command example

`k6 run -e BUCKET_ID=41046be7-43d8-486f-a97e-ee360043d454 -e API_PATH=http://localhost:3000/api/v1 -e AUTH_TOKEN=dXNlcjE6cGFzczE= -e FILE_PATH=./file.txt --vus=1 --iterations=1 createObject.js`
28 changes: 21 additions & 7 deletions k6/createObject.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import http from 'k6/http';
import { check, sleep } from 'k6';

// -------------------------------------------------------------------------------------------------
// Init
// -------------------------------------------------------------------------------------------------
// https://k6.io/docs/using-k6/environment-variables

const apiPath = `${__ENV.API_PATH}`
const bucketId = `${__ENV.BUCKET_ID}`
const filePath = `${__ENV.FILE_PATH}`
const authToken = `${__ENV.AUTH_TOKEN}`

'./file-in-cur-dir.txt'

// k6 options (https://k6.io/docs/using-k6/k6-options/)
export const options = {
scenarios: {
Expand All @@ -15,15 +27,16 @@ export const options = {
},
};

const apiPath = 'http://localhost:3000/api/v1';
const randomLetter = String.fromCharCode(65 + Math.floor(Math.random() * 26));
const bucketId = '<bucket ID (uuid)>';
const url = `${apiPath}/object?bucketId=${bucketId}&tagset[${randomLetter}]=${randomLetter}`;

const randomLetter = () => String.fromCharCode(65 + Math.floor(Math.random() * 26));
const url = `${apiPath}/object?bucketId=${bucketId}&tagset[${randomLetter()}]=${randomLetter()}`;

// open() the file as binary (with the 'b' argument, must be declared in init scope)
// ref: https://k6.io/docs/examples/data-uploads/#multipart-request-uploading-a-file
// eslint-disable-next-line
const binFile = open('./file-in-cur-dir.txt', 'b');
const binFile = open(filePath, 'b');

console.log(authToken );

// run k6
export default function () {
Expand All @@ -33,9 +46,10 @@ export default function () {
};
// make the http request
const res = http.post(url, data, {
// add headers
// Add Authorization header
// note: you can hardcode an auth token here or pass it as a paramter
headers: {
'Authorization': 'Bearer <user ID token>'
'Authorization': `Basic ${authToken}`
}
});
// tests
Expand Down
30 changes: 20 additions & 10 deletions k6/readObject.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import http from 'k6/http';
import { check, sleep } from 'k6';

// -------------------------------------------------------------------------------------------------
// Init
// -------------------------------------------------------------------------------------------------
// https://k6.io/docs/using-k6/environment-variables

const apiPath = `${__ENV.API_PATH}`
const objectId = `${__ENV.OBJECT_ID}`
const authToken = `${__ENV.AUTH_TOKEN}`

// k6 options (https://k6.io/docs/using-k6/k6-options/)
export const options = {
vus: 50,
Expand All @@ -16,18 +25,19 @@ export const options = {
},
};

// run k6
export default function () {
// request url
const url = `${apiPath}/object/${objectId}`;

const apiPath = 'http://localhost:3000/api/v1';
// request url
const url = `${apiPath}/object/<object id here>`;
// Add Authorization header
// note: you can hardcode an auth token here or pass it as a paramter
const params = {
headers: {
'Authorization': `Basic ${authToken}`
}
};

const params = {
headers: {
'Authorization': 'Bearer <token here>',
}
};
// run k6
export default function () {

// make the http request
const res = http.get(url, params);
Expand Down
32 changes: 19 additions & 13 deletions k6/searchObject.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import http from 'k6/http';
import { check, sleep } from 'k6';

// -------------------------------------------------------------------------------------------------
// Init
// -------------------------------------------------------------------------------------------------
// https://k6.io/docs/using-k6/environment-variables

const apiPath = `${__ENV.API_PATH}`
const bucketId = `${__ENV.BUCKET_ID}`
const authToken = `${__ENV.AUTH_TOKEN}`

// k6 options (https://k6.io/docs/using-k6/k6-options/)
export const options = {
// --- smoke testing (acceptable response times)
Expand All @@ -11,21 +20,18 @@ export const options = {
},
};

export default function () {

const apiPath = 'http://localhost:3000/api/v1';
const randomLetter = String.fromCharCode(65 + Math.floor(Math.random() * 26));
const bucketId = '<bucket ID (uuid)>';
const randomLetter = () => String.fromCharCode(65 + Math.floor(Math.random() * 26));
const url = `${apiPath}/object?bucketId=${bucketId}&latest=true&deleteMarker=false&tagset[${randomLetter}]=${randomLetter}`;

// request url
const url = `${apiPath}/object?bucketId=${bucketId}&latest=true&deleteMarker=false&tagset[${randomLetter}]=${randomLetter}`;
// Add Authorization header
// note: you can hardcode an auth token here or pass it as a paramter
const params = {
headers: {
'Authorization': `Basic ${authToken}`
}
};

// Add Authorization header
const params = {
headers: {
'Authorization': 'Bearer <user ID token here>'
}
};
export default function () {

// make the http request
const res = http.get(url, params);
Expand Down

0 comments on commit 0703347

Please sign in to comment.