diff --git a/k6/README.md b/k6/README.md new file mode 100644 index 00000000..2b4ea8b0 --- /dev/null +++ b/k6/README.md @@ -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` diff --git a/k6/createObject.js b/k6/createObject.js index ebfa7bbd..6750dba8 100644 --- a/k6/createObject.js +++ b/k6/createObject.js @@ -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: { @@ -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 = ''; -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 () { @@ -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 ' + 'Authorization': `Basic ${authToken}` } }); // tests diff --git a/k6/readObject.js b/k6/readObject.js index 5093f283..f8a2f476 100644 --- a/k6/readObject.js +++ b/k6/readObject.js @@ -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, @@ -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/`; +// 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 ', - } - }; +// run k6 +export default function () { // make the http request const res = http.get(url, params); diff --git a/k6/searchObject.js b/k6/searchObject.js index 51cd93c5..09c50d07 100644 --- a/k6/searchObject.js +++ b/k6/searchObject.js @@ -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) @@ -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 = ''; +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 ' - } - }; +export default function () { // make the http request const res = http.get(url, params);