Skip to content

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

Description

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

望采纳~

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions