연습: 범주 및 카운터 검색
업데이트: 2007년 11월
이 연습 단계에서는 PerformanceCounter 구성 요소 인스턴스를 만들고 구성한 다음 이 인스턴스를 사용하여 시스템의 성능 카운터 범주 및 카운터의 목록을 검색하는 절차를 보여 줍니다. Windows에서는 성능 카운터를 사용하여 다양한 시스템 리소스에 대한 성능 데이터를 수집합니다. Windows에는 범주별로 구성되어 있는 미리 정의된 카운터 집합이 있으며 프로그래머는 그러한 카운터와 상호 작용할 수 있습니다. 각 범주와 카운터는 시스템의 특정 영역과 관련되어 있습니다.
이 연습에서는 다음과 같은 작업을 수행합니다.
PerformanceCounter 구성 요소를 인스턴스화하고 구성하여 시스템 생성 카운터의 특정 범주와 상호 작용하도록 합니다.
목록 상자에 범주와 카운터에 대한 정보를 표시하는 Windows 응용 프로그램을 만듭니다.
GetCategories 메서드를 사용하여 로컬 컴퓨터에 있는 범주의 목록을 반환합니다.
GetCounters 메서드를 사용하여 지정한 범주에 있는 카운터의 목록을 반환합니다.
Windows 응용 프로그램을 만들려면
새 프로젝트 대화 상자에서 Visual Basic, Visual C# 또는 Visual J#Windows 응용 프로그램을 만듭니다.
도구 상자의 Windows Forms 탭에서 단추 두 개와 목록 상자 두 개를 폼에 추가하고 이를 원하는 순서로 정렬한 후 다음과 같이 속성을 설정합니다.
컨트롤
속성
값
Button1
Name
btnGetCounters
Text
Get Counters
Button2
Name
btnGetCategories
Text
Get Categories
ListBox1
Name
lstCounters
ScrollAlwaysVisible
True
ListBox2
Name
lstCategories
ScrollAlwaysVisible
True
작업한 내용을 저장합니다.
범주 목록을 검색하려면
디자인 뷰에서 Get Categories 단추를 두 번 클릭하여 코드 편집기에 액세스합니다.
Visual J# 버전에서는 화면 위쪽에 System.Diagnostics 네임스페이스를 참조하는 import 문을 추가합니다.
btnGetCategories_Click 프로시저에 로컬 컴퓨터에 있는 범주의 목록을 검색하는 다음 코드를 추가합니다.
Private Sub btnGetCategories_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnGetCategories.Click Dim myCat2 As PerformanceCounterCategory() Dim i As Integer ' Remove the current contents of the list. Me.lstCategories.Items.Clear() ' Retrieve the categories. myCat2 = PerformanceCounterCategory.GetCategories ' Add the retrieved categories to the list. For i = 0 To myCat2.Length - 1 Me.lstCategories.Items.Add(myCat2(i).CategoryName) Next End Sub
private void btnGetCategories_Click(object sender, System.EventArgs e) { System.Diagnostics.PerformanceCounterCategory[] myCat2; // Remove the current contents of the list. this.lstCategories.Items.Clear(); // Retrieve the categories. myCat2 = System.Diagnostics.PerformanceCounterCategory.GetCategories(); // Add the retrieved categories to the list. for (int i = 0; i < myCat2.Length; i++) { this.lstCategories.Items.Add(myCat2[i].CategoryName); } }
카운터 목록을 검색하려면
디자인 뷰에서 Get Counters 단추를 두 번 클릭하여 코드 편집기에 액세스합니다. 삽입 지점은 btnGetCounters_Click 이벤트에 배치됩니다.
선택한 범주에서 카운터 목록을 검색하는 아래의 코드를 추가합니다.
Private Sub btnGetCounters_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnGetCounters.Click Dim instanceNames() As String Dim counters As New System.Collections.ArrayList() If (Me.lstCategories.SelectedIndex <> -1) Then Dim mycat As New PerformanceCounterCategory( _ Me.lstCategories.SelectedItem.ToString()) ' Remove the current contents of the list. Me.lstCounters.Items.Clear() ' Retrieve the counters. Try instanceNames = mycat.GetInstanceNames() If (instanceNames.Length = 0) Then counters.AddRange(mycat.GetCounters()) Else Dim i As Integer For i = 0 To instanceNames.Length - 1 counters.AddRange( _ mycat.GetCounters(instanceNames(i))) Next End If ' Add the retrieved counters to the list. Dim counter As PerformanceCounter For Each counter In counters Me.lstCounters.Items.Add(counter.CounterName) Next Catch ex As Exception MessageBox.Show( _ "Unable to list the counters for this category:" _ & ControlChars.CrLf & ex.Message) End Try End If End Sub
private void btnGetCounters_Click(object sender, System.EventArgs e) { string[] instanceNames; System.Collections.ArrayList counters = new System.Collections.ArrayList(); if (this.lstCategories.SelectedIndex != -1) { System.Diagnostics.PerformanceCounterCategory mycat = new System.Diagnostics.PerformanceCounterCategory( this.lstCategories.SelectedItem.ToString()); // Remove the current contents of the list. this.lstCounters.Items.Clear(); // Retrieve the counters. try { instanceNames = mycat.GetInstanceNames(); if (instanceNames.Length == 0) { counters.AddRange(mycat.GetCounters()); } else { for (int i = 0; i < instanceNames.Length; i++) { counters.AddRange(mycat.GetCounters(instanceNames[i])); } } // Add the retrieved counters to the list. foreach (System.Diagnostics.PerformanceCounter counter in counters) { this.lstCounters.Items.Add(counter.CounterName); } } catch (System.Exception ex) { MessageBox.Show( "Unable to list the counters for this category:\n" + ex.Message); } } }
응용 프로그램을 테스트하려면
응용 프로그램을 저장하고 컴파일합니다.
Get Categories 단추를 클릭합니다. 목록 상자에 범주 목록이 표시됩니다.
범주 하나를 선택하고 Get Counters 단추를 클릭합니다. 선택한 범주에 대한 카운터 목록이 표시됩니다.