Skip to content

Google Cloud Region Latency in Google Apps Script

Published on Markdown

Ever wonder what the ping time is to Google Cloud regions from Google Apps Script? Here are the results for my Apps Script project with the default project id. Timezone is set to America/Denver, but I don’t think that matters!

Here is a short and sweet snippet for measuring latency to Google Cloud regions in Google Apps Script.

JavaScript
function ping() {
  const endpoints = JSON.parse(
    UrlFetchApp.fetch("https://gcping.com/api/endpoints").getContentText(),
  );
  const results = Object.entries(endpoints).map(([k, v]) => ({
    stats: latency(v.URL),
    endpoint: k,
  }));

  console.log(
    JSON.stringify(
      results.sort((a, b) => a.stats.average - b.stats.average),
      null,
      2,
    ),
  );
}

function latency(url, iterations = 5) {
  console.log(url);
  const executionTimes = [];

  for (let i = 0; i < iterations; i++) {
    const startTime = performance.now();
    UrlFetchApp.fetch(url);
    const endTime = performance.now();

    executionTimes.push(endTime - startTime);
  }

  // Calculate statistics
  const min = Math.min(...executionTimes);
  const max = Math.max(...executionTimes);
  const totalTime = executionTimes.reduce((sum, time) => sum + time, 0);
  const average = totalTime / iterations;

  return {
    min: min,
    max: max,
    mean: average,
    median: executionTimes.sort()[Math.floor(executionTimes.length / 2)],
    // times: executionTimes,
  };
}

globalThis.performance = globalThis.performance || {
  offset: Date.now(),
  now: function now() {
    return Date.now() - this.offset;
  },
};

This is a followup on the work done by GCPing and Ivan Kutil in 2019.

Disclaimer: I am a member of the Google Workspace Developer Relations team. The opinions expressed here are my own and do not necessarily represent those of Google.

© 2024 by Justin Poehnelt is licensed under CC BY-SA 4.0