当我从电子 GUI 调用上传速度测试功能时,它的行为有所不同

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

我写了一个简单的函数来测量我的网络的上传速度,当 运行 在 vscode 终端内时,它表现得很好并显示预期的结果。然而,在用 electron 实现函数并在按下按钮时调用它之后,return 值似乎是其正常值的 5-7 倍并且随机变化,而原始函数仅最大变化 0.02 MB/s。

上传速度测试功能是这个;

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
}

当我运行这段代码用nodeJS使用

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

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

我明白了,

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

204 数字是来自 google 驱动器 api 删除有效负载的响应 ID,上面是它测量的速度。我使用 google 驱动器 api v3 将 payload 上传到根文件夹并在测量完成后将其删除。

nodeJS 中的测量与 speedtest.net 中的测量一致, as shown in here

但是,当我使用我的 GUI 调用该函数时,我得到的结果似乎不对。as shown here.它们都是同时测量的,没有其他任何东西使用同一个网络。

GUI中的代码,相关函数在底部;

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";
  })();
}

我的 index.html 文件是;

<!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>

我能做些什么来使测量值至少接近真实值吗?

正如我在一些故障排除后了解到的那样,在 electron ipc 渲染器中调用函数将无法正常工作,根据我的代码,文件正在上传但文件内容为空,导致更少的上传时间。我已将该函数从它自己的文件转移到 app.js,从中调用其他函数,并为 ipc 渲染器创建了一个事件处理程序。

所以,如果我能帮助任何人,我的项目文件夹是这样的,

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; 我试图把应该在 app.js 中的函数之一放在 render.js 而且,出于某种原因,它不会那样工作。