oldratlee/useful-scripts

java-busy-thread 中 printStackOfThread 函数的性能优化

liuyangc3 opened this issue · 3 comments

#!/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

望采纳~

Awesome!我看看! 👍

@liuyangc3 方便给一个Pull Request 吗?

这样的改进可以直接体现在代码提交中,大家也能看到你的提交。

非常感谢你的贡献~

@liuyangc3 在 PR #50 中已经实现 👍