@vpalmisano/webrtcperf 4.2.2 → 4.3.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.
@@ -63,21 +63,17 @@ function calculateFailAmountPercentile(stat, percentile = 95) {
63
63
  class StatsWriter {
64
64
  fname;
65
65
  columns;
66
- _header_written = false;
66
+ headerWritten = false;
67
67
  constructor(fname = 'stats.log', columns) {
68
68
  this.fname = fname;
69
69
  this.columns = columns;
70
70
  }
71
- /**
72
- * push
73
- * @param dataColumns
74
- */
75
- async push(dataColumns) {
76
- if (!this._header_written) {
71
+ async push(dataColumns, append = true) {
72
+ if (!this.headerWritten || !append) {
77
73
  const data = ['datetime', ...this.columns].join(',') + '\n';
78
74
  await fs.promises.mkdir(path.dirname(this.fname), { recursive: true });
79
75
  await fs.promises.writeFile(this.fname, data);
80
- this._header_written = true;
76
+ this.headerWritten = true;
81
77
  }
82
78
  //
83
79
  const data = [Date.now(), ...dataColumns].join(',') + '\n';
@@ -228,6 +224,7 @@ class Stats extends events.EventEmitter {
228
224
  nextSessionId;
229
225
  statsWriter;
230
226
  detailedStatsWriter;
227
+ detailedStatsSummaryWriter;
231
228
  scheduler;
232
229
  alertRules = null;
233
230
  alertRulesOutput;
@@ -250,6 +247,7 @@ class Stats extends events.EventEmitter {
250
247
  };
251
248
  externalCollectedStats = new Map();
252
249
  pushStatsInstance = null;
250
+ detailedStatsSummary = {};
253
251
  running = false;
254
252
  /**
255
253
  * Stats aggregator class.
@@ -287,6 +285,7 @@ class Stats extends events.EventEmitter {
287
285
  : {};
288
286
  this.statsWriter = null;
289
287
  this.detailedStatsWriter = null;
288
+ this.detailedStatsSummaryWriter = null;
290
289
  if (alertRules.trim()) {
291
290
  this.alertRules = json5_1.default.parse(alertRules);
292
291
  log.debug(`using alertRules: ${JSON.stringify(this.alertRules, undefined, 2)}`);
@@ -405,6 +404,11 @@ class Stats extends events.EventEmitter {
405
404
  'trackId',
406
405
  ...this.statsNames,
407
406
  ]);
407
+ this.detailedStatsSummaryWriter = new StatsWriter(this.detailedStatsPath.replace(/\.(.+)$/, '-summary.$1'), [
408
+ 'participantName',
409
+ 'trackId',
410
+ ...this.statsNames,
411
+ ]);
408
412
  }
409
413
  if (this.prometheusPushgateway) {
410
414
  const register = new promClient.Registry();
@@ -588,7 +592,6 @@ class Stats extends events.EventEmitter {
588
592
  }
589
593
  else {
590
594
  for (const [key, value] of Object.entries(obj)) {
591
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
592
595
  if (typeof value === 'number' && isFinite(value)) {
593
596
  collectedStats.all.push(value);
594
597
  // Push host label.
@@ -729,18 +732,46 @@ class Stats extends events.EventEmitter {
729
732
  stats = {};
730
733
  participantTrackStats.set(label, stats);
731
734
  }
732
- stats[name] = (0, utils_1.toPrecision)(value, 6);
735
+ stats[name] = value;
733
736
  });
734
737
  });
735
738
  for (const [label, trackStats] of participantTrackStats.entries()) {
736
739
  const [participantName, trackId] = label.split(':', 2);
740
+ if (!this.detailedStatsSummary[label]) {
741
+ this.detailedStatsSummary[label] = {};
742
+ }
743
+ const summary = this.detailedStatsSummary[label];
737
744
  const values = [participantName, trackId];
738
745
  for (const name of this.statsNames) {
739
- values.push(trackStats[name] ?? '');
746
+ values.push(trackStats[name] !== undefined ? (0, utils_1.toPrecision)(trackStats[name], 6) : '');
747
+ // Update the summary stats.
748
+ if (!summary[name]) {
749
+ summary[name] = new fast_stats_1.Stats({ store_data: false });
750
+ }
751
+ const stat = summary[name];
752
+ if (trackStats[name] !== undefined) {
753
+ stat.push(trackStats[name]);
754
+ }
740
755
  }
741
756
  await this.detailedStatsWriter.push(values);
742
757
  }
743
758
  }
759
+ async writeDetailedStatsSummary() {
760
+ if (!this.detailedStatsSummaryWriter)
761
+ return;
762
+ let append = false;
763
+ for (const label of Object.keys(this.detailedStatsSummary)) {
764
+ const summary = this.detailedStatsSummary[label];
765
+ const [participantName, trackId] = label.split(':', 2);
766
+ const values = [participantName, trackId];
767
+ for (const name of this.statsNames) {
768
+ const stat = summary[name];
769
+ values.push(stat?.length > 0 ? (0, utils_1.toPrecision)(stat.amean(), 6) : '');
770
+ }
771
+ await this.detailedStatsSummaryWriter.push(values, append);
772
+ append = true;
773
+ }
774
+ }
744
775
  /**
745
776
  * addCollectedStats
746
777
  * @param id
@@ -1366,17 +1397,13 @@ class Stats extends events.EventEmitter {
1366
1397
  log.error(`writeAlertRulesReport error: ${err.stack}`);
1367
1398
  }
1368
1399
  }
1369
- /**
1370
- * Stop the stats collector and the added Sessions.
1371
- */
1372
1400
  async stop() {
1373
- if (!this.running) {
1401
+ if (!this.running)
1374
1402
  return;
1375
- }
1376
1403
  this.running = false;
1377
1404
  log.debug('stop');
1378
1405
  if (this.scheduler) {
1379
- this.scheduler.stop();
1406
+ await this.scheduler.stop();
1380
1407
  this.scheduler = undefined;
1381
1408
  }
1382
1409
  for (const session of this.sessions.values()) {
@@ -1389,7 +1416,11 @@ class Stats extends events.EventEmitter {
1389
1416
  }
1390
1417
  }
1391
1418
  this.sessions.clear();
1419
+ await this.writeDetailedStatsSummary();
1392
1420
  this.statsWriter = null;
1421
+ this.detailedStatsWriter = null;
1422
+ this.detailedStatsSummaryWriter = null;
1423
+ this.detailedStatsSummary = {};
1393
1424
  // delete metrics
1394
1425
  if (this.gateway) {
1395
1426
  await this.deletePushgatewayStats();