(bytesPerSecond: number)
| 143 | |
| 144 | // 格式化网络速度 |
| 145 | function formatSpeed(bytesPerSecond: number): string { |
| 146 | if (bytesPerSecond === 0) return "0 bps"; |
| 147 | |
| 148 | // 转换为比特每秒 |
| 149 | const bitsPerSecond = bytesPerSecond * 8; |
| 150 | |
| 151 | const units = [ |
| 152 | { name: "bps", value: 1 }, |
| 153 | { name: "Kbps", value: 1024 }, |
| 154 | { name: "Mbps", value: 1024 * 1024 }, |
| 155 | { name: "Gbps", value: 1024 * 1024 * 1024 }, |
| 156 | ]; |
| 157 | |
| 158 | // 找到最合适的单位 |
| 159 | let unitIndex = 0; |
| 160 | for (let i = units.length - 1; i >= 0; i--) { |
| 161 | if (bitsPerSecond >= units[i].value) { |
| 162 | unitIndex = i; |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | const value = bitsPerSecond / units[unitIndex].value; |
| 168 | |
| 169 | // 根据值的大小决定小数位数 |
| 170 | let decimals = 2; |
| 171 | if (value >= 100) decimals = 1; |
| 172 | if (value >= 1000) decimals = 0; |
| 173 | |
| 174 | return parseFloat(value.toFixed(decimals)) + " " + units[unitIndex].name; |
| 175 | } |
| 176 | |
| 177 | // 格式化运行时间 |
| 178 | function formatUptime(seconds: number): string { |
no outgoing calls
no test coverage detected