I have started working on a project in which I will be passing how many threads I need to use and then I will be trying to measure how much time SELECT sql
is taking in getting executed so for that I have a counter just before this line preparedStatement.executeQuery();
and a counter to measure the time after this line.
Below is the snippet of my code-
public class TestPool {
public static void main(String[] args) {
final int no_of_threads = 10;
// create thread pool with given size
ExecutorService service = Executors.newFixedThreadPool(no_of_threads);
// queue some tasks
for(int i = 0; i < 3 * no_of_threads; i++) {
service.submit(new ThreadTask());
}
// wait for termination
service.shutdown();
service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
// Now print the select histogram here
System.out.println(ThreadTask.selectHistogram);
}
}
Below is my ThreadTask class that implements Runnable interface-
class ThreadTask implements Runnable {
private PreparedStatement preparedStatement = null;
private ResultSet rs = null;
public static ConcurrentHashMap<Long, AtomicLong> selectHistogram = new ConcurrentHashMap<Long, AtomicLong>();
public ThreadTask() {
}
@Override
public void run() {
...........
long start = System.nanoTime();
rs = preparedStatement.executeQuery();
long end = System.nanoTime() - start;
final AtomicLong before = selectHistogram.putIfAbsent(end / 1000000L, new AtomicLong(1L));
if (before != null) {
before.incrementAndGet();
}
..............
}
}
Problem Statement:-
Today I had a design meeting, in which most of the folks said do not start measuring the time as soon as you are running your program. Have some warm up period and then start measuring it. So I thought after that it makes some sense to do that. And now I am thinking how should I incorporate that change here in my code. On what basis I will be doing this? Can anyone suggest something?