Search code examples
javaprofilingvisualvm

Profiling Java app : Cumulative times for methods recursively


Consider this producer-consumer code using a ArrayBlockingQueue : I want to find out a. How much total time is consumed by put()/take() overall ? b. how much total time is spent by the put() and take() calls in waiting ? Blocked ?

Taking a snapshot in VisualVM only shows upto a depth of run() , but not the put() / take() calls. Any idea how to get these times ?

In general , how do I get cumulative running times for all methods recursively down from the main() ?

final BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(QUEUE_SIZE);
    ExecutorService exec = Executors.newFixedThreadPool(2);
    exec.submit(new Runnable() {
        int i =0;
        @Override
        public void run() {
            while(i++<NUM)
                try {
                    queue.put(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

        }
    });

    exec.submit(new Runnable() {
        int i =0;
        @Override
        public void run() {
            while(i++<NUM)
                try {
                    queue.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            consumerFinished.countDown();
        }
    });

Solution

  • You need to change the "CPU settings" that filter out all code belonging java.* packages. Check "Settings" on the "Sampler" tab and edit the "CPU settings" in a new preset. Presets are managed at the bottom of the CPU-settings tab.