0

I have a simple Multithreaded program that is calling an External API and get the response back from that API. I am using RestTemplate.

Problem Statement:-

I am trying to find out

What is the estimated CPU and IO time for these calls?

I am working in Ubuntu.

Below is my program-

public class RestLnPTest {

    private final static Logger LOG = Logger.getLogger(NokiaLnPTest.class.getName());
    private static int noOfThreads = 10;

    public static void main(String[] args) {

        ExecutorService service = Executors.newFixedThreadPool(noOfThreads);

        try {

            for (int i = 0; i < 100 * noOfThreads; i++) {
                service.submit(new ThreadTask());
            }

            service.shutdown();
            service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);

        } catch (InterruptedException e) {

        } catch (Exception e) {

        } finally {
            logHistogramInfo();
        }
    }

    private static void logHistogramInfo() {

     System.out.println(ThreadTask.lookupHistogram);

    }
}

class ThreadTask implements Runnable {

    public static ConcurrentHashMap<Long, AtomicLong> lookupHistogram = new ConcurrentHashMap<Long, AtomicLong>();
    private static final String URL = "SOME_URL";

    @Override
    public void run() {

        RestTemplate restTemplate = new RestTemplate();

        long start = System.nanoTime();

        String result = restTemplate.getForObject(URL, String.class);

        long end = System.nanoTime() - start;

        final AtomicLong before = lookupHistogram.putIfAbsent(end / 1000000, new AtomicLong(1L));

        if (before != null) {
            before.incrementAndGet();
        }
    }
}

I am running the above program from Command Line in Ubuntu as-

java - jar REST.jar

Can anyone tell me in step by step how to figure it out CPU and IO time for these calls in Unix environment?. I needed just rough estimate of these calls.

AKIWEB
  • 19,008
  • 67
  • 180
  • 294
  • You can find `cpu use` on this thread http://stackoverflow.com/questions/3017162/how-to-get-total-cpu-usage-in-linux-c – cwhsu Mar 17 '13 at 00:53

1 Answers1

1

If the "external calls" were executions of an external application (e.g. via exec()) you could (in theory) get some stats by either wrapping the application in time or using the ac process accounting stuff.

However, you seem to want to find out the resources used by a service to process individual request. There's no way to get that information, other than by getting the service itself to measure and report it.

The only thing you can capture from the outside (i.e. from the client) is the elapsed time for each request.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks Stephen. Little bit background- I was filing Capacity request to make sure our boxes can handle the traffic when we host the service. So the Capacity guys have asked me - `Any idea what is the estimated CPU and IO time for these calls?` So I thought, I can figure out these numbers in the Unix environment? So that is the reason i wrote a sample program to get these numbers and whenever our service is ready, I will do the same thing to figure out CPU and IO time as well. – AKIWEB Mar 17 '13 at 22:13
  • Yea well, you will need to do the measurements on the server side. The information is not available at that granularity from the client side. – Stephen C Mar 18 '13 at 16:43
  • Thanks Stephen for the suggestion. Can you tell me how to do that? That is the part I am not sure of. Any help will be appreciated. – AKIWEB Mar 18 '13 at 17:09
  • Use the ThreadMXBean API - http://docs.oracle.com/javase/7/docs/api/java/lang/management/ThreadMXBean.html - that gives you all of the relevant information that is available in a JVM. – Stephen C Mar 18 '13 at 22:36