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,数据聚合,历史数据展现,提醒机制,用户自定义深度分析计数器。

相关文章
相关标签/搜索