Minor cleanup to get to test performance of smile handling (fails for some reason still)
diff --git a/src/perf/TestJsonPerf.java b/src/perf/TestJsonPerf.java
index ab401ad..f93d3b7 100644
--- a/src/perf/TestJsonPerf.java
+++ b/src/perf/TestJsonPerf.java
@@ -3,12 +3,11 @@
import org.codehaus.jackson.*;
import org.codehaus.jackson.io.IOContext;
import org.codehaus.jackson.map.*;
+import org.codehaus.jackson.smile.SmileFactory;
import org.codehaus.jackson.util.BufferRecycler;
// json.org's reference implementation
import org.json.*;
-// StringTree implementation
-import org.stringtree.json.JSONReader;
// Jsontool implementation
import com.sdicons.json.parser.JSONParser;
// Noggit:
@@ -20,22 +19,30 @@
private final static int TEST_PER_GC = 15;
- final JsonFactory mJsonFactory;
+ final JsonFactory _jsonFactory;
+
+ final ObjectMapper _mapper;
- final byte[] mData;
+ final SmileFactory _smileFactory;
+
+ final byte[] _jsonData;
+ final byte[] _smileData;
+
protected int mBatchSize;
- public TestJsonPerf(File f)
- throws Exception
+ public TestJsonPerf(File f) throws IOException
{
- mJsonFactory = new JsonFactory();
- mData = readData(f);
+ _jsonFactory = new JsonFactory();
+ _mapper = new ObjectMapper(_jsonFactory);
+ _smileFactory = new SmileFactory();
+ _jsonData = readData(f);
+ _smileData = convertToSmile(_jsonData);
// Let's try to guestimate suitable size... to get to 50 megs parsed
- REPS = (int) ((double) (50 * 1000 * 1000) / (double) mData.length);
+ REPS = (int) ((double) (50 * 1000 * 1000) / (double) _jsonData.length);
- System.out.println("Read "+mData.length+" bytes from '"+f+"'; will do "+REPS+" reps");
+ System.out.println("Read "+_jsonData.length+" bytes (smile: "+_smileData.length+") from '"+f+"'; will do "+REPS+" reps");
System.out.println();
}
@@ -58,42 +65,42 @@
case 0:
msg = "Jackson, stream/byte";
- sum += testJacksonStream(REPS, true);
+ sum += testJacksonStream(REPS, _jsonFactory, _jsonData, true);
break;
case 1:
msg = "Jackson, stream/char";
- sum += testJacksonStream(REPS, false);
+ sum += testJacksonStream(REPS, _jsonFactory, _jsonData, false);
break;
case 2:
+ msg = "Jackson/smile, stream";
+ sum += testJacksonStream(REPS, _smileFactory, _smileData, true);
+ break;
+ case 3:
msg = "Noggit";
sum += testNoggit(REPS);
break;
- case 3:
+ case 4:
msg = "Jackson, Java types";
- sum += testJacksonJavaTypes(REPS);
+ sum += testJacksonJavaTypes(_mapper, REPS);
break;
- case 4:
- msg = "Jackson, JSON types";
- sum += testJacksonJsonTypes(REPS);
- break;
case 5:
+ msg = "Jackson, JSON types";
+ sum += testJacksonJsonTypes(_mapper, REPS);
+ break;
+ case 6:
msg = "Json.org";
sum += testJsonOrg(REPS);
break;
- case 6:
+ case 7:
msg = "Json-simple";
sum += testJsonSimple(REPS);
break;
- case 7:
+ case 8:
msg = "JSONTools (berlios.de)";
sum += testJsonTools(REPS);
break;
- case 8:
- msg = "StringTree";
- sum += testStringTree(REPS);
- break;
default:
throw new Error("Internal error");
}
@@ -132,12 +139,25 @@
return data;
}
+ private byte[] convertToSmile(byte[] json) throws IOException
+ {
+ JsonParser jp = _jsonFactory.createJsonParser(json);
+ ByteArrayOutputStream out = new ByteArrayOutputStream(200);
+ JsonGenerator jg = _smileFactory.createJsonGenerator(out);
+ while (jp.nextToken() != null) {
+ jg.copyCurrentEvent(jp);
+ }
+ jp.close();
+ jg.close();
+ return out.toByteArray();
+ }
+
protected int testJsonOrg(int reps)
throws Exception
{
Object ob = null;
// Json.org's code only accepts Strings:
- String input = new String(mData, "UTF-8");
+ String input = new String(_jsonData, "UTF-8");
for (int i = 0; i < reps; ++i) {
JSONTokener tok = new JSONTokener(input);
ob = tok.nextValue();
@@ -145,13 +165,13 @@
return ob.hashCode();
}
- protected int testJsonTools(int reps)
+ private int testJsonTools(int reps)
throws Exception
{
Object ob = null;
for (int i = 0; i < reps; ++i) {
// Json-tools accepts streams, yay!
- JSONParser jp = new JSONParser(new ByteArrayInputStream(mData), "byte stream");
+ JSONParser jp = new JSONParser(new ByteArrayInputStream(_jsonData), "byte stream");
/* Hmmmh. Will we get just one object for the whole thing?
* Or a stream? Seems like just one
*/
@@ -161,23 +181,11 @@
return ob.hashCode();
}
- protected int testStringTree(int reps)
- throws Exception
- {
- Object ob = null;
- String input = new String(mData, "UTF-8");
- for (int i = 0; i < reps; ++i) {
- // StringTree impl only accepts Strings:
- ob = new JSONReader().read(input);
- }
- return ob.hashCode();
- }
-
- protected int testJsonSimple(int reps)
+ private int testJsonSimple(int reps)
throws Exception
{
// Json.org's code only accepts Strings:
- String input = new String(mData, "UTF-8");
+ String input = new String(_jsonData, "UTF-8");
Object ob = null;
for (int i = 0; i < reps; ++i) {
ob = org.json.simple.JSONValue.parse(input);
@@ -185,12 +193,12 @@
return ob.hashCode();
}
- protected int testNoggit(int reps)
+ private int testNoggit(int reps)
throws Exception
{
- ByteArrayInputStream bin = new ByteArrayInputStream(mData);
+ ByteArrayInputStream bin = new ByteArrayInputStream(_jsonData);
- char[] cbuf = new char[mData.length];
+ char[] cbuf = new char[_jsonData.length];
IOContext ctxt = new IOContext(new BufferRecycler(), this, false);
int sum = 0;
@@ -220,7 +228,7 @@
return sum;
}
- protected int testJacksonStream(int reps, boolean fast)
+ private int testJacksonStream(int reps, JsonFactory factory, byte[] data, boolean fast)
throws Exception
{
int sum = 0;
@@ -229,9 +237,9 @@
JsonParser jp;
if (fast) {
- jp = mJsonFactory.createJsonParser(mData, 0, mData.length);
+ jp = factory.createJsonParser(data, 0, data.length);
} else {
- jp = mJsonFactory.createJsonParser(new ByteArrayInputStream(mData));
+ jp = factory.createJsonParser(new ByteArrayInputStream(data));
}
JsonToken t;
while ((t = jp.nextToken()) != null) {
@@ -247,30 +255,23 @@
return sum;
}
- protected int testJacksonJavaTypes(int reps)
+ private int testJacksonJavaTypes(ObjectMapper mapper, int reps)
throws Exception
{
Object ob = null;
- ObjectMapper mapper = new ObjectMapper();
for (int i = 0; i < reps; ++i) {
- JsonParser jp = mJsonFactory.createJsonParser(new ByteArrayInputStream(mData));
// This is "untyped"... Maps, Lists etc
- ob = mapper.readValue(jp, Object.class);
- jp.close();
+ ob = mapper.readValue(_jsonData, 0, _jsonData.length, Object.class);
}
return ob.hashCode(); // just to get some non-optimizable number
}
- @SuppressWarnings("deprecation")
- protected int testJacksonJsonTypes(int reps)
+ private int testJacksonJsonTypes(ObjectMapper mapper, int reps)
throws Exception
{
Object ob = null;
- TreeMapper mapper = new TreeMapper();
for (int i = 0; i < reps; ++i) {
- JsonParser jp = mJsonFactory.createJsonParser(new ByteArrayInputStream(mData));
- ob = mapper.readTree(jp);
- jp.close();
+ ob = mapper.readValue(_jsonData, 0, _jsonData.length, JsonNode.class);
}
return ob.hashCode(); // just to get some non-optimizable number
}
diff --git a/src/smile/java/org/codehaus/jackson/smile/SmileParser.java b/src/smile/java/org/codehaus/jackson/smile/SmileParser.java
index 818837a..37b89cb 100644
--- a/src/smile/java/org/codehaus/jackson/smile/SmileParser.java
+++ b/src/smile/java/org/codehaus/jackson/smile/SmileParser.java
@@ -8,7 +8,6 @@
import java.math.BigInteger;
import org.codehaus.jackson.*;
-import org.codehaus.jackson.JsonParser.NumberType;
import org.codehaus.jackson.impl.StreamBasedParserBase;
import org.codehaus.jackson.io.IOContext;
import org.codehaus.jackson.sym.BytesToNameCanonicalizer;
@@ -319,7 +318,7 @@
return _parsingContext.getCurrentName();
case VALUE_NUMBER_INT:
case VALUE_NUMBER_FLOAT:
- // !!! TBI
+ // TODO: optimize
return getNumberValue().toString();
default:
@@ -358,7 +357,7 @@
// fall through
case VALUE_NUMBER_INT:
case VALUE_NUMBER_FLOAT:
- // !!! TBI
+ // TODO: optimize
return getNumberValue().toString().toCharArray();
default:
@@ -385,7 +384,7 @@
// fall through
case VALUE_NUMBER_INT:
case VALUE_NUMBER_FLOAT:
- // !!! TBI
+ // TODO: optimize
return getNumberValue().toString().length();
default:
@@ -413,6 +412,7 @@
throws IOException, JsonParseException
{
// !!! TBI
+ _throwInternal();
return null;
}
@@ -442,6 +442,7 @@
}
int index = ((ch & 0x3) << 8) | _inputBuffer[_inputPtr++];
// !!! TBI: fetch actual String
+ if (true) _throwInternal();
_parsingContext.setCurrentName("TBI");
}
return JsonToken.FIELD_NAME;
@@ -461,6 +462,7 @@
{
int nameIndex = (ch & 0x3F);
// !!! TBI: fetch actual String
+ if (true) _throwInternal();
_parsingContext.setCurrentName("TBI");
}
return JsonToken.FIELD_NAME;
@@ -589,10 +591,7 @@
case 4: // tiny unicode
// fall through
case 5: // short unicode
- {
- int len = (tb - 0x7F);
- _decodeShortUnicode(len);
- }
+ _decodeShortUnicode(tb - 0x7F);
return;
case 7:
@@ -602,9 +601,11 @@
case 0: // long variable length ascii
case 1: // long variable length unicode
// !!! TBI
+ if (true) _throwInternal();
case 2: // binary, raw
case 3: // binary, 7-bit
// !!! TBI
+ if (true) _throwInternal();
case 4: // VInt (zigzag) or BigDecimal
int subtype = tb & 0x03;
if (subtype == 0) { // (v)int
@@ -716,7 +717,9 @@
throws IOException, JsonParseException
{
// !!! TBI
- _numberBigInt = BigInteger.ZERO;
+ if (true) _throwInternal();
+
+ _numberBigInt = BigInteger.ZERO;
_numTypesValid = NR_BIGINT;
}
@@ -778,6 +781,7 @@
throws IOException, JsonParseException
{
// !!! TBI
+ if (true) _throwInternal();
_numberBigDecimal = BigDecimal.ZERO;
_numTypesValid = NR_BIGDECIMAL;
}
@@ -849,6 +853,7 @@
}
case 2: // big-int
// !!! TBI
+ if (true) _throwInternal();
break;
}
break;
@@ -861,6 +866,8 @@
_skipBytes(11);
return;
case 2: // big-decimal
+ // !!! TBI
+ if (true) _throwInternal();
}
break;
}
diff --git a/src/smile/test/org/codehaus/jackson/smile/TestSmileParser.java b/src/smile/test/org/codehaus/jackson/smile/TestSmileParser.java
index c101d65..9d6ff5c 100644
--- a/src/smile/test/org/codehaus/jackson/smile/TestSmileParser.java
+++ b/src/smile/test/org/codehaus/jackson/smile/TestSmileParser.java
@@ -2,7 +2,6 @@
import java.io.*;
-import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
public class TestSmileParser
@@ -72,4 +71,10 @@
assertToken(JsonToken.END_OBJECT, p.nextToken());
p.close();
}
+
+ public void testJsonSampleDoc() throws IOException
+ {
+ byte[] data = _smileDoc(SAMPLE_DOC_JSON_SPEC);
+ verifyJsonSpecSampleDoc(_parser(data), true);
+ }
}
diff --git a/src/test/main/BaseTest.java b/src/test/main/BaseTest.java
index d0302d3..53eb83a 100644
--- a/src/test/main/BaseTest.java
+++ b/src/test/main/BaseTest.java
@@ -12,9 +12,9 @@
extends TestCase
{
/*
- ////////////////////////////////////////////////////////
- // Some sample documents:
- ////////////////////////////////////////////////////////
+ /**********************************************************
+ /* Some sample documents:
+ /**********************************************************
*/
protected final static int SAMPLE_SPEC_VALUE_WIDTH = 800;
@@ -45,9 +45,9 @@
;
/*
- ////////////////////////////////////////////////////////
- // High-level helpers
- ////////////////////////////////////////////////////////
+ /**********************************************************
+ /* High-level helpers
+ /**********************************************************
*/
protected void verifyJsonSpecSampleDoc(JsonParser jp, boolean verifyContents)
@@ -159,9 +159,9 @@
}
/*
- ////////////////////////////////////////////////////////
- // Parser/generator construction
- ////////////////////////////////////////////////////////
+ /**********************************************************
+ /* Parser/generator construction
+ /**********************************************************
*/
protected JsonParser createParserUsingReader(String input)
@@ -203,9 +203,9 @@
}
/*
- ////////////////////////////////////////////////////////
- // Additional assertion methods
- ////////////////////////////////////////////////////////
+ /**********************************************************
+ /* Additional assertion methods
+ /**********************************************************
*/
protected void assertToken(JsonToken expToken, JsonToken actToken)
@@ -264,9 +264,9 @@
}
/*
- ////////////////////////////////////////////////////////
- // And other helpers
- ////////////////////////////////////////////////////////
+ /**********************************************************
+ /* And other helpers
+ /**********************************************************
*/
protected byte[] encodeInUTF32BE(String input)
diff --git a/src/xc/java/org/codehaus/jackson/xml/XmlFactory.java b/src/xc/java/org/codehaus/jackson/xml/XmlFactory.java
index ed793f5..ef49b3b 100644
--- a/src/xc/java/org/codehaus/jackson/xml/XmlFactory.java
+++ b/src/xc/java/org/codehaus/jackson/xml/XmlFactory.java
@@ -5,7 +5,6 @@
import javax.xml.stream.*;
import org.codehaus.jackson.*;
-import org.codehaus.jackson.impl.WriterBasedGenerator;
import org.codehaus.jackson.io.IOContext;
/**