Skip to content

Instantly share code, notes, and snippets.

@deeTEEcee
Created August 23, 2024 18:25
Show Gist options
  • Save deeTEEcee/4988394299d475bf62a96fce10dcfd32 to your computer and use it in GitHub Desktop.
Save deeTEEcee/4988394299d475bf62a96fce10dcfd32 to your computer and use it in GitHub Desktop.
K6 example
# Sandbox k6 example for basic testing when I was migrating an on-prem client application to AWS
# `k6 run benchmark.js`
import http from 'k6/http';
import { check, sleep } from 'k6';
export let options = {
vus: 1,
// duration: "60s"
iterations: 5,
// stages: [
// { duration: '1s', target: 2},
// { duration: '5s', target: 5}
// ]
};
// const baseUrl = "https://mustang-databrowser-cmm-165.dev.earthscope.org"
const baseUrl = "https://mustang-databrowser-cmm-165-disable-cache.dev.earthscope.org"
const starttime = "2024-07-22"
const endtime = "2024-08-21"
function requestMetricTimeseries() {
return `${baseUrl}/main.R?language=en&plotWidth=600&starttime=${starttime}&endtime=${endtime}&plotType=metricTimeseries&metric=sample_rms&timeseriesChannelSet=on&virtualNetwork=No+virtual+network&network=IU&station=ANMO&location=00&channel=BH?&archive=fdsnws`
}
function requestMultipleMetricTimeseries() {
return `${baseUrl}/main.R?language=en&plotWidth=600&starttime=${starttime}&endtime=${endtime}&plotType=stackedMetricTimeseries&metric=basic_stats&virtualNetwork=No+virtual+network&network=IU&station=ANMO&location=00&channel=BHZ&archive=fdsnws`
}
function requestNetworkBoxplots() {
return `${baseUrl}/main.R?language=en&plotWidth=600&starttime=${starttime}&endtime=${endtime}&plotType=networkBoxplot&metric=sample_rms&boxplotShowOutliers=on&virtualNetwork=No+virtual+network&network=IU&channel=BHZ&archive=fdsnws`
}
function requestStationBoxplots() {
return `${baseUrl}/main.R?language=en&plotWidth=600&starttime=${starttime}&endtime=${endtime}&plotType=stationBoxplot&metric=sample_rms&boxplotShowOutliers=on&virtualNetwork=No+virtual+network&network=IU&station=ANMO&archive=fdsnws`
}
function requestSeismicTrace() {
return `${baseUrl}/main.R?language=en&plotWidth=600&starttime=${starttime}&endtime=${endtime}&plotType=trace&virtualNetwork=No+virtual+network&network=IU&station=ANMO&location=00&channel=BHZ&archive=fdsnws`
}
function requestPdfPlot() {
return `${baseUrl}/main.R?language=en&plotWidth=600&starttime=${starttime}&endtime=${endtime}&plotType=pdf&virtualNetwork=No+virtual+network&network=IU&station=ANMO&location=00&channel=BHZ&archive=fdsnws`
}
function requestPdfNoiseModeTimeseriesPlot() {
return `${baseUrl}/main.R?language=en&plotWidth=600&starttime=${starttime}&endtime=${endtime}&plotType=noise-mode-timeseries&virtualNetwork=No+virtual+network&network=IU&station=ANMO&location=00&channel=BHZ&archive=fdsnws`
}
function requestPdfNoiseModeSpectrogramPlot() {
return `${baseUrl}/main.R?language=en&plotWidth=600&starttime=${starttime}&endtime=${endtime}&plotType=spectrogram&colorPalette=rainbow&virtualNetwork=No+virtual+network&network=IU&station=ANMO&location=00&channel=BHZ&archive=fdsnws`
}
function batch(requests) {
const rawRequests = requests.map((x) => x.slice(0, x.length - 1));
const rawResponses = http.batch(rawRequests);
const responses = [];
for(var i=0;i<rawResponses.length;i++) {
let response = [rawResponses[i], requests[i].slice(-1)];
responses.push(response);
}
return responses;
}
// debugging
// export default function () {
// const responses = batch([
// ['GET', "http://test.k6.io", "name"],
// ]);
// for (const resp of responses) {
// console.log(resp[1])
// check(resp[0], {
// [resp[1]]: r => r.status == 200
// })
// }
// // let res = http.get('http://test.k6.io');
// // check(res, {'k6test': r => r.status == 200}, { name: "k6test" });
// // res = http.get('https://mock.httpstatus.io/200');
// // check(res, {'mock': r => r.status == 200}, { name: "mocktest" });
// }
// main func
export default function () {
const responses = batch([
// ['GET', requestMetricTimeseries(), "metric timeseries"],
// ['GET', requestMultipleMetricTimeseries(), "multi-metric timeseries"],
// ['GET', requestNetworkBoxplots(), "network boxplots"],
// ['GET', requestStationBoxplots(), "station boxplots"],
['GET', requestSeismicTrace(), "seismic trace"],
// ['GET', requestPdfPlot(), "pdf plot"],
// ['GET', requestPdfNoiseModeTimeseriesPlot(), "pdf noise mode timeseries plot"],
// ['GET', requestPdfNoiseModeSpectrogramPlot(), "pdf noise mode spectrogram plot"]
]);
for (const resp of responses) {
const url = resp.url
check(resp[0], {
[resp[1]]: r => r.status == 200
})
}
// let res = http.get('http://test.k6.io');
// check(res, {'k6test': r => r.status == 200}, { name: "k6test" });
// res = http.get('https://mock.httpstatus.io/200');
// check(res, {'mock': r => r.status == 200}, { name: "mocktest" });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment