Skip to content

Instantly share code, notes, and snippets.

@zaguiini
Created September 16, 2022 18:23
Show Gist options
  • Save zaguiini/7f9a05e24711827ba06585528dcbb9c0 to your computer and use it in GitHub Desktop.
Save zaguiini/7f9a05e24711827ba06585528dcbb9c0 to your computer and use it in GitHub Desktop.
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function performanceURL(url, sampleSize, sleepMs = 1000) {
const performanceList = [];
for (let i = 0; i < sampleSize; i++) {
const t0 = performance.now();
await window.wpcom.request({ method: 'GET', path: url });
const t1 = performance.now();
performanceList.push(t1 - t0);
console.log(`Request ${i}: ${t1 - t0}ms`);
await sleep(sleepMs);
}
return performanceList;
}
function mean(arr) {
return arr.reduce((a, b) => a + b) / arr.length;
}
async function calculatePerformance(destinations, sampleSize = 10) {
const results = {};
for (let [key, url] of Object.entries(destinations)) {
console.log(`Calculating performance for ${key}`);
const performanceList = await performanceURL(url, sampleSize);
results[key] = {
performanceList,
mean: mean(performanceList),
};
}
window.results = results;
console.log(results);
return results;
}
calculatePerformance({
smd: '/me/sites?http_envelope=1&site_visibility=all&include_domain_only=true&site_activity=active&fields=ID%2CURL%2Cis_coming_soon%2Cis_private%2Cvisible%2Claunch_status%2Cicon%2Cname%2Coptions%2Cplan%2Cjetpack%2Cis_wpcom_atomic%2Cuser_interactions&options=is_wpforteams_site%2Cupdated_at%2Cis_redirect%2Cunmapped_url%2Cadmin_url',
smdWithSiteSwitcher: '/me/sites?http_envelope=1&site_visibility=all&include_domain_only=true&site_activity=active&fields=ID%2CURL%2Cis_coming_soon%2Cis_private%2Cvisible%2Claunch_status%2Cicon%2Cname%2Coptions%2Cplan%2Cjetpack%2Cis_wpcom_atomic%2Cuser_interactions%2Csingle_user_site%2Cdomain%2Cprimary%2Cupdates&options=is_wpforteams_site%2Cupdated_at%2Cis_redirect%2Cunmapped_url%2Cadmin_url%2Cwoocommerce_is_active%2Cis_difm_lite_in_progress%2Cis_domain_only%2Cp2_hub_blog_id%2Cis_multi_network%2Cfile_mod_disabled%2Cjetpack_connection_active_plugins%2Cis_automated_transfer'
}, 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment