java-busy-thread 中 printStackOfThread 函数的性能优化
liuyangc3 opened this issue · 3 comments
liuyangc3 commented
#!/bin/bash
printStackOfThread_new() {
while IFS=" " read -a line ; do
local pid=${line[0]}
local threadId=${line[1]}
local threadId0x=`printf %x ${threadId}`
local user=${line[2]}
local pcpu=${line[4]}
done
}
printStackOfThread() {
while read threadLine ; do
local pid=`echo ${threadLine} | awk '{print $1}'`
local threadId=`echo ${threadLine} | awk '{print $2}'`
local threadId0x=`printf %x ${threadId}`
local user=`echo ${threadLine} | awk '{print $3}'`
local pcpu=`echo ${threadLine} | awk '{print $5}'`
done
}
test() {
ps -Leo pid,lwp,user,comm,pcpu --no-headers| {
[ -z "${pid}" ] && awk '$4=="java"{print $0}'|| awk -v "pid=${pid}" '$1==pid,$4=="java"{print $0}'
} | printStackOfThread
}
test_new() {
ps -Leo pid,lwp,user,comm,pcpu --no-headers| {
[ -z "${pid}" ] && awk '$4=="java"{print $0}'|| awk -v "pid=${pid}" '$1==pid,$4=="java"{print $0}'
} | printStackOfThread_new
}
time test
time test_new
快9倍
real 0m0.361s
user 0m0.013s
sys 0m0.040s
----
real 0m0.043s
user 0m0.012s
sys 0m0.015s
望采纳~
oldratlee commented
Awesome!我看看! 👍
oldratlee commented
oldratlee commented
@liuyangc3 在 PR #50 中已经实现 👍