Added sample speedtest request

This commit is contained in:
2024-02-06 23:52:20 +01:00
parent 95366faf97
commit c05cd770c6
3 changed files with 83 additions and 3 deletions

View File

@ -1,4 +1,5 @@
import { Component } from '@angular/core';
import axios from "axios";
@Component({
selector: 'app-download',
@ -9,4 +10,32 @@ import { Component } from '@angular/core';
})
export class DownloadComponent {
constructor() {
this.speedTest();
}
private speedTest() {
const start = new Date().getTime(); // Start timer
axios({
method: 'get',
url: 'http://localhost:/api/v1/speed-test',
responseType: 'arraybuffer',
headers: {
'Access-Control-Allow-Origin': '*', // Allow CORS
}
})
.then(response => {
const end = new Date().getTime(); // End timer
const duration = (end - start) / 1000; // Convert ms to seconds
const bitsLoaded = response.data.byteLength * 8;
const speedBps = bitsLoaded / duration;
const speedKbps = speedBps / 1024;
const speedMbps = speedKbps / 1024;
console.log(`Download speed: ${speedMbps.toFixed(2)} Mbps`);
})
.catch(error => {
console.error('Error during download test:', error);
});
}
}