From aaba169f2f863584dbc37ce6bb17389c45a0aed6 Mon Sep 17 00:00:00 2001 From: Chuck Date: Sun, 21 Feb 2021 12:26:19 +0800 Subject: [PATCH] optimize the performance of obtaining CPU usage (#6399) Preformance Test (on NanoPi R2s, repeat 1000 times): old command cpu_usage=$(expr 100 - $(cat /tmp/top_tmp | grep 'CPU:' | awk -F '%' '{print$4}' | awk -F ' ' '{print$2}')) ``` real 0m 14.25s user 0m 7.96s sys 0m 20.33s ``` new command cpu_usage=$(cat /tmp/top_tmp | awk '/^CPU/ { printf("%d%%", 100 - $8) }') ``` real 0m 6.91s user 0m 4.29s sys 0m 6.06s ``` Signed-off-by: Chuck --- package/lean/autocore/files/arm/index.htm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/lean/autocore/files/arm/index.htm b/package/lean/autocore/files/arm/index.htm index 283339443..4f884744e 100644 --- a/package/lean/autocore/files/arm/index.htm +++ b/package/lean/autocore/files/arm/index.htm @@ -48,7 +48,7 @@ local user_info = luci.sys.exec("cat /proc/net/arp | grep 'br-lan' | grep '0x2' | wc -l") - local cpu_usage = (luci.sys.exec("expr 100 - $(top -n 1 | grep 'CPU:' | awk -F '%' '{print$4}' | awk -F ' ' '{print$2}')") or "6") .. "%" + local cpu_usage = luci.sys.exec("top -n1 | awk '/^CPU/ { printf(\"%d%%\", 100 - $8) }'") or "6%" local cpu_info = luci.sys.exec("/sbin/cpuinfo") or "ARM Processor x 0 (233MHz, 2.3°C)" local rv = {