Just try this format, I believe its work!
import http from 'k6/http';
// use example function to generate data
import k6example from 'https://raw.githubusercontent.com/grafana/k6/master/examples/thresholds_readme_example.js';
export const options = { vus: 5, iterations: 10 };
function calculateStandardDeviation(values) {
const mean = values.reduce((a, b) => a + b, 0) / values.length;
const squaredDiffs = values.map(value => Math.pow(value - mean, 2));
const variance = squaredDiffs.reduce((a, b) => a + b, 0) / values.length;
return Math.sqrt(variance);
}
export function handleSummary(data) {
console.log('Preparing the end-of-test summary...');
// Example: Calculate standard deviation of HTTP request durations
const requestDurations = data.metrics.http_req_duration.values; // metric to use
const durationsArray = Object.values(requestDurations); // extract as array
const stdDeviation = calculateStandardDeviation(durationsArray);
console.log(`Standard Deviation of request durations: ${stdDeviation.toFixed(2)}ms`);
// Send the results to some remote server or trigger a hook
const resp = http.post('https://httpbin.test.k6.io/anything', JSON.stringify(data));
if (resp.status != 200) {
console.error('Could not send summary, got status ' + resp.status);
}
}