Skip to content

Instantly share code, notes, and snippets.

@dgzlopes
Created October 30, 2023 16:03
Show Gist options
  • Save dgzlopes/e87d65a4b6497d8ad346e9e721ed6569 to your computer and use it in GitHub Desktop.
Save dgzlopes/e87d65a4b6497d8ad346e9e721ed6569 to your computer and use it in GitHub Desktop.
import { sleep } from "k6";
import { randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';
import http from "k6/http";
import { setTimeout, clearTimeout } from "k6/experimental/timers";
export const options = {
stages: [
{ duration: '5s', target: 5 },
{ duration: '10s', target: 5 },
{ duration: '5s', target: 0 },
],
};
export async function write() {
const url = `https://httpbin.test.k6.io/delay/${randomIntBetween(1, 15)}`;
const req = http.asyncRequest('GET', url);
// This runs if the request takes 10 or more seconds to complete
const timeout = setTimeout(function () {
console.log(`poll request (${__VU}/${__ITER}): ${url}`);
}, 10000);
await Promise.race([req])
clearTimeout(timeout);
sleep(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment