File size: 3,115 Bytes
6f6d8a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// place files you want to import through the `$lib` alias in this folder.

interface BandwidthCallback {
	(
		elapsedMs: number,
		loadedBytes: number,
		totalBytes: number,
		bytesPerSecond: number,
		done: boolean
	): void;
}

export async function bandwidthTest(
	onProgress: BandwidthCallback,
	onLatency: (latency: number) => void
) {
	performance.setResourceTimingBufferSize(100);
	performance.clearResourceTimings();
	// start timer
	const startTime = performance.now();
	// const url = 'https://cdn-test-cloudfront.hf.co/140gb.safetensors';
	const url = 'https://cdn-test-cloudfront.hf.co/15mb.json';
	const response = await fetch(url);
	if (!response.ok) {
		throw new Error(`Network response was not ok: ${response.status}`);
	}
	// setTimeout(() => {
	// 	const entries = performance.getEntriesByType('resource');
	// 	const resourceEntry = entries.find((e) => e.name === url);
	// 	if (!resourceEntry) {
	// 		return
	// 	}
	// 	console.log(resourceEntry);
	// 	const { requestStart, responseStart } = resourceEntry;
	// 	const latency = responseStart - requestStart;
	// 	onLatency(latency);
	// }, 2000);
	const latency = performance.now() - startTime;
	onLatency(latency);
	const contentLengthHeader = response.headers.get('Content-Length');
	const totalBytes = contentLengthHeader ? parseInt(contentLengthHeader, 10) : 0;
	const reader = response.body.getReader();
	let loadedBytes = 0;
	let lastTimestamp = performance.now();
	let lastLoaded = 0;
	const REPORT_INTERVAL_MS = 500;
	onProgress(latency, loadedBytes, totalBytes, 0, false);
	let bytesPerSecond= 0;
	while (true) {
		const { done, value } = await reader.read();
		if (done) {
			// stream is finished
			const elapsedMs = performance.now() - startTime;
			onProgress(elapsedMs, loadedBytes, totalBytes, bytesPerSecond, true);
			break;
		}

		// `value` is a Uint8Array for this chunk
		loadedBytes += value.byteLength;

		// Current time
		const now = performance.now();
		const deltaMs = now - lastTimestamp;

		if (deltaMs >= REPORT_INTERVAL_MS) {
			// compute bytes downloaded since last report
			const deltaBytes = loadedBytes - lastLoaded;
			// convert ms to seconds
			const deltaSeconds = deltaMs / 1000;
			bytesPerSecond = deltaBytes / deltaSeconds;

			// Invoke callback
			const elapsedMs = performance.now() - startTime;
			onProgress(elapsedMs, loadedBytes, totalBytes, bytesPerSecond, false);

			// Reset our “last” markers
			lastLoaded = loadedBytes;
			lastTimestamp = now;
		}
	}
}

export async function counter(callback: BandwidthCallback) {
	// start timer
	const startTime = performance.now();
	let counter = 0;
	// set a random value between 50 and 150
	const interval = setInterval(() => {
		counter = Math.floor(Math.random() * 100) + 50;
		// Calculate elapsed time in milliseconds
		const elapsedMs = performance.now() - startTime;
		callback(elapsedMs, 100, 100, counter, false);
	}, 1000);
	// Stop the counter after 3600 seconds
	setTimeout(() => {
		clearInterval(interval);
		const elapsedMs = performance.now() - startTime;
		callback(elapsedMs, 100, 100, counter, true);
	}, 3600 * 1000);
}