Skip to content

Commit

Permalink
add more pc events metrics and upgrade puppeteer
Browse files Browse the repository at this point in the history
  • Loading branch information
vpalmisano committed Sep 12, 2024
1 parent 927d923 commit 540d99e
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 109 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
},
"license": "AGPL-3.0-or-later",
"dependencies": {
"@puppeteer/browsers": "^2.2.3",
"@puppeteer/browsers": "^2.4.0",
"axios": "^1.7.2",
"browserify": "^17.0.0",
"camel-case": "^4.1.2",
Expand All @@ -72,8 +72,8 @@
"pidtree": "^0.6.0",
"pidusage": "^3.0.2",
"prom-client": "^15.1.3",
"puppeteer": "^22.12.1",
"puppeteer-core": "^22.12.1",
"puppeteer": "^23.3.0",
"puppeteer-core": "^23.3.0",
"puppeteer-intercept-and-modify-requests": "^1.3.1",
"sdp-transform": "^2.14.2",
"sprintf-js": "^1.1.3",
Expand Down
4 changes: 3 additions & 1 deletion scripts/peer-connection-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,11 @@ window.collectPeerConnectionStats = async (raw = false, verbose = false) => {

return {
stats,
activePeerConnections,
signalingHost,
participantName: window.getParticipantName(),
activePeerConnections,
peerConnectionsCreated: webrtcperf.peerConnectionNextId,
peerConnectionsConnected: webrtcperf.peerConnectionsConnected,
peerConnectionsDisconnected: webrtcperf.peerConnectionsDisconnected,
peerConnectionsFailed: webrtcperf.peerConnectionsFailed,
peerConnectionsClosed: webrtcperf.peerConnectionsClosed,
Expand Down
17 changes: 13 additions & 4 deletions scripts/peer-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const timestampInsertableStreams = !!window.PARAMS?.timestampInsertableStreams
const NativeRTCPeerConnection = window.RTCPeerConnection

webrtcperf.peerConnectionNextId = 0

webrtcperf.peerConnectionsConnected = 0
webrtcperf.peerConnectionsDisconnected = 0
webrtcperf.peerConnectionsFailed = 0
webrtcperf.peerConnectionsClosed = 0
Expand Down Expand Up @@ -36,6 +36,10 @@ window.RTCPeerConnection = function (conf, options) {
pc.addEventListener('connectionstatechange', () => {
debug(`connectionState: ${pc.connectionState}`)
switch (pc.connectionState) {
case 'connected': {
webrtcperf.peerConnectionsConnected++
break
}
case 'disconnected': {
webrtcperf.peerConnectionsDisconnected++
break
Expand All @@ -45,8 +49,10 @@ window.RTCPeerConnection = function (conf, options) {
break
}
case 'closed': {
webrtcperf.peerConnectionsClosed++
PeerConnections.delete(id)
if (PeerConnections.has(id)) {
PeerConnections.delete(id)
webrtcperf.peerConnectionsClosed++
}
break
}
}
Expand All @@ -55,7 +61,10 @@ window.RTCPeerConnection = function (conf, options) {
const closeNative = pc.close.bind(pc)
pc.close = () => {
debug('close')
PeerConnections.delete(id)
if (PeerConnections.has(id)) {
PeerConnections.delete(id)
webrtcperf.peerConnectionsClosed++
}
return closeNative()
}

Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ to be included into the random selection.`,
path is not provided.`,
format: String,
nullable: false,
default: puppeteer.default.browserRevision,
default: puppeteer.PUPPETEER_REVISIONS.chrome,
env: 'CHROMIUM_VERSION',
arg: 'chromium-version',
},
Expand Down
4 changes: 4 additions & 0 deletions src/rtcstats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export enum PageStatsNames {
pages = 'pages',
/** The current opened PeerConnections. */
peerConnections = 'peerConnections',
/** The total created PeerConnections. */
peerConnectionsCreated = 'peerConnectionsCreated',
/** The total connected PeerConnections. */
peerConnectionsConnected = 'peerConnectionsConnected',
/** The total closed PeerConnections. */
peerConnectionsClosed = 'peerConnectionsClosed',
/** The total disconnected PeerConnections. */
Expand Down
62 changes: 31 additions & 31 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ declare global {
signalingHost?: string
participantName?: string
activePeerConnections: number
peerConnectionsCreated: number
peerConnectionsConnected: number
peerConnectionsDisconnected: number
peerConnectionsFailed: number
peerConnectionsClosed: number
Expand Down Expand Up @@ -629,7 +631,6 @@ export class Session extends EventEmitter {
try {
this.browser = await puppeteer.connect({
browserURL: this.chromiumUrl,
ignoreHTTPSErrors: true,
defaultViewport: {
width: this.windowWidth,
height: this.windowHeight,
Expand Down Expand Up @@ -695,19 +696,20 @@ exec sg ${group} -c /tmp/webrtcperf-launcher-${mark}-browser`,
ignoreDefaultArgs.push('--enable-automation')
}

log.debug(`Using args:\n ${args.join('\n ')}`)
log.debug(`Default args:\n ${puppeteer.defaultArgs().join('\n ')}`)
log.debug(`[session ${this.id}] Using args:\n ${args.join('\n ')}`)
log.debug(
`[session ${this.id}] Default args:\n ${puppeteer.defaultArgs().join('\n ')}`,
)

try {
// log.debug('defaultArgs:', puppeteer.defaultArgs());
this.browser = await puppeteer.launch({
browser: 'chrome',
headless: this.display ? false : true,
executablePath,
handleSIGINT: false,
env,
dumpio: this.enableBrowserLogging,
// devtools: true,
ignoreHTTPSErrors: true,
defaultViewport: {
width: this.windowWidth,
height: this.windowHeight,
Expand All @@ -719,8 +721,8 @@ exec sg ${group} -c /tmp/webrtcperf-launcher-${mark}-browser`,
ignoreDefaultArgs,
args,
})
// const version = await this.browser.version();
// console.log(`[session ${this.id}] Using chrome version: ${version}`);
const version = await this.browser.version()
log.debug(`[session ${this.id}] Using chrome version: ${version}`)
} catch (err) {
log.error(
`[session ${this.id}] Browser launch error: ${(err as Error).stack}`,
Expand Down Expand Up @@ -1537,7 +1539,9 @@ window.SERVER_USE_HTTPS = ${this.serverUseHttps};

const pages: Record<string, number> = {}
const peerConnections: Record<string, number> = {}
const peerConnectionsCreated: Record<string, number> = {}
const peerConnectionsClosed: Record<string, number> = {}
const peerConnectionsConnected: Record<string, number> = {}
const peerConnectionsDisconnected: Record<string, number> = {}
const peerConnectionsFailed: Record<string, number> = {}
const audioEndToEndDelayStats: Record<string, number> = {}
Expand Down Expand Up @@ -1616,11 +1620,21 @@ window.SERVER_USE_HTTPS = ${this.serverUseHttps};

// Set peerConnections counters.
increaseKey(peerConnections, pageKey, activePeerConnections)
increaseKey(
peerConnectionsCreated,
pageKey,
peerConnectionStats.peerConnectionsCreated,
)
increaseKey(
peerConnectionsClosed,
pageKey,
peerConnectionStats.peerConnectionsClosed,
)
increaseKey(
peerConnectionsConnected,
pageKey,
peerConnectionStats.peerConnectionsConnected,
)
increaseKey(
peerConnectionsDisconnected,
pageKey,
Expand Down Expand Up @@ -1714,35 +1728,19 @@ window.SERVER_USE_HTTPS = ${this.serverUseHttps};
this.throttleIndex,
'up',
)
if (throttleUpValues.rate !== undefined) {
throttleUpValuesRate[pageKey] = throttleUpValues.rate
}
if (throttleUpValues.delay !== undefined) {
throttleUpValuesDelay[pageKey] = throttleUpValues.delay
}
if (throttleUpValues.loss !== undefined) {
throttleUpValuesLoss[pageKey] = throttleUpValues.loss
}
if (throttleUpValues.queue !== undefined) {
throttleUpValuesQueue[pageKey] = throttleUpValues.queue
}
throttleUpValuesRate[pageKey] = throttleUpValues.rate || 0
throttleUpValuesDelay[pageKey] = throttleUpValues.delay || 0
throttleUpValuesLoss[pageKey] = throttleUpValues.loss || 0
throttleUpValuesQueue[pageKey] = throttleUpValues.queue || 0

const throttleDownValues = getSessionThrottleValues(
this.throttleIndex,
'down',
)
if (throttleDownValues.rate !== undefined) {
throttleDownValuesRate[pageKey] = throttleDownValues.rate
}
if (throttleDownValues.delay !== undefined) {
throttleDownValuesDelay[pageKey] = throttleDownValues.delay
}
if (throttleDownValues.loss !== undefined) {
throttleDownValuesLoss[pageKey] = throttleDownValues.loss
}
if (throttleDownValues.queue !== undefined) {
throttleDownValuesQueue[pageKey] = throttleDownValues.queue
}
throttleDownValuesRate[pageKey] = throttleDownValues.rate || 0
throttleDownValuesDelay[pageKey] = throttleDownValues.delay || 0
throttleDownValuesLoss[pageKey] = throttleDownValues.loss || 0
throttleDownValuesQueue[pageKey] = throttleDownValues.queue || 0
} catch (err) {
log.error(
`collectPeerConnectionStats for page ${pageIndex} error: ${(err as Error).stack}`,
Expand All @@ -1755,6 +1753,8 @@ window.SERVER_USE_HTTPS = ${this.serverUseHttps};
if (this.pageErrors) collectedStats.errors = this.pageErrors
if (this.pageWarnings) collectedStats.warnings = this.pageWarnings
collectedStats.peerConnections = peerConnections
collectedStats.peerConnectionsCreated = peerConnectionsCreated
collectedStats.peerConnectionsConnected = peerConnectionsConnected
collectedStats.peerConnectionsClosed = peerConnectionsClosed
collectedStats.peerConnectionsDisconnected = peerConnectionsDisconnected
collectedStats.peerConnectionsFailed = peerConnectionsFailed
Expand Down
10 changes: 7 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ export async function checkChromeExecutable(): Promise<string> {
revisions.sort(getVersionComparator(browser))
log.debug(`Available chrome versions: ${revisions}`)
const requiredRevision = config.chromiumVersion
if (!requiredRevision) throw new Error('Chromium version not set')
if (revisions.indexOf(fixSemVer(requiredRevision)) === -1) {
log.info(`Downloading chrome ${requiredRevision}...`)
let progress = 0
Expand Down Expand Up @@ -1132,7 +1133,6 @@ export async function pageScreenshot(
}
const browser = await puppeteer.launch({
headless: true,
ignoreHTTPSErrors: true,
executablePath,
defaultViewport: {
width,
Expand Down Expand Up @@ -1216,8 +1216,12 @@ export function enabledForSession(
return false
}

export function increaseKey(o: Record<string, number>, key: string, value = 1) {
if (!value) return
export function increaseKey(
o: Record<string, number>,
key: string,
value?: number,
): void {
if (value === undefined || !isFinite(value)) return
if (o[key] === undefined) {
o[key] = 0
}
Expand Down
Loading

0 comments on commit 540d99e

Please sign in to comment.