使用脚本任务监视性能计数器

适用于:SQL Server Azure 数据工厂中的 SSIS Integration Runtime

管理员可能需要监视对大量数据执行复杂转换的 Integration Services 包的性能。 Microsoft .NET Framework 的 System.Diagnostics 命名空间不但提供使用现有性能计数器的类,还提供用于创建自己性能计数器的类

性能计数器存储应用程序的性能信息,您可以利用这些信息分析软件在一段时间内的性能。 使用“性能监视器”工具可以在本地或远程监视性能计数器。 您可以将性能计数器的值存储在变量中,用于以后在包中进行控制流分支跳转。

除了使用性能计数器,还可以通过 Dts 对象的 Events 属性引发 FireProgress 事件FireProgress 事件向 Integration Services 运行时返回增量进度和完成的百分比信息。

注意

如果希望创建可更方便地重用于多个包的任务,请考虑以此脚本任务示例中的代码为基础,创建自定义任务。 有关详细信息,请参阅 开发自定义任务

说明

下面的示例创建一个自定义性能计数器并递增该计数器。 首先,该示例确定性能计数器是否已存在。 如果尚未创建性能计数器,则脚本会调用 PerformanceCounterCategory 对象的 Create 方法来创建它。 创建性能计数器后,脚本将递增该计数器。 最后,该示例依照最佳实践,在不再需要该性能计数器时,对其调用 Close 方法。

注意

创建新的性能计数器类别和性能计数器需要管理权限。 此外,新类别和计数器在创建后将持久存在于计算机中。

配置此脚本任务示例

  • 使用代码中的 Imports 语句导入 System.Diagnostics 命名空间

示例代码

Public Sub Main()  
  
    Dim myCounter As PerformanceCounter  
  
    Try  
        'Create the performance counter if it does not already exist.  
        If Not _  
        PerformanceCounterCategory.Exists("TaskExample") Then  
            PerformanceCounterCategory.Create("TaskExample", _  
                "Task Performance Counter Example", "Iterations", _  
                "Number of times this task has been called.")  
        End If  
  
        'Initialize the performance counter.  
        myCounter = New PerformanceCounter("TaskExample", _  
            "Iterations", String.Empty, False)  
  
        'Increment the performance counter.  
        myCounter.Increment()  
  
         myCounter.Close()  
        Dts.TaskResult = ScriptResults.Success  
    Catch ex As Exception  
        Dts.Events.FireError(0, _  
            "Task Performance Counter Example", _  
            ex.Message & ControlChars.CrLf & ex.StackTrace, _  
            String.Empty, 0)  
        Dts.TaskResult = ScriptResults.Failure  
    End Try  
  
End Sub  
  
public class ScriptMain  
{  
  
public void Main()  
        {  
  
            PerformanceCounter myCounter;  
  
            try  
            {  
                //Create the performance counter if it does not already exist.  
                if (!PerformanceCounterCategory.Exists("TaskExample"))  
                {  
                    PerformanceCounterCategory.Create("TaskExample", "Task Performance Counter Example", "Iterations", "Number of times this task has been called.");  
                }  
  
                //Initialize the performance counter.  
                myCounter = new PerformanceCounter("TaskExample", "Iterations", String.Empty, false);  
  
                //Increment the performance counter.  
                myCounter.Increment();  
  
                myCounter.Close();  
                Dts.TaskResult = (int)ScriptResults.Success;  
            }  
            catch (Exception ex)  
            {  
                Dts.Events.FireError(0, "Task Performance Counter Example", ex.Message + "\r" + ex.StackTrace, String.Empty, 0);  
                Dts.TaskResult = (int)ScriptResults.Failure;  
            }  
  
            Dts.TaskResult = (int)ScriptResults.Success;  
        }