Last active
August 29, 2015 14:01
-
-
Save frsyuki/3742b704cdd626b09a85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: run | |
MSGPACK_06_JAR = msgpack-java-0.6.11.jar | |
$(MSGPACK_06_JAR): | |
curl "http://search.maven.org/remotecontent?filepath=org/msgpack/msgpack/0.6.11/msgpack-0.6.11.jar" -o $@ | |
JAVASSIST_JAR = javassist-3.18.2-GA.jar | |
$(JAVASSIST_JAR): | |
curl "http://search.maven.org/remotecontent?filepath=org/javassist/javassist/3.18.2-GA/javassist-3.18.2-GA.jar" -o $@ | |
CLASSPATH = $(MSGPACK_06_JAR):$(JAVASSIST_JAR):../msgpack-core/target/msgpack-core-0.7.0-SNAPSHOT.jar | |
Microbench.class: Microbench.java $(MSGPACK_06_JAR) $(JAVASSIST_JAR) | |
javac -cp $(CLASSPATH) Microbench.java | |
run: Microbench.class | |
java -cp $(CLASSPATH):. Microbench 1700461847 int 1000000 10 | |
java -cp $(CLASSPATH):. Microbench 1700461847 long 1000000 10 | |
java -cp $(CLASSPATH):. Microbench 1700461847 bin 1000000 10 | |
java -cp $(CLASSPATH):. Microbench 1700461847 str 1000000 10 | |
.PHONY: all run clean distclean | |
clean: | |
rm -f *.class | |
distclean: clean | |
rm -f $(MSGPACK_06_JAR) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
import java.nio.ByteBuffer; | |
import java.io.OutputStream; | |
import java.io.InputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.ByteArrayInputStream; | |
import java.io.IOException; | |
import java.io.PrintStream; | |
// v06 | |
import org.msgpack.MessagePack; | |
import org.msgpack.packer.Packer; | |
import org.msgpack.unpacker.Unpacker; | |
// v07 | |
import org.msgpack.core.MessagePacker; | |
import org.msgpack.core.MessageUnpacker; | |
public class Microbench { | |
public static void main(String[] args) throws Exception { | |
if (args.length != 4) { | |
System.out.println("usage: <seed> <type> <scale> <loop>"); | |
System.exit(1); | |
} | |
long seed = Long.parseLong(args[0]); | |
String type = args[1]; | |
int scale = Integer.parseInt(args[2]); | |
int loop = Integer.parseInt(args[3]); | |
new Microbench(seed, scale, loop).run(type); | |
} | |
final long seed; | |
final int scale; | |
final int loop; | |
public Microbench(long seed, int scale, int loop) { | |
if (seed == 0) { | |
seed = new Random().nextInt(); | |
} | |
this.seed = seed; | |
this.scale = scale; | |
this.loop = loop; | |
} | |
public void run(String type) throws Exception { | |
System.out.printf("seed: %d%n", seed); | |
System.out.printf("scale: %d%n", scale); | |
System.out.printf("loop: %d%n", loop); | |
BenchmarkRunner v06; | |
BenchmarkRunner v07; | |
if(type.equals("int")) { | |
v06 = new Int06(); | |
v07 = new Int07(); | |
} else if(type.equals("long")) { | |
v06 = new Long06(); | |
v07 = new Long07(); | |
} else if(type.equals("bin")) { | |
v06 = new Bin06(); | |
v07 = new Bin07(); | |
} else if(type.equals("str")) { | |
v06 = new Str06(); | |
v07 = new Str07(); | |
} else { | |
throw new RuntimeException("unsupported type: "+type); | |
} | |
// warm-up | |
v06.run(new PrintStream(new ByteArrayOutputStream())); | |
v07.run(new PrintStream(new ByteArrayOutputStream())); | |
// run | |
v06.run(System.out); | |
v07.run(System.out); | |
} | |
public abstract class BenchmarkRunner { | |
protected final Random rand = new Random(); | |
public void run(PrintStream out) throws Exception { | |
out.printf("-- %s serialize%n", getClass().toString()); | |
// serialize | |
rand.setSeed(seed); | |
long totalSize = 0; | |
{ | |
long startTime = System.currentTimeMillis(); | |
for(int i=0; i < loop; i++) { | |
totalSize += runSerialize().limit(); | |
} | |
showResult(out, startTime, (int) (totalSize / loop), loop); | |
} | |
// create data | |
rand.setSeed(seed); | |
ByteBuffer bb = runSerialize(); | |
out.printf("-- %s deserialize%n", getClass().toString()); | |
// deserialize warm-up | |
for(int i=0; i < loop; i++) { | |
runDeserialize(bb); | |
} | |
// deserialize | |
{ | |
long startTime = System.currentTimeMillis(); | |
for(int i=0; i < loop; i++) { | |
runDeserialize(bb); | |
} | |
showResult(out, startTime, bb.limit(), loop); | |
} | |
} | |
private void showResult(PrintStream out, long startTime, long size, int loop) { | |
long time = System.currentTimeMillis() - startTime; | |
double mbs = size * (long) loop / ((double) time / 1000) / 1024 / 1024; | |
double msec = time / (double) loop; | |
out.printf(" %.2f KB%n", (size / 1024.0)); | |
out.printf(" %.2f msec/loop%n", msec); | |
out.printf(" %.2f MB/s%n", mbs); | |
} | |
public abstract ByteBuffer runSerialize() throws Exception; | |
public abstract void runDeserialize(ByteBuffer bb) throws Exception; | |
protected GetBufferByteArrayOutputStream out; | |
protected final OutputStream newOutputStream(int size) { | |
out = new GetBufferByteArrayOutputStream(5 * scale); | |
return out; | |
} | |
protected final InputStream newInputStream(ByteBuffer bb) { | |
ByteArrayInputStream in = new ByteArrayInputStream(bb.array(), 0, bb.limit()); | |
return in; | |
} | |
protected final ByteBuffer getBuffer() { | |
return out.getBuffer(); | |
} | |
} | |
public class GetBufferByteArrayOutputStream extends ByteArrayOutputStream { | |
public GetBufferByteArrayOutputStream() { | |
super(); | |
} | |
public GetBufferByteArrayOutputStream(int size) { | |
super(size); | |
} | |
public ByteBuffer getBuffer() { | |
return ByteBuffer.wrap(buf, 0, count); | |
} | |
} | |
public class Int06 extends BenchmarkRunner { | |
private MessagePack msgpack = new MessagePack(); | |
public ByteBuffer runSerialize() throws Exception { | |
Packer packer = msgpack.createPacker(newOutputStream(5 * scale)); | |
for(int i=0; i < scale; i++) { | |
packer.write(rand.nextInt()); | |
} | |
packer.flush(); | |
return getBuffer(); | |
} | |
public void runDeserialize(ByteBuffer bb) throws Exception { | |
Unpacker unpacker = msgpack.createUnpacker(newInputStream(bb)); | |
for(int i=0; i < scale; i++) { | |
unpacker.readInt(); | |
} | |
} | |
} | |
public class Int07 extends BenchmarkRunner { | |
public ByteBuffer runSerialize() throws Exception { | |
MessagePacker packer = new MessagePacker(newOutputStream(5 * scale)); | |
for(int i=0; i < scale; i++) { | |
packer.packInt(rand.nextInt()); | |
} | |
return getBuffer(); | |
} | |
public void runDeserialize(ByteBuffer bb) throws Exception { | |
MessageUnpacker unpacker = new MessageUnpacker(newInputStream(bb)); | |
for(int i=0; i < scale; i++) { | |
unpacker.unpackInt(); | |
} | |
} | |
} | |
public class Long06 extends BenchmarkRunner { | |
private MessagePack msgpack = new MessagePack(); | |
public ByteBuffer runSerialize() throws Exception { | |
Packer packer = msgpack.createPacker(newOutputStream(5 * scale)); | |
for(int i=0; i < scale; i++) { | |
packer.write(rand.nextLong()); | |
} | |
packer.flush(); | |
return getBuffer(); | |
} | |
public void runDeserialize(ByteBuffer bb) throws Exception { | |
Unpacker unpacker = msgpack.createUnpacker(newInputStream(bb)); | |
for(int i=0; i < scale; i++) { | |
unpacker.readLong(); | |
} | |
} | |
} | |
public class Long07 extends BenchmarkRunner { | |
public ByteBuffer runSerialize() throws Exception { | |
MessagePacker packer = new MessagePacker(newOutputStream(5 * scale)); | |
for(int i=0; i < scale; i++) { | |
packer.packLong(rand.nextLong()); | |
} | |
return getBuffer(); | |
} | |
public void runDeserialize(ByteBuffer bb) throws Exception { | |
MessageUnpacker unpacker = new MessageUnpacker(newInputStream(bb)); | |
for(int i=0; i < scale; i++) { | |
unpacker.unpackLong(); | |
} | |
} | |
} | |
public class Bin06 extends BenchmarkRunner { | |
private MessagePack msgpack = new MessagePack(); | |
private byte[] data = new byte[scale]; | |
public ByteBuffer runSerialize() throws Exception { | |
Packer packer = msgpack.createPacker(newOutputStream((data.length + 5) * 100)); | |
for(int i=0; i < 100; i++) { | |
packer.write(data); | |
} | |
packer.flush(); | |
return getBuffer(); | |
} | |
public void runDeserialize(ByteBuffer bb) throws Exception { | |
Unpacker unpacker = msgpack.createUnpacker(newInputStream(bb)); | |
for(int i=0; i < 100; i++) { | |
unpacker.readByteArray(); | |
} | |
} | |
} | |
public class Bin07 extends BenchmarkRunner { | |
private byte[] data = new byte[scale]; | |
public ByteBuffer runSerialize() throws Exception { | |
MessagePacker packer = new MessagePacker(newOutputStream((data.length + 5) * 100)); | |
for(int i=0; i < 100; i++) { | |
packer.packRawStringHeader(data.length); | |
packer.writePayload(data); | |
} | |
return getBuffer(); | |
} | |
public void runDeserialize(ByteBuffer bb) throws Exception { | |
MessageUnpacker unpacker = new MessageUnpacker(newInputStream(bb)); | |
for(int i=0; i < 100; i++) { | |
int length = unpacker.unpackRawStringHeader(); | |
byte[] dst = new byte[length]; | |
unpacker.readPayload(dst, 0, length); | |
} | |
} | |
} | |
public class Str06 extends BenchmarkRunner { | |
private MessagePack msgpack = new MessagePack(); | |
private String data = new String(new byte[scale]); | |
public ByteBuffer runSerialize() throws Exception { | |
Packer packer = msgpack.createPacker(newOutputStream((data.length() + 5) * 100)); | |
for(int i=0; i < 100; i++) { | |
packer.write(data); | |
} | |
packer.flush(); | |
return getBuffer(); | |
} | |
public void runDeserialize(ByteBuffer bb) throws Exception { | |
Unpacker unpacker = msgpack.createUnpacker(newInputStream(bb)); | |
for(int i=0; i < 100; i++) { | |
unpacker.readByteArray(); | |
} | |
} | |
} | |
public class Str07 extends BenchmarkRunner { | |
private String data = new String(new byte[scale]); | |
public ByteBuffer runSerialize() throws Exception { | |
MessagePacker packer = new MessagePacker(newOutputStream((data.length() + 5) * 100)); | |
for(int i=0; i < 100; i++) { | |
packer.packString(data); | |
} | |
return getBuffer(); | |
} | |
public void runDeserialize(ByteBuffer bb) throws Exception { | |
MessageUnpacker unpacker = new MessageUnpacker(newInputStream(bb)); | |
for(int i=0; i < 100; i++) { | |
unpacker.unpackString(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/msgpack-core/src/main/java/org/msgpack/core/MessagePack.java b/msgpack-core/src/main/java/org/msgpack/core/MessagePack.java | |
index 9cd4238..a544a37 100644 | |
--- a/msgpack-core/src/main/java/org/msgpack/core/MessagePack.java | |
+++ b/msgpack-core/src/main/java/org/msgpack/core/MessagePack.java | |
@@ -52,11 +52,13 @@ public class MessagePack { | |
public static final class Code { | |
public static final boolean isFixInt(byte b) { | |
- return isPosFixInt(b) || isNegFixInt(b); | |
+ int v = b & 0xff; | |
+ return v >= 0xe0 || v < 0x7f; | |
+ //return isPosFixInt(b) || isNegFixInt(b); | |
} | |
public static final boolean isPosFixInt(byte b) { | |
- return (b & POSFIXINT_MASK) == (byte) 0; | |
+ return (b & POSFIXINT_MASK) == 0; | |
} | |
public static final boolean isNegFixInt(byte b) { | |
return (b & NEGFIXINT_PREFIX) == NEGFIXINT_PREFIX; | |
diff --git a/msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java b/msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java | |
index 00bcdbe..26d8df5 100644 | |
--- a/msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java | |
+++ b/msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java | |
@@ -62,6 +62,7 @@ import static org.msgpack.core.Preconditions.*; | |
* </pre> | |
*/ | |
public class MessageUnpacker implements Closeable { | |
+ private static final MessageBuffer EMPTY_BUFFER = MessageBuffer.wrap(new byte[0]); | |
private final MessagePack.Config config; | |
@@ -70,7 +71,9 @@ public class MessageUnpacker implements Closeable { | |
/** | |
* Points to the current buffer to read | |
*/ | |
- private MessageBuffer buffer; | |
+ //private MessageBuffer buffer; | |
+ private MessageBuffer buffer = EMPTY_BUFFER; | |
+ | |
/** | |
* Cursor position in the current buffer | |
*/ | |
@@ -176,6 +179,7 @@ public class MessageUnpacker implements Closeable { | |
} | |
private MessageBuffer takeNextBuffer() throws IOException { | |
+ /* | |
if(reachedEOF) | |
return null; | |
@@ -191,6 +195,15 @@ public class MessageUnpacker implements Closeable { | |
if(nextBuffer == null) { | |
reachedEOF = true; | |
} | |
+ | |
+ return nextBuffer; | |
+ */ | |
+ | |
+ MessageBuffer nextBuffer = in.next(); | |
+ if(nextBuffer == null) { | |
+ throw new EOFException(); | |
+ } | |
+ | |
return nextBuffer; | |
} | |
@@ -296,9 +309,17 @@ public class MessageUnpacker implements Closeable { | |
* Get the head byte value and proceeds the cursor by 1 | |
*/ | |
private byte consume() throws IOException { | |
+ /* | |
byte b = lookAhead(); | |
position += 1; | |
return b; | |
+ */ | |
+ | |
+ while(buffer.size() == position) { | |
+ buffer = takeNextBuffer(); | |
+ position = 0; | |
+ } | |
+ return buffer.getByte(position++); | |
} | |
/** | |
@@ -319,30 +340,76 @@ public class MessageUnpacker implements Closeable { | |
* @throws IOException | |
*/ | |
private byte readByte() throws IOException { | |
+ return consume(); | |
+ /* | |
if(!ensure(1)) { | |
throw new MessageFormatException("insufficient data length for reading byte value"); | |
} | |
byte b = buffer.getByte(position); | |
consume(1); | |
return b; | |
+ */ | |
} | |
private short readShort() throws IOException { | |
+ /* | |
if(!ensure(2)) { | |
throw new MessageFormatException("insufficient data length for reading short value"); | |
} | |
short s = buffer.getShort(position); | |
consume(2); | |
return s; | |
+ */ | |
+ | |
+ int remaining = buffer.size() - position; | |
+ if(remaining < 2) { | |
+ MessageBuffer nextBuffer = takeNextBuffer(); | |
+ if(remaining > 0 || nextBuffer.size() < 2) { | |
+ readToExtraBuffer(nextBuffer, 2); | |
+ return extraBuffer.getShort(0); | |
+ } | |
+ buffer = nextBuffer; | |
+ position = 0; | |
+ } | |
+ short v = buffer.getShort(position); | |
+ position += 2; | |
+ return v; | |
} | |
private int readInt() throws IOException { | |
+ /* | |
if(!ensure(4)) { | |
throw new MessageFormatException("insufficient data length for reading int value"); | |
} | |
int i = buffer.getInt(position); | |
consume(4); | |
return i; | |
+ */ | |
+ | |
+ int remaining = buffer.size() - position; | |
+ if(remaining < 4) { | |
+ MessageBuffer nextBuffer = takeNextBuffer(); | |
+ if(remaining > 0 || nextBuffer.size() < 4) { | |
+ readToExtraBuffer(nextBuffer, 4); | |
+ return extraBuffer.getInt(0); | |
+ } | |
+ buffer = nextBuffer; | |
+ position = 0; | |
+ } | |
+ int v = buffer.getInt(position); | |
+ position += 4; | |
+ return v; | |
+ } | |
+ | |
+ private void readToExtraBuffer(MessageBuffer nextBuffer, int requiredSize) { | |
+ int bufferSize = buffer.size() - position; | |
+ int nextBufferSize = requiredSize - bufferSize; | |
+ | |
+ buffer.copyTo(position, extraBuffer, 0, bufferSize); | |
+ nextBuffer.copyTo(0, extraBuffer, bufferSize, nextBufferSize); | |
+ | |
+ buffer = nextBuffer; | |
+ position = nextBufferSize; | |
} | |
private float readFloat() throws IOException { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CPU: 2.7 GHz Intel Core i7 | |
Memory: 16GB 1600 MHz DDR3 | |
OS: Mac OS X 10.9.2 | |
Java: 1.7.0_45 | |
java -cp msgpack-java-0.6.11.jar:javassist-3.18.2-GA.jar:../msgpack-core/target/msgpack-core-0.7.0-SNAPSHOT.jar:. Microbench 1700461847 int 1000000 10 | |
seed: 1700461847 | |
scale: 1000000 | |
loop: 10 | |
-- class Microbench$Int06 serialize | |
4882.76 KB | |
102.70 msec/loop | |
46.43 MB/s | |
-- class Microbench$Int06 deserialize | |
4882.76 KB | |
35.30 msec/loop | |
135.08 MB/s | |
-- class Microbench$Int07 serialize | |
4878.76 KB | |
18.60 msec/loop | |
256.15 MB/s | |
-- class Microbench$Int07 deserialize | |
4878.76 KB | |
12.70 msec/loop | |
375.15 MB/s | |
java -cp msgpack-java-0.6.11.jar:javassist-3.18.2-GA.jar:../msgpack-core/target/msgpack-core-0.7.0-SNAPSHOT.jar:. Microbench 1700461847 long 1000000 10 | |
seed: 1700461847 | |
scale: 1000000 | |
loop: 10 | |
-- class Microbench$Long06 serialize | |
8789.06 KB | |
85.70 msec/loop | |
100.15 MB/s | |
-- class Microbench$Long06 deserialize | |
8789.06 KB | |
37.50 msec/loop | |
228.88 MB/s | |
-- class Microbench$Long07 serialize | |
8781.86 KB | |
30.90 msec/loop | |
277.54 MB/s | |
-- class Microbench$Long07 deserialize | |
8781.86 KB | |
14.30 msec/loop | |
599.72 MB/s | |
java -cp msgpack-java-0.6.11.jar:javassist-3.18.2-GA.jar:../msgpack-core/target/msgpack-core-0.7.0-SNAPSHOT.jar:. Microbench 1700461847 bin 1000000 10 | |
seed: 1700461847 | |
scale: 1000000 | |
loop: 10 | |
-- class Microbench$Bin06 serialize | |
97656.74 KB | |
73.20 msec/loop | |
1302.84 MB/s | |
-- class Microbench$Bin06 deserialize | |
97656.74 KB | |
20.50 msec/loop | |
4652.09 MB/s | |
-- class Microbench$Bin07 serialize | |
97656.74 KB | |
94.20 msec/loop | |
1012.40 MB/s | |
-- class Microbench$Bin07 deserialize | |
97656.74 KB | |
43.10 msec/loop | |
2212.71 MB/s | |
java -cp msgpack-java-0.6.11.jar:javassist-3.18.2-GA.jar:../msgpack-core/target/msgpack-core-0.7.0-SNAPSHOT.jar:. Microbench 1700461847 str 1000000 10 | |
seed: 1700461847 | |
scale: 1000000 | |
loop: 10 | |
-- class Microbench$Str06 serialize | |
97656.74 KB | |
171.50 msec/loop | |
556.08 MB/s | |
-- class Microbench$Str06 deserialize | |
97656.74 KB | |
67.70 msec/loop | |
1408.68 MB/s | |
-- class Microbench$Str07 serialize | |
97656.74 KB | |
628.50 msec/loop | |
151.74 MB/s | |
-- class Microbench$Str07 deserialize | |
97656.74 KB | |
228.50 msec/loop | |
417.37 MB/s | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment