Giter VIP home page Giter VIP logo

Comments (1)

NiteshKant avatar NiteshKant commented on July 24, 2024

(Copying comments from #267 for reference here)

PR #266 fixes this issue as a new implementation. Existing implementation is preserved and is deprecated (Issue #209)

Comparison of object allocation between the new and old implementation is:

Old Implementation

image

New Implementation

image

The above benchmark was done for a ServerSentEvent data size of 10KB. The code for the test is below:

Server

package io.reactivex.netty.examples.http.sse;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.reactivex.netty.RxNetty;
import io.reactivex.netty.protocol.http.server.HttpServerRequest;
import io.reactivex.netty.protocol.http.server.HttpServerResponse;
import io.reactivex.netty.protocol.http.server.RequestHandler;
import rx.Observable;
import rx.functions.Func1;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

public final class TestSSEServerStart {

    private static final ByteBuf data;
    static {
        final byte[] dataBytes = new byte[10 * 1024];
        Arrays.fill(dataBytes, (byte) 'c');
        data = Unpooled.buffer().writeBytes(dataBytes).retain();
    }

    public static final byte[] DATA_PREFIX = "data: ".getBytes();
    public static final byte[] EOL = "\n\n".getBytes();

    public static void main(String[] args) {
        RxNetty.createHttpServer(8091, new RequestHandler<ByteBuf, ByteBuf>() {
            @Override
            public Observable<Void> handle(HttpServerRequest<ByteBuf> request,
                                           final HttpServerResponse<ByteBuf> response) {
                return Observable.interval(1, TimeUnit.SECONDS)
                                 .flatMap(new Func1<Long, Observable<Void>>() {
                                     @Override
                                     public Observable<Void> call(Long interval) {
                                         for (int i = 0; i < 5000; i++) {
                                             response.writeBytes(DATA_PREFIX);
                                             response.writeBytes(data.retain());
                                             response.writeBytes(EOL);
                                         }
                                         return response.flush();
                                     }
                                 });
            }
        }).startAndWait();
    }
}

Client (Old)

package io.reactivex.netty.examples.http.sse;

import io.netty.buffer.ByteBuf;
import io.reactivex.netty.RxNetty;
import io.reactivex.netty.pipeline.PipelineConfigurators;
import io.reactivex.netty.protocol.http.client.HttpClientRequest;
import io.reactivex.netty.protocol.http.client.HttpClientResponse;
import io.reactivex.netty.protocol.text.sse.ServerSentEvent;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

import java.util.concurrent.atomic.AtomicLong;

public final class TestSSEDecoderMemoryOld {

    public static void main(String[] args) {
        testOldSSEDecoder(8091);
    }

    private static void testOldSSEDecoder(int serverPort) {
        System.out.println("Testing old SSE decoder. Server port: " + serverPort);
        final AtomicLong counter = new AtomicLong();
        RxNetty.<ByteBuf, ServerSentEvent>newHttpClientBuilder("localhost", serverPort)
               .pipelineConfigurator(PipelineConfigurators.<ByteBuf>sseClientConfigurator())
               .build()
               .submit(HttpClientRequest.createGet("/"))
               .flatMap(new Func1<HttpClientResponse<ServerSentEvent>, Observable<ServerSentEvent>>() {
                   @Override
                   public Observable<ServerSentEvent> call(HttpClientResponse<ServerSentEvent> clientResponse) {
                       return clientResponse.getContent()
                                            .doOnNext(new Action1<ServerSentEvent>() {
                                                @Override
                                                public void call(ServerSentEvent event) {
                                                    if (counter.incrementAndGet() % 1000 == 0) {
                                                        System.out.println("Received events count: " + counter.get());
                                                    }
                                                }
                                            });
                   }
               }).toBlocking().last();
    }
}

Client (New)

package io.reactivex.netty.examples.http.sse;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.reactivex.netty.RxNetty;
import io.reactivex.netty.pipeline.PipelineConfigurators;
import io.reactivex.netty.protocol.http.client.HttpClientRequest;
import io.reactivex.netty.protocol.http.client.HttpClientResponse;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

import java.util.concurrent.atomic.AtomicLong;

public final class TestSSEDecoderMemoryNew {

    public static void main(String[] args) {
        testNewSSEDecoder(8091);
    }

    private static void testNewSSEDecoder(final int serverPort) {
        System.out.println("Testing new SSE decoder. Server port: " + serverPort);
        final AtomicLong counter = new AtomicLong();
        RxNetty.<ByteBuf, io.reactivex.netty.protocol.http.sse.ServerSentEvent>newHttpClientBuilder("localhost",
                                                                                                    serverPort)
               .pipelineConfigurator(PipelineConfigurators.<ByteBuf>clientSseConfigurator())
               .build()
               .submit(HttpClientRequest.createGet("/"))
               .flatMap(
                       new Func1<HttpClientResponse<io.reactivex.netty.protocol.http.sse.ServerSentEvent>, Observable<io.reactivex.netty.protocol.http.sse.ServerSentEvent>>() {
                           @Override
                           public Observable<io.reactivex.netty.protocol.http.sse.ServerSentEvent> call(
                                   HttpClientResponse<io.reactivex.netty.protocol.http.sse.ServerSentEvent> response) {
                               return response.getContent()
                                              .doOnNext(
                                                      new Action1<io.reactivex.netty.protocol.http.sse.ServerSentEvent>() {
                                                          @Override
                                                          public void call(
                                                                  io.reactivex.netty.protocol.http.sse.ServerSentEvent serverSentEvent) {
                                                              if (counter.incrementAndGet() % 1000 == 0) {
                                                                  System.out.println(
                                                                          "Received events count: " + counter.get());
                                                              }
                                                          }
                                                      });
                           }
                       })
               .toBlocking().last();
    }
}

from rxnetty.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.