No internet connection
  1. Home
  2. How to

Internet speed test - How to use a specific piece of text within a phrase as a variable

By Andrew Sherman @Andrew_Sherman
    2022-04-09 19:03:00.956Z

    I'm trying to set up a script that automates internet speed tests (setting up a new network - so much testing!). It's straightforward using the Speedtest command line. The process runs in the background and gives a result.

    How do I get Soundflow to interpret the output and show me only the variables I'm looking for?

    We can assume the format/syntax of the output will always be the same, but with different number values. For example, it gives me the following result after running the process:

    Retrieving speedtest.net configuration...
    Testing from Virgin Media Ireland (109.255.241.176)...
    Retrieving speedtest.net server list...
    Selecting best server based on ping...
    Hosted by Three Ireland (Dublin) [1.07 km]: 15.46 ms
    Testing download speed................................................................................
    Download: 859.37 Mbit/s
    Testing upload speed......................................................................................................
    Upload: 49.08 Mbit/s
    

    All I want to see is:

    Download: 859
    Upload: 49
    
    Solved in post #3, click to view
    • 3 replies
    1. A
      Andrew Sherman @Andrew_Sherman
        2022-04-09 19:11:21.494Z

        Ok I've partially answered my own question, you can simplify the result in the command line.
        The command line process is:

         speedtest-cli --simple 
        

        Which gives:

        Ping: 17.686 ms
        Download: 541.13 Mbit/s
        Upload: 22.49 Mbit/s
        
        1. In reply toAndrew_Sherman:

          Try something like this:

          
          let resultLines = sf.system.exec({
              commandLine: `speedtest-cli`
          }).result.split(/\n/g).map(l => l.trim());
          
          var downloadLine = resultLines.filter(l => l.startsWith("Download:"))[0];
          var uploadLine = resultLines.filter(l => l.startsWith("Upload:"))[0];
          
          var downloadSpeed = Number(downloadLine.split(':')[1].trim().split('Mbit')[0].trim());
          var uploadSpeed = Number(uploadLine.split(':')[1].trim().split('Mbit')[0].trim());
          
          log({
              downloadSpeed,
              uploadSpeed,
          });
          
          Reply1 LikeSolution
          1. AAndrew Sherman @Andrew_Sherman
              2022-04-09 19:36:16.894Z

              Thanks very much Christian. Those Javascript tricks for working with text are so useful.