@vpalmisano/webrtcperf 4.4.1 → 4.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/session.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { getSessionThrottleValues, throttleLauncher } from '@vpalmisano/throttler'
1
+ import { getSessionThrottleValues, throttleLauncher, throttleNotifier } from '@vpalmisano/throttler'
2
2
  import assert from 'assert'
3
3
  import axios from 'axios'
4
4
  import EventEmitter from 'events'
@@ -30,8 +30,8 @@ import { gunzipSync } from 'zlib'
30
30
  import { RtcStats, rtcStatKey, updateRtcStats } from './rtcstats'
31
31
  import { FastStats } from './stats'
32
32
  import {
33
- PeerConnectionExternal,
34
- PeerConnectionExternalMethod,
33
+ /* PeerConnectionExternal,
34
+ PeerConnectionExternalMethod, */
35
35
  checkChromeExecutable,
36
36
  downloadUrl,
37
37
  enabledForSession,
@@ -163,6 +163,7 @@ export interface SessionParams {
163
163
  userAgent: string
164
164
  id: number
165
165
  throttleIndex: number
166
+ useBrowserThrottling: boolean
166
167
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
167
168
  evaluateAfter?: any[]
168
169
  exposedFunctions?: string
@@ -279,6 +280,8 @@ export class Session extends EventEmitter {
279
280
  readonly id: number
280
281
  /** The throttle configuration index assigned to the session. */
281
282
  readonly throttleIndex: number
283
+ /** If true, the network will be throttled using the browser internal throttling mechanism. */
284
+ readonly useBrowserThrottling: boolean
282
285
  /** The test page url. */
283
286
  readonly url: string
284
287
  /** The url query. */
@@ -378,6 +381,7 @@ export class Session extends EventEmitter {
378
381
  userAgent,
379
382
  id,
380
383
  throttleIndex,
384
+ useBrowserThrottling,
381
385
  evaluateAfter,
382
386
  exposedFunctions,
383
387
  scriptParams,
@@ -475,6 +479,7 @@ export class Session extends EventEmitter {
475
479
  this.emulateCpuThrottling = emulateCpuThrottling
476
480
 
477
481
  this.throttleIndex = throttleIndex
482
+ this.useBrowserThrottling = useBrowserThrottling
478
483
  this.evaluateAfter = evaluateAfter || []
479
484
  this.exposedFunctions = exposedFunctions || {}
480
485
  if (scriptParams) {
@@ -892,6 +897,9 @@ webrtcperf.config.AUDIO_URL = "http${this.serverUseHttps ? 's' : ''}://localhost
892
897
 
893
898
  const page = await this.getNewPage(tabIndex)
894
899
 
900
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
901
+ const pageCDPSession = (page as any)._client() as CDPSession
902
+
895
903
  await page.setBypassCSP(true)
896
904
 
897
905
  if (this.userAgent) {
@@ -932,8 +940,7 @@ Object.defineProperty(window.screen.orientation, 'type', { value: 'landscape-pri
932
940
  // Clear cookies.
933
941
  if (this.clearCookies) {
934
942
  try {
935
- const client = await page.target().createCDPSession()
936
- await client.send('Network.clearBrowserCookies')
943
+ await pageCDPSession.send('Network.clearBrowserCookies')
937
944
  } catch (err) {
938
945
  log.error(`clearCookies error: ${(err as Error).stack}`)
939
946
  }
@@ -977,8 +984,6 @@ Object.defineProperty(window.screen.orientation, 'type', { value: 'landscape-pri
977
984
  // Enable request interception.
978
985
  let setRequestInterceptionState = true
979
986
 
980
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
981
- const pageCDPSession = (page as any)._client() as CDPSession
982
987
  await pageCDPSession.send('Network.setBypassServiceWorker', {
983
988
  bypass: true,
984
989
  })
@@ -1181,7 +1186,7 @@ Object.defineProperty(window.screen.orientation, 'type', { value: 'landscape-pri
1181
1186
  }
1182
1187
 
1183
1188
  // PeerConnectionExternal
1184
- await page.exposeFunction(
1189
+ /* await page.exposeFunction(
1185
1190
  'createPeerConnectionExternal',
1186
1191
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1187
1192
  async (options: any) => {
@@ -1199,7 +1204,7 @@ Object.defineProperty(window.screen.orientation, 'type', { value: 'landscape-pri
1199
1204
  return pc[name](arg)
1200
1205
  }
1201
1206
  },
1202
- )
1207
+ )*/
1203
1208
 
1204
1209
  // Simulate keypress
1205
1210
  await page.exposeFunction('keypressText', async (selector: string, text: string, delay = 20) => {
@@ -1427,12 +1432,25 @@ Object.defineProperty(window.screen.orientation, 'type', { value: 'landscape-pri
1427
1432
  resourcesStats.wsRecvBytes += event.response.payloadData.length
1428
1433
  })
1429
1434
 
1430
- // hardware concurrency
1435
+ // Hardware concurrency.
1431
1436
  if (this.hardwareConcurrency) {
1432
1437
  const plugin = NavigatorHardwareConcurrency({ hardwareConcurrency: this.hardwareConcurrency })
1433
1438
  await plugin.onPageCreated(page)
1434
1439
  }
1435
1440
 
1441
+ // Network throttling.
1442
+ if (this.throttleIndex > -1 && (process.platform !== 'linux' || this.useBrowserThrottling)) {
1443
+ log.debug(`Using internal network throttling`)
1444
+ await pageCDPSession.send('Network.emulateNetworkConditions', {
1445
+ offline: false,
1446
+ uploadThroughput: 100000000 / 8,
1447
+ downloadThroughput: 100000000 / 8,
1448
+ latency: 0,
1449
+ packetLoss: 0,
1450
+ packetQueueLength: 0,
1451
+ })
1452
+ }
1453
+
1436
1454
  // Load page script.
1437
1455
  {
1438
1456
  const filePath = resolvePackagePath('node_modules/@vpalmisano/webrtcperf-js/dist/webrtcperf.js')
@@ -1492,6 +1510,13 @@ Object.defineProperty(window.screen.orientation, 'type', { value: 'landscape-pri
1492
1510
  // add to pages map
1493
1511
  this.pages.set(index, page)
1494
1512
 
1513
+ if (this.throttleIndex > -1 && (process.platform !== 'linux' || this.useBrowserThrottling)) {
1514
+ await this.applyNetworkThrottling(pageCDPSession)
1515
+ throttleNotifier.on('change', async () => {
1516
+ await this.applyNetworkThrottling(pageCDPSession)
1517
+ })
1518
+ }
1519
+
1495
1520
  log.debug(`Page ${index + 1} "${url}" loaded in ${(Date.now() - pageLoadTime) / 1000}s`)
1496
1521
 
1497
1522
  for (let i = 0; i < this.evaluateAfter.length; i++) {
@@ -1503,6 +1528,25 @@ Object.defineProperty(window.screen.orientation, 'type', { value: 'landscape-pri
1503
1528
  }
1504
1529
  }
1505
1530
 
1531
+ private async applyNetworkThrottling(pageCDPSession: CDPSession) {
1532
+ const throttleUpValues = getSessionThrottleValues(this.throttleIndex, 'up')
1533
+ const throttleDownValues = getSessionThrottleValues(this.throttleIndex, 'down')
1534
+ const params = {
1535
+ offline: false,
1536
+ uploadThroughput: throttleUpValues.rate || -1,
1537
+ downloadThroughput: throttleDownValues.rate || -1,
1538
+ latency: Math.max(throttleUpValues.delay || 0, throttleDownValues.delay || 0),
1539
+ packetLoss: Math.max(throttleUpValues.loss || 0, throttleDownValues.loss || 0),
1540
+ packetQueueLength: Math.max(throttleUpValues.queue || 0, throttleDownValues.queue || 0),
1541
+ }
1542
+ log.debug(`Apply internal network throttling: ${JSON.stringify(params)}`)
1543
+ await pageCDPSession.send('Network.emulateNetworkConditions', {
1544
+ ...params,
1545
+ uploadThroughput: params.uploadThroughput !== -1 ? params.uploadThroughput / 8 : -1,
1546
+ downloadThroughput: params.downloadThroughput !== -1 ? params.downloadThroughput / 8 : -1,
1547
+ })
1548
+ }
1549
+
1506
1550
  private async getNewPage(tabIndex: number): Promise<Page> {
1507
1551
  log.debug(`getNewPage ${tabIndex}`)
1508
1552
  assert(this.context, 'NoBrowserContextCreated')