Skip to content

Instantly share code, notes, and snippets.

@t-davies
Created August 30, 2020 14:57
Show Gist options
  • Save t-davies/aea3a7e4149e830a0a7c92a3acfd9104 to your computer and use it in GitHub Desktop.
Save t-davies/aea3a7e4149e830a0a7c92a3acfd9104 to your computer and use it in GitHub Desktop.
Suggests optimal EC2 instance mix for single-core apps, like CS:GO servers
const hosts = [
{
name: "z1d.12xlarge",
capacity: 48,
cost: 5.273,
},
{
name: "z1d.6xlarge",
capacity: 24,
cost: 2.636,
},
{
name: "z1d.3xlarge",
capacity: 12,
cost: 1.318,
},
{
name: "z1d.2xlarge",
capacity: 8,
cost: 0.879,
},
{
name: "z1d.xlarge",
capacity: 4,
cost: 0.439,
},
{
name: "z1d.large",
capacity: 2,
cost: 0.22,
},
].sort((a, b) => a.capacity - b.capacity);
function suggestHostSizes(instanceCount) {
const calculateUtilisation = (definition, instanceCount) => {
return instanceCount / definition.capacity;
};
const findBestUtilisation = (instanceCount) => {
return hosts.reduce(
(currentBest, definition) => {
const utilisation = calculateUtilisation(definition, instanceCount);
console.log(`current best: ${currentBest.utilisation}`);
console.log(`${definition.name}, utilisation would be ${utilisation}`);
if (Math.abs(utilisation - 1) < Math.abs(currentBest.utilisation - 1)) {
return { definition, utilisation };
}
return currentBest;
},
{ utilisation: -Infinity, definition: undefined }
);
};
const suggestions = [];
let remainingToAllocate = instanceCount;
while (remainingToAllocate > 0) {
console.log("------");
console.log(`--- got ${remainingToAllocate} to allocate ---`);
console.log("------");
const suggestion = findBestUtilisation(remainingToAllocate);
if (!suggestion) {
console.log(`unable to find suitable host`);
process.exit(-1);
}
remainingToAllocate -= suggestion.definition.capacity;
suggestions.push(suggestion);
}
return suggestions;
}
const [_, __, instances = 1] = process.argv;
console.log(`provisioning ${instances} instances`);
const suggestions = suggestHostSizes(instances);
console.log(" ");
console.log(
`suggested hosts: ${suggestions
.map(({ definition: d }) => `${d.name} (${d.capacity})`)
.join(", ")}`
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment