From e55c2884e2e16340920d062a917caa2ed6c755c8 Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 2 Jan 2020 19:58:53 +0000 Subject: Add --order-by-time option --- .../me/lucko/spark/common/sampler/Sampler.java | 9 ++-- .../spark/common/sampler/ThreadNodeOrder.java | 53 ++++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadNodeOrder.java (limited to 'spark-common/src/main/java/me/lucko/spark/common/sampler') diff --git a/spark-common/src/main/java/me/lucko/spark/common/sampler/Sampler.java b/spark-common/src/main/java/me/lucko/spark/common/sampler/Sampler.java index 033a2d2..81a757a 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/sampler/Sampler.java +++ b/spark-common/src/main/java/me/lucko/spark/common/sampler/Sampler.java @@ -37,6 +37,7 @@ import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; import java.lang.management.ThreadMXBean; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; @@ -158,7 +159,7 @@ public class Sampler implements Runnable { } } - private SamplerData toProto(CommandSender creator) { + private SamplerData toProto(CommandSender creator, Comparator> outputOrder) { SamplerData.Builder proto = SamplerData.newBuilder(); proto.setMetadata(SamplerMetadata.newBuilder() .setUser(creator.toData().toProto()) @@ -170,7 +171,7 @@ public class Sampler implements Runnable { ); List> data = new ArrayList<>(this.dataAggregator.getData().entrySet()); - data.sort(Map.Entry.comparingByKey()); + data.sort(outputOrder); for (Map.Entry entry : data) { proto.addThreads(entry.getValue().toProto()); @@ -179,10 +180,10 @@ public class Sampler implements Runnable { return proto.build(); } - public byte[] formCompressedDataPayload(CommandSender creator) { + public byte[] formCompressedDataPayload(CommandSender creator, Comparator> outputOrder) { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); try (OutputStream out = new GZIPOutputStream(byteOut)) { - toProto(creator).writeTo(out); + toProto(creator, outputOrder).writeTo(out); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadNodeOrder.java b/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadNodeOrder.java new file mode 100644 index 0000000..3765314 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadNodeOrder.java @@ -0,0 +1,53 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.sampler; + +import me.lucko.spark.common.sampler.node.ThreadNode; + +import java.util.Comparator; +import java.util.Map; + +/** + * Methods of ordering {@link ThreadNode}s in the output data. + */ +public enum ThreadNodeOrder implements Comparator> { + + /** + * Order by the name of the thread (alphabetically) + */ + BY_NAME { + @Override + public int compare(Map.Entry o1, Map.Entry o2) { + return o1.getKey().compareTo(o2.getKey()); + } + }, + + /** + * Order by the time taken by the thread + */ + BY_TIME { + @Override + public int compare(Map.Entry o1, Map.Entry o2) { + return Double.compare(o1.getValue().getTotalTime(), o2.getValue().getTotalTime()); + } + } + +} -- cgit