Home / Function/ setup() — netty Function Reference

setup() — netty Function Reference

Architecture documentation for the setup() function in HttpRequestResponseBenchmark.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  a3ee551c_6f33_2720_76a9_71f1d4b84646["setup()"]
  cfb3f48f_3b42_7cd0_4f15_90945e3896fe["HttpRequestResponseBenchmark"]
  a3ee551c_6f33_2720_76a9_71f1d4b84646 -->|defined in| cfb3f48f_3b42_7cd0_4f15_90945e3896fe
  style a3ee551c_6f33_2720_76a9_71f1d4b84646 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

microbench/src/main/java/io/netty/microbench/http/HttpRequestResponseBenchmark.java lines 184–266

    @Setup
    public void setup() {
        HttpRequestDecoder httpRequestDecoder = new HttpRequestDecoder(
                HttpRequestDecoder.DEFAULT_MAX_INITIAL_LINE_LENGTH, HttpRequestDecoder.DEFAULT_MAX_HEADER_SIZE,
                HttpRequestDecoder.DEFAULT_MAX_CHUNK_SIZE, false);
        HttpResponseEncoder httpResponseEncoder = new HttpResponseEncoder();
        ChannelInboundHandlerAdapter inboundHandlerAdapter = new ChannelInboundHandlerAdapter() {

            private final byte[] STATIC_PLAINTEXT = "Hello, World!".getBytes(CharsetUtil.UTF_8);
            private final int STATIC_PLAINTEXT_LEN = STATIC_PLAINTEXT.length;
            private final ByteBuf PLAINTEXT_CONTENT_BUFFER =
                    Unpooled.unreleasableBuffer(Unpooled.directBuffer().writeBytes(STATIC_PLAINTEXT));
            private final CharSequence PLAINTEXT_CLHEADER_VALUE = new AsciiString(String.valueOf(STATIC_PLAINTEXT_LEN));

            private final CharSequence TYPE_PLAIN = new AsciiString("text/plain");
            private final CharSequence SERVER_NAME = new AsciiString("Netty");
            private final CharSequence CONTENT_TYPE_ENTITY = HttpHeaderNames.CONTENT_TYPE;
            private final CharSequence DATE_ENTITY = HttpHeaderNames.DATE;
            private final CharSequence CONTENT_LENGTH_ENTITY = HttpHeaderNames.CONTENT_LENGTH;
            private final CharSequence SERVER_ENTITY = HttpHeaderNames.SERVER;

            private final DateFormat FORMAT = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
            private final CharSequence date = new AsciiString(FORMAT.format(new Date()));

            @Override
            public void channelRead(ChannelHandlerContext ctx, Object o) {
                // this is saving a slow type check on LastHttpContent vs HttpRequest
                if (o == LastHttpContent.EMPTY_LAST_CONTENT) {
                    return;
                }
                if (o.getClass() == DefaultHttpRequest.class) {
                    writeResponse(ctx, PLAINTEXT_CONTENT_BUFFER.duplicate(), TYPE_PLAIN,
                                  PLAINTEXT_CLHEADER_VALUE);
                } else if (o instanceof HttpRequest) {
                    try {
                        // slow path: shouldn't happen here
                        writeResponse(ctx, PLAINTEXT_CONTENT_BUFFER.duplicate(), TYPE_PLAIN, PLAINTEXT_CLHEADER_VALUE);
                    } finally {
                        ReferenceCountUtil.release(o);
                    }
                } else {
                    ReferenceCountUtil.release(o);
                }
            }

            @Override
            public void channelReadComplete(ChannelHandlerContext ctx) {
                ctx.flush();
            }

            private void writeResponse(ChannelHandlerContext ctx, ByteBuf buf, CharSequence contentType,
                                       CharSequence contentLength) {
                // Build the response object.
                FullHttpResponse response = new DefaultFullHttpResponse(
                        HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf,
                        DefaultHttpHeadersFactory.headersFactory().withValidation(false),
                        DefaultHttpHeadersFactory.trailersFactory().withValidation(false));
                HttpHeaders headers = response.headers();
                headers.set(CONTENT_TYPE_ENTITY, contentType);
                headers.set(SERVER_ENTITY, SERVER_NAME);
                headers.set(DATE_ENTITY, date);
                headers.set(CONTENT_LENGTH_ENTITY, contentLength);
                ctx.write(response, ctx.voidPromise());
            }
        };
        if (websocket) {
            nettyChannel = new EmbeddedChannel(httpRequestDecoder, httpResponseEncoder,
                                               new WebSocketServerExtensionHandler(
                                                       new WebSocketServerExtensionHandshaker() {
                                                           @Override
                                                           public WebSocketServerExtension handshakeExtension(
                                                                   WebSocketExtensionData extensionData) {
                                                               return null;
                                                           }
                                                       }), inboundHandlerAdapter);
        } else {
            nettyChannel = new EmbeddedChannel(httpRequestDecoder, httpResponseEncoder, inboundHandlerAdapter);
        }
        nettyChannel.config().setAllocator(new Alloc());
        GET = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("GET / HTTP/1.1\r\n\r\n".getBytes()));
        readerIndex = GET.readerIndex();

Domain

Subdomains

Frequently Asked Questions

What does setup() do?
setup() is a function in the netty codebase, defined in microbench/src/main/java/io/netty/microbench/http/HttpRequestResponseBenchmark.java.
Where is setup() defined?
setup() is defined in microbench/src/main/java/io/netty/microbench/http/HttpRequestResponseBenchmark.java at line 184.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free