-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
chore: collect replay network metrics #6881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import * as playwright from 'playwright'; | ||
|
||
export class NetworkEvent { | ||
constructor( | ||
public url: string | undefined, | ||
public requestSize: number | undefined, | ||
public responseSize: number | undefined, | ||
public requestTimeNs: bigint | undefined, | ||
public responseTimeNs: bigint | undefined) { } | ||
|
||
public static fromJSON(data: Partial<NetworkEvent>): NetworkEvent { | ||
return new NetworkEvent( | ||
data.url as string, | ||
data.requestSize as number, | ||
data.responseSize as number, | ||
data.requestTimeNs == undefined ? undefined : BigInt(data.requestTimeNs), | ||
data.responseTimeNs == undefined ? undefined : BigInt(data.responseTimeNs), | ||
); | ||
} | ||
} | ||
|
||
export type NetworkUsageSerialized = Partial<{ events: Array<NetworkEvent> }>; | ||
|
||
export class NetworkUsage { | ||
public constructor(public events: Array<NetworkEvent>) { } | ||
|
||
public static fromJSON(data: NetworkUsageSerialized): NetworkUsage { | ||
return new NetworkUsage(data.events?.map(NetworkEvent.fromJSON) || []); | ||
} | ||
} | ||
|
||
export class NetworkUsageCollector { | ||
private _events = new Array<NetworkEvent>(); | ||
|
||
public static async create(page: playwright.Page): Promise<NetworkUsageCollector> { | ||
const self = new NetworkUsageCollector(); | ||
await page.route(_ => true, self._captureRequest.bind(self)); | ||
return self; | ||
} | ||
|
||
public getData(): NetworkUsage { | ||
return new NetworkUsage(this._events); | ||
} | ||
|
||
private async _captureRequest( | ||
route: playwright.Route, request: playwright.Request): Promise<void> { | ||
const url = request.url(); | ||
try { | ||
const event = new NetworkEvent( | ||
url, | ||
request.postDataBuffer()?.length, | ||
undefined, | ||
process.hrtime.bigint(), | ||
undefined | ||
); | ||
this._events.push(event); | ||
// Note: playwright would error out on file:/// requests. They are used to access local test app resources. | ||
if (url.startsWith('file:///')) { | ||
route.continue(); | ||
} else { | ||
const response = await route.fetch(); | ||
const body = await response.body(); | ||
route.fulfill({ response, body }); | ||
event.responseTimeNs = process.hrtime.bigint(); | ||
event.responseSize = body.length; | ||
} | ||
} catch (e) { | ||
console.log(`Error when capturing request: ${request.method()} ${url} - ${e}`) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.