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