Skip to content

Instantly share code, notes, and snippets.

@pobalopalous
Last active April 10, 2024 21:25
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 pobalopalous/dcf572abfce1b18f7f49168918231765 to your computer and use it in GitHub Desktop.
Save pobalopalous/dcf572abfce1b18f7f49168918231765 to your computer and use it in GitHub Desktop.
ORDS k6 artefacts
import http from 'k6/http';
import encoding from 'k6/encoding';
/**
* Authenticate using OAuth Client Credentials against ORDS
* @function
* @param {string} scheme - http or https
* @param {string} server - hostname and port of the server
* @param {string} clientId - Generated by ORDS
* @param {string} clientSecret - Generated by ORDS
*/
export function authenticateUsingORDSClientCredentials(scheme, server, alias, clientId, clientSecret) {
const url = `${scheme}://${server}/ords/${alias}/oauth/token`;
const requestBody = { grant_type: 'client_credentials' };
let response;
const encodedCredentials = encoding.b64encode(`${clientId}:${clientSecret}`);
const params = {
auth: 'basic',
headers: {
Authorization: `Basic ${encodedCredentials}`,
},
};
response = http.post(url, requestBody, params);
return response.json();
};
// k6 script for testing TEAM table endpoint.
import { check } from 'k6';
import http from 'k6/http';
import encoding from 'k6/encoding';
import { authenticateUsingORDSClientCredentials } from 'https://gist.githubusercontent.com/pobalopalous/dcf572abfce1b18f7f49168918231765/raw/e40f6dc53d7602a414a294eb91f823e0c7f11340/ords_auth.js';
export const options = {
scenarios: {
per_vu_scenario: {
executor: "per-vu-iterations",
vus: 10,
iterations: 1000
}
}
};
const ORDS_CLIENT_ID = 'ymDCZv5ePvte2kN8seOygw..';
const ORDS_CLIENT_SECRET = 'alU5L4Xp5GXOoT-qo9v6UQ..';
const ORDS_SCHEME = 'http';
const ORDS_SERVER = 'localhost:8080';
const ORDS_SCHEMA_ALIAS = 'hr';
export function setup() {
const clientAuthResp = authenticateUsingORDSClientCredentials(ORDS_SCHEME, ORDS_SERVER, ORDS_SCHEMA_ALIAS, ORDS_CLIENT_ID, ORDS_CLIENT_SECRET);
console.log(JSON.stringify(clientAuthResp));
return clientAuthResp;
};
// The function that defines VU logic.
//
// See https://grafana.com/docs/k6/latest/examples/get-started-with-k6/ to learn more
// about authoring k6 scripts.
//
export default function(data) {
// The setup data contains the access_token to use in every request
const params = {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${data.access_token}`
}
};
// Returns a random integer from 301 to 309:
var team_id = Math.floor(Math.random() * 9) + 301;
var res = http.get(ORDS_SCHEME + '://' + ORDS_SERVER + '/ords/' + ORDS_SCHEMA_ALIAS + '/team/' + team_id, params);
// Check that the response is valid
check(res, {
'is status 200': (r) => r.status === 200,
'is application/json': (r) => r.headers['Content-Type'] === 'application/json',
'is requested team resource': (r) => r.json().team_id === team_id
});
}
// k6 script for testing TEAM_DV Duality View endpoint.
import { check } from 'k6';
import http from 'k6/http';
import encoding from 'k6/encoding';
import { authenticateUsingORDSClientCredentials } from 'https://gist.githubusercontent.com/pobalopalous/dcf572abfce1b18f7f49168918231765/raw/e40f6dc53d7602a414a294eb91f823e0c7f11340/ords_auth.js';
export const options = {
scenarios: {
per_vu_scenario: {
executor: "per-vu-iterations",
vus: 10,
iterations: 1000
}
}
};
const ORDS_CLIENT_ID = 'ymDCZv5ePvte2kN8seOygw..';
const ORDS_CLIENT_SECRET = 'alU5L4Xp5GXOoT-qo9v6UQ..';
const ORDS_SCHEME = 'http';
const ORDS_SERVER = 'localhost:8080';
const ORDS_SCHEMA_ALIAS = 'hr';
export function setup() {
const clientAuthResp = authenticateUsingORDSClientCredentials(ORDS_SCHEME, ORDS_SERVER, ORDS_SCHEMA_ALIAS, ORDS_CLIENT_ID, ORDS_CLIENT_SECRET);
console.log(JSON.stringify(clientAuthResp));
return clientAuthResp;
};
// The function that defines VU logic.
//
// See https://grafana.com/docs/k6/latest/examples/get-started-with-k6/ to learn more
// about authoring k6 scripts.
//
export default function(data) {
// The setup data contains the access_token to use in every request
const params = {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${data.access_token}`
}
};
// Returns a random integer from 301 to 309:
var team_id = Math.floor(Math.random() * 9) + 301;
var res = http.get(ORDS_SCHEME + '://' + ORDS_SERVER + '/ords/' + ORDS_SCHEMA_ALIAS + '/team_dv/' + team_id, params);
// Check that the response is valid
check(res, {
'is status 200': (r) => r.status === 200,
'is application/json': (r) => r.headers['Content-Type'] === 'application/json',
'is requested team resource': (r) => r.json().teamId === team_id
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment