hf-fast / src /lib /index.ts
hlarcher's picture
hlarcher HF Staff
first import
6f6d8a6 unverified
raw
history blame
3.12 kB
// 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);
}