sebhildebrandt / systeminformation

System Information Library for Node.JS
MIT License
2.65k stars 300 forks source link

NetworkStats in Windows 10 #917

Open chinarui-na opened 6 days ago

chinarui-na commented 6 days ago

i use networkStats to get rx_bytes and tx_bytes in windows, Windows11 is fine, Windows10 these values are always 0

logs:

Microsoft Windows [版本 10.0.19045.4529]
(c) Microsoft Corporation。保留所有权利。

D:\java\test>npm install
npm WARN test No description
npm WARN test No repository field.
npm WARN test No license field.

up to date in 0.189s

1 package is looking for funding
  run `npm fund` for details

D:\java\test>node index.js
[
  {
    iface: '以太网',
    operstate: 'unknown',
    rx_bytes: 0,
    rx_dropped: 0,
    rx_errors: 0,
    tx_bytes: 0,
    tx_dropped: 0,
    tx_errors: 0,
    rx_sec: null,
    tx_sec: null,
    ms: 0
  }
]
[
  {
    iface: 'sdpclient',
    ifaceName: 'sdpclient',
    default: false,
    ip4: '1.0.1.3',
    ip4subnet: '255.255.255.0',
    ip6: 'fe80::85f0:5dbf:d4f1:6acf',
    ip6subnet: 'ffff:ffff:ffff:ffff::',
    mac: '00:00:00:00:00:00',
    internal: false,
    virtual: true,
    operstate: 'down',
    type: '',
    duplex: '',
    mtu: '',
    speed: null,
    dhcp: false,
    dnsSuffix: '',
    ieee8021xAuth: 'Unknown',
    ieee8021xState: 'Unknown',
    carrierChanges: 0
  },
  {
    iface: '以太网',
    ifaceName: 'Realtek PCIe GbE Family Controller',
    default: true,
    ip4: '172.20.232.183',
    ip4subnet: '255.255.254.0',
    ip6: 'fe80::b924:2fe1:9e9:bce2',
    ip6subnet: 'ffff:ffff:ffff:ffff::',
    mac: '18:31:bf:15:8a:fc',
    internal: false,
    virtual: false,
    operstate: 'up',
    type: 'wired',
    duplex: '',
    mtu: '',
    speed: 1000,
    dhcp: false,
    dnsSuffix: '',
    ieee8021xAuth: 'Unknown',
    ieee8021xState: 'Unknown',
    carrierChanges: 0
  },
  {
    iface: 'VMware Network Adapter VMnet1',
    ifaceName: 'VMware Virtual Ethernet Adapter for VMnet1',
    default: false,
    ip4: '192.168.254.1',
    ip4subnet: '255.255.255.0',
    ip6: 'fe80::8607:1584:f24d:2c23',
    ip6subnet: 'ffff:ffff:ffff:ffff::',
    mac: '00:50:56:c0:00:01',
    internal: false,
    virtual: true,
    operstate: 'up',
    type: 'wired',
    duplex: '',
    mtu: '',
    speed: 100,
    dhcp: true,
    dnsSuffix: '',
    ieee8021xAuth: 'Unknown',
    ieee8021xState: 'Unknown',
    carrierChanges: 0
  },
  {
    iface: 'VMware Network Adapter VMnet8',
    ifaceName: 'VMware Virtual Ethernet Adapter for VMnet8',
    default: false,
    ip4: '192.168.137.1',
    ip4subnet: '255.255.255.0',
    ip6: 'fe80::fc6b:a4db:a8e8:1c8b',
    ip6subnet: 'ffff:ffff:ffff:ffff::',
    mac: '00:50:56:c0:00:08',
    internal: false,
    virtual: true,
    operstate: 'up',
    type: 'wired',
    duplex: '',
    mtu: '',
    speed: 100,
    dhcp: false,
    dnsSuffix: '',
    ieee8021xAuth: 'Unknown',
    ieee8021xState: 'Unknown',
    carrierChanges: 0
  },
  {
    iface: 'Loopback Pseudo-Interface 1',
    ifaceName: 'Loopback Pseudo-Interface 1',
    default: false,
    ip4: '127.0.0.1',
    ip4subnet: '255.0.0.0',
    ip6: '::1',
    ip6subnet: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
    mac: '00:00:00:00:00:00',
    internal: true,
    virtual: false,
    operstate: 'down',
    type: '',
    duplex: '',
    mtu: '',
    speed: null,
    dhcp: false,
    dnsSuffix: '',
    ieee8021xAuth: 'Unknown',
    ieee8021xState: 'Unknown',
    carrierChanges: 0
  },
  {
    iface: 'WLAN',
    ifaceName: 'Qualcomm Atheros AR956x Wireless Network Adapter',
    default: false,
    ip4: '',
    ip4subnet: '',
    ip6: '',
    ip6subnet: '',
    mac: '',
    internal: undefined,
    virtual: false,
    operstate: 'down',
    type: 'wireless',
    duplex: '',
    mtu: '',
    speed: 0,
    dhcp: true,
    dnsSuffix: '',
    ieee8021xAuth: 'Unknown',
    ieee8021xState: 'Unknown',
    carrierChanges: 0
  }
]

D:\java\test>
chinarui-na commented 6 days ago

1s call once, this is the nodejs code

const si = require('systeminformation');

function formatSpeed(speed) {
    if (speed >= 1024) {
        return `${(speed / 1024).toFixed(0)}MB/s`;
    } else {
        return `${speed.toFixed(0)}kB/s`;
    }
}

function getNetworkInfo() {
    return new Promise(async (resolve, reject) => {
        try {
            const [networkStats, networkInterfaces] = await Promise.all([
                si.networkStats(),
                si.networkInterfaces()
            ]);

            const interfaceTypes = new Map(networkInterfaces.map(iface => [iface.iface, iface.type]));

            const formattedStats = networkStats.map(stat => {
                const downloadSpeed = stat.rx_sec / 1024; // Convert to kB/s
                const uploadSpeed = stat.tx_sec / 1024; // Convert to kB/s
                const type = interfaceTypes.get(stat.iface);

                return {
                    interface: stat.iface,
                    networkType: type === 'wireless' ? 'Wi-Fi' : (type === 'wired' ? 'Wired' : 'Other'),
                    download_kBps: Math.ceil(downloadSpeed),
                    upload_kBps: Math.ceil(uploadSpeed),
                    download_str: formatSpeed(downloadSpeed),
                    upload_str: formatSpeed(uploadSpeed)
                };
            });

            resolve(formattedStats);
        } catch (error) {
            reject(error);
        }
    });
}

getNetworkInfo()
chinarui-na commented 6 days ago

Sometimes it seems, sometimes it doesn't