Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
liuyangc3 opened this issue Nov 10, 2015 · 3 comments
Closed

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

liuyangc3 opened this issue Nov 10, 2015 · 3 comments
Assignees

Comments

@liuyangc3
Copy link
Contributor

#!/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
Copy link
Owner

oldratlee commented Nov 10, 2015

Awesome!我看看! 👍

@oldratlee
Copy link
Owner

@liuyangc3 方便给一个Pull Request 吗?

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

非常感谢你的贡献~

@oldratlee
Copy link
Owner

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

@oldratlee oldratlee changed the title java-busy-thread 中 printStackOfThread 函数新实现方式 java-busy-thread 中 printStackOfThread 函数的性能优化 Nov 15, 2015
@oldratlee oldratlee self-assigned this Apr 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants