Windows Azureアプリケーションでパフォーマンスカウンターを使用する

画像



Windows Azureアプリケーションでパフォーマンスカウンターを使用してデータを収集し、システムのボトルネックを特定して、システムとアプリケーションのパフォーマンスを調整できます。 Windows Azureは、Windows Server 2008、IIS、およびASP.NETで利用可能ないくつかのパフォーマンスカウンターを提供します。 Windows Azureアプリケーションに使用できるパフォーマンスカウンターの一覧については、 Windows Azureアプリケーションでパフォーマンスカウンターを作成および使用する方法について」を参照してください



このタスクを実装するには、次の手順を実行します。





手順1.パフォーマンスカウンターからデータを収集する



パフォーマンスカウンターからのデータのコレクションを構成するには、Windows AzureアプリケーションはGetDefaultInitialConfigurationメソッドを使用し、 PerformanceCounterConfigurationのインスタンスでPerformanceCountersデータソースを追加し、変更された構成でStartメソッドを呼び出します。 パフォーマンスカウンターからデータを収集するには、次の手順を実行します。



ロールのソースファイルを開きます。



ご注意 通常、次の手順では、ロールのOnStartメソッドにコードを追加します。



診断モニター構成のインスタンスを取得します。 次のコード例は、デフォルトの診断モニター構成オブジェクトを取得する方法を示しています。



var config = DiagnosticMonitor.GetDefaultInitialConfiguration();
      
      





監視対象のパフォーマンスカウンターを指定します。 次の例は、診断モニター構成に追加されるパフォーマンスカウンターを示しています。



 config.PerformanceCounters.DataSources.Add( new PerformanceCounterConfiguration()) { CounterSpecifier = @"\Processor(_Total)\% Processor Time", SampleRate = TimeSpan.FromSeconds(5) });
      
      





再構成された状態で診断モニターを実行します。 次のコード例は、モニターを開始する方法を示しています。



 DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", config);
      
      





ご注意 このコード例は、接続文字列の使用を示しています。 接続文字列の詳細については、 接続文字列の構成」を参照してください。



プロジェクトを保存してビルドし、アプリケーションをデプロイします。



これらの手順を完了すると、Windows Azure診断モニターはパフォーマンスカウンターからデータの収集を開始します。



ステップ2.カスタムパフォーマンスカウンターの作成(オプション)



新しいカスタムパフォーマンスカウンターをアプリケーションから診断モニター構成に追加できます。 これを行うには、カスタムカテゴリとカウンター名を使用して各カウンターのPerformanceCounterConfigurationインスタンスを作成し、それらをDiagnosticMonitorConfigurationの PerformanceCountersデータソースコレクションに追加します。 カスタムパフォーマンスカウンターを作成するには、次の手順を実行します。



アプリケーションのサービス定義ファイル(CSDEF)を開きます。



RuntimeエレメントをWebRoleまたはWorkerRoleエレメントに追加して、より高い特権で実行できるようにします。



 <Runtime executionContext="elevated" />
      
      





ファイルを保存します。 ロールのソースファイルを開きます。 次のusingステートメントが欠落している場合は、追加します。



 using System.Diagnostics;
      
      





