-
-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rename file. * Add bandwidth calculator. * Add bandwidth calculator. * Git use another approach. Suppress loading intervals together. * Use time shift instead of loading intervals. --------- Co-authored-by: Igor Zolotarenko <[email protected]>
- Loading branch information
1 parent
0508d9f
commit 39ec83d
Showing
8 changed files
with
91 additions
and
104 deletions.
There are no files selected for viewing
57 changes: 0 additions & 57 deletions
57
packages/p2p-media-loader-core/src/bandwidth-approximator.ts
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
packages/p2p-media-loader-core/src/bandwidth-calculator.ts
This file contains 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,63 @@ | ||
const CLEAR_THRESHOLD_MS = 3000; | ||
|
||
export class BandwidthCalculator { | ||
private simultaneousLoadingsCount = 0; | ||
private readonly bytes: number[] = []; | ||
private readonly timestamps: number[] = []; | ||
private noLoadingsTotalTime = 0; | ||
private allLoadingsStoppedTimestamp = 0; | ||
|
||
addBytes(bytesLength: number, now = performance.now()) { | ||
this.bytes.push(bytesLength); | ||
this.timestamps.push(now - this.noLoadingsTotalTime); | ||
} | ||
|
||
startLoading(now = performance.now()) { | ||
this.clearStale(); | ||
if (this.simultaneousLoadingsCount === 0) { | ||
this.noLoadingsTotalTime += now - this.allLoadingsStoppedTimestamp; | ||
} | ||
this.simultaneousLoadingsCount++; | ||
} | ||
|
||
// in bits per second | ||
stopLoading(now = performance.now()) { | ||
if (this.simultaneousLoadingsCount <= 0) return; | ||
this.simultaneousLoadingsCount--; | ||
if (this.simultaneousLoadingsCount !== 0) return; | ||
this.allLoadingsStoppedTimestamp = now; | ||
} | ||
|
||
getBandwidthForLastNSeconds(seconds: number) { | ||
if (!this.timestamps.length) return 0; | ||
const milliseconds = seconds * 1000; | ||
const lastItemTimestamp = this.timestamps[this.timestamps.length - 1]; | ||
let lastCountedTimestamp = lastItemTimestamp; | ||
const threshold = lastItemTimestamp - milliseconds; | ||
let totalBytes = 0; | ||
|
||
for (let i = this.bytes.length - 1; i >= 0; i--) { | ||
const timestamp = this.timestamps[i]; | ||
if (timestamp < threshold) break; | ||
lastCountedTimestamp = timestamp; | ||
totalBytes += this.bytes[i]; | ||
} | ||
|
||
return (totalBytes * 8000) / (lastItemTimestamp - lastCountedTimestamp); | ||
} | ||
|
||
clearStale() { | ||
if (!this.timestamps.length) return; | ||
const threshold = | ||
this.timestamps[this.timestamps.length - 1] - CLEAR_THRESHOLD_MS; | ||
|
||
let samplesToRemove = 0; | ||
for (const timestamp of this.timestamps) { | ||
if (timestamp > threshold) break; | ||
samplesToRemove++; | ||
} | ||
|
||
this.bytes.splice(0, samplesToRemove); | ||
this.timestamps.splice(0, samplesToRemove); | ||
} | ||
} |
This file contains 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 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 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 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 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 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