|
| 1 | +// Copyright Epic Games, Inc. All Rights Reserved. |
| 2 | + |
| 3 | +import { |
| 4 | + AggregatedStats, |
| 5 | + LatencyInfo, |
| 6 | + Logger, |
| 7 | + SettingNumber |
| 8 | +} from '@epicgames-ps/lib-pixelstreamingfrontend-ue5.6'; |
| 9 | +import { SettingUINumber } from '../Config/SettingUINumber'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Session test UI elements and results handling. |
| 13 | + * Creates a button to start the test and collects stats and latency info during the test. |
| 14 | + * After the test is finished, it generates CSV files for stats and latency info. |
| 15 | + * The test runs for a specified time frame, which can be set in the UI. |
| 16 | + */ |
| 17 | +export class SessionTest { |
| 18 | + _rootElement: HTMLElement; |
| 19 | + _latencyTestButton: HTMLInputElement; |
| 20 | + _testTimeFrameSetting: SettingNumber<'TestTimeFrame'>; |
| 21 | + |
| 22 | + isCollectingStats: boolean; |
| 23 | + |
| 24 | + records: AggregatedStats[]; |
| 25 | + latencyRecords: LatencyInfo[]; |
| 26 | + |
| 27 | + constructor() { |
| 28 | + this.isCollectingStats = false; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Make the elements for the session test: e.g. button and test time input. |
| 33 | + */ |
| 34 | + public get rootElement(): HTMLElement { |
| 35 | + if (!this._rootElement) { |
| 36 | + this._rootElement = document.createElement('section'); |
| 37 | + this._rootElement.classList.add('settingsContainer'); |
| 38 | + |
| 39 | + // make heading |
| 40 | + const heading = document.createElement('div'); |
| 41 | + heading.id = 'latencyTestHeader'; |
| 42 | + heading.classList.add('settings-text'); |
| 43 | + heading.classList.add('settingsHeader'); |
| 44 | + this._rootElement.appendChild(heading); |
| 45 | + |
| 46 | + const headingText = document.createElement('div'); |
| 47 | + headingText.innerHTML = 'Session Test'; |
| 48 | + heading.appendChild(headingText); |
| 49 | + |
| 50 | + // make test results element |
| 51 | + const resultsParentElem = document.createElement('div'); |
| 52 | + resultsParentElem.id = 'latencyTestContainer'; |
| 53 | + resultsParentElem.classList.add('d-none'); |
| 54 | + this._rootElement.appendChild(resultsParentElem); |
| 55 | + |
| 56 | + this._testTimeFrameSetting = new SettingNumber( |
| 57 | + 'TestTimeFrame', |
| 58 | + 'Test Time Frame', |
| 59 | + 'How long the test runs for (seconds)', |
| 60 | + 0 /*min*/, |
| 61 | + 3600 /*max*/, |
| 62 | + 60 /*default*/, |
| 63 | + false |
| 64 | + ); |
| 65 | + const testTimeFrameSetting = new SettingUINumber(this._testTimeFrameSetting); |
| 66 | + resultsParentElem.appendChild(testTimeFrameSetting.rootElement); |
| 67 | + resultsParentElem.appendChild(this.latencyTestButton); |
| 68 | + } |
| 69 | + return this._rootElement; |
| 70 | + } |
| 71 | + |
| 72 | + public get latencyTestButton(): HTMLInputElement { |
| 73 | + if (!this._latencyTestButton) { |
| 74 | + this._latencyTestButton = document.createElement('input'); |
| 75 | + this._latencyTestButton.type = 'button'; |
| 76 | + this._latencyTestButton.value = 'Run Test'; |
| 77 | + this._latencyTestButton.id = 'btn-start-latency-test'; |
| 78 | + this._latencyTestButton.classList.add('streamTools-button'); |
| 79 | + this._latencyTestButton.classList.add('btn-flat'); |
| 80 | + |
| 81 | + this._latencyTestButton.onclick = () => { |
| 82 | + this.records = []; |
| 83 | + this.latencyRecords = []; |
| 84 | + this.isCollectingStats = true; |
| 85 | + this._latencyTestButton.disabled = true; |
| 86 | + this._latencyTestButton.value = 'Running...'; |
| 87 | + Logger.Info(`Starting session test. Duration: [${this._testTimeFrameSetting.number}]`); |
| 88 | + setTimeout(() => { |
| 89 | + this.onCollectingFinished(); |
| 90 | + this._latencyTestButton.disabled = false; |
| 91 | + this._latencyTestButton.value = 'Run Test'; |
| 92 | + }, this._testTimeFrameSetting.number * 1000); |
| 93 | + }; |
| 94 | + } |
| 95 | + return this._latencyTestButton; |
| 96 | + } |
| 97 | + |
| 98 | + public handleStats(stats: AggregatedStats) { |
| 99 | + if (!this.isCollectingStats) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + const statsCopy = structuredClone(stats); |
| 104 | + this.records.push(statsCopy); |
| 105 | + } |
| 106 | + |
| 107 | + public handleLatencyInfo(latencyInfo: LatencyInfo) { |
| 108 | + if (!this.isCollectingStats) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + const latencyInfoCopy = structuredClone(latencyInfo); |
| 113 | + this.latencyRecords.push(latencyInfoCopy); |
| 114 | + } |
| 115 | + |
| 116 | + private onCollectingFinished() { |
| 117 | + this.isCollectingStats = false; |
| 118 | + Logger.Info(`Finished session test`); |
| 119 | + |
| 120 | + this.generateStatsCsv(); |
| 121 | + this.generateLatencyCsv(); |
| 122 | + } |
| 123 | + |
| 124 | + private generateStatsCsv() { |
| 125 | + const csvHeader: string[] = []; |
| 126 | + |
| 127 | + this.records.forEach((record: AggregatedStats) => { |
| 128 | + for (const i in record) { |
| 129 | + const obj: {} = record[i as never]; |
| 130 | + |
| 131 | + if (Array.isArray(obj)) { |
| 132 | + for (let j = 0; j < obj.length; j++) { |
| 133 | + const arrayVal = obj[j]; |
| 134 | + for (const k in arrayVal) { |
| 135 | + if (csvHeader.indexOf(`${i}.${j}.${k}`) === -1) { |
| 136 | + csvHeader.push(`${i}.${j}.${k}`); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } else if (obj instanceof Map) { |
| 141 | + for (const j in obj.keys()) { |
| 142 | + const mapVal = obj.get(j); |
| 143 | + for (const k in mapVal) { |
| 144 | + if (csvHeader.indexOf(`${i}.${j}.${k}`) === -1) { |
| 145 | + csvHeader.push(`${i}.${j}.${k}`); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } else { |
| 150 | + for (const j in obj) { |
| 151 | + if (csvHeader.indexOf(`${i}.${j}`) === -1) { |
| 152 | + csvHeader.push(`${i}.${j}`); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + }); |
| 158 | + |
| 159 | + let csvBody = ''; |
| 160 | + this.records.forEach((record) => { |
| 161 | + csvHeader.forEach((field) => { |
| 162 | + try { |
| 163 | + csvBody += `"${field.split('.').reduce((o, k) => o[k as never], record)}",`; |
| 164 | + } catch (_) { |
| 165 | + csvBody += `"",`; |
| 166 | + } |
| 167 | + }); |
| 168 | + csvBody += `\n`; |
| 169 | + }); |
| 170 | + |
| 171 | + const file = new Blob([`${csvHeader.join(',')}\n${csvBody}`], { type: 'text/plain' }); |
| 172 | + const a = document.createElement('a'); |
| 173 | + const url = URL.createObjectURL(file); |
| 174 | + a.href = url; |
| 175 | + a.download = 'stats.csv'; |
| 176 | + document.body.appendChild(a); |
| 177 | + a.click(); |
| 178 | + setTimeout(function () { |
| 179 | + document.body.removeChild(a); |
| 180 | + window.URL.revokeObjectURL(url); |
| 181 | + }, 0); |
| 182 | + } |
| 183 | + |
| 184 | + private generateLatencyCsv() { |
| 185 | + const csvHeader: string[] = []; |
| 186 | + |
| 187 | + this.latencyRecords.forEach((record) => { |
| 188 | + for (const i in record) { |
| 189 | + const obj = record[i as never]; |
| 190 | + |
| 191 | + if (typeof obj === 'object') { |
| 192 | + for (const j in obj as object) { |
| 193 | + if (csvHeader.indexOf(`${i}.${j}`) === -1) { |
| 194 | + csvHeader.push(`${i}.${j}`); |
| 195 | + } |
| 196 | + } |
| 197 | + } else if (csvHeader.indexOf(`${i}`) === -1) { |
| 198 | + csvHeader.push(`${i}`); |
| 199 | + } |
| 200 | + } |
| 201 | + }); |
| 202 | + |
| 203 | + let csvBody = ''; |
| 204 | + this.latencyRecords.forEach((record) => { |
| 205 | + csvHeader.forEach((field) => { |
| 206 | + try { |
| 207 | + csvBody += `"${field.split('.').reduce((o, k) => o[k as never], record)}",`; |
| 208 | + } catch (_) { |
| 209 | + csvBody += `"",`; |
| 210 | + } |
| 211 | + }); |
| 212 | + csvBody += `\n`; |
| 213 | + }); |
| 214 | + |
| 215 | + const file = new Blob([`${csvHeader.join(',')}\n${csvBody}`], { type: 'text/plain' }); |
| 216 | + const a = document.createElement('a'); |
| 217 | + const url = URL.createObjectURL(file); |
| 218 | + a.href = url; |
| 219 | + a.download = 'latency.csv'; |
| 220 | + document.body.appendChild(a); |
| 221 | + a.click(); |
| 222 | + setTimeout(function () { |
| 223 | + document.body.removeChild(a); |
| 224 | + window.URL.revokeObjectURL(url); |
| 225 | + }, 0); |
| 226 | + } |
| 227 | +} |
0 commit comments