-
-
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.
- Loading branch information
1 parent
b119d6f
commit 41f6292
Showing
7 changed files
with
82 additions
and
86 deletions.
There are no files selected for viewing
113 changes: 52 additions & 61 deletions
113
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 |
---|---|---|
@@ -1,83 +1,74 @@ | ||
import { LoadProgress } from "./request"; | ||
import { arrayBackwards } from "./utils/utils"; | ||
|
||
export class BandwidthCalculator { | ||
private readonly loadings: LoadProgress[] = []; | ||
|
||
addLoading(progress: LoadProgress) { | ||
this.clearStale(); | ||
this.loadings.push(progress); | ||
} | ||
|
||
// in bits per second | ||
getBandwidth(): number { | ||
this.clearStale(); | ||
return getBandwidthByProgressList(this.loadings); | ||
} | ||
|
||
private clearStale() { | ||
const now = performance.now(); | ||
for (const { startTimestamp } of this.loadings) { | ||
if (now - startTimestamp <= 15000) break; | ||
this.loadings.shift(); | ||
} | ||
} | ||
|
||
destroy() { | ||
this.loadings.length = 0; | ||
} | ||
} | ||
|
||
function getBandwidthByProgressList(loadings: LoadProgress[]) { | ||
if (!loadings.length) return 0; | ||
let margin: number | undefined; | ||
let totalLoadingTime = 0; | ||
let totalBytes = 0; | ||
const now = performance.now(); | ||
|
||
for (const { | ||
startTimestamp: from, | ||
lastLoadedChunkTimestamp: to = now, | ||
loadedBytes, | ||
} of loadings) { | ||
totalBytes += loadedBytes; | ||
|
||
if (margin === undefined || from > margin) { | ||
margin = to; | ||
totalLoadingTime += to - from; | ||
continue; | ||
} | ||
|
||
if (from <= margin && to > margin) { | ||
totalLoadingTime += to - margin; | ||
margin = to; | ||
} | ||
} | ||
|
||
return (totalBytes * 8000) / totalLoadingTime; | ||
} | ||
|
||
class BandwidthCalculator1 { | ||
private simultaneousLoadingsCount = 0; | ||
private readonly bytes: number[] = []; | ||
private readonly timestamps: number[] = []; | ||
private loadingIntervals: { start: number; end?: number }[] = []; | ||
|
||
addBytes(bytesLength: number) { | ||
this.bytes.push(bytesLength); | ||
this.timestamps.push(performance.now()); | ||
} | ||
|
||
startLoading() { | ||
this.clearStale(); | ||
if (this.simultaneousLoadingsCount === 0) { | ||
this.loadingIntervals.push({ start: performance.now() }); | ||
} | ||
this.simultaneousLoadingsCount++; | ||
} | ||
|
||
stopLoading() { | ||
if (this.simultaneousLoadingsCount === 0) return; | ||
this.clearStale(); | ||
if (this.simultaneousLoadingsCount <= 0) return; | ||
this.simultaneousLoadingsCount--; | ||
if (this.simultaneousLoadingsCount !== 0) return; | ||
this.loadingIntervals[this.loadingIntervals.length - 1].end = | ||
performance.now(); | ||
} | ||
|
||
private clearStale() { | ||
const length = this.bytes.length; | ||
// in bits per second | ||
getBandwidthForLastNSeconds(seconds: number) { | ||
this.clearStale(); | ||
const { bytes, timestamps, loadingIntervals } = this; | ||
const samplesLength = bytes.length; | ||
const now = performance.now(); | ||
const threshold = now - seconds * 1000; | ||
|
||
let loadedBytes = 0; | ||
for (let i = samplesLength - 1; i >= 0; i--) { | ||
if (timestamps[i] < threshold) break; | ||
loadedBytes += bytes[i]; | ||
} | ||
|
||
let clearLoadingTime = 0; | ||
for (const { start, end } of arrayBackwards(loadingIntervals)) { | ||
if (start < threshold && end !== undefined && end < threshold) break; | ||
const from = Math.max(start, threshold); | ||
const to = end ?? now; | ||
clearLoadingTime += to - from; | ||
} | ||
|
||
if (clearLoadingTime === 0) return 0; | ||
return (loadedBytes * 8000) / clearLoadingTime; | ||
} | ||
|
||
for (let i = 0; i < length; i++) {} | ||
private clearStale() { | ||
const { timestamps, bytes, loadingIntervals } = this; | ||
const samplesLength = bytes.length; | ||
const threshold = performance.now() - 15000; | ||
|
||
let count = 0; | ||
while (count < samplesLength && timestamps[count] < threshold) count++; | ||
bytes.splice(0, count); | ||
timestamps.splice(0, count); | ||
|
||
count = 0; | ||
for (const { start, end } of loadingIntervals) { | ||
if (!(start < threshold && end !== undefined && end <= threshold)) break; | ||
count++; | ||
} | ||
loadingIntervals.splice(0, count); | ||
} | ||
} |
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