Here is a simple example of what I would use to do some basic and not precise test.
We first need some basic methods to start and print the time easily
public class Main {
static long start;
static void startTest(){
start = System.nanoTime();
}
static void stopTest(){
System.out.format("%012.6f ms\n",(System.nanoTime() - start)/1000000.0);
start = -1;
}
}
Single Thread
Lets run a test on an empty loop :
startTest();
for(int i = 0; i < Integer.MAX_VALUE; ++i){}
stopTest();
This will wait that the loop end before calling stopTest()
and print the time it takes.
Multi-thread
With a different thread, you need to wait that they ends, this is simple and can be don't in multiple ways. Here is one :
startTest();
List<Callable<String>> list = new ArrayList();
for(int i=0;i<5;i++){
list.add(new Callable() {
public Object call() throws Exception {
for(int i = 0; i < Integer.MAX_VALUE/5; ++i){}
System.out.println("End of Thread");
return null;
}
});
}
ExecutorService es = Executors.newCachedThreadPool();
try {
es.invokeAll(list);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
stopTest();
es.shutdown();
}
With this Executors
, we can invoke all Callable
at the same time an wait for them to end.