ロールのOnStartメソッドでカスタムパフォーマンスカウンターカテゴリを作成します。 次の例は、2つのカウンターを持つカスタムカテゴリを作成する方法を示しています(1つが欠落している場合)。



 if (!PerformanceCounterCategory.Exists("MyCustomCounterCategory")) { CounterCreationDataCollection counterCollection = new CounterCreationDataCollection(); // add a counter tracking user button1 clicks CounterCreationData operationTotal1 = new CounterCreationData(); operationTotal1.CounterName = "MyButton1Counter"; operationTotal1.CounterHelp = "My Custom Counter for Button1"; operationTotal1.CounterType = PerformanceCounterType.NumberOfItems32; counterCollection.Add(operationTotal1); // add a counter tracking user button2 clicks CounterCreationData operationTotal2 = new CounterCreationData(); operationTotal2.CounterName = "MyButton2Counter"; operationTotal2.CounterHelp = "My Custom Counter for Button2"; operationTotal2.CounterType = PerformanceCounterType.NumberOfItems32; counterCollection.Add(operationTotal2); PerformanceCounterCategory.Create( "MyCustomCounterCategory", "My Custom Counter Category", PerformanceCounterCategoryType.SingleInstance, counterCollection); Trace.WriteLine("Custom counter category created."); } else{ Trace.WriteLine("Custom counter category already exists."); }
      
      





base.OnStartを呼び出す前に、新しいカスタムパフォーマンスカウンターを診断モニター構成に追加し、 OnStartメソッドで診断モニターを実行します。



 DiagnosticMonitorConfiguration config = DiagnosticMonitor.GetDefaultInitialConfiguration(); config.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromMinutes(2D); config.PerformanceCounters.BufferQuotaInMB = 512; TimeSpan perfSampleRate = TimeSpan.FromSeconds(30D); // Add configuration settings for custom performance counters. config.PerformanceCounters.DataSources.Add( new PerformanceCounterConfiguration() { CounterSpecifier = @"\MyCustomCounterCategory\MyButton1Counter", SampleRate = perfSampleRate }); config.PerformanceCounters.DataSources.Add( new PerformanceCounterConfiguration() { CounterSpecifier = @"\MyCustomCounterCategory\MyButton2Counter", SampleRate = perfSampleRate }); // Apply the updated configuration to the diagnostic monitor. DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", config);
      
      





アプリケーションのカウンターを更新します。 次の例は、 Button1_Clickイベントのカスタムパフォーマンスカウンターを更新する方法を示しています。



 protected void Button1_Click(object sender, EventArgs e) { button1Counter = new PerformanceCounter( "MyCustomCounterCategory", "MyButton1Counter", string.Empty, false); button1Counter.Increment(); this.Button1.Text = "Button 1 count: " + button1Counter.RawValue.ToString(); }
      
      





プロジェクトを保存してビルドし、アプリケーションをデプロイします。



これらの手順を完了すると、Windows Azure Diagnostics Monitorはカスタムパフォーマンスカウンターからデータの収集を開始します。



ステップ3.パフォーマンスカウンターからデータを要求する



パフォーマンスカウンターデータを収集してWindows Azureストレージに転送するようにWindows Azure診断モニターを構成したら、このデータを使用してレポートを作成できます。 Windows Azureアプリケーションのパフォーマンスカウンターからのデータは、Windows AzureストレージのWADPerformanceCountersTableCloudTableQueryクエリの結果をリストすることにより転送されます。 パフォーマンスカウンターからデータを要求するには、次の手順を実行します。



コードを含むロールのソースファイルを開きます。 次のusingステートメントが欠落している場合、それらを追加します。



 using System.Linq; using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.StorageClient;
      
      





パフォーマンスカウンターテーブルを照会するためのテーブルスキーマビュークラスを作成します。



 public class PerformanceCountersEntity : TableServiceEntity { public long EventTickCount { get; set; } public string DeploymentId { get; set; } public string Role { get; set; } public string RoleInstance { get; set; } public string CounterName { get; set; } public string CounterValue { get; set; } }
      
      





テーブルサービスコンテキストのインスタンスを取得します。 次のコード例は、デフォルトの診断モニターテーブルサービスコンテキストを取得する方法を示しています。



 CloudStorageAccount storageAccount = CloudStorageAccount.Parse( "Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString"); CloudTableClient cloudTableClient = storageAccount.CreateCloudTableClient(); TableServiceContext serviceContext = cloudTableClient.GetDataServiceContext();
      
      





返されたテーブルエントリを指定するクエリを作成します。 次の例は、現在のロールインスタンスの過去5分間のCPU使用率レコードを返す方法を示しています。



 IQueryable<PerformanceCountersEntity> performanceCountersTable = serviceContext.CreateQuery<PerformanceCountersEntity>( "WADPerformanceCountersTable"); var selection = from row in performanceCountersTable where row.EventTickCount > DateTime.UtcNow.AddMinutes(-5.0).Ticks && row.CounterName.Equals(@"\Processor(_Total)\% Processor Time") select row; CloudTableQuery<PerformanceCountersEntity> query = selection.AsTableServiceQuery<PerformanceCountersEntity>(); // Use the Execute command explicitly on the TableServiceQuery to // take advantage of continuation tokens automatically and get all the data. IEnumerable<PerformanceCountersEntity> result = query.Execute();
      
      





ご注意 クエリ構文の詳細については、 LINQ:.NET Language-Integrated Queryを参照してください。



データを使用して、アプリケーションのパフォーマンスを分析およびレポートします。



 List<PerformanceCountersEntity> list = result.ToList(); // Display list members here.
      
      





プロジェクトを保存してビルドし、アプリケーションをデプロイします。



これらの手順を完了すると、パフォーマンスカウンターのデータがレポートに使用できるようになります。



追加のリソース






All Articles