How to Measure execution time for a multi threaded task in java

SomeOnionGamer

I have a method which makes lets say 100k http calls to an API, for that reason I am making the http calls concurrently to avoid http bottleneck. But I would also like to time the whole method without blocking any threads.

        ExecutorService service = Executors.newFixedThreadPool(500);
        for (int i = 0; i < 100_000; i++) {
            int finalI = i;
            Runnable runnable = () -> {
                // My http call goes here....
            };
            service.submit(runnable);
        }
DuncG

If you wish to get a rough idea of the time the whole set of operations takes then use shutdown then awaitTermination to wait for the executor service to finish all of the submitted tasks. Here is a simple example:

long start= System.nanoTime();
ExecutorService exec = Executors.newFixedThreadPool(100);
try {
    for (int i = 0; i < 100_000; i++) {
        final int x = i;
        Runnable runnable = () -> System.out.println(x);
        exec.submit(runnable);
    }
} finally {
    exec.shutdown();
    exec.awaitTermination(365, TimeUnit.DAYS);
}
long elapsed = System.nanoTime() - start;
System.out.println("Elapsed millisec "+TimeUnit.NANOSECONDS.toMillis(elapsed));

It's not a good idea to put System.out.println or other logging inside the tasks as then you are only timing your console I/O, and not getting an estimate of how quick your processing is.

As in the comments, using multi-thread access to same set of servers / resource / disk can make overall elapsed time get longer or cause issues elsewhere.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to measure the execution time with Java

How to measure Java thread execution time?

How can I measure the execution time of a npm/yarn task/script?

multi threaded task scheduler in java

Is there a command in java to measure the execution time?

How would you measure Response Time of Multi-threaded C# Request?

How to measure the execution time of a promise?

How to measure cudaMalloc execution time

Measure sql execution time in a Java Application

How to measure the execution time of a query on Spark

How to measure execution time of thread in macos?

How to measure the execution time in micro seconds?

How to measure execution time of each thread in openmp?

how to measure execution time in specific part of View

How to measure execution time of Spark GraphX application?

How to measure execution time of methods on app startup

How to measure the execution time of an asynchronous function in nodejs?

Measure script execution time

Measure the execution time of a function

How to measure the execution time of a servlet's method from client side java code?

measure Time for two task

How to implement a multi-threaded MergeSort in Java

How to create an efficient multi-threaded task scheduler in C++?

common techniques to measure execution time provide different values (java)

Using Java agent and byte-buddy for measure execution time

How do I measure the execution time of JavaScript code with callbacks?

How to measure containers execution time inside a kubernetes POD?

How do I measure execution time of a command on the Windows command line?

How to measure time of program execution and store that inside a variable