Skip to content

Instantly share code, notes, and snippets.

@fed135
Last active January 22, 2024 20:28
Show Gist options
  • Save fed135/fdac7af0a49099ad45cc2547a45bf762 to your computer and use it in GitHub Desktop.
Save fed135/fdac7af0a49099ad45cc2547a45bf762 to your computer and use it in GitHub Desktop.
Calculate best case scenario unidirectional latency and ping between AWS DCs
const toRadians = num => num * Math.PI / 180;
// Get the long, lat values for the desired dc
module.exports.datacenters = {
'ap-east-1': [22.29, 114.16 ],
'ap-northeast-1': [35.41, 139.42 ],
'ap-northeast-2': [37.56, 126.98 ],
'ap-northeast-3': [34.65, 136 ],
'ap-south-1': [19.08, 72.88 ],
'ap-southeast-1': [1.37, 103.8 ],
'ap-southeast-2': [-33.86, 151.2 ],
'ca-central-1': [45.5, -73.6 ],
'cn-north-1': [39.92, 116.44 ],
'cn-northwest-1': [38.49, 106.25 ],
'eu-central-1': [50, 8 ],
'eu-north-1': [59.33, 18.06 ],
'eu-west-1': [53, -8 ],
'eu-west-2': [51, -0.1 ],
'eu-west-3': [48.86, 2.35 ],
'me-south-1': [25.93, 50.64 ],
'sa-east-1': [-23.34, -46.38 ],
'us-east-1': [38.13, -78.45 ],
'us-east-2': [39.96, -83 ],
'us-west-1': [37.35, -121.96],
'us-west-2': [46.15, -123.88],
};
/**
* Returns the distance in KM between coords/ dc names
*/
module.exports.distance = (dc1, dc2) => {
const [lat1, lon1] = Array.isArray(dc1) ? dc1 : module.exports.datacenters[dc1];
const [lat2, lon2] = Array.isArray(dc2) ? dc2 : module.exports.datacenters[dc2];
// Calculate distance between points
const R = 6371e3; // metres
const φ1 = toRadians(lat1);
const φ2 = toRadians(lat2);
const Δφ = toRadians(lat2-lat1);
const Δλ = toRadians(lon2-lon1);
const a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return (R * c / 1000);
}
/**
* Returns minimal unidirectional latency in MS between coords or dc names (speed of light)
*/
module.exports.minLatency = (dc1, dc2) => {
return module.exports.distance(dc1, dc2) * 0.00333564095198152;
}
/**
* Return estimated optimal ping value between coords or dc names in MS
*/
module.exports.minPing = (dc1, dc2) => {
// Calculate bi-directional time + OS + network hops
// Hops ratio assumes AVG traffic distance traveled for a packet is 850Km for points geographically separated by 500Km
const hopsRatio = 850 / 500;
const uniLatency = module.exports.minLatency(dc1, dc2);
return 1 + (uniLatency * hopsRatio) * 2;
}
@fed135
Copy link
Author

fed135 commented Sep 24, 2019

The 850/500 ratio is a rough estimate. I'd have to dig into https://pdfs.semanticscholar.org/cd7c/e2525ef56f36949ba75761587e211b63785a.pdf to get the actual data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment