Added benchmark lib
This commit is contained in:
parent
fe66cc9962
commit
b9a662e4b3
5 changed files with 169 additions and 44 deletions
|
|
@ -37,11 +37,11 @@ import javax.activation.UnsupportedDataTypeException;
|
|||
|
||||
public class JSONObjectInputStream extends InputStream implements ObjectInput, Closeable{
|
||||
private JSONParser parser;
|
||||
private HashMap<Integer, Object> objCache;
|
||||
private HashMap<Integer, Object> objectCache;
|
||||
|
||||
public JSONObjectInputStream(Reader in) {
|
||||
this.parser = new JSONParser(in);
|
||||
this.objCache = new HashMap<Integer, Object>();
|
||||
this.objectCache = new HashMap<Integer, Object>();
|
||||
}
|
||||
|
||||
public Object readObject() throws IOException {
|
||||
|
|
@ -60,15 +60,15 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
|
|||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
objCache.clear();
|
||||
objectCache.clear();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected Object readObject(DataNode json) throws IllegalAccessException, InstantiationException, ClassNotFoundException, IllegalArgumentException, UnsupportedDataTypeException {
|
||||
// See if the Object id is in the cache before continuing
|
||||
if(json.getString("@object_id") != null && objCache.containsKey(json.getInt("@object_id")))
|
||||
return objCache.get(json.getInt("@object_id"));
|
||||
if(json.getString("@object_id") != null && objectCache.containsKey(json.getInt("@object_id")))
|
||||
return objectCache.get(json.getInt("@object_id"));
|
||||
|
||||
Class<?> objClass = Class.forName(json.getString("@class"));
|
||||
Object obj = objClass.newInstance();
|
||||
|
|
@ -87,7 +87,7 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
|
|||
}
|
||||
// Add object to the cache
|
||||
if(json.getString("@object_id") != null)
|
||||
objCache.put(json.getInt("@object_id"), obj);
|
||||
objectCache.put(json.getInt("@object_id"), obj);
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -89,6 +89,11 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
|
|||
out.write(new DataNode(s));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
out.write(new DataNode(b));
|
||||
}
|
||||
|
||||
public void writeUTF(String s) throws IOException {
|
||||
out.write(new DataNode(s));
|
||||
}
|
||||
|
|
@ -98,6 +103,8 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
|
|||
out.write(getDataNode(obj));
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IOException("Unable to serialize object", e);
|
||||
} finally {
|
||||
objectCache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -185,11 +192,6 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
|
|||
return node;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
// TODO:
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disables the use of meta data in the JSON
|
||||
* stream for class def and caching.
|
||||
|
|
@ -200,11 +202,4 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
|
|||
generateMetaData = generate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the Object stream (clears the cache)
|
||||
*/
|
||||
public void reset(){
|
||||
objectCache.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
128
src/zutil/test/JSONSerializerBenchmark.java
Normal file
128
src/zutil/test/JSONSerializerBenchmark.java
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
/*
|
||||
* Copyright (c) 2013 ezivkoc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.test;
|
||||
|
||||
import com.carrotsearch.junitbenchmarks.BenchmarkRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.MethodRule;
|
||||
import zutil.io.StringOutputStream;
|
||||
import zutil.parser.json.JSONObjectInputStream;
|
||||
import zutil.parser.json.JSONObjectOutputStream;
|
||||
import zutil.test.JSONSerializerTest.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class JSONSerializerBenchmark {
|
||||
private static final int TEST_EXECUTIONS = 2000;
|
||||
|
||||
@Rule
|
||||
public BenchmarkRule benchmarkRun = new BenchmarkRule();
|
||||
|
||||
@Test
|
||||
public void testJavaLegacySerialize() throws InterruptedException, IOException, ClassNotFoundException{
|
||||
for(int i=0; i<TEST_EXECUTIONS; i++){
|
||||
TestClass sourceObj = new TestClass().init();
|
||||
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
ObjectOutputStream out = new ObjectOutputStream(bout);
|
||||
out.writeObject(sourceObj);
|
||||
out.flush();
|
||||
out.close();
|
||||
byte[] data = bout.toByteArray();
|
||||
|
||||
ByteArrayInputStream bin = new ByteArrayInputStream(data);
|
||||
ObjectInputStream in = new ObjectInputStream(bin);
|
||||
TestClass targetObj = (TestClass) in.readObject();
|
||||
in.close();
|
||||
|
||||
assertEquals( sourceObj, targetObj );
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavaJSONSerialize() throws InterruptedException, IOException, ClassNotFoundException{
|
||||
for(int i=0; i<TEST_EXECUTIONS; i++){
|
||||
TestClass sourceObj = new TestClass().init();
|
||||
|
||||
TestClass targetObj = JSONSerializerTest.sendReceiveObject(sourceObj);
|
||||
|
||||
assertEquals( sourceObj, targetObj );
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOutputJavaLegacySerialize() throws InterruptedException, IOException, ClassNotFoundException{
|
||||
for(int i=0; i<TEST_EXECUTIONS; i++){
|
||||
TestClass sourceObj = new TestClass().init();
|
||||
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
ObjectOutputStream out = new ObjectOutputStream(bout);
|
||||
out.writeObject(sourceObj);
|
||||
out.flush();
|
||||
out.close();
|
||||
byte[] data = bout.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOutputJavaJSONSerialize() throws InterruptedException, IOException, ClassNotFoundException{
|
||||
for(int i=0; i<TEST_EXECUTIONS; i++){
|
||||
TestClass sourceObj = new TestClass().init();
|
||||
|
||||
String targetObj = JSONSerializerTest.writeObjectToJson(sourceObj);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInputJavaLegacySerialize() throws InterruptedException, IOException, ClassNotFoundException{
|
||||
TestClass sourceObj = new TestClass().init();
|
||||
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
ObjectOutputStream out = new ObjectOutputStream(bout);
|
||||
out.writeObject(sourceObj);
|
||||
out.flush();
|
||||
out.close();
|
||||
byte[] data = bout.toByteArray();
|
||||
|
||||
for(int i=0; i<TEST_EXECUTIONS; i++){
|
||||
ByteArrayInputStream bin = new ByteArrayInputStream(data);
|
||||
ObjectInputStream in = new ObjectInputStream(bin);
|
||||
TestClass targetObj = (TestClass) in.readObject();
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInputJavaJSONSerialize() throws InterruptedException, IOException, ClassNotFoundException{
|
||||
TestClass sourceObj = new TestClass().init();
|
||||
String sourceStr = JSONSerializerTest.writeObjectToJson(sourceObj);
|
||||
|
||||
for(int i=0; i<TEST_EXECUTIONS; i++){
|
||||
TestClass targetObj = JSONSerializerTest.readObjectFromJson(sourceStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -35,25 +35,6 @@ import zutil.parser.json.JSONObjectOutputStream;
|
|||
|
||||
public class JSONSerializerTest{
|
||||
|
||||
@Test
|
||||
public void testJavaLegacySerialize() throws InterruptedException, IOException, ClassNotFoundException{
|
||||
TestClass sourceObj = new TestClass().init();
|
||||
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
ObjectOutputStream out = new ObjectOutputStream(bout);
|
||||
out.writeObject(sourceObj);
|
||||
out.flush();
|
||||
out.close();
|
||||
byte[] data = bout.toByteArray();
|
||||
|
||||
ByteArrayInputStream bin = new ByteArrayInputStream(data);
|
||||
ObjectInputStream in = new ObjectInputStream(bin);
|
||||
TestClass targetObj = (TestClass) in.readObject();
|
||||
in.close();
|
||||
|
||||
assertEquals( sourceObj, targetObj );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOutputSerializerWithPrimitives() throws InterruptedException, IOException, ClassNotFoundException{
|
||||
TestClass sourceObj = new TestClass().init();
|
||||
|
|
@ -116,19 +97,26 @@ public class JSONSerializerTest{
|
|||
|
||||
assertEquals( sourceObj, targetObj );
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testInputSerializerWithStringArrays() throws InterruptedException, IOException, ClassNotFoundException{
|
||||
TestClassStringArray sourceObj = new TestClassStringArray().init();
|
||||
|
||||
TestClassStringArray targetObj = sendReceiveObject(sourceObj);
|
||||
|
||||
assertEquals( sourceObj, targetObj );
|
||||
}
|
||||
|
||||
|
||||
/******************* Utility Functions ************************************/
|
||||
|
||||
private static <T> T sendReceiveObject(T sourceObj) throws IOException{
|
||||
public static <T> T sendReceiveObject(T sourceObj) throws IOException{
|
||||
return readObjectFromJson(
|
||||
writeObjectToJson(sourceObj));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> T readObjectFromJson(String json) throws IOException{
|
||||
public static <T> T readObjectFromJson(String json) throws IOException{
|
||||
StringReader bin = new StringReader(json);
|
||||
JSONObjectInputStream in = new JSONObjectInputStream(bin);
|
||||
T targetObj = (T) in.readObject();
|
||||
|
|
@ -136,8 +124,8 @@ public class JSONSerializerTest{
|
|||
|
||||
return targetObj;
|
||||
}
|
||||
|
||||
private static <T> String writeObjectToJson(T sourceObj) throws IOException{
|
||||
|
||||
public static <T> String writeObjectToJson(T sourceObj) throws IOException{
|
||||
StringOutputStream bout = new StringOutputStream();
|
||||
JSONObjectOutputStream out = new JSONObjectOutputStream(bout);
|
||||
|
||||
|
|
@ -146,7 +134,7 @@ public class JSONSerializerTest{
|
|||
out.close();
|
||||
|
||||
String data = bout.toString();
|
||||
System.out.println(data);
|
||||
//System.out.println(data);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
|
@ -222,4 +210,18 @@ public class JSONSerializerTest{
|
|||
Arrays.equals(this.array ,((TestClassArray)obj).array);
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestClassStringArray{
|
||||
private String[] array;
|
||||
|
||||
public TestClassStringArray init(){
|
||||
array = new String[]{"test","string","array"};
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj){
|
||||
return obj instanceof TestClassStringArray &&
|
||||
Arrays.equals(this.array ,((TestClassStringArray)obj).array);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue