forked from ATTWoWAddon/AllTheThings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__test.lua
418 lines (337 loc) · 7.41 KB
/
__test.lua
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
local _, app = ...;
local api = {}
app.Modules.Test = api
app.Testraw = function(count)
local a
local t = {}
local mt = { __index = { [1] = true } }
local mtt = setmetatable({}, mt);
local mtf = { __index = function(t, key) return key; end }
local mttf = setmetatable({}, mtf);
local mti = { __newindex = function(t,key,val) rawset(t,key,val); end }
local mtti = setmetatable({}, mti);
-- Items are likely the most complex of Lib types, though in practice they are all inherently 1 layer deep
local o = CreateObject({itemID=203408});
local function Benchmark(t, type)
app.PrintDebug(type,"rawset")
for i=1,count do
rawset(t, i, true)
end
app.PrintDebugPrior("---")
app.PrintDebug(type,"[]<=")
for i=1,count do
t[i] = true
end
app.PrintDebugPrior("---")
app.PrintDebug(type,"rawget")
for i=1,count do
a = rawget(t, i)
end
app.PrintDebugPrior("---")
app.PrintDebug(type,"<=[]")
for i=1,count do
a = t[i]
end
app.PrintDebugPrior("---")
end
app.PrintDebug("Test:Start",count)
Benchmark(t, "raw")
Benchmark(mtt, "__index-table")
Benchmark(mttf, "__index-func")
Benchmark(mtti, "__newindex")
Benchmark(o, "Item Type")
app.PrintDebug("Test:End")
end
app.Testwraps = function(count)
local a
-- Items are likely the most complex of Lib types, though in practice they are all inherently 1 layer deep
local o = CreateObject({itemID=203408});
local Wrap = app.CreateWrapHeader;
local WrapFilter = app.CreateWrapFilterHeader;
local function Benchmark(t, type)
app.PrintDebug(type,"CreateObject")
for i=1,count do
a = CreateObject(t)
end
app.PrintDebugPrior("---")
app.PrintDebug(type,"CreateObject.rootOnly")
for i=1,count do
a = CreateObject(t, true)
end
app.PrintDebugPrior("---")
app.PrintDebug(type,"CreateWrapHeader")
for i=1,count do
a = Wrap(t)
end
app.PrintDebugPrior("---")
app.PrintDebug(type,"CreateWrapFilterHeader")
for i=1,count do
a = WrapFilter(t)
end
app.PrintDebugPrior("---")
end
app.PrintDebug("Test:Start",count)
Benchmark(o, "Item Type")
app.PrintDebug("Test:End")
end
app.Testaccess = function(count)
local t = {
a = 1,
b = 2,
c = 3
}
local z
local function DirectAccessx1()
if t.a then
return t.a
end
end
local function LocalAccessx1()
local a = t.a;
if a then
return a
end
end
local function Benchmark()
app.PrintDebug("DirectAccess",count)
for i=1,count do
z = DirectAccessx1()
end
app.PrintDebugPrior("---")
app.PrintDebug("LocalAccess",count)
for i=1,count do
z = LocalAccessx1()
end
app.PrintDebugPrior("---")
app.PrintDebug("local in loop",count)
for i=1,count do
local z = i
end
app.PrintDebugPrior("---")
app.PrintDebug("local before loop",count)
local z
for i=1,count do
z = i
end
app.PrintDebugPrior("---")
end
Benchmark();
end
app.metacompare = function(count)
local t = {
a = 1,
b = 2,
c = 3
}
local m = setmetatable(t, { __index = function(t,key) return 0; end})
local z
local function Benchmark()
-- 0.320742 @ 1M
app.PrintDebug("Basic Access",count)
for i=1,count do
z = t[i]
end
app.PrintDebugPrior("---")
-- 0.389216 @ 1M
app.PrintDebug("Basic Access with fallback",count)
for i=1,count do
z = t[i] or 0
end
app.PrintDebugPrior("---")
-- 0.335911 @ 1M
app.PrintDebug("Meta Access",count)
for i=1,count do
z = m[i]
end
app.PrintDebugPrior("---")
end
Benchmark();
end
app.errors = function(msg)
print("ATT Errors Test", msg)
local function throw(msg)
print("error",msg)
error(msg)
end
local function throwroutine()
local i = 0
print(i) coroutine.yield() i = i + 1
print(i) coroutine.yield() i = i + 1
print(i) coroutine.yield() i = i + 1
print(i) coroutine.yield() i = i + 1
print(i) coroutine.yield() i = i + 1
throw("coroutine"..msg)
end
local runner1 = app.CreateRunner("error1")
local runner2 = app.CreateRunner("error2")
-- push function error
app.Push("push"..msg, "test", throw)
-- start coroutine error
app.StartCoroutine("testroutine", throwroutine, 1)
-- runner errors
runner1.Run(throw, msg.."1")
runner1.Run(throw, msg.."2")
runner2.Run(throw, msg.."3")
runner2.Run(throw, msg.."4")
end
function ATTarrayappend()
local a,b,c,d,e
local r
app.PrintTable(r)
local append = app.ArrayAppend
app.PrintTable(append(r,a,b,c,d,e))
e = {7,8,9}
app.PrintTable(append(r,a,b,c,d,e))
d = {5,6}
c = {3,4}
b = {2}
a = {1}
app.PrintTable(append(r,a,b,c,d,e))
end
function ATTinserts(count)
count = count or 1000
local a,b,c,d = {},{},{},{}
app.PrintDebug("table.insert",count)
-- 0.221352 @ 1M
for i=1,count do
table.insert(a,i)
end
app.PrintDebugPrior("---")
local table_insert = table.insert
app.PrintDebug("table_insert",count)
-- 0.214889 @ 1M
for i=1,count do
table_insert(b,i)
end
app.PrintDebugPrior("---")
local tinsert = tinsert
app.PrintDebug("tinsert",count)
-- 0.214733 @ 1M
for i=1,count do
tinsert(c,i)
end
app.PrintDebugPrior("---")
local tinsert = tinsert
app.PrintDebug("t[#t+1]",count)
-- 0.087703 @ 1M
for i=1,count do
d[#d+1]=i
end
app.PrintDebugPrior("---")
end
function ATTfuncvsor(count)
count = count or 1000
local function hash(t)
local a = t.hash
if a then return a end
a = app.CreateHash(t)
return a
end
local o = app.__CreateObject(app.SearchForObject("questID", 8440))
local p = {key="text",text="temp"}
local temp
app.PrintDebug("direct-only-object",count)
-- 2.010676 @ 1M
for i=1,count do
temp = o.hash
o.hash = nil
end
app.PrintDebugPrior("---")
app.PrintDebug("func-only-object",count)
-- 2.088579 @ 1M
for i=1,count do
temp = hash(o)
o.hash = nil
end
app.PrintDebugPrior("---")
app.PrintDebug("direct|func-object",count)
-- 2.025655 @ 1M
for i=1,count do
temp = o.hash or hash(o)
o.hash = nil
end
app.PrintDebugPrior("---")
app.PrintDebug("direct-only-table",count)
-- 0.029104 @ 1M
for i=1,count do
temp = p.hash
p.hash = nil
end
app.PrintDebugPrior("---")
app.PrintDebug("func-only-table",count)
-- 0.510414 @ 1M
for i=1,count do
temp = hash(p)
p.hash = nil
end
app.PrintDebugPrior("---")
app.PrintDebug("direct|func-table",count)
-- 0.527799 @ 1M
for i=1,count do
temp = p.hash or hash(p)
p.hash = nil
end
app.PrintDebugPrior("---")
end
function ATTcoroutines(count)
count = count or 1000
local a,b = 1,1
local aa,bb
local cy = coroutine.yield
local function fa()
app.PrintDebug("fa",a,aa)
a = a + 1
end
local function fb()
app.PrintDebug("fb",b,bb)
b = b + 1
end
local function ca()
app.PrintDebug("ca")
-- for i=1,count do
fa()
-- cy()
-- end
app.PrintDebug("ca:done")
-- app.PrintMemoryUsage()
end
local function cb()
app.PrintDebug("cb")
-- for i=1,count do
fb()
-- cy()
-- end
app.PrintDebug("cb:done")
-- app.PrintMemoryUsage()
end
local function cca()
app.PrintDebug("cca")
for i=1,count do
app.PrintDebug("cca",i)
aa = i
app.StartCoroutine("ca",ca)
-- app.PrintMemoryUsage()
cy()
end
app.PrintDebug("cca:done")
-- app.PrintMemoryUsage()
end
local function ccb()
app.PrintDebug("ccb")
for i=1,count do
app.PrintDebug("ccb",i)
bb = i
app:StartATTCoroutine("cb",cb)
-- app.PrintMemoryUsage()
cy()
end
app.PrintDebug("ccb:done")
-- app.PrintMemoryUsage()
end
app.PrintDebug("Test Coroutines")
-- app.PrintMemoryUsage()
app.StartCoroutine("cca",cca)
-- app.PrintMemoryUsage()
app:StartATTCoroutine("ccb",ccb)
-- app.PrintMemoryUsage()
end