Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Component for handling the speed test.
  3.  */
  4.  
  5. import { Component, OnInit } from '@angular/core';
  6. import { SpeedTestService } from '../../services/speed-test.service';
  7.  
  8. @Component({
  9.   selector: 'app-speed-test-page',
  10.   templateUrl: './speed-test-page.component.html',
  11.   styleUrls: ['./speed-test-page.component.css'],
  12.   providers: [SpeedTestService]
  13. })
  14. export class SpeedTestPageComponent implements OnInit {
  15.  
  16.   // Object to hold data to send to database:
  17.   model: any = {};
  18.  
  19.   currentImageIndexDownload = 0;
  20.   currentImageIndexUpload = 0;
  21.   images = [
  22.     {'url': '350x350', 'size': 240000},
  23.     {'url': '500x500', 'size': 494000},
  24.     {'url': '750x750', 'size': 1100000},
  25.     {'url': '1000x1000', 'size': 1900000},
  26.     {'url': '1500x1500', 'size': 4300000},
  27.     {'url': '2000x2000', 'size': 7500000},
  28.     {'url': '2500x2500', 'size': 12000000},
  29.     {'url': '3000x3000', 'size': 17000000},
  30.     {'url': '3500x3500', 'size': 23000000},
  31.     {'url': '4000x4000', 'size': 30000000},
  32.   ];
  33.  
  34.   pingTestStartedAt: number;
  35.   uploadSpeedTotalSpeed: number;
  36.   uploadSpeedBestSpeed: number;
  37.   downloadSpeedTotalSpeed: number;
  38.   downloadSpeedBestSpeed: number;
  39.  
  40.   //data: string;
  41.  
  42.   constructor(private speedTestService: SpeedTestService) {
  43.  
  44.   }
  45.  
  46.   ngOnInit() {
  47.     //this.getLocation();
  48.     //this.getIPandISP();
  49.  
  50.     //this.downloadSpeed();
  51.     this.uploadSpeedTotalSpeed = 0;
  52.     this.uploadSpeedBestSpeed = 0;
  53.     this.downloadSpeedTotalSpeed = 0;
  54.     this.downloadSpeedBestSpeed = 0;
  55.     this.uploadSpeed();
  56.  
  57.     //this.pingTestStartedAt = new Date().getTime();
  58.     //this.pingTest(0);
  59.   }
  60.  
  61.   /**
  62.    * Get the position of the user in latitude and longitude.
  63.    * If geolocation is supported, it sends the position to storeCurrentPosition.
  64.    * If not supported, it shows a message about this to the user.
  65.    * The code is inspired from: https://www.w3schools.com/html/html5_geolocation.asp
  66.    */
  67.   getLocation() {
  68.     if (navigator.geolocation) {
  69.       navigator.geolocation.getCurrentPosition(this.storeCurrentPosition);
  70.     } else {
  71.       document.getElementById('currentPositionTextArea').innerHTML =
  72.         'Geografisk posisjonering er ikke støttet av nettleseren. Vennligst skriv inn din adresse.';
  73.     }
  74.   }
  75.  
  76.   /**
  77.    * Stores latitude and longitude to the object.
  78.    */
  79.   storeCurrentPosition = (position) => {
  80.     this.model.y_coord = position.coords.latitude;
  81.     this.model.x_coord = position.coords.longitude;
  82.   }
  83.  
  84.   /**
  85.    * Get the IP address and ISP provider to the user and stores it to the object.
  86.    */
  87.   getIPandISP() {
  88.     this.speedTestService.getIPandISP().subscribe(response => {
  89.       this.model.ip = response.query;
  90.       this.model.isp = response.isp;
  91.     });
  92.   }
  93.  
  94.   downloadSpeed = () => {
  95.     this.speedTestService.getDownloadSpeed(this.images[this.currentImageIndexDownload].url, (duration: number) => {
  96.  
  97.       console.log('currentImageIndexDownload: ' + this.currentImageIndexDownload);
  98.       console.log('duration: ' + duration);
  99.  
  100.       // Get speed in Mbit/s
  101.       let speed = this.calculateSpeed(duration, this.images[this.currentImageIndexDownload].size);
  102.       this.downloadSpeedTotalSpeed += speed;
  103.  
  104.       // If first request, or if this speed is the best so far
  105.       if (speed > this.downloadSpeedBestSpeed || this.downloadSpeedBestSpeed === 0) {
  106.         this.downloadSpeedBestSpeed = speed;
  107.       }
  108.  
  109.       // Do we need another request?
  110.       if (this.currentImageIndexDownload < (this.images.length-1) && duration < 15000) {
  111.         this.currentImageIndexDownload++;
  112.         this.downloadSpeed();
  113.       } else {
  114.         // We now have average speed and best speed
  115.         let averageSpeed = this.downloadSpeedTotalSpeed / (this.currentImageIndexDownload + 1);
  116.         let bestSpeed = this.downloadSpeedBestSpeed;
  117.        
  118.         //this.model.downloadSpeed = this.calculateSpeed(duration, this.images[this.currentImageIndexDownload].size);
  119.         //console.log("this.model.downloadSpeed: " + this.model.downloadSpeed);
  120.       }
  121.     });
  122.   }
  123.  
  124.   uploadSpeed = () => {
  125.     // Size of the string:
  126.     let dataSize = this.images[this.currentImageIndexUpload].size;
  127.     let dataString = "";
  128.     // Build up a string of a specified size
  129.     for (let i = 0; i < dataSize; i++) {
  130.      dataString += "k";  // Add one byte of data
  131.     }
  132.  
  133.     this.speedTestService.getUploadSpeed(dataString, (duration: number) => {
  134.       console.log("duration: " + duration);
  135.      
  136.       // Get speed in Mbit/s
  137.       let speed = this.calculateSpeed(duration, dataSize);
  138.       this.uploadSpeedTotalSpeed += speed;
  139.      
  140.       // If first request, or if this speed is the best so far
  141.       if (speed > this.uploadSpeedBestSpeed || this.uploadSpeedBestSpeed === 0) {
  142.         this.uploadSpeedBestSpeed = speed;
  143.       }
  144.  
  145.       // Do we need another request?
  146.       if (this.currentImageIndexUpload < (this.images.length-1) && duration < 15000) {
  147.         this.currentImageIndexUpload++;      
  148.         this.uploadSpeed();
  149.       } else {
  150.         // We now have average speed and best speed
  151.         let averageSpeed = this.uploadSpeedTotalSpeed / (this.currentImageIndexUpload + 1);
  152.         let bestSpeed = this.uploadSpeedBestSpeed;
  153.         //this.model.uploadSpeed = this.calculateSpeed(duration, dataSize);
  154.       }
  155.     });
  156.   }
  157.  
  158.  
  159.   calculateSpeed(duration: number, downloadSize: number) {
  160.     const bitsLoaded = downloadSize * 8;
  161.     const speedBps = (bitsLoaded / (duration / 1000)).toFixed(2);
  162.     const speedKbps = (+speedBps / 1024).toFixed(2);
  163.     const speedMbps = (+speedKbps / 1024).toFixed(2);
  164.  
  165.     return speedMbps;
  166.   }
  167.  
  168.   /**
  169.    * Get the users ping speed
  170.    * @param lowestDuration
  171.    */
  172.   pingTest(lowestDuration: number) {
  173.     this.speedTestService.getPing("latency.txt", (duration: number) => {
  174.       if (duration < lowestDuration || lowestDuration === 0) {
  175.         lowestDuration = duration;
  176.       }
  177.       if (new Date().getTime() < this.pingTestStartedAt + 6000) {
  178.         this.pingTest(lowestDuration);
  179.       } else {
  180.         this.model.ping = duration;
  181.       }
  182.     });
  183.   }
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement