Last active
June 22, 2022 14:22
-
-
Save srperf/b64472869f9532700c83b7d4513a8f4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Debugging | |
//////////////////////////////////////// | |
// Example of what I am trying to do, for rendezvous | |
// without promises for Pepe to figure out | |
//////////////////////////////////////// | |
import redis from 'k6/x/redis'; | |
import http from 'k6/http'; | |
import { sleep } from 'k6'; | |
// Running 15 users for POC | |
export const options = { | |
stages: [ | |
{ duration: '15s', target: 15 }, | |
{ duration: '30s', target: 15 }, | |
{ duration: '5s', target: 0 }, | |
], | |
}; | |
const client = new redis.Client({ | |
addrs: new Array("localhost:6379"), | |
}); | |
// Initialize the counter, although with the new function it may not be needed | |
client.set('rendez',0); | |
// Rendezvous function, will be called by each VUser | |
// and receives the number of users to wait for | |
// at that spot | |
export function rendezByVUs(rendezAmount) { | |
//As users arrive, they will increase the counter in Redis | |
let thisVUNum = 0;//This is the number that the user will receive as it reaches the rendezvous | |
let userCount = 0;//To store the redis usercount | |
thisVUNum = client.incr('rendez'); //Increase the counter in redis and get the new value of the user count | |
//The logic of this function will be to divide the rendevous number by the number the user received | |
//and keep the difference. With this we do not need to restart the counter and we use that difference to count | |
let rendezDiff = newNum%rendezAmount;//Get the remainder of the division to know which this user is in this block | |
//let rendezMult = Math.floor(newNum/rendezAmount) | |
let rendezToAchieve = thisVUNum-rendezDiff+rendezAmount;//Calculate the total number of users to do the rendezvous | |
//Then we will wait and constantly check for the redis count increased by other VUs until it reaches or passes the rendevous number | |
do { | |
userCount=client.get('rendez'); | |
} while (userCount<=rendezToAchieve); | |
} | |
export default function () { | |
/*Step1*/http.get('https://test-api.k6.io/public/crocodiles/1/'); | |
sleep(Math.random() * 2); | |
/*Step2*/http.get('https://test-api.k6.io/public/crocodiles/2/'); | |
sleep(Math.random() * 2); | |
/*Step3*/http.get('https://test-api.k6.io/public/crocodiles/3/'); | |
sleep(Math.random() * 2); | |
/*Step4*/http.get('https://test-api.k6.io/public/crocodiles/4/'); | |
rendezByVUs(parseInt(10)); | |
/*Step1*/http.get('https://test-api.k6.io/public/crocodiles/1/'); | |
sleep(Math.random() * 2); | |
/*Step2*/http.get('https://test-api.k6.io/public/crocodiles/2/'); | |
sleep(Math.random() * 2); | |
/*Step3*/http.get('https://test-api.k6.io/public/crocodiles/3/'); | |
sleep(Math.random() * 2); | |
/*Step4*/http.get('https://test-api.k6.io/public/crocodiles/4/'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment