-
Notifications
You must be signed in to change notification settings - Fork 0
/
M_clsBenchmark.def
232 lines (188 loc) · 6.84 KB
/
M_clsBenchmark.def
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'---------------------------------------------------------------------------------------
' Module : clsBenchmark
' Author : K.Gundermann
' Date : 01.07.2012
' Purpose : Benchmarks your Code
' Create a Class with some Sub's
' For Running a single Benchmark:
' Benchmark.Run(New clsMyBenchmark).Report
' For running several Benchmarks:
' Benchmark.RunClasses("clsMyBenchmark clsOtherBench").Report
' Updates : 30.07.12 Changed to Timeout Model
' 02.10.12 Code cleanup + Setup/Teardown methods
' 26.02.14 moved from timeGetTime to clsStopwatch for higher resolution
'---------------------------------------------------------------------------------------
Option Compare Database
Option Explicit
Private Const DEFAULT_TIMEOUT = 200 ' ms
Private Const MINIMUM_ITERATIONS = 1
Private Const FACTORY_NAME = "__Bench_Factory"
Private Const FACTORY_PROC_NAME = "GetBenchClass"
Private c_ClassName As String
Private c_Results As clsBenchmark_Results
Private c_MinIterations As Long
Private c_Timeout As Long
Private c_BaseDuration As Long
Private c_Overhead As Currency
Private Sub Class_Initialize()
Set c_Results = New clsBenchmark_Results
c_MinIterations = MINIMUM_ITERATIONS
c_Timeout = DEFAULT_TIMEOUT
End Sub
' ----------------------------------------------------------------------------------
' --- Properties
' ----------------------------------------------------------------------------------
Public Property Get Timeout() As Long
Timeout = c_Timeout
End Property
Public Property Let Timeout(ByVal ms As Long)
c_Timeout = ms
End Property
Public Function SetTimeout(ByVal ms As Long) As clsBenchmark
Me.Timeout = ms
Set SetTimeout = Me
End Function
Public Property Get MinIterations() As Long
MinIterations = c_MinIterations
End Property
Public Property Let MinIterations(ByVal TheValue As Long)
c_MinIterations = TheValue
End Property
Public Function SetMinIterations(ByVal TheValue As Long) As clsBenchmark
Me.MinIterations = TheValue
Set SetMinIterations = Me
End Function
Public Property Get TestCount() As Integer
TestCount = c_Results.Count
End Property
Public Property Get Results() As clsBenchmark_Results
Set Results = c_Results
End Property
Public Property Get Title() As String
Title = "Running Benchmark " & c_ClassName & " " & Now
End Property
' ----------------------------------------------------------------------------------
' --- Methods / Functions
' ----------------------------------------------------------------------------------
Public Function Run(ByVal BenchmarkClass As Variant, Optional ByVal Timeout As Long = 0) As clsBenchmark
Dim objBench As Object
Dim miMethod As TLI.MemberInfo
If Timeout > 0 Then Me.Timeout = Timeout
If VarType(BenchmarkClass) = vbObject Then
Set objBench = BenchmarkClass
ElseIf VarType(BenchmarkClass) = vbString Then
Set objBench = GetClassObj(BenchmarkClass)
Else
Err.Raise vbObject, , "Unknown Class Description: " & BenchmarkClass
Exit Function
End If
If objBench Is Nothing Then Exit Function
With ObjectInfo(objBench)
c_ClassName = .ObjectName
If .AllMembers.Count > 0 Then
Call Calibrate
For Each miMethod In .AllMembers
If miMethod.InvokeKind = INVOKE_FUNC Then
Call Run_SingleMethod(objBench, miMethod.Name)
End If
Next
End If
End With
Set Run = Me
End Function
Public Function Report() As String
Report = Me.Title & vbCrLf & Me.Results.Report
End Function
' ----------------------------------------------------------------------------------
' Create a Factory and the Create an Object of the Class with the Benchmark code
Private Function GetClassObj(ByVal Classname As String) As Object
If CreateFactory(Classname) Then
Set GetClassObj = Application.Run(FACTORY_PROC_NAME)
End If
End Function
Private Function CreateFactory(ByVal Classname As String) As Boolean
Dim VBKomp As VBComponent
Dim str As String
str = str & "Public Function " & FACTORY_PROC_NAME & " as " & Classname & vbCrLf
str = str & " Set " & FACTORY_PROC_NAME & " = New " & Classname & vbCrLf
str = str & "End Function" & vbCrLf
With MainApp_Project
On Error Resume Next
Set VBKomp = .VBComponents.Item(Classname)
If Err = 9 Then
On Error GoTo 0
Err.Raise 9, , "Can not find Benchmark Class " & Classname
Exit Function
End If
On Error GoTo 0
End With
DestroyFactory
With MainApp_Project
Set VBKomp = .VBComponents.Add(vbext_ct_StdModule)
VBKomp.Name = FACTORY_NAME
VBKomp.CodeModule.AddFromString str
End With
CreateFactory = True
End Function
Private Function MainApp_Project() As VBProject
Static proj As VBProject
If proj Is Nothing Then
For Each proj In Application.VBE.VBProjects
If proj.FileName = Application.CurrentProject.FullName Then Exit For
Next
End If
Set MainApp_Project = proj
End Function
Private Sub DestroyFactory()
On Error Resume Next
With MainApp_Project
.VBComponents.Remove .VBComponents(FACTORY_NAME)
End With
On Error GoTo 0
End Sub
Private Sub Run_SingleMethod(ByVal TheClass As Object, ByVal Methodname As String)
Dim cTotalTime As Currency
Dim lCount As Long
If Methodname = "Setup" Then Exit Sub
If Methodname = "Teardown" Then Exit Sub
Do While True
With ObjectInfo(TheClass)
If .HasFunction("Setup") Then Call .CallFunction("Setup", Methodname)
End With
lCount = lCount + 1
With StopWatch
Call TLI.InvokeHook(TheClass, Methodname, INVOKE_FUNC)
cTotalTime = cTotalTime + .Elapsedms
End With
With ObjectInfo(TheClass)
If .HasFunction("Teardown") Then Call .CallFunction("Teardown", Methodname)
End With
If (Me.MinIterations > 0) And (lCount >= Me.MinIterations) Then Exit Do
If (Me.Timeout > 0) And (cTotalTime > Me.Timeout) Then Exit Do
Loop
Call c_Results.Add(Methodname, lCount, cTotalTime - (lCount * c_Overhead))
End Sub
Private Sub Calibrate()
Dim TheClass As clsNull_Bench
Dim Methodname As String
Set TheClass = New clsNull_Bench
Methodname = "DoNothing"
' Warmup
With StopWatch
Call TLI.InvokeHook(TheClass, Methodname, INVOKE_FUNC)
c_Overhead = .Elapsedms
End With
' Measure
With StopWatch
Call TLI.InvokeHook(TheClass, Methodname, INVOKE_FUNC)
c_Overhead = .Elapsedms
End With
End Sub
Private Sub Class_Terminate()
DestroyFactory
Set c_Results = Nothing
End Sub