Skip to content

Commit

Permalink
Improve benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Jul 3, 2017
1 parent d48f783 commit ff37ada
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 38 deletions.
Expand Up @@ -22,7 +22,6 @@
import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State; import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;


import static io.vertx.benchmarks.HeadersUtils.setBaseHeaders; import static io.vertx.benchmarks.HeadersUtils.setBaseHeaders;


Expand All @@ -44,14 +43,12 @@ public void setup() {
} }


@Benchmark @Benchmark
public void nettySmall(Blackhole blackhole) throws Exception { public boolean nettySmall() throws Exception {
boolean result = nettySmallHeaders.contains(HeadersUtils.CONTENT_LENGTH_HEADER); return nettySmallHeaders.contains(HeadersUtils.CONTENT_LENGTH_HEADER);
blackhole.consume(result);
} }


@Benchmark @Benchmark
public void vertxSmall(Blackhole blackhole) throws Exception { public boolean vertxSmall() throws Exception {
boolean result = vertxSmallHeaders.contains(HeadersUtils.CONTENT_LENGTH_HEADER); return vertxSmallHeaders.contains(HeadersUtils.CONTENT_LENGTH_HEADER);
blackhole.consume(result);
} }
} }
Expand Up @@ -23,10 +23,10 @@
import io.netty.handler.codec.http.HttpResponseEncoder; import io.netty.handler.codec.http.HttpResponseEncoder;
import io.vertx.core.http.impl.headers.VertxHttpHeaders; import io.vertx.core.http.impl.headers.VertxHttpHeaders;
import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.CompilerControl;
import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State; import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;


import static io.vertx.benchmarks.HeadersUtils.setBaseHeaders; import static io.vertx.benchmarks.HeadersUtils.setBaseHeaders;


Expand All @@ -36,6 +36,10 @@
@State(Scope.Thread) @State(Scope.Thread)
public class HeadersEncodeBenchmark extends BenchmarkBase { public class HeadersEncodeBenchmark extends BenchmarkBase {


@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public static void consume(final ByteBuf buf) {
}

static class PublicEncoder extends HttpResponseEncoder { static class PublicEncoder extends HttpResponseEncoder {


// Make it public // Make it public
Expand Down Expand Up @@ -63,23 +67,23 @@ public void setup() {
} }


@Benchmark @Benchmark
public void baseline(Blackhole blackhole) throws Exception { public void baseline() throws Exception {
byteBuf.resetWriterIndex(); byteBuf.resetWriterIndex();
encoder.encodeHeaders(emptyHeaders, byteBuf); encoder.encodeHeaders(emptyHeaders, byteBuf);
blackhole.consume(byteBuf); consume(byteBuf);
} }


@Benchmark @Benchmark
public void nettySmall(Blackhole blackhole) throws Exception { public void nettySmall() throws Exception {
byteBuf.resetWriterIndex(); byteBuf.resetWriterIndex();
encoder.encodeHeaders(nettySmallHeaders, byteBuf); encoder.encodeHeaders(nettySmallHeaders, byteBuf);
blackhole.consume(byteBuf); consume(byteBuf);
} }


@Benchmark @Benchmark
public void vertxSmall(Blackhole blackhole) throws Exception { public void vertxSmall() throws Exception {
byteBuf.resetWriterIndex(); byteBuf.resetWriterIndex();
encoder.encodeHeaders(vertxSmallHeaders, byteBuf); encoder.encodeHeaders(vertxSmallHeaders, byteBuf);
blackhole.consume(byteBuf); consume(byteBuf);
} }
} }
14 changes: 9 additions & 5 deletions src/test/benchmarks/io/vertx/benchmarks/HeadersSetBenchmark.java
Expand Up @@ -19,10 +19,10 @@
import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpHeaders;
import io.vertx.core.http.impl.headers.VertxHttpHeaders; import io.vertx.core.http.impl.headers.VertxHttpHeaders;
import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.CompilerControl;
import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State; import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;


import static io.vertx.benchmarks.HeadersUtils.setBaseHeaders; import static io.vertx.benchmarks.HeadersUtils.setBaseHeaders;


Expand All @@ -32,6 +32,10 @@
@State(Scope.Thread) @State(Scope.Thread)
public class HeadersSetBenchmark extends BenchmarkBase { public class HeadersSetBenchmark extends BenchmarkBase {


@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public static void consume(final HttpHeaders headers) {
}

private HttpHeaders nettySmallHeaders; private HttpHeaders nettySmallHeaders;
private VertxHttpHeaders vertxSmallHeaders; private VertxHttpHeaders vertxSmallHeaders;


Expand All @@ -42,16 +46,16 @@ public void setup() {
} }


@Benchmark @Benchmark
public void nettySmall(Blackhole blackhole) throws Exception { public void nettySmall() throws Exception {
nettySmallHeaders.clear(); nettySmallHeaders.clear();
setBaseHeaders(nettySmallHeaders); setBaseHeaders(nettySmallHeaders);
blackhole.consume(nettySmallHeaders); consume(nettySmallHeaders);
} }


@Benchmark @Benchmark
public void vertxSmall(Blackhole blackhole) throws Exception { public void vertxSmall() throws Exception {
vertxSmallHeaders.clear(); vertxSmallHeaders.clear();
setBaseHeaders(vertxSmallHeaders); setBaseHeaders(vertxSmallHeaders);
blackhole.consume(vertxSmallHeaders); consume(vertxSmallHeaders);
} }
} }
161 changes: 147 additions & 14 deletions src/test/benchmarks/io/vertx/benchmarks/HttpServerHandlerBenchmark.java
Expand Up @@ -16,6 +16,8 @@
package io.vertx.benchmarks; package io.vertx.benchmarks;


