以指令碼工作監視效能計數器

適用於:Azure Data Factory 中的 SQL Server SSIS Integration Runtime

系統管理員可能需要監視 Integration Service 套件執行大量資料複雜轉換的效能。 Microsoft .NET Framework 的 System.Diagnostics 命名空間提供的類別,可讓您使用現有的效能計數器以及建立專屬的效能計數器。

效能計數器會儲存應用程式效能資訊,可用以分析某段時間的軟體效能。 透過使用 [效能監視器] 工具,就可以在本機或是遠端監視效能計數器。 您可以將效能計數器值儲存在變數中,以供之後在封裝中的控制流程分支使用。

若不使用效能計數器,您也可以透過 Dts 物件的 Events 屬性,引發 FireProgress 事件。 FireProgress 事件會將增加的進度與完成百分比資訊傳回 Integration Service 執行階段。

注意

如果您想要建立可更輕鬆地在多個封裝之間重複使用的工作,請考慮使用此指令碼工作範例中的程式碼做為自訂工作的起點。 如需詳細資訊,請參閱 開發自訂工作

描述

下列範例會建立自訂效能計數器並遞增計數器。 首先,範例會判斷效能計數器是否已經存在。 如果尚未建立效能計數器,指令碼會呼叫 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;  
        }