diff options
author | Luck <git@lucko.me> | 2018-06-01 12:18:43 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2018-06-01 12:18:43 +0100 |
commit | dca7407ed4ca955b7b692473a6a85263dc6a09f0 (patch) | |
tree | 0c6b9a874f5ca6dc9cc2f86e127db841b4c19a41 | |
parent | e1baaa4d0a0f67be82bb58ad10743d5fe46fd8fc (diff) | |
download | spark-dca7407ed4ca955b7b692473a6a85263dc6a09f0.tar.gz spark-dca7407ed4ca955b7b692473a6a85263dc6a09f0.tar.bz2 spark-dca7407ed4ca955b7b692473a6a85263dc6a09f0.zip |
Improve serialization methods
7 files changed, 59 insertions, 55 deletions
diff --git a/common/src/main/java/me/lucko/spark/common/CommandHandler.java b/common/src/main/java/me/lucko/spark/common/CommandHandler.java index a9bbe61..ed188ea 100644 --- a/common/src/main/java/me/lucko/spark/common/CommandHandler.java +++ b/common/src/main/java/me/lucko/spark/common/CommandHandler.java @@ -3,7 +3,6 @@ package me.lucko.spark.common; import com.google.common.collect.HashMultimap; import com.google.common.collect.SetMultimap; import com.google.common.collect.Sets; -import com.google.gson.JsonObject; import me.lucko.spark.common.http.Bytebin; import me.lucko.spark.profiler.Sampler; @@ -114,6 +113,7 @@ public abstract class CommandHandler<T> { sendMessage(sender, "&b&l> &7/profiler stop"); sendMessage(sender, "&b&l> &7/profiler cancel"); sendMessage(sender, "&b&l> &7/profiler monitoring"); + sendMessage(sender, " &8[&7--threshold&8 <percentage increase>]"); } private void handleStart(T sender, List<String> args) { @@ -266,9 +266,9 @@ public abstract class CommandHandler<T> { private void handleUpload(Sampler sampler) { runAsync(() -> { - JsonObject output = sampler.formOutput(); + byte[] output = sampler.formCompressedDataPayload(); try { - String pasteId = Bytebin.postContent(output); + String pasteId = Bytebin.postCompressedContent(output); sendPrefixedMessage("&bSampling results:"); sendLink(VIEWER_URL + pasteId); } catch (IOException e) { diff --git a/common/src/main/java/me/lucko/spark/common/TickMonitor.java b/common/src/main/java/me/lucko/spark/common/TickMonitor.java index efe79ab..a30a4db 100644 --- a/common/src/main/java/me/lucko/spark/common/TickMonitor.java +++ b/common/src/main/java/me/lucko/spark/common/TickMonitor.java @@ -76,7 +76,7 @@ public abstract class TickMonitor implements Runnable { double percentageChange = (increase * 100d) / this.avg; if (percentageChange > this.percentageChangeThreshold) { sendMessage("&7Tick &8#" + this.tickCounter.getCurrentTick() + " &7lasted &b" + df.format(diff) + "&7 milliseconds. " + - "&7(a &b" + df.format(percentageChange) + "% &7increase from average)"); + "&7(&b" + df.format(percentageChange) + "% &7increase from average)"); } } } diff --git a/common/src/main/java/me/lucko/spark/common/http/Bytebin.java b/common/src/main/java/me/lucko/spark/common/http/Bytebin.java index b2a7749..3cd5e4c 100644 --- a/common/src/main/java/me/lucko/spark/common/http/Bytebin.java +++ b/common/src/main/java/me/lucko/spark/common/http/Bytebin.java @@ -1,8 +1,6 @@ package me.lucko.spark.common.http; import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonElement; import com.google.gson.JsonObject; import okhttp3.MediaType; @@ -12,36 +10,23 @@ import okhttp3.Response; import okhttp3.ResponseBody; import java.io.BufferedReader; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.Writer; import java.nio.charset.StandardCharsets; -import java.util.zip.GZIPOutputStream; /** * Utility for uploading JSON data to bytebin. */ public final class Bytebin { - /** Shared GSON instance */ - private static final Gson GSON = new GsonBuilder().disableHtmlEscaping().create(); /** Media type for JSON data */ private static final MediaType JSON_TYPE = MediaType.parse("application/json; charset=utf-8"); /** The URL used to upload sampling data */ private static final String UPLOAD_ENDPOINT = "https://bytebin.lucko.me/post"; - public static String postContent(JsonElement content) throws IOException { - ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); - try (Writer writer = new OutputStreamWriter(new GZIPOutputStream(byteOut), StandardCharsets.UTF_8)) { - GSON.toJson(content, writer); - } catch (IOException e) { - throw new RuntimeException(e); - } - - RequestBody body = RequestBody.create(JSON_TYPE, byteOut.toByteArray()); + public static String postCompressedContent(byte[] buf) throws IOException { + RequestBody body = RequestBody.create(JSON_TYPE, buf); Request.Builder requestBuilder = new Request.Builder() .url(UPLOAD_ENDPOINT) diff --git a/common/src/main/java/me/lucko/spark/profiler/Sampler.java b/common/src/main/java/me/lucko/spark/profiler/Sampler.java index 44cf445..3476f03 100644 --- a/common/src/main/java/me/lucko/spark/profiler/Sampler.java +++ b/common/src/main/java/me/lucko/spark/profiler/Sampler.java @@ -19,12 +19,16 @@ package me.lucko.spark.profiler; import com.google.common.util.concurrent.ThreadFactoryBuilder; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; +import com.google.gson.stream.JsonWriter; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; import java.lang.management.ThreadMXBean; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -34,6 +38,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; +import java.util.zip.GZIPOutputStream; /** * Main sampler class. @@ -129,25 +134,37 @@ public class Sampler extends TimerTask { } } - public JsonObject formOutput() { - JsonObject out = new JsonObject(); + private void writeOutput(JsonWriter writer) throws IOException { + writer.beginObject(); - JsonArray threads = new JsonArray(); + writer.name("threads").beginArray(); List<Map.Entry<String, StackNode>> data = new ArrayList<>(this.dataAggregator.getData().entrySet()); data.sort(Map.Entry.comparingByKey()); for (Map.Entry<String, StackNode> entry : data) { - JsonObject o = new JsonObject(); - o.addProperty("threadName", entry.getKey()); - o.addProperty("totalTime", entry.getValue().getTotalTime()); - o.add("rootNode", entry.getValue().serialize()); - - threads.add(o); + writer.beginObject(); + writer.name("threadName").value(entry.getKey()); + writer.name("totalTime").value(entry.getValue().getTotalTime()); + writer.name("rootNode"); + entry.getValue().serializeTo(writer); + writer.endObject(); } - out.add("threads", threads); - return out; + writer.endArray(); + writer.endObject(); + } + + public byte[] formCompressedDataPayload() { + ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); + try (Writer writer = new OutputStreamWriter(new GZIPOutputStream(byteOut), StandardCharsets.UTF_8)) { + try (JsonWriter jsonWriter = new JsonWriter(writer)) { + writeOutput(jsonWriter); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + return byteOut.toByteArray(); } } diff --git a/common/src/main/java/me/lucko/spark/profiler/SamplerBuilder.java b/common/src/main/java/me/lucko/spark/profiler/SamplerBuilder.java index 4c16d50..3d304d6 100644 --- a/common/src/main/java/me/lucko/spark/profiler/SamplerBuilder.java +++ b/common/src/main/java/me/lucko/spark/profiler/SamplerBuilder.java @@ -1,7 +1,5 @@ package me.lucko.spark.profiler; -import com.google.common.base.Preconditions; - import java.util.Timer; import java.util.concurrent.TimeUnit; @@ -27,7 +25,9 @@ public class SamplerBuilder { } public SamplerBuilder completeAfter(long timeout, TimeUnit unit) { - Preconditions.checkArgument(timeout > 0, "time > 0"); + if (timeout <= 0) { + throw new IllegalArgumentException("timeout > 0"); + } this.timeout = System.currentTimeMillis() + unit.toMillis(timeout); return this; } diff --git a/common/src/main/java/me/lucko/spark/profiler/StackNode.java b/common/src/main/java/me/lucko/spark/profiler/StackNode.java index b2b6f88..575400a 100644 --- a/common/src/main/java/me/lucko/spark/profiler/StackNode.java +++ b/common/src/main/java/me/lucko/spark/profiler/StackNode.java @@ -18,9 +18,9 @@ package me.lucko.spark.profiler; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; +import com.google.gson.stream.JsonWriter; +import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -112,30 +112,30 @@ public class StackNode implements Comparable<StackNode> { return getName().compareTo(o.getName()); } - public JsonObject serialize() { - JsonObject ret = new JsonObject(); + public void serializeTo(JsonWriter writer) throws IOException { + writer.beginObject(); // append metadata about this node - appendMetadata(ret); + appendMetadata(writer); // include the total time recorded for this node - ret.addProperty("totalTime", getTotalTime()); + writer.name("totalTime").value(getTotalTime()); // append child nodes, if any are present Collection<StackNode> childNodes = getChildren(); if (!childNodes.isEmpty()) { - JsonArray children = new JsonArray(); + writer.name("children").beginArray(); for (StackNode child : childNodes) { - children.add(child.serialize()); + child.serializeTo(writer); } - ret.add("children", children); + writer.endArray(); } - return ret; + writer.endObject(); } - protected void appendMetadata(JsonObject obj) { - obj.addProperty("name", getName()); + protected void appendMetadata(JsonWriter writer) throws IOException { + writer.name("name").value(getName()); } } diff --git a/common/src/main/java/me/lucko/spark/profiler/StackTraceNode.java b/common/src/main/java/me/lucko/spark/profiler/StackTraceNode.java index 7207801..d46a547 100644 --- a/common/src/main/java/me/lucko/spark/profiler/StackTraceNode.java +++ b/common/src/main/java/me/lucko/spark/profiler/StackTraceNode.java @@ -18,7 +18,9 @@ package me.lucko.spark.profiler; -import com.google.gson.JsonObject; +import com.google.gson.stream.JsonWriter; + +import java.io.IOException; /** * Represents a {@link StackNode node} for a method call. @@ -48,17 +50,17 @@ public class StackTraceNode extends StackNode { } public String getClassName() { - return className; + return this.className; } public String getMethodName() { - return methodName; + return this.methodName; } @Override - protected void appendMetadata(JsonObject obj) { - obj.addProperty("className", className); - obj.addProperty("methodName", methodName); + protected void appendMetadata(JsonWriter writer) throws IOException { + writer.name("className").value(this.className); + writer.name("methodName").value(this.methodName); } @Override |