import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
Expand Down Expand Up @@ -47,12 +49,11 @@
import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonObject;
import io.vertx.core.net.impl.HandlerHolder; import io.vertx.core.net.impl.HandlerHolder;
import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.CompilerControl;
import org.openjdk.jmh.annotations.Fork; import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State; import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.infra.Blackhole;


import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
Expand All @@ -62,17 +63,135 @@
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a> * @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/ */
@State(Scope.Thread) @State(Scope.Thread)
@Threads(16)
public class HttpServerHandlerBenchmark extends BenchmarkBase { public class HttpServerHandlerBenchmark extends BenchmarkBase {


private static final ByteBuf GET = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer(( @CompilerControl(CompilerControl.Mode.DONT_INLINE)
"GET / HTTP/1.1\r\n" + public static void consume(final ByteBuf buf) {
"\r\n").getBytes())); }


ByteBuf GET;
int readerIndex;
int writeIndex;
VertxInternal vertx; VertxInternal vertx;
EmbeddedChannel vertxChannel; EmbeddedChannel vertxChannel;
EmbeddedChannel nettyChannel; EmbeddedChannel nettyChannel;


static class Alloc implements ByteBufAllocator {

private final ByteBuf buf = Unpooled.buffer();
private final int capacity = buf.capacity();

@Override
public ByteBuf buffer() {
buf.clear();
return buf;
}

@Override
public ByteBuf buffer(int initialCapacity) {
if (initialCapacity < capacity) {
return buffer();
} else {
throw new IllegalArgumentException();
}
}

@Override
public ByteBuf buffer(int initialCapacity, int maxCapacity) {
if (initialCapacity < capacity) {
return buffer();
} else {
throw new IllegalArgumentException();
}
}

@Override
public ByteBuf ioBuffer() {
throw new UnsupportedOperationException();
}

@Override
public ByteBuf ioBuffer(int initialCapacity) {
throw new UnsupportedOperationException();
}

@Override
public ByteBuf ioBuffer(int initialCapacity, int maxCapacity) {
throw new UnsupportedOperationException();
}

@Override
public ByteBuf heapBuffer() {
throw new UnsupportedOperationException();
}

@Override
public ByteBuf heapBuffer(int initialCapacity) {
throw new UnsupportedOperationException();
}

@Override
public ByteBuf heapBuffer(int initialCapacity, int maxCapacity) {
throw new UnsupportedOperationException();
}

@Override
public ByteBuf directBuffer() {
throw new UnsupportedOperationException();
}

@Override
public ByteBuf directBuffer(int initialCapacity) {
throw new UnsupportedOperationException();
}

@Override
public ByteBuf directBuffer(int initialCapacity, int maxCapacity) {
throw new UnsupportedOperationException();
}

@Override
public CompositeByteBuf compositeBuffer() {
throw new UnsupportedOperationException();
}

@Override
public CompositeByteBuf compositeBuffer(int maxNumComponents) {
throw new UnsupportedOperationException();
}

@Override
public CompositeByteBuf compositeHeapBuffer() {
throw new UnsupportedOperationException();
}

@Override
public CompositeByteBuf compositeHeapBuffer(int maxNumComponents) {
throw new UnsupportedOperationException();
}

@Override
public CompositeByteBuf compositeDirectBuffer() {
throw new UnsupportedOperationException();
}

@Override
public CompositeByteBuf compositeDirectBuffer(int maxNumComponents) {
throw new UnsupportedOperationException();
}

@Override
public boolean isDirectBufferPooled() {
throw new UnsupportedOperationException();
}

@Override
public int calculateNewCapacity(int minNewCapacity, int maxCapacity) {
throw new UnsupportedOperationException();
}
}


private static final CharSequence RESPONSE_TYPE_PLAIN = io.vertx.core.http.HttpHeaders.createOptimized("text/plain"); private static final CharSequence RESPONSE_TYPE_PLAIN = io.vertx.core.http.HttpHeaders.createOptimized("text/plain");


private static final String HELLO_WORLD = "Hello, world!"; private static final String HELLO_WORLD = "Hello, world!";
Expand Down Expand Up @@ -100,6 +219,7 @@ public void setup() {
options.getDecoderInitialBufferSize()), options.getDecoderInitialBufferSize()),
new HttpResponseEncoder() new HttpResponseEncoder()
); );
vertxChannel.config().setAllocator(new Alloc());
ContextImpl context = new EventLoopContext(vertx, vertxChannel.eventLoop(), null, null, null, new JsonObject(), Thread.currentThread().getContextClassLoader()); ContextImpl context = new EventLoopContext(vertx, vertxChannel.eventLoop(), null, null, null, new JsonObject(), Thread.currentThread().getContextClassLoader());
Handler<HttpServerRequest> app = request -> { Handler<HttpServerRequest> app = request -> {
HttpServerResponse response = request.response(); HttpServerResponse response = request.response();
Expand Down Expand Up @@ -141,8 +261,11 @@ public void setup() {
@Override @Override
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception { protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
writeResponse(ctx, msg, PLAINTEXT_CONTENT_BUFFER.duplicate(), TYPE_PLAIN, PLAINTEXT_CLHEADER_VALUE); writeResponse(ctx, msg, PLAINTEXT_CONTENT_BUFFER.duplicate(), TYPE_PLAIN, PLAINTEXT_CLHEADER_VALUE);
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND, Unpooled.EMPTY_BUFFER, false); }
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
} }


