Skip to content

Instantly share code, notes, and snippets.

@harisfi
Created September 3, 2021 15:55
Show Gist options
  • Save harisfi/a5f17bf0f7bfe9083d143f1b04fa81d8 to your computer and use it in GitHub Desktop.
Save harisfi/a5f17bf0f7bfe9083d143f1b04fa81d8 to your computer and use it in GitHub Desktop.
Split large traffic into pieces, so there will no more server down :)
function splitTraffic() {
const origServerList = [
'server1',
'server2',
'server3',
'server4',
];
const serverCount = origServerList.length;
const firstServer = origServerList[Math.floor(Math.random() * serverCount)];
let modServerList = origServerList.filter(serv => serv !== firstServer);
modServerList.unshift(firstServer);
return modServerList;
}
/* CODE BELOW FOR TEST USE */
let [totalTraffic, serv1, serv2, serv3, serv4, x] = [1400, 0, 0, 0, 0, 0];
for (let i = 0; i < totalTraffic; i++) {
x = splitTraffic()[0];
switch (x) {
case 'server1':
serv1++;
break;
case 'server2':
serv2++;
break;
case 'server3':
serv3++
break;
case 'server4':
serv4++;
break;
default:
break;
}
}
serv1 = serv1 / totalTraffic * 100;
serv2 = serv2 / totalTraffic * 100;
serv3 = serv3 / totalTraffic * 100;
serv4 = serv4 / totalTraffic * 100;
let totalHit = serv1 + serv2 + serv3 + serv4;
console.log(
`server1 hit: ${serv1}%`,
`server2 hit: ${serv2}%`,
`server3 hit: ${serv3}%`,
`server4 hit: ${serv4}%`,
`total hit: ${totalHit}%`
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment