diff --git a/metrics/pom.xml b/metrics/pom.xml index bd04d4f8..6b49a59f 100644 --- a/metrics/pom.xml +++ b/metrics/pom.xml @@ -39,7 +39,7 @@ com.google.caliper caliper - 0.5-rc1 + 1.0-beta-3 diff --git a/metrics/src/main/java/com/google/gson/metrics/BagOfPrimitivesDeserializationBenchmark.java b/metrics/src/main/java/com/google/gson/metrics/BagOfPrimitivesDeserializationBenchmark.java index 8e6ea2b2..80881dc8 100644 --- a/metrics/src/main/java/com/google/gson/metrics/BagOfPrimitivesDeserializationBenchmark.java +++ b/metrics/src/main/java/com/google/gson/metrics/BagOfPrimitivesDeserializationBenchmark.java @@ -15,15 +15,13 @@ */ package com.google.gson.metrics; +import com.google.caliper.BeforeExperiment; +import com.google.gson.Gson; +import com.google.gson.stream.JsonReader; import java.io.IOException; import java.io.StringReader; import java.lang.reflect.Field; -import com.google.caliper.Runner; -import com.google.caliper.SimpleBenchmark; -import com.google.gson.Gson; -import com.google.gson.stream.JsonReader; - /** * Caliper based micro benchmarks for Gson * @@ -31,17 +29,17 @@ import com.google.gson.stream.JsonReader; * @author Jesse Wilson * @author Joel Leitch */ -public class BagOfPrimitivesDeserializationBenchmark extends SimpleBenchmark { +public class BagOfPrimitivesDeserializationBenchmark { private Gson gson; private String json; public static void main(String[] args) { - Runner.main(BagOfPrimitivesDeserializationBenchmark.class, args); + NonUploadingCaliperRunner.run(BagOfPrimitivesDeserializationBenchmark.class, args); } - @Override - protected void setUp() throws Exception { + @BeforeExperiment + void setUp() throws Exception { this.gson = new Gson(); BagOfPrimitives bag = new BagOfPrimitives(10L, 1, false, "foo"); this.json = gson.toJson(bag); diff --git a/metrics/src/main/java/com/google/gson/metrics/CollectionsDeserializationBenchmark.java b/metrics/src/main/java/com/google/gson/metrics/CollectionsDeserializationBenchmark.java index 09a5782a..57b5d630 100644 --- a/metrics/src/main/java/com/google/gson/metrics/CollectionsDeserializationBenchmark.java +++ b/metrics/src/main/java/com/google/gson/metrics/CollectionsDeserializationBenchmark.java @@ -15,6 +15,10 @@ */ package com.google.gson.metrics; +import com.google.caliper.BeforeExperiment; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import com.google.gson.stream.JsonReader; import java.io.IOException; import java.io.StringReader; import java.lang.reflect.Field; @@ -22,29 +26,23 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; -import com.google.caliper.Runner; -import com.google.caliper.SimpleBenchmark; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; -import com.google.gson.stream.JsonReader; - /** * Caliper based micro benchmarks for Gson * * @author Inderjeet Singh */ -public class CollectionsDeserializationBenchmark extends SimpleBenchmark { +public class CollectionsDeserializationBenchmark { private static final Type LIST_TYPE = new TypeToken>(){}.getType(); private Gson gson; private String json; public static void main(String[] args) { - Runner.main(CollectionsDeserializationBenchmark.class, args); + NonUploadingCaliperRunner.run(CollectionsDeserializationBenchmark.class, args); } - @Override - protected void setUp() throws Exception { + @BeforeExperiment + void setUp() throws Exception { this.gson = new Gson(); List bags = new ArrayList(); for (int i = 0; i < 100; ++i) { diff --git a/metrics/src/main/java/com/google/gson/metrics/NonUploadingCaliperRunner.java b/metrics/src/main/java/com/google/gson/metrics/NonUploadingCaliperRunner.java new file mode 100644 index 00000000..80633e14 --- /dev/null +++ b/metrics/src/main/java/com/google/gson/metrics/NonUploadingCaliperRunner.java @@ -0,0 +1,21 @@ +package com.google.gson.metrics; + +import com.google.caliper.runner.CaliperMain; + +class NonUploadingCaliperRunner { + private static String[] concat(String first, String... others) { + if (others.length == 0) { + return new String[] { first }; + } else { + String[] result = new String[others.length + 1]; + result[0] = first; + System.arraycopy(others, 0, result, 1, others.length); + return result; + } + } + + public static void run(Class c, String[] args) { + // Disable result upload; Caliper uploads results to webapp by default, see https://github.com/google/caliper/issues/356 + CaliperMain.main(c, concat("-Cresults.upload.options.url=", args)); + } +} diff --git a/metrics/src/main/java/com/google/gson/metrics/ParseBenchmark.java b/metrics/src/main/java/com/google/gson/metrics/ParseBenchmark.java index a9e0f4e2..cc228e87 100644 --- a/metrics/src/main/java/com/google/gson/metrics/ParseBenchmark.java +++ b/metrics/src/main/java/com/google/gson/metrics/ParseBenchmark.java @@ -24,24 +24,27 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.json.JsonMapper; +import com.google.caliper.BeforeExperiment; import com.google.caliper.Param; -import com.google.caliper.Runner; -import com.google.caliper.SimpleBenchmark; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonParser; import com.google.gson.annotations.SerializedName; import com.google.gson.reflect.TypeToken; import java.io.CharArrayReader; +import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringWriter; import java.lang.reflect.Type; +import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; +import java.util.Locale; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; /** * Measure Gson and Jackson parsing and binding performance. @@ -50,7 +53,7 @@ import java.util.List; * That file contains Twitter feed data, which is representative of what * applications will be parsing. */ -public final class ParseBenchmark extends SimpleBenchmark { +public final class ParseBenchmark { @Param Document document; @Param Api api; @@ -105,8 +108,9 @@ public final class ParseBenchmark extends SimpleBenchmark { private char[] text; private Parser parser; - @Override protected void setUp() throws Exception { - text = resourceToString("/" + document.name() + ".json").toCharArray(); + @BeforeExperiment + void setUp() throws Exception { + text = resourceToString(document.name() + ".json").toCharArray(); parser = api.newParser(); } @@ -116,25 +120,39 @@ public final class ParseBenchmark extends SimpleBenchmark { } } - private static String resourceToString(String path) throws Exception { - InputStream in = ParseBenchmark.class.getResourceAsStream(path); - if (in == null) { - throw new IllegalArgumentException("No such file: " + path); + private static File getResourceFile(String path) throws Exception { + URL url = ParseBenchmark.class.getResource(path); + if (url == null) { + throw new IllegalArgumentException("Resource " + path + " does not exist"); } + File file = new File(url.toURI()); + if (!file.isFile()) { + throw new IllegalArgumentException("Resource " + path + " is not a file"); + } + return file; + } - Reader reader = new InputStreamReader(in, "UTF-8"); - char[] buffer = new char[8192]; - StringWriter writer = new StringWriter(); - int count; - while ((count = reader.read(buffer)) != -1) { - writer.write(buffer, 0, count); + private static String resourceToString(String fileName) throws Exception { + ZipFile zipFile = new ZipFile(getResourceFile("/ParseBenchmarkData.zip")); + try { + ZipEntry zipEntry = zipFile.getEntry(fileName); + Reader reader = new InputStreamReader(zipFile.getInputStream(zipEntry)); + char[] buffer = new char[8192]; + StringWriter writer = new StringWriter(); + int count; + while ((count = reader.read(buffer)) != -1) { + writer.write(buffer, 0, count); + } + reader.close(); + return writer.toString(); + + } finally { + zipFile.close(); } - reader.close(); - return writer.toString(); } public static void main(String[] args) throws Exception { - Runner.main(ParseBenchmark.class, args); + NonUploadingCaliperRunner.run(ParseBenchmark.class, args); } interface Parser { @@ -257,7 +275,7 @@ public final class ParseBenchmark extends SimpleBenchmark { .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .configure(MapperFeature.AUTO_DETECT_FIELDS, true) .build(); - mapper.setDateFormat(new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy")); + mapper.setDateFormat(new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH)); } @Override diff --git a/metrics/src/main/java/com/google/gson/metrics/SerializationBenchmark.java b/metrics/src/main/java/com/google/gson/metrics/SerializationBenchmark.java index 9cdf085e..bf67a66b 100644 --- a/metrics/src/main/java/com/google/gson/metrics/SerializationBenchmark.java +++ b/metrics/src/main/java/com/google/gson/metrics/SerializationBenchmark.java @@ -15,9 +15,8 @@ */ package com.google.gson.metrics; +import com.google.caliper.BeforeExperiment; import com.google.caliper.Param; -import com.google.caliper.Runner; -import com.google.caliper.SimpleBenchmark; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -28,7 +27,7 @@ import com.google.gson.GsonBuilder; * @author Jesse Wilson * @author Joel Leitch */ -public class SerializationBenchmark extends SimpleBenchmark { +public class SerializationBenchmark { private Gson gson; private BagOfPrimitives bag; @@ -36,11 +35,11 @@ public class SerializationBenchmark extends SimpleBenchmark { private boolean pretty; public static void main(String[] args) { - Runner.main(SerializationBenchmark.class, args); + NonUploadingCaliperRunner.run(SerializationBenchmark.class, args); } - @Override - protected void setUp() throws Exception { + @BeforeExperiment + void setUp() throws Exception { this.gson = pretty ? new GsonBuilder().setPrettyPrinting().create() : new Gson(); this.bag = new BagOfPrimitives(10L, 1, false, "foo"); } diff --git a/metrics/src/main/java/com/google/gson/metrics/ParseBenchmarkData.zip b/metrics/src/main/resources/ParseBenchmarkData.zip similarity index 100% rename from metrics/src/main/java/com/google/gson/metrics/ParseBenchmarkData.zip rename to metrics/src/main/resources/ParseBenchmarkData.zip