Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor server optimizations #11306

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions benchmarks/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ dependencies {
api project(":inject-java-test")
api project(":http-server")
api project(":http-server-netty")
api project(":http-client")
api project(":jackson-databind")
api project(":router")
api project(":runtime")

api platform(libs.test.boms.micronaut.validation)
api libs.managed.reactor
api (libs.micronaut.validation) {
exclude group: 'io.micronaut'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import io.micronaut.context.ApplicationContext;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Produces;
import io.micronaut.http.annotation.QueryValue;
import io.micronaut.http.server.netty.NettyHttpServer;
import io.micronaut.runtime.server.EmbeddedServer;
import io.netty.buffer.ByteBuf;
Expand All @@ -18,13 +21,17 @@
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Assertions;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.profile.AsyncProfiler;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
Expand All @@ -37,10 +44,10 @@
/**
* JMH benchmark that mimics the TechEmpower framework benchmarks.
*/
public class TfbLikeBenchmark {
public class ControllersBenchmark {
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(TfbLikeBenchmark.class.getName() + ".*")
.include(ControllersBenchmark.class.getName() + ".*")
.warmupIterations(20)
.measurementIterations(30)
.mode(Mode.AverageTime)
Expand All @@ -62,27 +69,29 @@ public void test(Holder holder) {

@State(Scope.Thread)
public static class Holder {
@Param
Request request;

ApplicationContext ctx;
EmbeddedChannel channel;
ByteBuf requestBytes;
ByteBuf responseBytes;

@Setup
public void setUp() {
public void setUp(Blackhole blackhole) {
ctx = ApplicationContext.run(Map.of(
"spec.name", "TfbLikeBenchmark",
"spec.name", "ControllersBenchmark",
"micronaut.server.date-header", false // disabling this makes the response identical each time
));
ctx.registerSingleton(Blackhole.class, blackhole);
EmbeddedServer server = ctx.getBean(EmbeddedServer.class);
channel = ((NettyHttpServer) server).buildEmbeddedChannel(false);

EmbeddedChannel clientChannel = new EmbeddedChannel();
clientChannel.pipeline().addLast(new HttpClientCodec());
clientChannel.pipeline().addLast(new HttpObjectAggregator(1000));

FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/plaintext");
request.headers().add(HttpHeaderNames.ACCEPT, "text/plain,text/html;q=0.9,application/xhtml+xml;q=0.9,application/xml;q=0.8,*/*;q=0.7");
clientChannel.writeOutbound(request);
clientChannel.writeOutbound(request.request());
clientChannel.flushOutbound();

requestBytes = NettyUtil.readAllOutboundContiguous(clientChannel);
Expand All @@ -91,11 +100,7 @@ public void setUp() {
responseBytes = exchange();
clientChannel.writeInbound(responseBytes.retainedDuplicate());
FullHttpResponse response = clientChannel.readInbound();
Assertions.assertEquals(HttpResponseStatus.OK, response.status());
Assertions.assertEquals("text/plain", response.headers().get(HttpHeaderNames.CONTENT_TYPE));
String expectedResponseBody = "Hello, World!";
Assertions.assertEquals(expectedResponseBody, response.content().toString(StandardCharsets.UTF_8));
Assertions.assertEquals(expectedResponseBody.length(), response.headers().getInt(HttpHeaderNames.CONTENT_LENGTH));
request.verifyResponse(response);
response.release();
}

Expand All @@ -113,9 +118,51 @@ public void tearDown() {
}
}

@Controller("/plaintext")
@Requires(property = "spec.name", value = "TfbLikeBenchmark")
static class PlainTextController {
public enum Request {
TFB_LIKE {
@Override
FullHttpRequest request() {
FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/tfblike");
request.headers().add(HttpHeaderNames.ACCEPT, "text/plain,text/html;q=0.9,application/xhtml+xml;q=0.9,application/xml;q=0.8,*/*;q=0.7");
return request;
}

@Override
void verifyResponse(FullHttpResponse response) {
Assertions.assertEquals(HttpResponseStatus.OK, response.status());
Assertions.assertEquals("text/plain", response.headers().get(HttpHeaderNames.CONTENT_TYPE));
String expectedResponseBody = "Hello, World!";
Assertions.assertEquals(expectedResponseBody, response.content().toString(StandardCharsets.UTF_8));
Assertions.assertEquals(expectedResponseBody.length(), response.headers().getInt(HttpHeaderNames.CONTENT_LENGTH));
}
},
MISSING_QUERY_PARAMETER {
@Override
FullHttpRequest request() {
FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/ctrl/text-echo/foo");
request.headers().add(HttpHeaderNames.ACCEPT, "text/plain");
return request;
}

@Override
void verifyResponse(FullHttpResponse response) {
Assertions.assertEquals(HttpResponseStatus.OK, response.status());
Assertions.assertEquals("text/plain", response.headers().get(HttpHeaderNames.CONTENT_TYPE));
String expectedResponseBody = "foo";
Assertions.assertEquals(expectedResponseBody, response.content().toString(StandardCharsets.UTF_8));
Assertions.assertEquals(expectedResponseBody.length(), response.headers().getInt(HttpHeaderNames.CONTENT_LENGTH));
}
}
;

abstract FullHttpRequest request();

abstract void verifyResponse(FullHttpResponse response);
}

@Controller("/tfblike")
@Requires(property = "spec.name", value = "ControllersBenchmark")
static class TfbLikeController {

private static final byte[] TEXT = "Hello, World!".getBytes(StandardCharsets.UTF_8);

Expand All @@ -124,4 +171,21 @@ public byte[] getPlainText() {
return TEXT;
}
}

@Controller("/ctrl")
@Requires(property = "spec.name", value = "ControllersBenchmark")
static class MyController {
@Inject
Blackhole blackhole;

@Get(uri = "/text-echo/{text}")
@Produces(MediaType.TEXT_PLAIN)
String echoMissingParameter(String text,
@Nullable @QueryValue("firstParameter") Integer firstParameter,
@Nullable @QueryValue("secondParameter") Integer secondParameter) {
blackhole.consume(firstParameter);
blackhole.consume(secondParameter);
return text;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package io.micronaut.http.server.stack;

import io.micronaut.context.ApplicationContext;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.http.ByteBodyHttpResponse;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.body.CloseableAvailableByteBody;
import io.micronaut.http.client.RawHttpClient;
import io.micronaut.http.netty.body.AvailableNettyByteBody;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.EmptyHttpHeaders;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.LastHttpContent;
import org.junit.jupiter.api.Assertions;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class RawClientBenchmark {
public static void main(String[] args) throws Exception {
JmhFastThreadLocalExecutor exec = new JmhFastThreadLocalExecutor(1, "init-test");
exec.submit(() -> {
// simple test that everything works properly
for (FullHttpStackBenchmark.StackFactory stack : FullHttpStackBenchmark.StackFactory.values()) {
FullHttpStackBenchmark.Holder holder = new FullHttpStackBenchmark.Holder();
holder.stack = stack;
holder.setUp();
holder.tearDown();
}
return null;
}).get();
exec.shutdown();

Options opt = new OptionsBuilder()
.include(RawClientBenchmark.class.getName() + ".*")
.warmupIterations(5)
.measurementIterations(10)
.mode(Mode.AverageTime)
.timeUnit(TimeUnit.NANOSECONDS)
.forks(1)
.build();

new Runner(opt).run();
}

@Benchmark
public byte @NonNull [] benchmark(Holder holder) throws Exception {
try (ByteBodyHttpResponse<?> response = (ByteBodyHttpResponse<?>) Mono.from(
holder.client.exchange(holder.request, holder.requestBody.split(), null)).block()) {
assert response != null;
return response.byteBody().buffer().get().toByteArray();
}
}

@State(Scope.Thread)
public static class Holder {
ApplicationContext ctx;
RawHttpClient client;

HttpRequest<?> request;
CloseableAvailableByteBody requestBody;

EventLoopGroup serverLoop;

@Setup
public void setUp() throws Exception {
ctx = ApplicationContext.run(Map.of("spec.name", "RawClientBenchmark"));
client = ctx.getBean(RawHttpClient.class);

serverLoop = new NioEventLoopGroup(1);
ServerSocketChannel server = (ServerSocketChannel) new ServerBootstrap()
.group(serverLoop)
.channel(NioServerSocketChannel.class)
.localAddress(0)
.childHandler(new ChannelInitializer<>() {
FullHttpResponse response;

@Override
public void handlerAdded(ChannelHandlerContext ctx) {
ByteBuf resp = ctx.alloc().buffer();
ByteBufUtil.writeAscii(resp, "bar");
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, resp, new DefaultHttpHeaders().add(HttpHeaderNames.CONTENT_LENGTH, resp.readableBytes()), EmptyHttpHeaders.INSTANCE);
}

@Override
protected void initChannel(@NonNull Channel ch) {
ch.pipeline()
.addLast(new HttpServerCodec())
.addLast(new ChannelInboundHandlerAdapter() {
boolean inBody = false;

@Override
public void channelRead(@NonNull ChannelHandlerContext ctx, @NonNull Object msg) throws Exception {
if (!inBody) {
inBody = true;
if (!(msg instanceof FullHttpResponse)) {
return;
}
}
((HttpContent) msg).release();
if (msg instanceof LastHttpContent) {
ctx.writeAndFlush(new DefaultFullHttpResponse(
response.protocolVersion(),
response.status(),
response.content().retainedSlice(),
response.headers(),
response.trailingHeaders()
));
inBody = false;
}
}
});
}
})
.bind().syncUninterruptibly().channel();

request = HttpRequest.POST("http://127.0.0.1:" + server.localAddress().getPort() + "/foo", null);
ByteBuf req = ByteBufAllocator.DEFAULT.buffer();
ByteBufUtil.writeUtf8(req, "foo");
requestBody = new AvailableNettyByteBody(req);

try (ByteBodyHttpResponse<?> response = (ByteBodyHttpResponse<?>) Mono.from(client.exchange(request, requestBody.split(), null)).block()) {
Assertions.assertEquals("bar", response.byteBody().buffer().get().toString(StandardCharsets.UTF_8));
}
}

@TearDown
public void tearDown() {
requestBody.close();
ctx.close();
serverLoop.shutdownGracefully();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package example;

import io.micronaut.http.server.stack.ControllersBenchmark;
import io.micronaut.http.server.stack.FullHttpStackBenchmark;
import io.micronaut.http.server.stack.TfbLikeBenchmark;
import io.micronaut.test.typepollution.FocusListener;
import io.micronaut.test.typepollution.ThresholdFocusListener;
import io.micronaut.test.typepollution.TypePollutionTransformer;
Expand Down Expand Up @@ -91,7 +91,7 @@ static class Concrete implements A, B {
@Test
public void testFromJmh() throws RunnerException {
Options opt = new OptionsBuilder()
.include(Stream.of(FullHttpStackBenchmark.class, TfbLikeBenchmark.class)
.include(Stream.of(FullHttpStackBenchmark.class, ControllersBenchmark.class)
.map(Class::getName)
.collect(Collectors.joining("|", "(", ")"))
+ ".*")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -128,4 +129,9 @@ public int hashCode() {
public void setConversionService(ConversionService conversionService) {
this.conversionService = conversionService;
}

@Override
public void forEach(BiConsumer<String, V> action) {
map.forEach((k, v) -> action.accept(k.toString(), v));
}
}
Loading
Loading