private void writeResponse(ChannelHandlerContext ctx, HttpRequest request, ByteBuf buf, CharSequence contentType, CharSequence contentLength) { private void writeResponse(ChannelHandlerContext ctx, HttpRequest request, ByteBuf buf, CharSequence contentType, CharSequence contentLength) {
Expand All @@ -159,13 +282,21 @@ private void writeResponse(ChannelHandlerContext ctx, HttpRequest request, ByteB
ctx.write(response, ctx.voidPromise()); ctx.write(response, ctx.voidPromise());
} }
}); });
nettyChannel.config().setAllocator(new Alloc());

GET = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer((
"GET / HTTP/1.1\r\n" +
"\r\n").getBytes()));
readerIndex = GET.readerIndex();
writeIndex = GET.writerIndex();
} }


@Benchmark @Benchmark
public void vertx(Blackhole blackhole) { public void vertx() {
GET.setIndex(readerIndex, writeIndex);
vertxChannel.writeInbound(GET); vertxChannel.writeInbound(GET);
ByteBuf result = (ByteBuf) vertxChannel.outboundMessages().poll(); ByteBuf result = (ByteBuf) vertxChannel.outboundMessages().poll();
blackhole.consume(result); consume(result);
} }


@Fork(value = 1, jvmArgsAppend = { @Fork(value = 1, jvmArgsAppend = {
Expand All @@ -174,16 +305,18 @@ public void vertx(Blackhole blackhole) {
"-Dvertx.disableTCCL=true ", "-Dvertx.disableTCCL=true ",
}) })
@Benchmark @Benchmark
public void vertxOpt(Blackhole blackhole) { public void vertxOpt() {
GET.setIndex(readerIndex, writeIndex);
vertxChannel.writeInbound(GET); vertxChannel.writeInbound(GET);
ByteBuf result = (ByteBuf) vertxChannel.outboundMessages().poll(); ByteBuf result = (ByteBuf) vertxChannel.outboundMessages().poll();
blackhole.consume(result); consume(result);
} }


@Benchmark @Benchmark
public void netty(Blackhole blackhole) { public void netty() {
GET.setIndex(readerIndex, writeIndex);
nettyChannel.writeInbound(GET); nettyChannel.writeInbound(GET);
ByteBuf result = (ByteBuf) nettyChannel.outboundMessages().poll(); ByteBuf result = (ByteBuf) nettyChannel.outboundMessages().poll();
blackhole.consume(result); consume(result);
} }
} }
Expand Up @@ -19,18 +19,22 @@
import io.vertx.core.Vertx; import io.vertx.core.Vertx;
import io.vertx.core.impl.BenchmarkContext; import io.vertx.core.impl.BenchmarkContext;
import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.CompilerControl;
import org.openjdk.jmh.annotations.Fork; import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State; import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;


/** /**
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a> * @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/ */
@State(Scope.Thread) @State(Scope.Thread)
public class RunOnContextBenchmark extends BenchmarkBase { public class RunOnContextBenchmark extends BenchmarkBase {


@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public static void consume(final String buf) {
}

@State(Scope.Thread) @State(Scope.Thread)
public static class BaselineState { public static class BaselineState {


Expand All @@ -39,12 +43,10 @@ public static class BaselineState {
Handler<Void> task; Handler<Void> task;


@Setup @Setup
public void setup(Blackhole hole) { public void setup() {
vertx = Vertx.vertx(); vertx = Vertx.vertx();
context = BenchmarkContext.create(vertx); context = BenchmarkContext.create(vertx);
task = v -> { task = v -> consume("the-string");
hole.consume("the-string");
};
} }
} }


Expand Down

0 comments on commit ff37ada

Please sign in to comment.