eclipse-vertx / vert.x

Vert.x is a tool-kit for building reactive applications on the JVM
http://vertx.io
Other
14.25k stars 2.07k forks source link

performance degradation when no handler #5257

Open ckoutsouridis opened 1 month ago

ckoutsouridis commented 1 month ago

i have noticed that when we publish messages on addresses with no handlers, it is actually more expensive than publishing messages on addresses with simple consumers.

the bellow example demonstrates

    @Test
    void noHandlers() {

        var vertx = Vertx.vertx();

        var eventBus = vertx.eventBus();

        AtomicLong atomicLong = new AtomicLong();
        eventBus.localConsumer("test", event -> {
            atomicLong.incrementAndGet();
        });
        eventBus.publish("test", "payload");

        eventBus.publish("test", "payload");

        eventBus.publish("test", "payload");

        long start = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            eventBus.publish("test", "payload");
        }
        long end = System.currentTimeMillis();
        System.out.println("time with consumer: "+ (end - start));

        start = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            eventBus.publish("no addr", "payload");
        }
        end = System.currentTimeMillis();
        System.out.println("time with consumer: "+ (end - start));
    }

which outputs on my machine

time with consumer: 185
time with no consumer: 895

i believe this boils down to the way the no handlers is modelled by throwing exceptions and concatenating strings in EventBusImpl.deliverMessageLocally

    ConcurrentCyclicSequence<HandlerHolder> handlers = handlerMap.get(msg.address());
    boolean messageLocal = isMessageLocal(msg);
    if (handlers != null) {
       ...
      return null;
    } else {
      if (metrics != null) {
        metrics.messageReceived(msg.address(), !msg.isSend(), messageLocal, 0);
      }
      return new ReplyException(ReplyFailure.NO_HANDLERS, "No handlers for address " + msg.address);
    }
tsegismont commented 1 month ago

Would you be able to create a microbenchmark with JMH (we have tests like this already in Vert.x core)?

With such a test it would be possible to validate changes making the no_handlers case more efficient.