2015-04-07 61 views
0

我正在使用SIAGR API來計算CPU使用率,但得到例外時,它是使用LOAD RUNNER進行測試,其他方式也可以。使用SIGAR API來計算CPU使用率

org.hyperic.sigar.SigarFileNotFoundException: No such file or directory 
    at org.hyperic.sigar.Cpu.gather(Native Method) 
    at org.hyperic.sigar.Cpu.fetch(Cpu.java:30) 
    at org.hyperic.sigar.Sigar.getCpu(Sigar.java:320) 
    at org.hyperic.sigar.Sigar.getCpuPerc(Sigar.java:348) 

下面

public static void main(String[] args) 
{ 
    try 
    { 
     Shell shell = new Shell(); 
     shell.setPageSize(PageControl.SIZE_UNLIMITED); 

     Sigar sigar = shell.getSigar(); 

     CpuPerc cpu = null; 

     for (int i = 0; i < 10; i++) 
     { 
      try 
      { 
       Thread.sleep(500L); 
      } catch (Exception e) 
      { 
       // TODO: handle exception 
      } 

      cpu = sigar.getCpuPerc(); 
      double val = cpu.getCombined(); 

      int cpuUsage = (int) (val * 100); 
      // System.out.println(CpuPerc.format(val)); 
      System.out.println(cpuUsage); 
     } 
    } 
    catch (SigarException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

我的要求,請找到示例代碼是收集在特定的時間間隔的CPU使用率50次..並記錄平均和最高的。

請幫我解決這個問題

回答

0

你爲什麼不使用LoadRunner的的內置功能連接到Windows和Linux/Unix的本地接口來收集CPU數據?你似乎在重新設計一個已經構造好的滾輪

+0

我覺得你誤解了我的問題,或者可能是我錯誤地傳達了它。在這裏我沒有寫出上面的代碼來加載亞軍。我的產品需要定期收集CPU使用情況,而不是Loadrunner.Here我的產品使用負載運行器進行了一些巨大的load.Here後臺線程將收集CPU使用情況。當有負載時,我正在超出例外。 –

+0

是的,這確實是澄清。謝謝 –