'Upload speed testing function behaves differently when I call it from electron GUI

I've wrote a simple function to measure the upload speed of my network, which when run inside vscode terminal behaves just fine and displays the expected result. However, after implemeniting the function with electron and calling it when a button is pressed the return value seems 5-7 times its normal value and randomly changes, whereas the original function only changes by 0.02 MB/s max.

The upload speed test function is this;

const {uploadFileToCloud} = require("./app.js")
const {deleteFile} = require("./app.js")
const path = require("path");

  const measureUSpd = async () => {
    var startTime, endTime, payload;
    payload = null
    const file_size = 448800;
    endTime = new Date().getTime()
    await uploadFileToCloud("uploadPayload.txt", (path.resolve("./").replace(/\\/g, "/") +"/" ), "upload measure payload").then(async response => {
    payload = response
    if(typeof(payload) == typeof("")){
        startTime = await new Date().getTime();
        var duration = (startTime - endTime) / 1000;
        var bitsLoaded = file_size * 8;
        global.speedMbps = (bitsLoaded / (duration * 1048576)).toFixed(2);
        deleteFile(payload)
        payload = null
        }
    })
    return global.speedMbps;    

};

module.exports = {
    measureUSpd
}

when I run this piece of code with nodeJS using

const {measureUSpd} = require("./networkSpd");

(async function(){
    let spd = await measureUSpd();
    global.spd = spd
    console.log(spd);
  })();

I get,

PS C:\Users\efeba\Desktop\pythonAndJS\ITGSProjectGUI> node .\test.js
compete file path is C:/Users/efeba/Desktop/pythonAndJS/ITGSProjectGUI/uploadPayload.txt
0.48
 204
PS C:\Users\efeba\Desktop\pythonAndJS\ITGSProjectGUI> node .\test.js
compete file path is C:/Users/efeba/Desktop/pythonAndJS/ITGSProjectGUI/uploadPayload.txt
0.50
 204
PS C:\Users\efeba\Desktop\pythonAndJS\ITGSProjectGUI> node .\test.js
compete file path is C:/Users/efeba/Desktop/pythonAndJS/ITGSProjectGUI/uploadPayload.txt
0.51
 204
PS C:\Users\efeba\Desktop\pythonAndJS\ITGSProjectGUI> node .\test.js
compete file path is C:/Users/efeba/Desktop/pythonAndJS/ITGSProjectGUI/uploadPayload.txt
0.51
 204
PS C:\Users\efeba\Desktop\pythonAndJS\ITGSProjectGUI> node .\test.js
compete file path is C:/Users/efeba/Desktop/pythonAndJS/ITGSProjectGUI/uploadPayload.txt
0.51
 204

The 204 number is the response id from the google drive api deleting the payload, above that is the speed that it measured. I used google drive api v3 to upload the payload to the root folder and to delete it after measurement is done.

This measurement in nodeJS is in line with the measurement in speedtest.net, as shown in here

However, when I use my GUI to invoke the function, I get seemingly off results.as shown here. Both of them were measured at the same time and nothing else was using the same network.

The code from the GUI, the relevant function is at the bottom;

const ipcRenderer = require("electron").ipcRenderer;
const fs = require("fs");
const path = require("path")
const {measureUSpd} = require("./networkSpd")




const FileUploadPing = () => {
  var name = ""+document.getElementById("myFile").files[0].name
  name = name.replace("C:\\fakepath\\","")
  console.log(name)
  var path = document.getElementById("myFile").files[0].path
  path = path.replace(name,"")
  path = path.replaceAll("\\", "/")
  console.log(path)
  var Dname = ""+document.getElementById("Dname").value
  console.log(Dname)
  if (path) {
    const sentPayload = [name, path, Dname] 
    console.log("sent payload = "+sentPayload)
    ipcRenderer.invoke('uploadPing', sentPayload).then((result) => {
      alert("Uploaded file named " + result)
    })

  }else{console.log("no path value")}

};


