2014-07-03 32 views
0

我想用PerformanceCounter來檢索Windows上的一些性能細節。我使用了PerformanceCounterCategory.GetCategories方法,並從鏈接中運行示例代碼,該鏈接提供了運行的計算機上可用的所有類別名稱。找出有關PerformanceCounter的信息

我從BlackWasp.co.uk運行代碼,演示瞭如何在示例代碼中使用PerformanceCounter。

我缺少的是constructor需要至少兩個字符串作爲參數counterCategory和counterName。我可以從PerformanceCounterCategory.GetCategories中獲得計數器類別,但是如何設置名稱?

我明白了,我應該使用PerformanceCounterCategory.Create(...)來設置名稱,但是如何將它與我想要監視的指定行爲(如磁盤讀取等)聯繫起來呢?

的示例代碼:

Console.WriteLine("Creating Inventory custom counter"); 
if (!PerformanceCounterCategory.Exists("Inventory")) 
    PerformanceCounterCategory.Create("Inventory", 
     "Truck inventory", 
     PerformanceCounterCategoryType.SingleInstance, 
     "Trucks", "Number of trucks on hand"); 

這種讓我在黑暗中,我不知道在哪裏「卡車庫存」或「卡車」從何而來。

任何人都可以指向正確的方向嗎?有沒有更好的方法來進行性能監控?

那麼如何創建一個性能計數器並將其與有意義的硬件性能聯繫起來呢?

+0

http://social.msdn.microsoft.com/Forums/en-US/37b8b63a-da32-4497-b570-3811a2255dee/how-to-得到-磁盤IO-countersdisk寫時間磁盤讀出時間 - 使用 - CNET?論壇= csharplanguage – Andreas

+0

性能計數器必須先註冊。並且需要有*某種類型的應用程序來設定它們的價值。只有這樣你才能期望能夠閱讀這樣的計數器。你的問題並沒有暗示你照顧到了這些初步步驟。 –

回答

0

我發現我正在尋找here(解釋如下)和here如果您想以編程方式執行此操作。

從本質上講,如果你需要找出哪些性能計數器,您可以訪問去 -

開始 - >搜索框 - >性能監視器

點擊PERFMON.EXE。這會給你一個窗口。點擊

監視工具 - >性能監視器

這會給你的圖表。在圖上單擊鼠標右鍵,然後單擊添加計數器

這給出了所有可能的計數器類別的列表。點擊類別列表左側的箭頭,將出現所有可能的名稱和實例列表。

這涵蓋的PerformanceCounter的兩個主要的構造函數:

PerformanceCounter(string counterCategory, string counterName) 
PerformanceCounter(string counterCategory, string counterName, string counterInstance) 

另外,如果你是一個總的新手到這個話題,好像我是,你要知道這已經被捆綁到它可能是有用的定時器(一般來說),因爲它需要從最後一個時間間隔進行測量。

這裏是一個裸露的骨頭控制檯應用程序:

using System; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Threading; 

namespace Performance 
{ 
    class PerformanceTest 
    { 
     PerformanceCounter memory; 

     Timer timer; 

     public PerformanceTest() 
     { 
      memory = new PerformanceCounter("Memory", "Available MBytes"); 
      // Get a new value every 10 seconds 
      timer = new Timer(PerformanceTimer_Tick, null, 0, 10000); 
     } 

     static void Main(string[] args) 
     { 

      PerformanceTest pt = new PerformanceTest(); 

      Console.ReadLine(); 
     } 

     private void PerformanceTimer_Tick(object sender) 
     { 
      Console.WriteLine("Memory Available: " + memory.NextValue().ToString() + "\n"); 
      // Force garbage collection 
      GC.Collect(); 
     } 
    } 
}