Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Add special performance event handling for time to last byte (#2698)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonmade authored Dec 1, 2023
1 parent 7269f27 commit 790e16a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .changeset/pretty-timers-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@shopify/koa-performance': minor
'@shopify/performance': minor
---

Add special performance event handling for time to last byte
1 change: 1 addition & 0 deletions packages/koa-performance/src/enums.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum LifecycleMetric {
TimeToFirstByte = 'time_to_first_byte',
TimeToLastByte = 'time_to_last_byte',
TimeToFirstContentfulPaint = 'time_to_first_contentful_paint',
TimeToLargestContentfulPaint = 'time_to_largest_contentful_paint',
TimeToFirstPaint = 'time_to_first_paint',
Expand Down
1 change: 1 addition & 0 deletions packages/koa-performance/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ function getAnomalousNavigationDurationTag(

const EVENT_METRIC_MAPPING = {
[EventType.TimeToFirstByte]: LifecycleMetric.TimeToFirstByte,
[EventType.TimeToLastByte]: LifecycleMetric.TimeToLastByte,
[EventType.TimeToFirstContentfulPaint]:
LifecycleMetric.TimeToFirstContentfulPaint,
[EventType.TimeToLargestContentfulPaint]:
Expand Down
52 changes: 34 additions & 18 deletions packages/performance/src/performance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,43 @@ export class Performance {
this.supportsTimingEntries &&
(!this.supportsDetailedTime || !this.supportsNavigationEntries)
) {
withTiming(({domContentLoadedEventStart, loadEventStart}) => {
// window.performance.timing uses full timestamps, while
// the ones coming from observing navigation entries are
// time from performance.timeOrigin. We just normalize these
// ones to be relative to "start" since things listening for
// events expect them to be relative to when the navigation
// began.
this.lifecycleEvent({
type: EventType.DomContentLoaded,
start: domContentLoadedEventStart - this.timeOrigin,
duration: 0,
});
withTiming(
({responseEnd, domContentLoadedEventStart, loadEventStart}) => {
// window.performance.timing uses full timestamps, while
// the ones coming from observing navigation entries are
// time from performance.timeOrigin. We just normalize these
// ones to be relative to "start" since things listening for
// events expect them to be relative to when the navigation
// began.
this.lifecycleEvent({
type: EventType.TimeToLastByte,
start: responseEnd - this.timeOrigin,
duration: 0,
});

this.lifecycleEvent({
type: EventType.Load,
start: loadEventStart - this.timeOrigin,
duration: 0,
});
});
this.lifecycleEvent({
type: EventType.DomContentLoaded,
start: domContentLoadedEventStart - this.timeOrigin,
duration: 0,
});

this.lifecycleEvent({
type: EventType.Load,
start: loadEventStart - this.timeOrigin,
duration: 0,
});
},
);
} else {
withEntriesOfType('navigation', (entry) => {
if (entry.responseEnd > 0) {
this.lifecycleEvent({
type: EventType.TimeToLastByte,
start: entry.responseEnd,
duration: 0,
});
}

if (entry.domContentLoadedEventStart > 0) {
this.lifecycleEvent({
type: EventType.DomContentLoaded,
Expand Down
10 changes: 10 additions & 0 deletions packages/performance/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum EventType {
TimeToFirstByte = 'ttfb',
TimeToLastByte = 'ttlb',
TimeToFirstPaint = 'ttfp',
TimeToFirstContentfulPaint = 'ttfcp',
TimeToLargestContentfulPaint = 'ttlcp',
Expand Down Expand Up @@ -28,6 +29,13 @@ export interface TimeToFirstByteEvent extends BasicEvent {
};
}

export interface TimeToLastByteEvent extends BasicEvent {
type: EventType.TimeToLastByte;
metadata?: {
[key: string]: any;
};
}

export interface TimeToFirstPaintEvent extends BasicEvent {
type: EventType.TimeToFirstPaint;
metadata?: undefined;
Expand Down Expand Up @@ -89,6 +97,7 @@ export interface CustomEvent extends BasicEvent {

export type LifecycleEvent =
| TimeToFirstByteEvent
| TimeToLastByteEvent
| TimeToFirstPaintEvent
| TimeToFirstContentfulPaintEvent
| TimeToLargestContentfulPaintEvent
Expand All @@ -107,6 +116,7 @@ export type Event =

export interface EventMap {
[EventType.TimeToFirstByte]: TimeToFirstByteEvent;
[EventType.TimeToLastByte]: TimeToLastByteEvent;
[EventType.TimeToFirstPaint]: TimeToFirstPaintEvent;
[EventType.TimeToFirstContentfulPaint]: TimeToFirstContentfulPaintEvent;
[EventType.TimeToLargestContentfulPaint]: TimeToLargestContentfulPaintEvent;
Expand Down

0 comments on commit 790e16a

Please sign in to comment.