I am building a program that tries to simulate real clients request to measure the "quality" of the service. I am trying to measure the maximum acceptable number of clients. In other words, how many clients my webserver is able to service at same time. I measure that "quality" using the response time of each request and the number of reloads. Then, I have 10 thousand threads executing at same time, but just doing request in a random time (to avoid all of these requests being sent at same time and causing a deny of service).
My question is how can I get the response time without being affected by switching threads? So far, the only way that I found to get that response time was by doing:
long starTime = System.nanoTime();
InputStream inputStream = this.conn.getInputStream();
long endTime = System.nanoTime();
double elapsedTime = TimeUnit.NANOSECONDS.toMillis((endTime - starTime))/1000.0;
Where this.conn
object is a HttpURLConnection.
The problem is that the threads can be switched right after getting the inputStream, and the endTime will not be accurate. It can get worst with even more threads.
How can I get the exact time that will not be affected by thread switching?