Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Component for handling the speed test.
- */
- import { Component, OnInit } from '@angular/core';
- import { SpeedTestService } from '../../services/speed-test.service';
- @Component({
- selector: 'app-speed-test-page',
- templateUrl: './speed-test-page.component.html',
- styleUrls: ['./speed-test-page.component.css'],
- providers: [SpeedTestService]
- })
- export class SpeedTestPageComponent implements OnInit {
- // Object to hold data to send to database:
- model: any = {};
- currentImageIndexDownload = 0;
- currentImageIndexUpload = 0;
- images = [
- {'url': '350x350', 'size': 240000},
- {'url': '500x500', 'size': 494000},
- {'url': '750x750', 'size': 1100000},
- {'url': '1000x1000', 'size': 1900000},
- {'url': '1500x1500', 'size': 4300000},
- {'url': '2000x2000', 'size': 7500000},
- {'url': '2500x2500', 'size': 12000000},
- {'url': '3000x3000', 'size': 17000000},
- {'url': '3500x3500', 'size': 23000000},
- {'url': '4000x4000', 'size': 30000000},
- ];
- pingTestStartedAt: number;
- uploadSpeedTotalSpeed: number;
- uploadSpeedBestSpeed: number;
- downloadSpeedTotalSpeed: number;
- downloadSpeedBestSpeed: number;
- //data: string;
- constructor(private speedTestService: SpeedTestService) {
- }
- ngOnInit() {
- //this.getLocation();
- //this.getIPandISP();
- //this.downloadSpeed();
- this.uploadSpeedTotalSpeed = 0;
- this.uploadSpeedBestSpeed = 0;
- this.downloadSpeedTotalSpeed = 0;
- this.downloadSpeedBestSpeed = 0;
- this.uploadSpeed();
- //this.pingTestStartedAt = new Date().getTime();
- //this.pingTest(0);
- }
- /**
- * Get the position of the user in latitude and longitude.
- * If geolocation is supported, it sends the position to storeCurrentPosition.
- * If not supported, it shows a message about this to the user.
- * The code is inspired from: https://www.w3schools.com/html/html5_geolocation.asp
- */
- getLocation() {
- if (navigator.geolocation) {
- navigator.geolocation.getCurrentPosition(this.storeCurrentPosition);
- } else {
- document.getElementById('currentPositionTextArea').innerHTML =
- 'Geografisk posisjonering er ikke støttet av nettleseren. Vennligst skriv inn din adresse.';
- }
- }
- /**
- * Stores latitude and longitude to the object.
- */
- storeCurrentPosition = (position) => {
- this.model.y_coord = position.coords.latitude;
- this.model.x_coord = position.coords.longitude;
- }
- /**
- * Get the IP address and ISP provider to the user and stores it to the object.
- */
- getIPandISP() {
- this.speedTestService.getIPandISP().subscribe(response => {
- this.model.ip = response.query;
- this.model.isp = response.isp;
- });
- }
- downloadSpeed = () => {
- this.speedTestService.getDownloadSpeed(this.images[this.currentImageIndexDownload].url, (duration: number) => {
- console.log('currentImageIndexDownload: ' + this.currentImageIndexDownload);
- console.log('duration: ' + duration);
- // Get speed in Mbit/s
- let speed = this.calculateSpeed(duration, this.images[this.currentImageIndexDownload].size);
- this.downloadSpeedTotalSpeed += speed;
- // If first request, or if this speed is the best so far
- if (speed > this.downloadSpeedBestSpeed || this.downloadSpeedBestSpeed === 0) {
- this.downloadSpeedBestSpeed = speed;
- }
- // Do we need another request?
- if (this.currentImageIndexDownload < (this.images.length-1) && duration < 15000) {
- this.currentImageIndexDownload++;
- this.downloadSpeed();
- } else {
- // We now have average speed and best speed
- let averageSpeed = this.downloadSpeedTotalSpeed / (this.currentImageIndexDownload + 1);
- let bestSpeed = this.downloadSpeedBestSpeed;
- //this.model.downloadSpeed = this.calculateSpeed(duration, this.images[this.currentImageIndexDownload].size);
- //console.log("this.model.downloadSpeed: " + this.model.downloadSpeed);
- }
- });
- }
- uploadSpeed = () => {
- // Size of the string:
- let dataSize = this.images[this.currentImageIndexUpload].size;
- let dataString = "";
- // Build up a string of a specified size
- for (let i = 0; i < dataSize; i++) {
- dataString += "k"; // Add one byte of data
- }
- this.speedTestService.getUploadSpeed(dataString, (duration: number) => {
- console.log("duration: " + duration);
- // Get speed in Mbit/s
- let speed = this.calculateSpeed(duration, dataSize);
- this.uploadSpeedTotalSpeed += speed;
- // If first request, or if this speed is the best so far
- if (speed > this.uploadSpeedBestSpeed || this.uploadSpeedBestSpeed === 0) {
- this.uploadSpeedBestSpeed = speed;
- }
- // Do we need another request?
- if (this.currentImageIndexUpload < (this.images.length-1) && duration < 15000) {
- this.currentImageIndexUpload++;
- this.uploadSpeed();
- } else {
- // We now have average speed and best speed
- let averageSpeed = this.uploadSpeedTotalSpeed / (this.currentImageIndexUpload + 1);
- let bestSpeed = this.uploadSpeedBestSpeed;
- //this.model.uploadSpeed = this.calculateSpeed(duration, dataSize);
- }
- });
- }
- calculateSpeed(duration: number, downloadSize: number) {
- const bitsLoaded = downloadSize * 8;
- const speedBps = (bitsLoaded / (duration / 1000)).toFixed(2);
- const speedKbps = (+speedBps / 1024).toFixed(2);
- const speedMbps = (+speedKbps / 1024).toFixed(2);
- return speedMbps;
- }
- /**
- * Get the users ping speed
- * @param lowestDuration
- */
- pingTest(lowestDuration: number) {
- this.speedTestService.getPing("latency.txt", (duration: number) => {
- if (duration < lowestDuration || lowestDuration === 0) {
- lowestDuration = duration;
- }
- if (new Date().getTime() < this.pingTestStartedAt + 6000) {
- this.pingTest(lowestDuration);
- } else {
- this.model.ping = duration;
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement