The Apps Script Lab

Log Function Performance in Google Apps Script 📊

In Google Apps Script, execution time matters. With quotas on simultaneous runs and total runtime, long or inconsistent function execution can cause failures or delays. This is especially important when using services like LockService, where you need to define realistic wait times.

This article shows how to track execution time and errors in your functions—helping you stay within limits and optimize performance as your scripts grow.

1. Build a Simple Performance Tracker

 

The trackPerformance function takes three parameters:

It starts by recording the current time, then attempts to execute the given function with the provided arguments. 

If the function runs successfully, it stores the result; if it fails, the error is caught and stored for later logging.

If an error was captured, it gets re-thrown so it can still be handled or reported as needed. 

trackPerformance Code Block
JSCode.gs

/**
 * Logs execution metrics for a given function.
 * @param {Function} func - The function to track.
 * @param {string} functionName - The name of the function being tracked.
 * @param {Array} args - Arguments to pass to the function.
 * @returns {any} - The result of the executed function.
 */
function trackPerformance(func, functionName, args = []) {
  const startTime = new Date();
  let result;
  let errorMessage = '';

  try {
    result = func.apply(null, args);
  } catch (error) {
    errorMessage = error.toString();
  }

  const endTime = new Date();
  const executionTime = (endTime - startTime) / 1000; // Convert to seconds

  logMetrics(functionName, startTime, executionTime, errorMessage);

  if (errorMessage) {
    throw new Error(errorMessage); // Re-throw the error after logging
  }

  return result;
}
    
2. Choose Where to Store Your Metrics

To make performance tracking actionable, you’ll want to log each execution’s data somewhere persistent. A simple and effective way is to store the metrics in a Google Sheet.

 

Here’s an example of how you can do that with the logMetrics function:

Log Metrics Code Block
JSCode.gs

/**
 * Logs metrics to a Google Sheet for performance tracking.
 * @param {string} functionName - The name of the function executed.
 * @param {Date} startTime - The start time of the execution.
 * @param {number} executionTime - Time taken for execution in seconds.
 * @param {string} errorMessage - Error message if any occurred.
 */
function logMetrics(functionName, startTime, executionTime, errorMessage) {
  const sheetName = 'Performance_Log';
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = ss.getSheetByName(sheetName);

  if (!sheet) {
    sheet = ss.insertSheet(sheetName);
    sheet.appendRow(['Timestamp', 'Function Name', 'Execution Time (s)', 'Error']);
  }

  sheet.appendRow([startTime, functionName, executionTime, errorMessage || 'None']);
}
    
3. Apply Tracking to Your Existing Functions

Once you’ve set up your tracker and decided how to log the results, the final step is to wrap any function you want to monitor using trackPerformance. 

This lets you measure execution time and automatically log any errors without changing the function’s core logic.

Code Block with Copy and Highlight
JSCode.gs

/**
 * Entry point for the web app. This will track execution metrics.
 * @param {Object} e - The event parameter containing request information.
 * @returns {HtmlOutput} - The HTML output for the web app.
 */
function doGet(e) {
  return trackPerformance(handleGetRequest, 'doGet', [e]);
}
    
Scroll to Top