
   *   Nearest number of bytes to round to
   * @returns {number}
   *   Number of bytes rounded to the `nearest` granularity
   *
   * @example fuzzByteSize(1500, 1000) === 2000
   * @example fuzzByteSize(1001, 1000) === 1000
   * @example fuzzByteSize(1024, 10) === 1020
   * @example fuzzByteSize(512, 1000) === 1000
   * @example fuzzByteSize(256, 1000) === 1000
   */
  fuzzByteSize(bytes, nearest) {
    const fuzzed = Math.round(bytes / nearest) * nearest;
    return Math.max(fuzzed, nearest);
  },

  /**
   * Helper wrapper for timing an async operation with Glean. This style of
   * helper is available in some Glean SDKs, but not JavaScript yet.
   *
   * @see https://mozilla.github.io/glean/book/reference/metrics/timing_distribution.html#measure
   *
   * @template T
   * @param {GleanTimingDistribution} timer
   *   Glean TimingDistribution object
   * @param {Promise<T>} operation
   *   Async operation to time
   * @returns {Promise<T>}
   *   Result of `operation`
   */
  async measure(timer, operation) {
    const timerId = timer.start();

    try {
      const result = await operation;
      timer.stopAndAccumulate(timerId);
      return result;
    } catch (err) {
      timer.cancel(timerId);
      throw err;
    }
  },
};
PK