const CredentialPing = () => {
  var credentials = JSON.parse(fs.readFileSync("./credentials.json"))
  credentials.clientId = document.getElementById("clID").value
  credentials.clientSecret = document.getElementById("clScr").value
  credentials.refToken = document.getElementById("refTkn").value
  const writefile = fs.writeFileSync('./credentials.json', JSON.stringify(credentials), function(err){if(err) console.log(err)});
  if(JSON.parse(fs.readFileSync("./credentials.json")).clientId.length >= 5 && JSON.parse(fs.readFileSync("./credentials.json")).clientSecret.length >= 5 && JSON.parse(fs.readFileSync("./credentials.json")).refToken.length >= 5 ){
    const payload = 1
    ipcRenderer.invoke('entry-close', payload).then((result) => {
    })
    ipcRenderer.invoke('settings-open', payload).then((result) => {
    })
  }else{;
    alert("Please Check Your Input");
  }

};


  const setWinSize = () => {
    const payload = 1
    ipcRenderer.invoke('setWinSize' , payload).then((reult) =>{
    })}

  const setFolder = () => {
    const payload = ""+document.getElementById("folderName").value
    alert(payload)
    ipcRenderer.invoke('setFolder' , payload).then((reult) =>{
    })
    ipcRenderer.invoke('settings-close' , payload).then((reult) =>{
    })
    ipcRenderer.invoke('main-open' , payload).then((reult) =>{
    })
  }
  
  const settings = () => {
    const payload = 1
    ipcRenderer.invoke('main-close' , payload).then((reult) =>{
    })
    ipcRenderer.invoke('settings-open' , payload).then((reult) =>{
    })
  }

  const gotoMain = () => {
    const payload = 1
    ipcRenderer.invoke('settings-close' , payload).then((reult) =>{
    })
    ipcRenderer.invoke('main-open' , payload).then((reult) =>{
    })
  }

const CredentialPingShared = () => {
  var credentials = JSON.parse(fs.readFileSync("./credentials.json"))
  credentials.clientId = (my client id)
  credentials.clientSecret = (my client secret)
  credentials.refToken = (my refresh token)
  const writefile = fs.writeFileSync('./credentials.json', JSON.stringify(credentials), function(err){if(err) console.log(err)});
  if(JSON.parse(fs.readFileSync("./credentials.json")).clientId.length >= 5 && JSON.parse(fs.readFileSync("./credentials.json")).clientSecret.length >= 5 && JSON.parse(fs.readFileSync("./credentials.json")).refToken.length >= 5 ){
    const payload = 1
    ipcRenderer.invoke('entry-close', payload).then((result) => {})
    ipcRenderer.invoke('settings-open', payload).then((result) => {})
  }};


const logOut = () => {
  var credentials = JSON.parse(fs.readFileSync("./credentials.json"))
  credentials.clientId = ""
  credentials.clientSecret = ""
  credentials.refToken = ""
  const writefile = fs.writeFileSync('./credentials.json', JSON.stringify(credentials), function(err){if(err) console.log(err)});
  console.log("Logged Out")
  if(JSON.parse(fs.readFileSync("./credentials.json")).clientId.length == 0 && JSON.parse(fs.readFileSync("./credentials.json")).clientSecret.length == 0 && JSON.parse(fs.readFileSync("./credentials.json")).refToken.length == 0 ){
    const payload = 1
    ipcRenderer.invoke('main-close', payload).then((result) => {
    })
    ipcRenderer.invoke('entry-open', payload).then((result) => {
    })
  }
}

const speed = () => {
  (async function(){
    let spd = await measureUSpd();
    global.spd = spd
    console.log(spd);
    //var txtBox = document.getElementById("upspd");
    //txtBox.value = "Your upload speed is " + global.spd; + "MB/s";
  })();
}

and my index.html file is;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> File Uploader </title>
    <link rel="stylesheet" href="style.css">
    <script defer src="render.js"></script>
</head>
<body>
    
    <h1>Drive File Uploader</h1>
    <input type="file" id="myFile" name="myFile" multiple>
    <input type="text" id="Dname" placeholder="File name (optional)">
    <text id="upspd" name="upspd"></text>
    <button onclick="FileUploadPing()">Upload your file</button>
    <button onclick="speed()">Check upload speed</button>
    <button onclick="logOut()">Log Out</button>
    <button onclick="settings()">Settings</button>
</body>
</html>

Is there anything I can do to make the measurement at least close to the real value?



Solution 1:[1]

As I've learned after some troubleshooting, calling functions inside electron ipc renderer will not work properly, as of my code, the file was uploading but the content of the file was empty, resulting in a lesser upload time. I've transferred the function from its own file to app.js, where the other functions were being called from, and made an event handler for the ipc renderer.

So, if I can help anyone, my project folder is looking like this,

1) MAIN-FOLDER
   1) APP.JS (hosts all the functions I call from the rendered includin the measurement function)
   2) MAIN.JS (hosts all the `html` files and events I call from the renderer)
   3) RENDER.JS (where I make my event handling)
2) HTML
   1) CREDENTIAL.HTML
   2) SETTINGS.HTML
   3) INDEX.HTML(my main screen)
3)SOME OTHER STUFF

TLDR; I was trying to put one of the functions that should be in app.js inside render.js and, for some reason, it won’t work like that.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1