2011-10-03 35 views
7

Xin chào Tôi đang cố gắng tạo bộ đếm hiệu suất tùy chỉnh để sử dụng trong perfmon. Mã sau đây hoạt động khá tốt, tuy nhiên tôi có một vấn đề ..truy cập hiệu suất tùy chỉnh trong C#/perfmon

Với giải pháp này tôi có bộ đếm thời gian cập nhật giá trị của bộ đếm hiệu suất, tuy nhiên tôi không phải chạy tệp này để lấy dữ liệu tôi cần . Đó là tôi muốn chỉ có thể cài đặt truy cập như là một điều một thời gian và sau đó có truy vấn perfmon cho dữ liệu (như nó với tất cả các quầy được cài đặt sẵn).

Tôi làm cách nào để đạt được điều này?

using System; 
using System.Diagnostics; 
using System.Net.NetworkInformation; 

namespace PerfCounter 
{ 
class PerfCounter 
{ 
    private const String categoryName = "Custom category"; 
    private const String counterName = "Total bytes received"; 
    private const String categoryHelp = "A category for custom performance counters"; 
    private const String counterHelp = "Total bytes received on network interface"; 
    private const String lanName = "Local Area Connection"; // change this to match your network connection 
    private const int sampleRateInMillis = 1000; 
    private const int numberofSamples = 100; 

    private static NetworkInterface lan = null; 
    private static PerformanceCounter perfCounter; 

    static void Main(string[] args) 
    { 
     setupLAN(); 
     setupCategory(); 
     createCounters(); 
     updatePerfCounters(); 
    } 

    private static void setupCategory() 
    { 
     if (!PerformanceCounterCategory.Exists(categoryName)) 
     { 
      CounterCreationDataCollection counterCreationDataCollection = new CounterCreationDataCollection(); 
      CounterCreationData totalBytesReceived = new CounterCreationData(); 
      totalBytesReceived.CounterType = PerformanceCounterType.NumberOfItems64; 
      totalBytesReceived.CounterName = counterName; 
      counterCreationDataCollection.Add(totalBytesReceived); 
      PerformanceCounterCategory.Create(categoryName, categoryHelp, PerformanceCounterCategoryType.MultiInstance, counterCreationDataCollection); 
     } 
     else 
      Console.WriteLine("Category {0} exists", categoryName); 
    } 

    private static void createCounters() { 
     perfCounter = new PerformanceCounter(categoryName, counterName, false); 
     perfCounter.RawValue = getTotalBytesReceived(); 
    } 

    private static long getTotalBytesReceived() 
    { 
     return lan.GetIPv4Statistics().BytesReceived; 
    } 

    private static void setupLAN() 
    { 
     NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); 
     foreach (NetworkInterface networkInterface in interfaces) 
     { 
      if (networkInterface.Name.Equals(lanName)) 
       lan = networkInterface; 
     } 
    } 
    private static void updatePerfCounters() 
    { 
     for (int i = 0; i < numberofSamples; i++) 
     { 
      perfCounter.RawValue = getTotalBytesReceived(); 
      Console.WriteLine("perfCounter.RawValue = {0}", perfCounter.RawValue); 
      System.Threading.Thread.Sleep(sampleRateInMillis); 
     } 
    } 
} 

}

Trả lời

6

Trong Win32, Hiệu suất đếm làm việc bằng cách PerfMon tải một DLL trong đó cung cấp các giá trị truy cập.

Trong .NET, tệp DLL này là một nhánh sử dụng bộ nhớ dùng chung để giao tiếp với tiến trình .NET đang chạy. Quá trình này định kỳ đẩy các giá trị mới vào khối bộ nhớ chia sẻ, và DLL làm cho chúng sẵn sàng như các bộ đếm hiệu suất. Vì vậy, về cơ bản, bạn có thể sẽ phải thực hiện của bạn truy cập hiệu suất DLL trong mã nguồn gốc, bởi vì NET hiệu suất quầy giả định rằng có một quá trình đang chạy.

Các vấn đề liên quan