主婦と.NETと犬のぶろぐ

奥様 と プログラム と お犬様 の 楽しい日常

目次

Blog 利用状況

ニュース

書庫

日記カテゴリ

PerformanceCounter(System.Diagnostics.PerformanceCounter)

(@ @;)
PerformanceCounter で取得した値を NPlot の折れ線グラフで描画してみました。
Nplot は ここからダウンロード できます。
今日ダウンロードしたのは nplot-0.9.10.0.zip です。

NPlot はだーいぶ前に遊びで使ったことがあったんですが、
PerformanceCounter は初めて。しかも関連する情報を調べると山のようにあります...。
なんか難しいし...。
NPlot のサンプルもそのままじゃ動いてくれないし...。
でも実行結果みたら、おもろかったのでそのうち もう一度チャレンジしてみようかな...。

■参考文献
PerformanceCounter クラス
PerformanceCounterCategory クラス
パフォーマンスしきい値の監視
パフォーマンスカウンタのカテゴリ、カウンタの一覧を取得する(DOBON!さん)
NPlot

■実行画像
Process _Total %Processor Time を監視しています
Panel

Public Class PerformanceCounterTest

Private Const CATEGORY_COMBO_NAME As String = "categoryComboBox" Private Const COUNTER_COMBO_NAME As String = "counterComboBox" Private Const PLOT_AREA_NAME As String = "plotSurface"
Private m_timer As Timer = New System.Windows.Forms.Timer() Private m_graphData As List(Of Single) = New List(Of Single)
Private Sub PerformanceCounterTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' カテゴリの一覧を取得して ComboBox に表示する Dim categories As System.Diagnostics.PerformanceCounterCategory() = _ System.Diagnostics.PerformanceCounterCategory.GetCategories()
Dim categoryComboBox As ComboBox = New ComboBox() Me.Controls.Add(categoryComboBox) With categoryComboBox .Name = CATEGORY_COMBO_NAME .DropDownStyle = ComboBoxStyle.DropDownList .Location = New Point(0, 0) .Size = New Size(250, .Size.Height) For Each cat As System.Diagnostics.PerformanceCounterCategory In categories If cat.CategoryType = PerformanceCounterCategoryType.SingleInstance AndAlso _ cat.GetCounters().Length > 0 Then .Items.Add(cat.CategoryName) ElseIf cat.CategoryType = PerformanceCounterCategoryType.MultiInstance Then ' MultiInstance の場合は一番最初のインスタンス名をくっつける For Each instanceName As String In cat.GetInstanceNames() .Items.Add(cat.CategoryName & ":" & instanceName) Next End If Next AddHandler .SelectedIndexChanged, AddressOf CategoryComboBox_SelectedIndexChanged .SelectedIndex = -1 End With
' カウンタの ComboBox の設定 Dim counterComboBox As ComboBox = New ComboBox() Me.Controls.Add(counterComboBox) With counterComboBox .Name = COUNTER_COMBO_NAME .DropDownStyle = ComboBoxStyle.DropDownList .Location = New Point(0, 20) .Size = New Size(250, .Size.Height) End With
' 表示ボタン Dim execButton As Button = New Button() Me.Controls.Add(execButton) With execButton .Location = New Point(0, 40) .Text = "表示" AddHandler .Click, AddressOf ExecButton_Click End With
' 停止ボタン Dim stopButton As Button = New Button() Me.Controls.Add(stopButton) With stopButton .Location = New Point(100, 40) .Text = "停止" AddHandler .Click, AddressOf StopButton_Click End With
With Me.m_timer .Stop() .Interval = 1000 AddHandler .Tick, AddressOf Timer_Tick End With
' ここから Nplot の設定 Dim linePlot As NPlot.LinePlot = New NPlot.LinePlot With linePlot .DataSource = Me.m_graphData .Pen.Color = Color.Green .Pen.Width = 1.5F End With
Dim plotSurface As NPlot.Windows.PlotSurface2D = New NPlot.Windows.PlotSurface2D() With plotSurface .Name = PLOT_AREA_NAME .Size = New Size(280, 200) .Location = New Point(0, 65)
.Clear() .Add(linePlot) .YAxis1.WorldMax = 1000 .YAxis1.Label = "Y" .XAxis1.WorldMax = 100 .XAxis1.Label = "X" .Refresh() End With Me.Controls.Add(plotSurface)
End Sub
Private Sub CategoryComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) ' カテゴリの ComboBox で選択された値を取得 Dim selectedCat As String = DirectCast(DirectCast(sender, ComboBox).SelectedItem, String) Dim catName As String = selectedCat Dim instanceName As String = String.Empty If selectedCat.Contains(":") Then catName = selectedCat.Split(":"c)(0) instanceName = selectedCat.Split(":"c)(1) End If Dim categories As System.Diagnostics.PerformanceCounterCategory() = _ System.Diagnostics.PerformanceCounterCategory.GetCategories() Dim category As System.Diagnostics.PerformanceCounterCategory = Nothing For Each cat As System.Diagnostics.PerformanceCounterCategory In categories If cat.CategoryName = catName Then category = cat Exit For End If Next
' カテゴリとインスタンス名からカウンタの一覧を取得して ComboBox に表示 Dim cmb As ComboBox = DirectCast(Me.Controls(COUNTER_COMBO_NAME), ComboBox) cmb.Items.Clear() If category Is Nothing Then Return
Dim counters As System.Diagnostics.PerformanceCounter() If instanceName = String.Empty Then counters = category.GetCounters() Else counters = category.GetCounters(instanceName) End If For Each counter As System.Diagnostics.PerformanceCounter In counters cmb.Items.Add(counter.CounterName) Next cmb.SelectedIndex = -1 End Sub
' 表示ボタン Private Sub ExecButton_Click(ByVal sender As Object, ByVal e As EventArgs) Me.m_timer.Stop() Me.m_graphData.Clear() Dim plotSurface As NPlot.Windows.PlotSurface2D = DirectCast(Me.Controls(PLOT_AREA_NAME), NPlot.Windows.PlotSurface2D) plotSurface.Refresh()
Dim catCombo As ComboBox = DirectCast(Me.Controls(CATEGORY_COMBO_NAME), ComboBox) Dim counterCombo As ComboBox = DirectCast(Me.Controls(COUNTER_COMBO_NAME), ComboBox)
If catCombo.SelectedIndex < 0 OrElse counterCombo.SelectedIndex < 0 Then Return
Dim selectedCat As String = DirectCast(catCombo.SelectedItem, String) Dim catName As String = selectedCat Dim instanceName As String = String.Empty If selectedCat.Contains(":") Then catName = selectedCat.Split(":"c)(0) instanceName = selectedCat.Split(":"c)(1) End If Dim counterName As String = DirectCast(counterCombo.SelectedItem, String)
' PerformanceCounter の設定 With Me.PerformanceCounter1 .MachineName = "." .CategoryName = catName .InstanceName = instanceName .CounterName = counterName End With Me.m_timer.Start() End Sub
' 停止ボタン Private Sub StopButton_Click(ByVal sender As Object, ByVal e As EventArgs) Me.m_timer.Stop() End Sub
Private Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs) Me.m_graphData.Add(Me.PerformanceCounter1.NextValue) Dim plotSurface As NPlot.Windows.PlotSurface2D = DirectCast(Me.Controls(PLOT_AREA_NAME), NPlot.Windows.PlotSurface2D) plotSurface.Refresh() End Sub
End Class

投稿日時 : 2007年3月6日 10:36

Feedback

# jyHxkZpLaxXoqMFemuT 2014/08/05 5:50 http://crorkz.com/

U90321 Very neat blog article. Great.

# LjdOIOnRdhhwUv 2014/09/09 20:34 http://www.designingdigitally.com

This web site is mostly a stroll-by way of for the entire information you wished about this and didn't know who to ask. Glimpse right here, and also you'll positively discover it.

タイトル  
名前  
Url
コメント