Reduce small objects generated by dashboard commands (#1533) #1542

pull/1543/head
gongdewei 4 years ago committed by GitHub
parent b628eb7e2e
commit 832573e1a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -306,8 +306,8 @@ public class DashboardCommand extends AnnotatedCommand {
DashboardModel dashboardModel = new DashboardModel(); DashboardModel dashboardModel = new DashboardModel();
//thread sample //thread sample
Map<String, ThreadVO> threads = ThreadUtil.getThreads(); List<ThreadVO> threads = ThreadUtil.getThreads();
dashboardModel.setThreads(threadSampler.sample(threads.values())); dashboardModel.setThreads(threadSampler.sample(threads));
//memory //memory
addMemoryInfo(dashboardModel); addMemoryInfo(dashboardModel);

@ -129,7 +129,7 @@ public class ThreadCommand extends AnnotatedCommand {
} }
private ExitStatus processAllThreads(CommandProcess process) { private ExitStatus processAllThreads(CommandProcess process) {
Map<String, ThreadVO> threads = ThreadUtil.getThreads(); List<ThreadVO> threads = ThreadUtil.getThreads();
// 统计各种线程状态 // 统计各种线程状态
Map<State, Integer> stateCountMap = new LinkedHashMap<State, Integer>(); Map<State, Integer> stateCountMap = new LinkedHashMap<State, Integer>();
@ -137,7 +137,7 @@ public class ThreadCommand extends AnnotatedCommand {
stateCountMap.put(s, 0); stateCountMap.put(s, 0);
} }
for (ThreadVO thread : threads.values()) { for (ThreadVO thread : threads) {
State threadState = thread.getState(); State threadState = thread.getState();
Integer count = stateCountMap.get(threadState); Integer count = stateCountMap.get(threadState);
stateCountMap.put(threadState, count + 1); stateCountMap.put(threadState, count + 1);
@ -149,7 +149,7 @@ public class ThreadCommand extends AnnotatedCommand {
this.state = this.state.toUpperCase(); this.state = this.state.toUpperCase();
if (states.contains(this.state)) { if (states.contains(this.state)) {
includeInternalThreads = false; includeInternalThreads = false;
for (ThreadVO thread : threads.values()) { for (ThreadVO thread : threads) {
if (thread.getState() != null && state.equals(thread.getState().name())) { if (thread.getState() != null && state.equals(thread.getState().name())) {
resultThreads.add(thread); resultThreads.add(thread);
} }
@ -158,7 +158,7 @@ public class ThreadCommand extends AnnotatedCommand {
return ExitStatus.failure(1, "Illegal argument, state should be one of " + states); return ExitStatus.failure(1, "Illegal argument, state should be one of " + states);
} }
} else { } else {
resultThreads = threads.values(); resultThreads = threads;
} }
//thread stats //thread stats
@ -183,9 +183,9 @@ public class ThreadCommand extends AnnotatedCommand {
private ExitStatus processTopBusyThreads(CommandProcess process) { private ExitStatus processTopBusyThreads(CommandProcess process) {
ThreadSampler threadSampler = new ThreadSampler(); ThreadSampler threadSampler = new ThreadSampler();
threadSampler.sample(ThreadUtil.getThreads().values()); threadSampler.sample(ThreadUtil.getThreads());
threadSampler.pause(sampleInterval); threadSampler.pause(sampleInterval);
List<ThreadVO> threadStats = threadSampler.sample(ThreadUtil.getThreads().values()); List<ThreadVO> threadStats = threadSampler.sample(ThreadUtil.getThreads());
int limit = Math.min(threadStats.size(), topNBusy); int limit = Math.min(threadStats.size(), topNBusy);
List<ThreadVO> topNThreads = threadStats.subList(0, limit); List<ThreadVO> topNThreads = threadStats.subList(0, limit);

@ -123,6 +123,7 @@ public class ViewRenderUtil {
) )
); );
int count = 0;
for (ThreadVO thread : threads) { for (ThreadVO thread : threads) {
Color color = colorMapping.get(thread.getState()); Color color = colorMapping.get(thread.getState());
String time = formatTimeMills(thread.getTime()); String time = formatTimeMills(thread.getTime());
@ -151,6 +152,9 @@ public class ViewRenderUtil {
new LabelElement(thread.isInterrupted()), new LabelElement(thread.isInterrupted()),
daemonLabel daemonLabel
); );
if (++count >= height) {
break;
}
} }
return RenderUtil.render(table, width, height); return RenderUtil.render(table, width, height);
} }
@ -159,13 +163,33 @@ public class ViewRenderUtil {
long seconds = timeMills / 1000; long seconds = timeMills / 1000;
long mills = timeMills % 1000; long mills = timeMills % 1000;
long min = seconds / 60; long min = seconds / 60;
//return min + ":" + (seconds % 60); seconds = seconds % 60;
return String.format("%d:%d.%03d", min, seconds, mills);
//return String.format("%d:%d.%03d", min, seconds, mills);
String str;
if (mills >= 100) {
str = min + ":" + seconds + "." + mills;
} else if (mills >= 10) {
str = min + ":" + seconds + ".0" + mills;
} else {
str = min + ":" + seconds + ".00" + mills;
}
return str;
} }
private static String formatTimeMillsToSeconds(long timeMills) { private static String formatTimeMillsToSeconds(long timeMills) {
long seconds = timeMills / 1000; long seconds = timeMills / 1000;
long mills = timeMills % 1000; long mills = timeMills % 1000;
return String.format("%d.%03d", seconds, mills);
//return String.format("%d.%03d", seconds, mills);
String str;
if (mills >= 100) {
str = seconds + "." + mills;
} else if (mills >= 10) {
str = seconds + ".0" + mills;
} else {
str = seconds + ".00" + mills;
}
return str;
} }
} }

