Home > database >  Spring Restful API - Sometimes, response time is too long
Spring Restful API - Sometimes, response time is too long

Time:10-29

There is a server that I operate. I built a Restful API server with Spring Boot, and when I test my API with Postman, it usually takes about 20ms to 50ms to respond. But sometimes it takes up to 5.2 seconds to respond.

I wanted to find out the cause, so I tried to check where the bottleneck occurred. When I calculate the actual execution time by implementing PreHandle and PostHandle in the InterceptorHandler class, it ends in 5 ms. In the java log, I can check the 200OK log with the Servelet log 5 seconds after the business logic is already over.

What's a way to check what's happening in the meantime? Both network and memory CPU utilization have no singularity. There are no users yet because it is a test server. However, there are about three spring boot apps running on this server.

I checked these things.

    1004  pidstat 1
 1005  uptime
 1006   dmesg | tail
 1008  vmstat 1
 1011  mpstat -P ALL 1
 1012  iostat -xz 1
 1013   free -m
 1014  sar -n DEV 1
1019  sar -n TCP,ETCP 1

Normal CASE

Too Long CASE

CodePudding user response:

Concerning the difference of performance between Tomcat/Jetty. May I suggest you to try an integration test with a MockMvc See the get started

I know how much is hard to split test between OPS infrastructures and DEV framework, but with SpringBoot you can test and display the execution performance with a unit test.

Iterate a call to one on your API any time you want and add a StopWatch around the execution.

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ApplicationTest {
      private final static Logger LOGGER = LoggerFactory.getLogger(ApplicationTest .class);
  @Autowired
  private MockMvc mockMvc;

  @Test
  public void shouldReturnDefaultMessage() throws Exception {
    StopWatch stopWatch = new StopWatch("Testing REST API performances");

    for(int i=1; i<=5; i<  ) {
      stopWatch.start("Test iteration "   i);
      this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
            .andExpect(content().string(containsString("Hello World")));
      stopWatch.stop();
    }
    // Print the result
    LOGGER.info(stopWatch.prettyPrint());
  }
}

This test should bring to light the performance issue should be the init of the rest controller.

Locally I have also a latency at the first call.

StopWatch 'Testing REST API performances': running time (millis) = 806

00510ms 063 % Test iteration 1 00072ms 009 % Test iteration 2 00080ms 010 % Test iteration 3 00071ms 009 % Test iteration 4 00073ms 009 % Test iteration 5 As you can see the first call take a long time.

Hope I my answer will help you to have reach you performance expected and help you in your choice.

CodePudding user response:

Transfert time is the time required to get the first byte of the response.

According to this definition, I would tend to say that it's the time your API doing its processing --> At the API level, some requests could explain a longer processing time? Complexity of algorithm, not the same number of data processed ... ?

By the way, at first call, don't forget that the servlet is created so the first response can take more time.

  • Related