0

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?

Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
  • Are you trying to measure the performance of SQL? Why not measure it on the database side as it takes all of your application side uncertainty out. – Nick.Mc Feb 28 '13 at 04:34

2 Answers2

1

One of the simplest ways to warm up is to run the same test, without timing, in the same JVM before you run the actual (timed) test. You should run thousands of iterations to give the JIT a chance to identify and optimise hot spots. There is a lot more detail in the answer to How do I write a correct micro-benchmark in Java? (about this and other matters you need to consider)

Community
  • 1
  • 1
Jason Sankey
  • 2,328
  • 1
  • 15
  • 12
0

You can run it for about 15 minutes creating new thread task. After that clear the selectHistogram re-run with required number of threads to print the metrics.

Prabhakar
  • 113
  • 1
  • 1
  • 9