enable packed encode in benchmarks. still some bugs

This commit is contained in:
David Renshaw 2014-09-29 14:34:17 -04:00
parent b186b4b794
commit 8264742779
6 changed files with 81 additions and 29 deletions

View file

@ -0,0 +1,15 @@
package org.capnproto.benchmark;
import java.io.IOException;
public interface Compression {
public void writeBuffered(org.capnproto.BufferedOutputStream writer,
org.capnproto.MessageBuilder message) throws IOException;
public org.capnproto.MessageReader newBufferedReader(
org.capnproto.BufferedInputStream inputStream) throws IOException;
public final Compression PACKED = new Packed();
public final Compression UNCOMPRESSED = new Uncompressed();
}

View file

@ -0,0 +1,15 @@
package org.capnproto.benchmark;
import java.io.IOException;
public final class Packed implements Compression {
public void writeBuffered(org.capnproto.BufferedOutputStream writer,
org.capnproto.MessageBuilder message) throws IOException {
org.capnproto.SerializePacked.writeMessage(writer, message);
}
public org.capnproto.MessageReader newBufferedReader(
org.capnproto.BufferedInputStream inputStream) throws IOException {
return org.capnproto.SerializePacked.newReader(inputStream);
}
}

View file

@ -23,7 +23,7 @@ public abstract class TestCase<RequestFactory extends StructFactory<RequestBuild
public void passByObject(RequestFactory requestFactory, ResponseFactory responseFactory, public void passByObject(RequestFactory requestFactory, ResponseFactory responseFactory,
long iters) { Compression compression, long iters) {
Common.FastRand rng = new Common.FastRand(); Common.FastRand rng = new Common.FastRand();
@ -43,7 +43,7 @@ public abstract class TestCase<RequestFactory extends StructFactory<RequestBuild
static final int SCRATCH_SIZE = 128 * 1024; static final int SCRATCH_SIZE = 128 * 1024;
public void passByBytes(RequestFactory requestFactory, ResponseFactory responseFactory, public void passByBytes(RequestFactory requestFactory, ResponseFactory responseFactory,
long iters) throws IOException { Compression compression, long iters) throws IOException {
ByteBuffer requestBytes = ByteBuffer.allocate(SCRATCH_SIZE * 8); ByteBuffer requestBytes = ByteBuffer.allocate(SCRATCH_SIZE * 8);
ByteBuffer responseBytes = ByteBuffer.allocate(SCRATCH_SIZE * 8); ByteBuffer responseBytes = ByteBuffer.allocate(SCRATCH_SIZE * 8);
@ -58,23 +58,23 @@ public abstract class TestCase<RequestFactory extends StructFactory<RequestBuild
{ {
org.capnproto.ArrayOutputStream writer = new org.capnproto.ArrayOutputStream(requestBytes); org.capnproto.ArrayOutputStream writer = new org.capnproto.ArrayOutputStream(requestBytes);
org.capnproto.Serialize.writeMessage(writer, requestMessage); compression.writeBuffered(writer, requestMessage);
} }
{ {
org.capnproto.MessageReader messageReader = org.capnproto.MessageReader messageReader =
org.capnproto.ByteChannelMessageReader.create(new org.capnproto.ArrayInputStream(requestBytes)); compression.newBufferedReader(new org.capnproto.ArrayInputStream(requestBytes));
this.handleRequest(messageReader.getRoot(requestFactory), response); this.handleRequest(messageReader.getRoot(requestFactory), response);
} }
{ {
org.capnproto.ArrayOutputStream writer = new org.capnproto.ArrayOutputStream(responseBytes); org.capnproto.ArrayOutputStream writer = new org.capnproto.ArrayOutputStream(responseBytes);
org.capnproto.Serialize.writeMessage(writer, responseMessage); compression.writeBuffered(writer, responseMessage);
} }
{ {
org.capnproto.MessageReader messageReader = org.capnproto.MessageReader messageReader =
org.capnproto.ByteChannelMessageReader.create(new org.capnproto.ArrayInputStream(responseBytes)); compression.newBufferedReader(new org.capnproto.ArrayInputStream(responseBytes));
if (!this.checkResponse(messageReader.getRoot(responseFactory), expected)) { if (!this.checkResponse(messageReader.getRoot(responseFactory), expected)) {
throw new Error("incorrect response"); throw new Error("incorrect response");
} }
@ -83,38 +83,39 @@ public abstract class TestCase<RequestFactory extends StructFactory<RequestBuild
} }
public void syncServer(RequestFactory requestFactory, ResponseFactory responseFactory, public void syncServer(RequestFactory requestFactory, ResponseFactory responseFactory,
long iters) throws IOException { Compression compression, long iters) throws IOException {
org.capnproto.BufferedOutputStreamWrapper outBuffered =
new org.capnproto.BufferedOutputStreamWrapper((new FileOutputStream(FileDescriptor.out)).getChannel());
org.capnproto.BufferedInputStreamWrapper inBuffered =
new org.capnproto.BufferedInputStreamWrapper((new FileInputStream(FileDescriptor.in)).getChannel());
for (int ii = 0; ii < iters; ++ii) { for (int ii = 0; ii < iters; ++ii) {
MessageBuilder responseMessage = new MessageBuilder(); MessageBuilder responseMessage = new MessageBuilder();
{ {
ResponseBuilder response = responseMessage.initRoot(responseFactory); ResponseBuilder response = responseMessage.initRoot(responseFactory);
MessageReader messageReader = compression.newBufferedReader(inBuffered);
MessageReader messageReader = org.capnproto.ByteChannelMessageReader.create(
(new FileInputStream(FileDescriptor.in)).getChannel());
RequestReader request = messageReader.getRoot(requestFactory); RequestReader request = messageReader.getRoot(requestFactory);
this.handleRequest(request, response); this.handleRequest(request, response);
} }
compression.writeBuffered(outBuffered, responseMessage);
org.capnproto.Serialize.writeMessage((new FileOutputStream(FileDescriptor.out)).getChannel(),
responseMessage);
} }
} }
public void syncClient(RequestFactory requestFactory, ResponseFactory responseFactory, public void syncClient(RequestFactory requestFactory, ResponseFactory responseFactory,
long iters) throws IOException { Compression compression, long iters) throws IOException {
Common.FastRand rng = new Common.FastRand(); Common.FastRand rng = new Common.FastRand();
org.capnproto.BufferedOutputStreamWrapper outBuffered =
new org.capnproto.BufferedOutputStreamWrapper((new FileOutputStream(FileDescriptor.out)).getChannel());
org.capnproto.BufferedInputStreamWrapper inBuffered =
new org.capnproto.BufferedInputStreamWrapper((new FileInputStream(FileDescriptor.in)).getChannel());
for (int ii = 0; ii < iters; ++ii) { for (int ii = 0; ii < iters; ++ii) {
MessageBuilder requestMessage = new MessageBuilder(); MessageBuilder requestMessage = new MessageBuilder();
RequestBuilder request = requestMessage.initRoot(requestFactory); RequestBuilder request = requestMessage.initRoot(requestFactory);
Expectation expected = this.setupRequest(rng, request); Expectation expected = this.setupRequest(rng, request);
org.capnproto.Serialize.writeMessage((new FileOutputStream(FileDescriptor.out)).getChannel(), compression.writeBuffered(outBuffered, requestMessage);
requestMessage); MessageReader messageReader = compression.newBufferedReader(inBuffered);
MessageReader messageReader = org.capnproto.ByteChannelMessageReader.create(
(new FileInputStream(FileDescriptor.in)).getChannel());
ResponseReader response = messageReader.getRoot(responseFactory); ResponseReader response = messageReader.getRoot(responseFactory);
if (!this.checkResponse(response, expected)) { if (!this.checkResponse(response, expected)) {
throw new Error("incorrect response"); throw new Error("incorrect response");
@ -131,20 +132,27 @@ public abstract class TestCase<RequestFactory extends StructFactory<RequestBuild
String mode = args[0]; String mode = args[0];
String reuse = args[1]; String reuse = args[1];
String compression = args[2]; Compression compression = null;
if (args[2].equals("packed")) {
compression = Compression.PACKED;
} else if (args[2].equals("none")) {
compression = Compression.UNCOMPRESSED;
} else {
throw new Error("unrecognized compression: " + args[2]);
}
long iters = Long.parseLong(args[3]); long iters = Long.parseLong(args[3]);
try { try {
if (mode.equals("object")) { if (mode.equals("object")) {
passByObject(requestFactory, responseFactory, iters); passByObject(requestFactory, responseFactory, compression, iters);
} else if (mode.equals("bytes")) { } else if (mode.equals("bytes")) {
passByBytes(requestFactory, responseFactory, iters); passByBytes(requestFactory, responseFactory, compression, iters);
} else if (mode.equals("client")) { } else if (mode.equals("client")) {
syncClient(requestFactory, responseFactory, iters); syncClient(requestFactory, responseFactory, compression, iters);
} else if (mode.equals("server")) { } else if (mode.equals("server")) {
syncServer(requestFactory, responseFactory, iters); syncServer(requestFactory, responseFactory, compression, iters);
} else { } else {
System.out.println("unknown mode: " + mode); System.out.println("unrecognized mode: " + mode);
} }
} catch (IOException e) { } catch (IOException e) {
System.err.println("IOException: " + e); System.err.println("IOException: " + e);

View file

@ -0,0 +1,16 @@
package org.capnproto.benchmark;
import java.io.IOException;
public final class Uncompressed implements Compression {
public final void writeBuffered(org.capnproto.BufferedOutputStream writer,
org.capnproto.MessageBuilder message) throws IOException {
org.capnproto.Serialize.writeMessage(writer, message);
writer.flush();
}
public final org.capnproto.MessageReader newBufferedReader(
org.capnproto.BufferedInputStream inputStream) throws IOException {
return org.capnproto.ByteChannelMessageReader.create(inputStream);
}
}

View file

@ -49,7 +49,6 @@ public final class BufferedInputStreamWrapper implements BufferedInputStream {
return fromFirstBuffer + numBytes; return fromFirstBuffer + numBytes;
} else { } else {
//# Forward large read to the underlying stream. //# Forward large read to the underlying stream.
this.buf.clear(); this.buf.clear();
return fromFirstBuffer + readAtLeast(this.inner, dst, numBytes); return fromFirstBuffer + readAtLeast(this.inner, dst, numBytes);
} }

View file

@ -68,7 +68,6 @@ public final class ByteChannelMessageReader {
segmentSlices[0].order(ByteOrder.LITTLE_ENDIAN); segmentSlices[0].order(ByteOrder.LITTLE_ENDIAN);
int offset = segment0Size; int offset = segment0Size;
for (int ii = 1; ii < segmentCount; ++ii) { for (int ii = 1; ii < segmentCount; ++ii) {
allSegments.position(offset * Constants.BYTES_PER_WORD); allSegments.position(offset * Constants.BYTES_PER_WORD);
segmentSlices[ii] = allSegments.slice(); segmentSlices[ii] = allSegments.slice();