-
Notifications
You must be signed in to change notification settings - Fork 3
/
SimpleBench.lua
378 lines (365 loc) · 10.7 KB
/
SimpleBench.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
------------- ABOUT
-- SimpleBench
-- A Simple Pure Lua VM Benchmarking Library
-- Version: 1.1.2-DEV.2
-- Author: Expo#3961
-- License: MIT
-- https://github.com/AstolfoBrew/SimpleBench
------------- DO NOT TOUCH
local Version = '1.2.0-DEV.1'
print('Setting up configuration for SimpleBench ' .. Version .. ' by Expo#3961')
------------- CONFIG
local Iterations = 50 -- Change to amount of total benchmark runs | Default: 50 | The higher, the longer the benchmark takes, but the more accurate/stable the result
local RBXWaitAfterRun = false -- Enable if, in roblox, an obfuscator (or horrible lua env) freezes for longer than 1s*runs
------------- CODE
local print, warn, error = print, warn, error
-- External Settings
if _G and _G.SimpleBenchSettings then
local Settings = _G.SimpleBenchSettings
if typeof(Settings) ~= 'table' then
return error 'Invalid _G.SimpleBenchSettings.'
end
if typeof(Settings.Iterations) == 'number' then
Iterations = Settings.Iterations
end
if typeof(Settings.RBXWaitAfterRun) == 'boolean' or Settings.RBXWaitAfterRun then
RBXWaitAfterRun = Settings.RBXWaitAfterRun
end
if Settings.Branch and Settings.Branch ~= 'Release' then
warn 'Branch is not release. Results may vary between commits.'
end
if typeof(Settings.Silent) == 'table' then
if typeof(Settings.Silent.print) == 'function' then
print = Settings.Silent.print
elseif not Settings.Silent.print then
print = function() end
end
if typeof(Settings.Silent.warn) == 'function' then
warn = Settings.Silent.warn
elseif not Settings.Silent.warn then
warn = function() end
end
if typeof(Settings.Silent.error) == 'function' then
error = Settings.Silent.error
elseif not Settings.Silent.error then
error = function() end
end
elseif Settings.Silent then
print = function() end
end
end
-- Check to make sure Settings are valid
if Iterations < 1 then
return error 'Too little iterations! Must atleast have 1 iteration.'
end
-- To Hex Utility
local function tohex(num)
local v = string.format('%02X', num)
if #v == 1 then
v = '0' .. v
end
return v
end
print '-- Opcode Test'
print '-- -> Tests if the VM is functioning as expected'
print '-- (expected: 0, 1, 2, 3)'
for i = 0, 3, 1 do
print('loop 0x01 | ', i)
end
print '-- (expected: 1 : 1, 2: 7, 3 : 5)'
local p = { 1, '7', 5 }
for k in pairs(p) do
print('loop 0x02 | ', k, ':', p[k])
end
print '-- (expected: 1 : 1, 2 : 7, 3 : 5)'
for k, v in ipairs(p) do
print('loop 0x03 | ', k, ':', v)
end
print '-- (expected: 6)'
print('len 0x04 | ', #'length')
print '-- below are checked for errors'
local failed = {}
local check = (function()
local code = 4
local padding = 5
local paddingRes = 23
return function(name, res, expected)
local pres = tostring(res)
if #name < padding then
name = name .. string.rep(' ', padding - #name)
end
if #pres < paddingRes then
pres = pres .. string.rep(' ', paddingRes - #pres)
end
code = code + 1
print(name, '0x' .. tohex(code), '|', pres, res == expected and 'Success' or 'Failed with ' .. res)
if res ~= expected then
table.insert(failed, { name, '0x' .. tohex(code) })
end
end
end)()
check('general', 1, 1)
check('general', 0, 0)
check('general', true, true)
check('general', false, false)
check('general', '', '')
check('general', 'test', 'test')
check('general', nil, nil)
check('concat', 'string1' .. 'string2', 'string1string2')
check('add', 60 + 9, 69)
check(
'call',
(function()
return 'Call Return Value'
end)(),
'Call Return Value'
)
check('div', 690 / 10, 69)
check('eq', 5 == 5, true)
local v = 0
v = v + 1
if false then
v = v - 2
end -- Disable Constant
check('eq', v == 5, false)
v = 0
v = v + 5
check('eq', v == 5, true)
check('ne', 5 ~= 5, false)
check('ne', 1 ~= 5, true)
v = 0
v = v + 5
check('ne', v ~= 5, false)
check('le', v <= 5, true)
check('le', v <= 6, true)
check('le', v <= 1, false)
check('not', not true, false)
check('not', not false, true)
local module = {}
function module:Yeet()
self.a = 1
return 2
end
check('oopcall', module:Yeet(), 2)
check('oopprop', module.a, 1)
if #failed == 0 then
print(string.rep('-', 18))
print '-- TEST SUCCESS --'
print(string.rep('-', 18))
else
local x = #('-- ' .. tostring(#failed) .. ' TEST(s) FAILED --')
local v = string.rep('-', x)
print(v)
print(x)
print(v)
for _, o in pairs(failed) do
print('Test', o[1], '(' .. tostring(o[2]) .. ')', 'Failed')
end
print(v)
end
print '\n-- Benchmarks'
local TotalScore = 0
local runBenchmark = function(n, c, cb, div)
local div = div or 1 -- For proportionally giant scores (such as empty func)
local padding = 20
if #n < padding then
n = n .. string.rep(' ', padding - #n)
end
local start = os.clock()
for i = 0, c, 1 do
cb()
end
local _end = os.clock()
-- get average
local avg = (_end - start) / c
local ThisScore = (1 / avg / c) / div
print(
'Benchmark',
n,
'Ran ' .. tostring(c) .. ' times with an average duration of ' .. tostring(avg) .. 's/run',
'(Score: ' .. ThisScore .. ')'
)
TotalScore = TotalScore + ThisScore
end
local itPerBench = 262144
local rsw = game and RBXWaitAfterRun and function()
game:GetService('RunService').RenderStepped:Wait()
end or function() end
-- local BenchmarkStart = os.clock();
local void = function(...) end
-- runBenchmark('Empty Function', 512, function() end)
do
local i = 679483
local s = 'abcdefg'
local iStr = tostring(i)
for run = 1, Iterations, 1 do
print('//////// RUN ' .. tostring(run) .. ' OF ' .. tostring(Iterations) .. ' ////////')
runBenchmark('Empty Function', itPerBench, function() end, 26 / 3)
runBenchmark('void', itPerBench, function()
void()
end, 22 / 3)
runBenchmark('i+1', itPerBench, function()
void(i + 1)
end, 18 / 3)
runBenchmark('i-1', itPerBench, function()
void(i - 1)
end, 18 / 3)
runBenchmark('i/2', itPerBench, function()
void(i / 2)
end, 12 / 3)
runBenchmark('i*2', itPerBench, function()
void(i * 2)
end, 12 / 3)
runBenchmark('i%2', itPerBench, function()
void(i % 2)
end)
runBenchmark('i^2', itPerBench, function()
void(i ^ 2)
end)
runBenchmark('1/i', itPerBench, function()
void(1 / i)
end)
runBenchmark('sqrt(i)', itPerBench, function()
void(math.sqrt(i))
end)
runBenchmark('tostring(i)', itPerBench, function()
void(tostring(i))
end)
runBenchmark('s..s', itPerBench, function()
void(s .. s)
end)
runBenchmark(
'index table nil',
itPerBench,
(function()
local t = {}
return function()
void(t.a)
end
end)()
)
runBenchmark(
'index table str',
itPerBench,
(function()
local t = { ['a'] = '' }
return function()
void(t.a)
end
end)()
)
runBenchmark(
'newindex table',
itPerBench,
(function()
local t = {}
return function()
t['a'] = ''
void(t)
end
end)()
)
runBenchmark(
'index metamethod',
itPerBench,
(function()
local t = setmetatable({}, { __newindex = function() end })
return function()
void(t['a'])
end
end)()
)
runBenchmark(
'newindex metamethod',
itPerBench,
(function()
local t = setmetatable({}, { __newindex = function() end })
return function()
t['a'] = ''
void(t)
end
end)()
)
runBenchmark('if', itPerBench, function()
if true then
void(true)
end
end)
runBenchmark('for loop', itPerBench, function()
for loopIterator = 0, 1, 1 do
void(loopIterator)
end
end)
runBenchmark('for in loop', itPerBench, function()
for loopIterator in pairs { 1 } do
void(loopIterator)
end
end)
if unpack or table.unpack then
local un = unpack or table.unpack
runBenchmark('unpack array', itPerBench, function()
void(un { 1, 2, 3 })
end)
end
runBenchmark('or(false,true)', itPerBench, function()
void(false or true)
end, 20 / 3)
runBenchmark('or(false,false)', itPerBench, function()
void(false or false)
end, 20 / 3)
runBenchmark('or(true,true)', itPerBench, function()
void(true or true)
end, 20 / 3)
runBenchmark('and(false,true)', itPerBench, function()
void(false and true)
end, 20 / 3)
runBenchmark('and(false,false)', itPerBench, function()
void(false and false)
end, 20 / 3)
runBenchmark('and(true,true)', itPerBench, function()
void(true and true)
end, 20 / 3)
runBenchmark('and(true,1+1)', itPerBench, function()
void(true and 1 + 1)
end, 20 / 3)
if string and string.format then
runBenchmark('str.format', itPerBench, function()
void(string.format('%s %s', s, s))
end)
end
runBenchmark('tonumber(i)', itPerBench, function()
void(tonumber(iStr))
end, 5)
runBenchmark('tostring(i)', itPerBench, function()
void(tostring(i))
end)
rsw()
end
end
-- local BenchmarkEnd = os.clock();
-- local BenchmarkTime = BenchmarkEnd - BenchmarkStart
-- print('Final Benchmark Score', ' ', ' ', 1 / BenchmarkTime)
print('Final Benchmark Score', ' ', ' ', TotalScore / Iterations, '(Averaged across ' .. Iterations .. ' iterations)')
print('SimpleBench ' .. Version .. ' by Expo#3961')
return TotalScore / Iterations
--[[
https://github.com/AstolfoBrew/SimpleBench/blob/main/LICENSE
============================================================
MIT License
Copyright (c) 2022 Expo#3961
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]