Rank: Advanced Member
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
|
|
|
|
Rank: Administration
Groups: Translators, Members, Administrators Joined: 1/11/2018(UTC) Posts: 1,360 Location: Tampa, FL Thanks: 28 times Was thanked: 421 time(s) in 357 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)
|
|
|
|
Rank: Advanced Member
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
|
|
|
|
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.
Important Information:
The StrokesPlus.net Forum uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close