0
\$\begingroup\$

This script is meant to log the time it takes to complete functions in order to find the locations where performance could be improved in QTP. If a duplicate log is attempted, the script changes the value of the log so it can be added as a dictionary value without an error. Hopefully this behavior is avoided in the first place.

In case of any empty keys, a boolean will continue if the key doesn't contain a value. Then the script will print all values in a log at once.

Note: MsgBox is a placeholder for logging output

Do you see a better way to do this, perhaps without a dictionary?

Environment("logTimes") = CreateObject("Scripting.Dictionary") 

Call logStart("TestLog")
Call logStart("TestLog2")
Call logEnd("TestLog")
Call logEnd("TestLog2")
Call logTimesPrint

Function logStart(nameOfLog) 

    If Environment("logTimes").Exists(nameOfLog) Then
        For i = 1 To Environment("logTimes").Count Step 1
            If Environment("logTimes").Exists(nameOfLog & "_" & i) Then
'               MsgBox "Already exists"
                If i > Environment("logTimes").Count Then Exit For
            Else 
                Environment("logTimes").Add nameOfLog & "_" & i, CreateObject("Scripting.Dictionary")
                Environment("logTimes")(nameOfLog & "_" & i).Add "StartTime", now()
                Exit Function
            End If
        Next        
    Else 
       Environment("logTimes").Add nameOfLog, CreateObject("Scripting.Dictionary")   
       Environment("logTimes")(nameOfLog).Add "StartTime", Now()
    End If

End Function

Function logEnd(nameOfLog)

Set list = CreateObject("System.Collections.ArrayList")
If Environment("logTimes").Exists(nameOfLog & "_1") Then
    For Each key In Environment("logTimes").Keys
        If InStr(1, key, nameOfLog) Then  
            list.Add(key)
        End If
    Next 
    For i = 0 To list.Count Step 1
        If IsEmpty(Environment("logTimes")(list.Item(i))("FinishTime")) Then  
            Environment("logTimes")(list.Item(i))("FinishTime") = Now()
            Exit Function 
        End If              
    Next    
ElseIf Environment("logTimes").Exists(nameOfLog) Then
    Environment("logTimes")(nameOfLog)("FinishTime") = Now()
Else
    'MsgBox "No matching key found" 
End If 
End Function

Function logTimesPrint

    For Each key In Environment("logTimes").Keys 
        Continue = True
        If IsEmpty(key) Then Continue = False
        If Continue Then
            If not IsEmpty(Environment("logTimes")(key)("FinishTime")) Then  
                MsgBox key & " - " & CStr(DateDiff("s",Environment("logTimes")(key)("StartTime"), Environment("logTimes")(key)("FinishTime"))) & " seconds"
            Else
'               MsgBox "key not closed"
            End If
        End If
    Next    
End Function
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.