@ -449,10 +449,14 @@ public abstract class StringUtils {
public static String replace(String inString, String oldPattern, String newPattern) { public static String replace(String inString, String oldPattern, String newPattern) {
if(hasLength(inString) && hasLength(oldPattern) && newPattern != null) { if(hasLength(inString) && hasLength(oldPattern) && newPattern != null) {
StringBuilder sb = new StringBuilder();
int pos = 0; int pos = 0;
int index = inString.indexOf(oldPattern); int index = inString.indexOf(oldPattern);
if (index < 0) {
//no need to replace
return inString;
}
StringBuilder sb = new StringBuilder();
for(int patLen = oldPattern.length(); index >= 0; index = inString.indexOf(oldPattern, pos)) { for(int patLen = oldPattern.length(); index >= 0; index = inString.indexOf(oldPattern, pos)) {
sb.append(inString.substring(pos, index)); sb.append(inString.substring(pos, index));
sb.append(newPattern); sb.append(newPattern);

@ -36,29 +36,22 @@ abstract public class ThreadUtil {
} }
/** /**
* 线Map线Name-IDkey * 线
*
* @return
*/ */
public static Map<String, ThreadVO> getThreads() { public static List<ThreadVO> getThreads() {
ThreadGroup root = getRoot(); ThreadGroup root = getRoot();
Thread[] threads = new Thread[root.activeCount()]; Thread[] threads = new Thread[root.activeCount()];
while (root.enumerate(threads, true) == threads.length) { while (root.enumerate(threads, true) == threads.length) {
threads = new Thread[threads.length * 2]; threads = new Thread[threads.length * 2];
} }
SortedMap<String, ThreadVO> map = new TreeMap<String, ThreadVO>(new Comparator<String>() { List<ThreadVO> list = new ArrayList<ThreadVO>(threads.length);
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
for (Thread thread : threads) { for (Thread thread : threads) {
if (thread != null) { if (thread != null) {
ThreadVO threadVO = createThreadVO(thread); ThreadVO threadVO = createThreadVO(thread);
map.put(thread.getName() + "-" + thread.getId(), threadVO); list.add(threadVO);
} }
} }
return map; return list;
} }
private static ThreadVO createThreadVO(Thread thread) { private static ThreadVO createThreadVO(Thread thread) {

Loading…
Cancel
Save