Quarkus websockets не кажется асин c или просто медленным? - PullRequest
0 голосов
/ 23 апреля 2020

У меня есть этот код сервера Websocket, из-за которого у меня возникают проблемы с производительностью:

@ServerEndpoint("/video/{username}")
@ApplicationScoped
public class VideoSocket {

    Map<String, Session> sessions = new ConcurrentHashMap<>();

    @OnOpen
    public void onOpen(Session session, @PathParam("username") String username) {
         sessions.put(username, session);
    }

    @OnClose
    public void onClose(Session session, @PathParam("username") String username) {
         sessions.remove(username);
    }

    @OnError
    public void onError(Session session, @PathParam("username") String username, Throwable throwable) {
         sessions.remove(username);
    }

    @OnMessage
    public void onMessage(ByteBuffer message, @PathParam("username") String username) {
        try {
            broadcast(message, username);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void broadcast(ByteBuffer message, String username) {
        sessions.forEach((u,s)->{
                Date start = new Date();
                s.getAsyncRemote().sendObject(message, result ->  {
                    Date finish = new Date();
                    System.out.println("Total send time: " + (finish.getTime() - start.getTime() + " milliseconds."));
                    if (result.getException() != null) {
                        System.out.println("Unable to send message: " + result.getException());
                    }
                });
        });
    }
}

Я основал свой код с помощью быстрого запуска: https://quarkus.io/guides/websockets Когда клиент-сервер всего один Если производительность невелика, время отправки веб-сокета составляет 1-5 мс, что нормально. Однако, когда я настраиваю 20-25 клиентов, производительность начинает ухудшаться. Он изменяется от быстрого к медленному, и потоковая передача видео байтов задерживается:

Total send time: 1 milliseconds.
Total send time: 0 milliseconds.
Total send time: 3 milliseconds.
Total send time: 95 milliseconds.
Total send time: 1 milliseconds.
Total send time: 6285 milliseconds.
Total send time: 6162 milliseconds.
Total send time: 207 milliseconds.
Total send time: 412 milliseconds.
Total send time: 8143 milliseconds.
Total send time: 6747 milliseconds.
Total send time: 5561 milliseconds.
Total send time: 5472 milliseconds.
Total send time: 5358 milliseconds.
Total send time: 5257 milliseconds.
Total send time: 5093 milliseconds.
Total send time: 4963 milliseconds.
Total send time: 4845 milliseconds.
Total send time: 6357 milliseconds.
Total send time: 488 milliseconds.
Total send time: 7491 milliseconds.
Total send time: 7462 milliseconds.
Total send time: 528 milliseconds.
Total send time: 6299 milliseconds.
Total send time: 4752 milliseconds.
Total send time: 4671 milliseconds.
Total send time: 554 milliseconds.
Total send time: 6730 milliseconds.
Total send time: 8051 milliseconds.
Total send time: 428 milliseconds.
Total send time: 129 milliseconds.
Total send time: 6042 milliseconds.
Total send time: 5949 milliseconds.
Total send time: 5855 milliseconds.
Total send time: 5753 milliseconds.
Total send time: 1 milliseconds.
Total send time: 1 milliseconds.
Total send time: 1 milliseconds.
Total send time: 1 milliseconds.
Total send time: 1 milliseconds.
Total send time: 5 milliseconds

И чем дольше сервер работает с таким количеством соединений, тем больше задержка:

Total send time: 391493 milliseconds.
Total send time: 391290 milliseconds.
Total send time: 391186 milliseconds.
Total send time: 391054 milliseconds.
Total send time: 390932 milliseconds.

Что может быть лучшим решением для этого? Когда только для 20-25 одновременно работающих клиентов он уже недостаточно эффективен (, не говоря уже о том, что моя цель - 500-1000 одновременных подключений на сервер ), и, кстати, сервер, на котором он работает, это Intel Core-i7 с ~ 3.5 Процессор ГГц с 16 ГБ ОЗУ, я думаю, этого должно быть более чем достаточно для обработки 25 соединений, но как-то не получается.

...