Imports System Imports Microsoft.SqlServer.Dts.Runtime Imports System.Collections.Generic Public Class ScriptMain Enum Bits ' Separate the harness states from your benchmark states NoBitsSet = 0 ExitLoop = 1 Initialize = 2 Cleanup = 4 ResetEnvironment = 8 FirstBenchmark = 1024 SecondBenchmark = 2048 End Enum Public Sub Main() If FSM.Empty Then Dim records, bufferRows As Int32 FSM.SetVariablesToLog("NumberOfRecords", "DefaultBufferMaxRows") For records = 100000 To 1000000 Step 300000 FSM.AddState(Nothing, Bits.Initialize, "NumberOfRecords", records) For bufferRows = 20000 To 200000 Step 90000 FSM.AddState(Nothing, Bits.ResetEnvironment) FSM.AddState("FirstBenchmark", Bits.FirstBenchmark, "DefaultBufferMaxRows", bufferRows) FSM.AddState(Nothing, Bits.ResetEnvironment) FSM.AddState("SecondBenchmark", Bits.SecondBenchmark) Next Next FSM.AddState(Nothing, Bits.Cleanup) End If FSM.ExecuteState() ' Perform the actions specified for the current state Dts.TaskResult = Dts.Results.Success End Sub Class FSM Private Shared ReadOnly Property Data() As FSMData Get If Dts.Variables("FSMData").Value.GetType().ToString() = "System.Object" Then Dts.Variables("FSMData").Value = New FSMData() End If Return CType(Dts.Variables("FSMData").Value, FSMData) End Get End Property Friend Class FSMData Private CurrentStateValue As Int32 Friend StateList As New List(Of State) Friend StateStartTime As DateTime Friend VariablesToLog As String() Friend LogFilename As String Sub New() CurrentState = 0 LogFilename = Dts.Variables("ReportBaseFilename").Value.ToString() & _ CDate(Dts.Variables("StartTime").Value).ToString("yyyy-MM-dd_HH_mm_ss") & ".txt" End Sub Public Property CurrentState() As Int32 Get Return CurrentStateValue End Get Set(ByVal value As Int32) CurrentStateValue = value End Set End Property End Class Friend Class State Friend Name As String Friend BitMask As Bits Friend Variables As Object() Sub New(ByVal _name As String, ByVal _bitMask As Bits, ByVal ParamArray _variables As Object()) Name = _name BitMask = _bitMask Variables = _variables End Sub End Class Public Shared ReadOnly Property Empty() As Boolean Get Return Data.StateList.Count = 0 End Get End Property ' stateName = Nothing => Don't report elapsed time for this state Public Shared Sub AddState(ByVal stateName As String, ByVal bitMask As Bits, ByVal ParamArray setVariables As Object()) Data.StateList.Add(New State(stateName, bitMask, setVariables)) End Sub Public Shared Sub ExecuteState() If Data.CurrentState = 0 Then LogInitial() End If ' Log elapsed time of previous state If Not FSM.Empty And Data.CurrentState > 0 Then LogLine(Data.StateList(Data.CurrentState - 1).Name, FSM.ElapsedTime.ToString(), "") End If If Data.CurrentState >= Data.StateList.Count Then LogLine("FinishElapsed", (Now() - _ CDate(Dts.Variables("StartTime").Value)).TotalSeconds.ToString(), "") Dts.Variables("BitMask").Value = Bits.ExitLoop Else Dts.Variables("BitMask").Value = Data.StateList(Data.CurrentState).BitMask Dim i As Int32 For i = 0 To Data.StateList(Data.CurrentState).Variables.Length - 1 Step 2 Dts.Variables(Data.StateList(Data.CurrentState).Variables(i).ToString()).Value = _ Data.StateList(Data.CurrentState).Variables(i + 1) Next Data.StateStartTime = Now() Data.CurrentState += 1 ' Set the next state End If End Sub Public Shared Sub SetVariablesToLog(ByVal ParamArray setVariables As String()) Data.VariablesToLog = setVariables End Sub Private Shared Function GetVariablesToLog() As String() Return Data.VariablesToLog End Function Private Shared ReadOnly Property ElapsedTime() As Double Get Return (Now() - Data.StateStartTime).TotalSeconds End Get End Property Private Shared Sub LogInitial() If (Not IO.File.Exists(Data.LogFilename)) Then Dim s, v As String For Each v In FSM.GetVariablesToLog() s += v & "," Next LogLineOutput("StartTime,CurrentTime,Type," & s & "ElapsedTime,Info") End If LogLine("Start", "", "") LogLine("RunInfo", "", Dts.Variables("RunInfo").Value.ToString()) LogLine("BenchmarkInfo", "", Dts.Variables("BenchmarkInfo").Value.ToString()) LogLine("SystemInfo", "", Dts.Variables("SystemInfo").Value.ToString()) End Sub Private Shared Sub LogLine(ByVal type As String, ByVal elapsedTime As String, ByVal info As String) If Not IsNothing(type) Then Dim s, v As String For Each v In FSM.GetVariablesToLog() s += Dts.Variables(v).Value.ToString() & "," Next LogLineOutput(CDate(Dts.Variables("StartTime").Value).ToString("yyyy-MM-dd HH:mm:ss.ffffff") _ & "," & Now().ToString("yyyy-MM-dd HH:mm:ss.ffffff") _ & "," & ControlChars.Quote & type & ControlChars.Quote & "," & s & elapsedTime & "," & _ ControlChars.Quote & info & ControlChars.Quote) End If End Sub Private Shared Sub LogLineOutput(ByVal msg As String) IO.File.AppendAll(Data.LogFilename, msg & ControlChars.CrLf) End Sub End Class End Class