StrokesPlus.net
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
randomConstant  
#1 Posted : Monday, December 6, 2021 9:09:30 AM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
Hi all,

I often open task manager and go to performance tab just to see network activity, sometimes even open resource monitor to see which process is using internet.

I know of a program called NetSpeedMonitor which displays an upload/download reading on the taskbar near tray icons. I've even thought of installing it in the past but decided against it since its not a portable program and I do not need to see the speed 24/7, just sometimes.

I was wondering if S+ could fetch and display network speed readings using a script, and if that would be ok performance wise. Any ideas or code is much appreciated.

Thanks
Rob  
#2 Posted : Monday, December 6, 2021 2:54:03 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,359
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 419 time(s) in 356 post(s)
Sure, here's an example which outputs to the console.

Note that to get speed, you have to sample at two points in time and calculate the difference. This just uses sp.Pause(1) to wait one second.
Code:
// Alias namespaces to make code shorter/easier to read
var netinfo = System.Net.NetworkInformation;
var netint = System.Net.NetworkInformation.NetworkInterface;

if (netint.GetIsNetworkAvailable())
{
    StrokesPlus.Console.Log("Checking network speed for all interfaces...\n");
    var output = "\n";
    var interfaces = netint.GetAllNetworkInterfaces();

    // For each network interface
    for (var ni = 0; ni < interfaces.Length; ni++) {   

        // Only those which are up, not tunnel, and not local loopback
        if(interfaces[ni].OperationalStatus == netinfo.OperationalStatus.Up
           && interfaces[ni].NetworkInterfaceType != netinfo.NetworkInterfaceType.Tunnel 
           && interfaces[ni].NetworkInterfaceType != netinfo.NetworkInterfaceType.Loopback) 
        {
            var adapter = interfaces[ni].GetIPProperties(); 
            var addresses = adapter.UnicastAddresses.ToArray();

            // If there are no unicast addresses, move to next
            if(addresses.Length == 0) {
                continue;
            }
            output += "----------------------------------------------------\n";
            
            //Print connection name and IP
            output += `Name: ${interfaces[ni].Description}\n`;
            for (var ip = 0; ip < addresses.Length; ip++)
            {
                if (addresses[ip].Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    output += `IP: ${addresses[ip].Address.ToString()}\n`;
                }
            }

            // Get sent/received byte data
            var netstats = interfaces[ni].GetIPv4Statistics();
            var startBytesSent = netstats.BytesSent;
            var startBytesRec = netstats.BytesReceived;

            //Wait 1 second to compare results for speed
            sp.Pause(1);

            // Get latest sent/receieved bytes and calculate/format speed
            netstats = interfaces[ni].GetIPv4Statistics();

            //Convert total bytes to KB and add commas for large numbers
            var bytesSentFormatted = (netstats.BytesSent / 1024).toFixed(0).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
            var bytesReceivedFormatted = (netstats.BytesReceived / 1024).toFixed(0).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');

            //Calculate speed in KB/s, only to last 2 decimals
            var bytesSentSpeedFormatted = ((netstats.BytesSent - startBytesSent) / 1024).toFixed(2);
            var bytesReceivedSpeedFormatted = ((netstats.BytesReceived - startBytesRec) / 1024).toFixed(2);

            //Print formatted bytes/speed
            output += `Bytes Sent: ${bytesSentFormatted} KB (${bytesSentSpeedFormatted} KB/s)\n`;
            output += `Bytes Received: ${bytesReceivedFormatted} KB (${bytesReceivedSpeedFormatted} KB/s)\n`;
        }
    }

    StrokesPlus.Console.Log(output);
}

Example output:
Code:
2021-12-06 09:51:48.72: Checking network speed for all interfaces...

2021-12-06 09:51:51.81: 
----------------------------------------------------
Name: Killer E2500 Gigabit Ethernet Controller
IP: 192.168.1.121
Bytes Sent: 2,133,277,477 KB (14.13 KB/s)
Bytes Received: 260,396,937 KB (34.24 KB/s)
----------------------------------------------------
Name: VMware Virtual Ethernet Adapter for VMnet1
IP: 192.168.68.1
Bytes Sent: 338 KB (0.00 KB/s)
Bytes Received: 0 KB (0.00 KB/s)
----------------------------------------------------
Name: VMware Virtual Ethernet Adapter for VMnet8
IP: 192.168.37.1
Bytes Sent: 341 KB (0.00 KB/s)
Bytes Received: 95 KB (0.00 KB/s)
randomConstant  
#3 Posted : Monday, December 6, 2021 4:19:24 PM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
Thanks a lot Rob.

Works very well Laugh
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.