Skip to content

Instantly share code, notes, and snippets.

@srperf
Created May 30, 2022 13:12
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 srperf/8cb344b543511922806425cf06480ef7 to your computer and use it in GitHub Desktop.
Save srperf/8cb344b543511922806425cf06480ef7 to your computer and use it in GitHub Desktop.
import http from 'k6/http';
import exec from 'k6/execution';
import { sleep } from 'k6';
export const options = {
stages: [
{ duration: '40s', target: 20 },
{ duration: '2m', target: 20 },
{ duration: '5s', target: 0 },
],
};
export default function () {
const rendezPeriod = 30000;
/*Step1*/http.get('https://test-api.k6.io/public/crocodiles/1/');
sleep(Math.random() * 5);
/*Step2*/http.get('https://test-api.k6.io/public/crocodiles/2/');
sleep(Math.random() * 5);
/*Step3*/http.get('https://test-api.k6.io/public/crocodiles/3/');
sleep(Math.random() * 5);
/*Step4*/http.get('https://test-api.k6.io/public/crocodiles/4/');
rendez(rendezPeriod);
/*Step5*/http.get('https://test-api.k6.io/public/crocodiles/');
sleep(Math.random() * 5);
/*Step6*/http.get('https://test-api.k6.io/public/crocodiles/5/');
sleep(Math.random() * 5);
/*Step7*/http.get('https://test-api.k6.io/public/crocodiles/6/');
sleep(Math.random() * 5);
/*Step8*/http.get('https://test-api.k6.io/public/crocodiles/1/');
sleep(Math.random() * 5);
}
// comment: quick implementation of rendezvous function in k6
// stopping at periods, no user count ... yet
function rendez(rendezPeriod) {
//comment: soFar is the time since the test started
let soFar = new Date().getTime() - exec.scenario.startTime;
//comment: calculate how much to wait until next rendez
const waitTime = (rendezPeriod-(soFar%rendezPeriod))/1000;
sleep(waitTime);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment