Windows Azure Platform 性能監視器(轉載)

Windows操做系統提供了查看性能監視器的功能,用於監視CPU使用率、內存使用率,硬盤讀寫速度,網絡速度等。您能夠在開始-->運行-->輸入Perfmon,就能夠打開性能監視器。

  咱們知道,雲計算的特色之一是:彈性計算。若是在某一時間點,用於對雲計算資源的使用量超過了實際部署的硬件水平,咱們就能夠經過修改:html

  • 增長單個計算節點的硬件配置,好比配置VM Size,從Small改爲Extra Large。來提升硬件水平以響應更多的客戶請求。
  • 多個計算節點並行計算,好比修改Instance Count,從1(單個計算節點),改爲2或者更多(多個計算節點)。這樣也能夠提升服務水平。

  可是Windows Azure畢竟是在遠程數據中心的託管服務,咱們在本地是沒法監視和查看Windows Azure託管服務的資源使用狀況。若是咱們想查看Windows Azure的性能監視器,你可能會想到:web

  • 使用遠程桌面鏈接。由於Windows Azure提供遠程桌面(RDP)鏈接查看Azure 計算節點的功能,因此您在部署完託管服務後,能夠登陸到一個計算節點的遠程桌面,而後運行Perfmon進行查看。

  可是這種方法的缺點顯而易見:windows

  • 首先,你只能查看到某一個計算節點的性能監視器而沒法查看全部的計算節點。
  • 其次,使用遠程桌面的步驟是及其繁瑣的,須要鏈接、輸入用戶名密碼、輸入命令行查看。
  • 並且,經過遠程桌面查看到Windows Azure性能監視器的內容,沒法保存在客戶端。

  考慮到這些問題,微軟建立了Windows Azure Diagnostics(診斷)的機制。Windows Azure Diagnostics能夠診斷檢索許多內容,好比跟蹤日誌,奔潰事件日誌等,而且保存下來。在本章裏,我將側重於Peformance Counter(性能計數器),該功能能夠提供WIndows Azure應用程序的關鍵信息。實際上,性能計數器,能夠幫助您隔離性能問題。最重要的是,它能夠幫您省錢,讓Windows Azure的資源獲得最佳利用。緩存

 

Windows Azure Diagnostics網絡

  每個Windows Azure計算節點都有一個內置的診斷進程(DiagnosticsAgent.exe),負責按期收集診斷數據,並緩存到本地文件,並最終存儲到一個預約義的Azure存儲。請注意,在診斷過程當中,也能夠手動觸發。ide

  具體來講,Peformance Counter的數據被保存到Windows Azure Storage的Table中,表名爲WADPerformanceCountersTable。其餘Diagnostics,好比跟蹤日誌,事件日誌等數據也都存儲在指定的表,像WadLogsTableWADDiagnosticInfrastructureLogsTable。更多信息,能夠參考這裏工具

  每個Role Instance都有一個Configuration File (配置文件),位於Azure Storage Blob下,Container(概念上相似於文件夾)爲wad-control-container。配置文件主要收集性能計數器數據和相關的使用率。post

  下面顯示的是Disnostics Configuration File的位置。你能夠下載Cloud Storage Manager訪問wad-control-container。性能

配置文件中使用了一個標準的XML格式,並能夠手動修改(不推薦)。雲計算

 

使用代碼

在這篇文章中,咱們會研究

  1.若是配置Role.cs,而且從外部程序讀取Peformance Counter

  2.如何訪問Azure Storage Table中的數據

  3.性能計數器的快速分析

Windows Azure診斷API

參考自MSDN

  爲了Diagnostics 你的Role,你必須先在ServiceDefinition.csdef中修改配置文件。具體的您能夠參考個人博文Windows
Azure Platform (二十)使用Windows Azure診斷收集日誌記錄數據

 

在WebRole.cs中修改代碼

 

