JasonPlutext
JasonPlutext

Reputation: 15863

CPU cores versus response time on Tomcat

With the following simple servlet 3 servlet on Tomcat 8 running Java 8:

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;

import java.io.IOException;

@WebServlet(value="/min", name="helloServlet")
public class HelloServlet extends GenericServlet {
    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        double j = 0;
        for (int i=0 ; i<99999 ; i++) {
            j += Math.random();
        }       
        res.getWriter().println("Hello world!!" + j);
    }
}

I used JMeter (on a separate host) to send it requests

On a processor with 4 physical cores with hyperthreading disabled, I expected response time to be the similar with 1 to 4 JMeter threads/users (since there are 4 cores). With 8 threads, I expected response time to double.

In fact what happened with <4 threads/users was quite different.

With 1 or 2 users, JMeter "graph results" reports median 4 ms.

With 3 users, the median is 38 ms

With 4 users, the median is 53 (average 61)

With 8 users, the median is 120 (average 135).

The difference between 4 and 8 users is inline with my expectations.

But the difference between 1 or 2 users, and 3 or 4 users surprised me.

Explanation or suggestions as to what to try next? thanks...

Upvotes: 1

Views: 125

Answers (1)

Vikas Madhusudana
Vikas Madhusudana

Reputation: 1482

This is because you are using Math.random() try using ThreadLocalRandom

https://docs.oracle.com/javase/tutorial/essential/concurrency/threadlocalrandom.html

Upvotes: 1

Related Questions