2013-03-17 58 views
0

我有一個簡單的Multithreaded program,它調用External API並從該API獲取響應。我正在使用RestTemplate如何找出Unix環境下的CPU和IO時間

問題陳述: -

我試圖找出

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

我在Ubuntu的工作。

下面是我的程序 -

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(); 
     } 
    } 
} 

我從命令行運行上面的程序在Ubuntu AS-

java - jar REST.jar

誰能告訴我如何一步一步來看着辦吧在Unix環境下爲這些調用輸出CPUIO time ?.我只需要粗略估計這些電話。

+0

你可以在這個線程上找到'cpu use' http://stackoverflow.com/questions/3017162/how-to-get-total-cpu-usage-in-linux-c – cwhsu 2013-03-17 00:53:08

回答

1

如果「外部呼叫」是一個外部應用程序(例如,通過exec())的執行,你可以(在理論上)通過包裝或者在time的應用或使用ac進程記帳的東西得到一些統計數據。

但是,您似乎想要了解服務用於處理單個請求的資源。沒有辦法獲得這些信息,除了讓服務本身來衡量和報告。

您可以從外部(即從客戶端)捕獲的唯一東西是每個請求的已用時間。

+0

謝謝Stephen。一點點背景 - 我正在提交容量請求,以確保我們的盒子可以在我們託管服務時處理流量。所以容量人員問我 - '不知道這些調用的估計CPU和IO時間是多少?'所以我想,我可以在Unix環境中計算出這些數字嗎?所以這就是我編寫一個示例程序來獲取這些數字的原因,並且每當我們的服務準備就緒時,我也會做同樣的事情來計算CPU和IO時間。 – AKIWEB 2013-03-17 22:13:07

+0

沒錯,你需要在服務器端進行測量。這些信息在客戶端的這個粒度上是不可用的。 – 2013-03-18 16:43:02

+0

謝謝Stephen的建議。你能告訴我該怎麼做嗎?這是我不確定的部分。任何幫助將不勝感激。 – AKIWEB 2013-03-18 17:09:38