-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(deviceDetector): ACT-841 replace benchmark to s3, add timeout (#88)
- Loading branch information
1 parent
5d78f84
commit 5c0ebc8
Showing
2 changed files
with
19 additions
and
11 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,25 @@ | ||
export async function checkDownloadSpeed(baseUrl: string): Promise<number> { | ||
const startTime = performance.now(); | ||
|
||
const response = await fetch(baseUrl); | ||
try { | ||
const response = await fetch(baseUrl, { signal: AbortSignal.timeout(4000) }); | ||
|
||
const data = await response.blob(); | ||
const data = await response.blob(); | ||
|
||
const endTime = performance.now(); | ||
const endTime = performance.now(); | ||
|
||
const duration = (endTime - startTime) / 1000; // convert to seconds | ||
const sizeInBytes = data.size; | ||
const sizeInMegabits = (sizeInBytes * 8) / (1024 * 1024); // convert to megabits | ||
const duration = (endTime - startTime) / 1000; // convert to seconds | ||
const sizeInBytes = data.size; | ||
const sizeInMegabits = (sizeInBytes * 8) / (1024 * 1024); // convert to megabits | ||
|
||
const speedMbps = sizeInMegabits / duration; | ||
const speedMbps = sizeInMegabits / duration; | ||
|
||
return speedMbps; | ||
return speedMbps; | ||
} catch (error) { | ||
if (['TimeoutError', 'AbortError'].includes((error as Error).name)) { | ||
return 0; | ||
} | ||
|
||
return 10; | ||
} | ||
} |