i-Cell-Mobilsoft-Open-Source / coffee

Coff:ee - JakartaEE solution set
http://i-cell-mobilsoft-open-source.github.io/coffee/
Apache License 2.0
22 stars 13 forks source link

RedisStreamPublisher list publish alternative #691

Open Balazs-Joo opened 4 months ago

Balazs-Joo commented 4 months ago

protected List<Optional<StreamEntryID>> publishInActiveConnection(String streamGroup, List<String> streamMessages, Map<String, String> parameters)

This method uses for and publishes messages individual.

Would be great if there where an alternative method using Jedis Pipeline.

@Test
void test() throws BaseException {
    List<String> ids = IntStream.range(0, 5000).mapToObj(i -> RandomUtil.generateId()).toList();
    Jedis jedis = new Jedis("localhost", 6379);

    String streamKey = "mystream";

    long startTime = System.currentTimeMillis();
    for (var id : ids) {
        Map<String, String> message = new HashMap<>();
        message.put("message_id", id);
        message.put("content", id);
        jedis.xadd(streamKey, (StreamEntryID) null, message);
    }

    System.out.println("Elapsed time: " + (System.currentTimeMillis() - startTime));

    Pipeline pipeline = jedis.pipelined();

    startTime = System.currentTimeMillis();
    for (var id : ids) {
        Map<String, String> message = new HashMap<>();
        message.put("message_id", id);
        message.put("content", id);
        pipeline.xadd(streamKey, (StreamEntryID) null, message);
    }

    List<Object> responses = pipeline.syncAndReturnAll();

    System.out.println("Pipelined Elapsed time: " + (System.currentTimeMillis() - startTime));

    jedis.close();
}

The result of this test Elapsed time: 221 Pipelined Elapsed time: 23