Initial version of Gson benchmark using Caliper
This commit is contained in:
parent
a09c3ab1c5
commit
1ad0489b84
100
metrics/pom.xml
Normal file
100
metrics/pom.xml
Normal file
@ -0,0 +1,100 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson-metrics</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<inceptionYear>2011</inceptionYear>
|
||||
<name>Gson Metrics</name>
|
||||
<parent>
|
||||
<groupId>org.sonatype.oss</groupId>
|
||||
<artifactId>oss-parent</artifactId>
|
||||
<version>5</version>
|
||||
</parent>
|
||||
<url>http://code.google.com/p/google-gson/</url>
|
||||
<description>Performance Metrics for Google Gson library</description>
|
||||
<licenses>
|
||||
<license>
|
||||
<name>The Apache Software License, Version 2.0</name>
|
||||
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
|
||||
<distribution>repo</distribution>
|
||||
</license>
|
||||
</licenses>
|
||||
<scm>
|
||||
<connection>scm:svn:http://google-gson.googlecode.com/svn/trunk/metrics</connection>
|
||||
<developerConnection>scm:svn:https://google-gson.googlecode.com/svn/trunk/metrics</developerConnection>
|
||||
<url>http://google-gson.codegoogle.com/svn/trunk/metrics</url>
|
||||
</scm>
|
||||
<issueManagement>
|
||||
<system>Google Code Issue Tracking</system>
|
||||
<url>http://code.google.com/p/google-gson/issues/list</url>
|
||||
</issueManagement>
|
||||
<organization>
|
||||
<name>Google, Inc.</name>
|
||||
<url>http://www.google.com</url>
|
||||
</organization>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>1.7-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<defaultGoal>package</defaultGoal>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.3.2</version>
|
||||
<configuration>
|
||||
<source>1.5</source>
|
||||
<target>1.5</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-eclipse-plugin</artifactId>
|
||||
<version>2.8</version>
|
||||
<configuration>
|
||||
<downloadSources>true</downloadSources>
|
||||
<downloadJavadocs>true</downloadJavadocs>
|
||||
<workspace>../eclipse-ws/</workspace>
|
||||
<workspaceCodeStylesURL>
|
||||
file:///${basedir}/../lib/gson-formatting-styles.xml
|
||||
</workspaceCodeStylesURL>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-release-plugin</artifactId>
|
||||
<version>2.1</version>
|
||||
<configuration>
|
||||
<arguments>-DenableCiProfile=true</arguments>
|
||||
<tagBase>https://google-gson.googlecode.com/svn/tags/</tagBase>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<developers>
|
||||
<developer>
|
||||
<name>Inderjeet Singh</name>
|
||||
<organization>Google Inc.</organization>
|
||||
</developer>
|
||||
<developer>
|
||||
<name>Joel Leitch</name>
|
||||
<organization>Google Inc.</organization>
|
||||
</developer>
|
||||
<developer>
|
||||
<name>Jesse Wilson</name>
|
||||
<organization>Google Inc.</organization>
|
||||
</developer>
|
||||
</developers>
|
||||
</project>
|
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (C) 2011 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.google.gson.metrics;
|
||||
|
||||
/**
|
||||
* Class with a bunch of primitive fields
|
||||
*
|
||||
* @author Inderjeet Singh
|
||||
*/
|
||||
public class BagOfPrimitives {
|
||||
public static final long DEFAULT_VALUE = 0;
|
||||
public long longValue;
|
||||
public int intValue;
|
||||
public boolean booleanValue;
|
||||
public String stringValue;
|
||||
|
||||
public BagOfPrimitives() {
|
||||
this(DEFAULT_VALUE, 0, false, "");
|
||||
}
|
||||
|
||||
public BagOfPrimitives(long longValue, int intValue, boolean booleanValue, String stringValue) {
|
||||
this.longValue = longValue;
|
||||
this.intValue = intValue;
|
||||
this.booleanValue = booleanValue;
|
||||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
public int getIntValue() {
|
||||
return intValue;
|
||||
}
|
||||
|
||||
public String getExpectedJson() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("{");
|
||||
sb.append("\"longValue\":").append(longValue).append(",");
|
||||
sb.append("\"intValue\":").append(intValue).append(",");
|
||||
sb.append("\"booleanValue\":").append(booleanValue).append(",");
|
||||
sb.append("\"stringValue\":\"").append(stringValue).append("\"");
|
||||
sb.append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + (booleanValue ? 1231 : 1237);
|
||||
result = prime * result + intValue;
|
||||
result = prime * result + (int) (longValue ^ (longValue >>> 32));
|
||||
result = prime * result + ((stringValue == null) ? 0 : stringValue.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
BagOfPrimitives other = (BagOfPrimitives) obj;
|
||||
if (booleanValue != other.booleanValue)
|
||||
return false;
|
||||
if (intValue != other.intValue)
|
||||
return false;
|
||||
if (longValue != other.longValue)
|
||||
return false;
|
||||
if (stringValue == null) {
|
||||
if (other.stringValue != null)
|
||||
return false;
|
||||
} else if (!stringValue.equals(other.stringValue))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("(longValue=%d,intValue=%d,booleanValue=%b,stringValue=%s)",
|
||||
longValue, intValue, booleanValue, stringValue);
|
||||
}
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright (C) 2011 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.google.gson.metrics;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
|
||||
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.stream.JsonReader;
|
||||
|
||||
/**
|
||||
* Caliper based micro benchmarks for Gson
|
||||
*
|
||||
* @author Inderjeet Singh
|
||||
* @author Jesse Wilson
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class GsonBenchmark extends SimpleBenchmark {
|
||||
|
||||
private Gson gson;
|
||||
private BagOfPrimitives bag;
|
||||
private String json;
|
||||
@Param
|
||||
private boolean pretty;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Runner.main(GsonBenchmark.class, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
this.gson = pretty ? new GsonBuilder().setPrettyPrinting().create() : new Gson();
|
||||
this.bag = new BagOfPrimitives(10L, 1, false, "foo");
|
||||
this.json = gson.toJson(bag);
|
||||
}
|
||||
|
||||
public void timeObjectSerialization(int reps) {
|
||||
for (int i=0; i<reps; ++i) {
|
||||
gson.toJson(bag);
|
||||
}
|
||||
}
|
||||
|
||||
public void timeObjectDeserialization(int reps) {
|
||||
for (int i=0; i<reps; ++i) {
|
||||
gson.fromJson(json, BagOfPrimitives.class);
|
||||
}
|
||||
}
|
||||
|
||||
public void timeStreamingParserDeserialization(int reps) throws IOException {
|
||||
for (int i=0; i<reps; ++i) {
|
||||
StringReader reader = new StringReader(json);
|
||||
JsonReader jr = new JsonReader(reader);
|
||||
jr.beginObject();
|
||||
long longValue = 0;
|
||||
int intValue = 0;
|
||||
boolean booleanValue = false;
|
||||
String stringValue = null;
|
||||
while(jr.hasNext()) {
|
||||
String name = jr.nextName();
|
||||
if (name.equals("longValue")) {
|
||||
longValue = jr.nextLong();
|
||||
} else if (name.equals("intValue")) {
|
||||
intValue = jr.nextInt();
|
||||
} else if (name.equals("booleanValue")) {
|
||||
booleanValue = jr.nextBoolean();
|
||||
} else if (name.equals("stringValue")) {
|
||||
stringValue = jr.nextString();
|
||||
} else {
|
||||
throw new IOException("Unexpected name: " + name);
|
||||
}
|
||||
}
|
||||
jr.endObject();
|
||||
new BagOfPrimitives(longValue, intValue, booleanValue, stringValue);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user