0

Suppose I have a function that do some operation for me. Let's call it operation(). I run this operation() function in a loop for ever in another thread

My question is how can I precisely (as much as possible) compute the throughput of executing operation function. In other word, I want to know how many times this function executes in say a millisecond?

This is my implementation:

MainClass.java

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;

public class MainClass {

    static int numOfIterations = 100;

    static AtomicInteger numOfOperationsDone = new AtomicInteger(0);
    static ArrayList<Long> clientOperationTimes = new ArrayList<>();
    static ClientThread[] clientThreads; 
    static int numOfClients = 1;

    static long timeBeforeExp = 1000;
    static long expDuration = 3000;

    static boolean record = false; 
    static boolean stop = false;

    public static void main(String[] args) throws InterruptedException {
        clientThreads = new ClientThread[numOfClients];
        Thread[] threads = new Thread[numOfClients];
        for (int i=0; i< numOfClients; i++){
            clientThreads[i] = new ClientThread();
            Thread t = new Thread(clientThreads[i]);
            threads[i] = t; 
        }
        for (int i=0; i< numOfClients; i++){
            threads[i].start();
        }

        Thread.sleep(timeBeforeExp);

        record = true;


        long start = System.nanoTime();
        Thread.sleep(expDuration);
        stop = true;
        record = false;

        long elapsed = System.nanoTime() - start; 

        System.out.println("Actual elapsed time= " + Math.pow(10, -6) * elapsed);

        double throughput = Math.pow(10, 6)*numOfOperationsDone.longValue() / (double)elapsed;  
        System.out.println("Throughput= " + throughput);

    }

}

ClientThread.java

  public class ClientThread implements Runnable{

    public void run(){
        while (!MainClass.stop)
            operation();
    }

    void operation(){
        Utils.waistTimeHaha(MainClass.numOfIterations);
        if (MainClass.record) MainClass.numOfOperationsDone.incrementAndGet();
    }
}

Is it the way you would do it?

Thanks

Mohammad Roohitavaf
  • 439
  • 2
  • 6
  • 15
  • You could make start and stop time and incremental counter to calculate the average time – Maytham Fahmi Jun 17 '17 at 00:10
  • 2
    https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – ajb Jun 17 '17 at 00:13
  • Yes, this is my ultimate goal actually. I didn't want to write that part to keep the question short. I want to obtain the throughput in two ways and compare them to validate. The first way, is like I have posted. The second one is using average time (the one you mentioned). I expect the throughput be 1/avg_time, but they differ. And their difference grow when I run the codes in a faster PC. I don't know which one I am computing wrong. – Mohammad Roohitavaf Jun 17 '17 at 00:15
  • Ultimately, I want to use this measurements in a complex model. I need to run a distributed java program. I don't think that something like JMH can help me. – Mohammad Roohitavaf Jun 17 '17 at 00:48
  • If you're running on Linux and benchmarking a real application (not a microbenchmark that the compiler can optimize away), perf with java-map-agent can help. – Jeffrey Bosboom Jun 17 '17 at 01:35

0 Answers0