From 43e96004d3cc1d6545127d45d78a0cd8fb481f4a Mon Sep 17 00:00:00 2001 From: Anthony Date: Tue, 21 Sep 2021 19:07:57 +0300 Subject: [PATCH] saving download speed and updating it every 30s --- ui/analytics.js | 12 +++-- ui/util/detect-user-bandwidth.js | 80 ++++++-------------------------- 2 files changed, 20 insertions(+), 72 deletions(-) diff --git a/ui/analytics.js b/ui/analytics.js index 505fb1180..ec96f4bbf 100644 --- a/ui/analytics.js +++ b/ui/analytics.js @@ -12,11 +12,13 @@ import { generateInitialUrl } from 'util/url'; import { MATOMO_ID, MATOMO_URL } from 'config'; import getConnectionSpeed from 'util/detect-user-bandwidth'; -let downloadSpeed; -getConnectionSpeed(function(speedInMbps){ - downloadSpeed = speedInMbps; - console.log(downloadSpeed); -}); +let userDownloadBandwidth; +async function getUserBandwidth() { + userDownloadBandwidth = await getConnectionSpeed(); +} + +getUserBandwidth(); +setInterval(getUserBandwidth, 1000 * 30); const isProduction = process.env.NODE_ENV === 'production'; const devInternalApis = process.env.LBRY_API_URL && process.env.LBRY_API_URL.includes('dev'); diff --git a/ui/util/detect-user-bandwidth.js b/ui/util/detect-user-bandwidth.js index bc41d3e62..353579ee3 100644 --- a/ui/util/detect-user-bandwidth.js +++ b/ui/util/detect-user-bandwidth.js @@ -1,76 +1,22 @@ -// var startTime; -// var endTime; - -var testConnectionSpeed = { - imageAddr: 'https://upload.wikimedia.org/wikipedia/commons/a/a6/Brandenburger_Tor_abends.jpg', // this is just an example, you rather want an image hosted on your server - downloadSize: 2707459, // this must match with the image above - - getConnectionSpeed: function(callback) { - testConnectionSpeed.InitiateSpeedDetection(); - testConnectionSpeed.callback = callback; - }, - InitiateSpeedDetection: function() { - window.setTimeout(testConnectionSpeed.MeasureConnectionSpeed, 1); - }, - - result: function() { - var duration = (endTime - startTime) / 1000; - var bitsLoaded = testConnectionSpeed.downloadSize * 8; - var speedBps = (bitsLoaded / duration).toFixed(2); - var speedKbps = (speedBps / 1024).toFixed(2); - var speedMbps = (speedKbps / 1024).toFixed(2); - testConnectionSpeed.callback(speedMbps); - }, - - MeasureConnectionSpeed: function() { - var download = new Image(); - download.onload = function() { - endTime = (new Date()).getTime(); - testConnectionSpeed.result(); - }; - startTime = (new Date()).getTime(); - var cacheBuster = '?nnn=' + startTime; - download.src = testConnectionSpeed.imageAddr + cacheBuster; - }, -}; - -function timeout(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); -} - const imageAddr = 'https://upload.wikimedia.org/wikipedia/commons/a/a6/Brandenburger_Tor_abends.jpg'; const downloadSize = 2707459; // this must match with the image above let startTime, endTime; -function measureConnectionSpeed() { +async function measureConnectionSpeed() { startTime = (new Date()).getTime(); - var cacheBuster = '?nnn=' + startTime; + const cacheBuster = '?nnn=' + startTime; - var download = new Image(); + const download = new Image(); download.src = imageAddr + cacheBuster; - - download.onload = function() { - endTime = (new Date()).getTime(); - var duration = (endTime - startTime) / 1000; - var bitsLoaded = downloadSize * 8; - var speedBps = (bitsLoaded / duration).toFixed(2); - var speedKbps = (speedBps / 1024).toFixed(2); - var speedMbps = (speedKbps / 1024).toFixed(2); - console.log(speedMbps); - return new Promise(resolve => resolve(speedMbps)); - // return speedMbps; - }; - - + // this returns when the image is finished downloading + await download.decode(); + endTime = (new Date()).getTime(); + const duration = (endTime - startTime) / 1000; + const bitsLoaded = downloadSize * 8; + const speedBps = (bitsLoaded / duration).toFixed(2); + const speedKbps = (speedBps / 1024).toFixed(2); + const speedMbps = (speedKbps / 1024).toFixed(2); + return speedMbps; } -async function getDownloadSpeed() { - await timeout(1); - const downloadSpeed = await measureConnectionSpeed(); - console.log(downloadSpeed); -} - -// start test immediatly, you could also call this on any event or whenever you want -// testConnectionSpeed.getConnectionSpeed(function(time) { console.log(time) }); - -module.exports = testConnectionSpeed.getConnectionSpeed; +module.exports = measureConnectionSpeed;