複製代碼
public class WebRole : RoleEntryPoint {     /// <summary>     /// Entry point before starting the Role     /// </summary>     public override bool OnStart()     {         // Get the Role instance Diagnostics configuration.          var config = DiagnosticMonitor.GetDefaultInitialConfiguration();          // Define interval for persisting the performance counter data to azure storage.         config.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromMinutes(5);         // CPU Utilization Counter         PerformanceCounterConfiguration cpuUtilizationCounter =                  new PerformanceCounterConfiguration()         {             CounterSpecifier = @"\Processor(_Total)\% Processor Time",             //define the sample internal for the specific performance counter             SampleRate = TimeSpan.FromSeconds(1)         };         if (!config.PerformanceCounters.DataSources.Contains(cpuUtilizationCounter,                      new PerformanceCounterComparer()))         {             config.PerformanceCounters.DataSources.Add(cpuUtilizationCounter);         }         // Start diagnostic monitor with the new configuration.         DiagnosticMonitor.Start     ("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", config);         return base.OnStart();     }      /// <summary>     /// Performance Counter Comparer     /// </summary>     private class PerformanceCounterComparer :          IEqualityComparer<PerformanceCounterConfiguration>     {         public bool Equals(PerformanceCounterConfiguration a,                  PerformanceCounterConfiguration b)         {             //Check whether the compared objects reference the same data.             if (Object.ReferenceEquals(a, b)) return true;             //Check whether any of the compared objects is null.             if (Object.ReferenceEquals(a, null) || Object.ReferenceEquals(b, null))                 return false;             // Check if the counter specifier and the sampling rate are the same.             return (a.CounterSpecifier == b.CounterSpecifier &&                  a.SampleRate == b.SampleRate);         }         public int GetHashCode(PerformanceCounterConfiguration counter)         {             //Check whether the object is null             if (Object.ReferenceEquals(counter, null)) return 0;             //Get hash code for the CounterSpecifier field if it is not null.             int hashCounterSpecifier = counter.CounterSpecifier ==                             null ? 0 : counter.CounterSpecifier.GetHashCode();             //Calculate the hash code for the counter.             return hashCounterSpecifier ^ counter.SampleRate.GetHashCode();         }     } }
複製代碼

 

遠程配置 Performance Counter(性能計數器)

使用 Microsoft.WindowsAzure.Diagnostics.Management.

複製代碼
const string storageAccoutName = "Storage-Name-Here";  const string privateKey = "Storge-Private-Key-Here"; const string deploymentId = "Deployment-Id-Here"; var storageAccount = CloudStorageAccount.Parse(String.Format(     "DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",      storageAccoutName, privateKey));  // Get the diagnostics manager for the entire storage account. var diagnosticManager = storageAccount.CreateDeploymentDiagnosticManager(deploymentId);  // Get diagnostics manager for the specific role instance. RoleInstanceDiagnosticManager roleDiagManager =    diagnosticManager.GetRoleInstanceDiagnosticManager("WebRole1", "WebRole1_IN_0");  //Modify current configuration var currentConfiguariton = roleDiagManager.GetCurrentConfiguration(); currentConfiguariton.PerformanceCounters.ScheduledTransferPeriod =                          TimeSpan.FromMinutes(5); currentConfiguariton.PerformanceCounters.DataSources.Add             (new PerformanceCounterConfiguration() {     CounterSpecifier = @"\Processor(_Total)\% Processor Time",     SampleRate = TimeSpan.FromSeconds(1) });  //Commit the changes roleDiagManager.SetCurrentConfiguration(currentConfiguariton);
複製代碼

 

檢索性能計數器數據

  如今,咱們已經配置了咱們要監視的計數器。讓咱們訪問Azure Storage Table(表名WADPerformanceCountersTable),並顯示出來。

  我建立了PerformanceDataContext,派生自TableServiceContext這是爲了鏈接到Azure表,Microsoft提供的ADO擴展的一部分。你可使用LINQ查詢以檢 索數據。

複製代碼
/// <summary> /// Query helper for retrieving data from the WADPerformanceCountersTable /// </summary> public class QueryExecuter {     /// <summary>     /// Cloud storage account client     /// </summary>     private CloudStorageAccount accountStorage;     /// <summary>     /// Default Constructor - Use development storage emulator.     /// </summary>     public QueryExecuter()     {         accountStorage = CloudStorageAccount.DevelopmentStorageAccount;     }     /// <summary>     /// Constructor     /// </summary>     /// <param name="accountName">Azure storage name</param>     /// <param name="privateKey">Azure storage private key</param>     public QueryExecuter(string accountName, string privateKey)     {         accountStorage = CloudStorageAccount.Parse(String.Format(           "DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", account     }     /// <summary>     /// Retrieve Performance counter data     /// </summary>     /// <param name="counterFullName">Counter specifier full name</param>     /// <param name="deploymentid">Deployment id</param>     /// <param name="roleName">Role name</param>     /// <param name="roleInstanceName">Role instance name</param>     /// <param name="startPeriod">Start sample date time</param>     /// <param name="endPeriod">End sample date time</param>     /// <returns></returns>     public List<PerformanceData> QueryPerformanceCounter(string counterFullName,          string deploymentid, string roleName,          string roleInstanceName, DateTime startPeriod, DateTime endPeriod)     {         PerformanceDataContext context = new PerformanceDataContext(           accountStorage.TableEndpoint.ToString(), accountStorage.Credentials);         var data = context.PerfData;         CloudTableQuery<PerformanceData> query = null;         query = (from d in data                  where d.PartitionKey.CompareTo("0" + startPeriod.Ticks) >= 0                                         && d.PartitionKey.CompareTo                     ("0" + endPeriod.Ticks) <= 0                                          && d.CounterName == counterFullName                                              && d.EventTickCount >= startPeriod.Ticks                                                  && d.EventTickCount <= endPeriod.Ticks                                                       && d.DeploymentId == deploymentid                                                          && d.Role == roleName                                                              && d.RoleInstance ==                              roleInstanceName                  select d).AsTableServiceQuery<PerformanceData>();         List<PerformanceData> selectedData = new List<PerformanceData>();         try         {             selectedData = query.Execute().ToList<PerformanceData>();         }         catch         {         }         return selectedData;     } }
複製代碼

此演示,我建立了一個WindowsForm,將Diagnostics Table中的數據填充到圖表中。

顯示的是過去2個小時內,某一個Role Instance的CPU使用率。

 

第三方工具

Quest 軟件公司開發了一個很是方便和容易使用的工具。稱爲Spotlight on Azure。爲咱們對整個Azure訂閱提供了深度監控的功能,包括Role Instance,數據聚合,歷史數據展現,提醒機制,用戶自定義深度分析計數器。

相關文章
相關標籤/搜索