function calculateSelfTime()

in packages/rum-core/src/performance-monitoring/breakdown.js [47:92]


function calculateSelfTime(transaction) {
  const { spans, _start, _end } = transaction
  /**
   * When there are no spans transaction duration accounts for
   * the overall self time
   */
  if (spans.length === 0) {
    return transaction.duration()
  }
  /**
   * The below selfTime calculation logic assumes the spans are sorted
   * based on the start time
   */
  spans.sort((span1, span2) => span1._start - span2._start)

  let span = spans[0]
  let spanEnd = span._end
  let spanStart = span._start

  let lastContinuousEnd = spanEnd
  let selfTime = spanStart - _start

  for (let i = 1; i < spans.length; i++) {
    span = spans[i]
    spanStart = span._start
    spanEnd = span._end

    /**
     * Add the gaps between the spans to the self time
     */
    if (spanStart > lastContinuousEnd) {
      selfTime += spanStart - lastContinuousEnd
      lastContinuousEnd = spanEnd
    } else if (spanEnd > lastContinuousEnd) {
      lastContinuousEnd = spanEnd
    }
  }
  /**
   * Add the remaining gaps in transaction duration to
   * the self time of the transaction
   */
  if (lastContinuousEnd < _end) {
    selfTime += _end - lastContinuousEnd
  }
  return selfTime
}