Webclient print log will increase the request time?

Viewed 30

When I use the ".log()" method, the WebClient causes the JVM to run slowly, which indirectly affects the interface request time If I log out of the.log() method, the JVM and time go back to normal, Is there anyone else I'm running into the same problem,is there something to do with configuration or something else?

This is a Maven project Version is 3.8.6,JDK11,mvn clean packaged to two jar:

1、one jar started normally with 8080 image

2、the other JAR started with 8082 image

3、Request path http://localhost:8080/rpc/send/webclient4/{cycle}

4、cycle:you can write the number of loops if you write 1000 you call it 1000 times and 10000 you call it 10 times image

This time goes up with every call. I tried to change logback to asynchronous, but it didn't work

My project is on Github: https://github.com/Levi-dong/webclient-demo

The following code:

private static final WebClient webClient = WebClient.builder()
        .baseUrl("http://localhost:8082")
        .build();

public String sendWebClient4(int cycle) {
    List<Integer> list = Lists.newArrayList();
    Random random = new Random();
    for (int i = 0; i < cycle; i++) {
        list.add(random.nextInt(10));
    }
    List<List<Integer>> partition = Lists.partition(list, 1000);

    List<Integer> result = Lists.newArrayList();
    Instant start = Instant.now();

    LongAdder longAdder = new LongAdder();

    Flux.fromIterable(partition)
            .buffer(2)
            .flatMap(element -> {
                List<Mono<Integer>> monoList = Lists.newArrayList();
                for (List<Integer> ele : element) {
                    for (Integer num : ele) {
                        Mono<Integer> mono = webClient
                                .get()
                                .uri("/rpc/get/{id}", num)
                                .retrieve()
                                .bodyToMono(Integer.class)

                  //It is this log() method that has 
                  //a significant impact on JVM runtime 
                  //and interface request consumption
                  //----------------------------------
                                //.log()
                  //----------------------------------
                                ;
                        monoList.add(mono);
                    }
                }
                return Mono.just(monoList);
            })
            .subscribe(element -> {
                longAdder.increment();
                log.info("第{}轮执行完毕,数目{}", longAdder.intValue(), element.size());
                List<Integer> block = Mono.zip(element, objectArray -> Arrays.stream(objectArray)
                        .map(Integer.class::cast)
                        .collect(Collectors.toList())).block();
                result.addAll(block);
            });
    log.info("获取到数目:{}", result.size());
    Instant end = Instant.now();
    long ms = Duration.between(start, end).toMillis();
    log.info("耗时:{}", ms);

    return String.valueOf(ms);
}

public Integer getNum(int num) {
    int sleep = 1;
    try {
        int i = new Random().nextInt(10);
        sleep = num * i;
        Thread.sleep(sleep);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    return sleep;
}
0 Answers
Related