-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.lua
executable file
·336 lines (261 loc) · 7.5 KB
/
benchmark.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
#! /usr/bin/luajit
local socket = require('socket')
Benchmark = {}
Benchmark.__index = Benchmark
setmetatable(Benchmark, {
__call = function(self, desc, iterations, fn, ...)
if iterations > 0 and iterations < 1 then iterations = 1 end
local start = socket.gettime()
for i = 1, iterations do fn(...) end
local stop = socket.gettime() - start
print(desc .. "\t" .. string.format("%.6f", stop / iterations))
end
})
function Benchmark:start(desc)
local instance = {desc = desc}
setmetatable(instance, Benchmark)
instance.start = socket.gettime()
return instance
end
function Benchmark:stop()
local stop = socket.gettime() - self.start
print(self.desc .. "\t" .. string.format("%.6f", stop))
end
Benchmark("Print Message:\t", 1, function()
print("Hello world! " .. 4 * 90)
end)
print("Formspec in Minetest:\t0.000021")
local variation_fields = {
{ "x", "number", separator = "," },
{ "name", "string" },
{ "y", "number", required = false },
}
local RENDER_COUNT = 1
local BENCHMARK_COUNT = 100
local benchmark = {}
------------
-- Mocks --
------------
local mock = require("tests/mock")
minetest = {}
function minetest.get_modpath()
return "."
end
function minetest.register_on_player_receive_fields() end
local shared_form = import("formspec/form.lua"):new(mock.FormspecManager:new(), "shared_form", {}, {},
import("formspec/model.lua"):new({}))
------------------------
-- ErrorBuilder Class --
------------------------
function benchmark.errorbuilder()
local ErrorBuilder = import("errors.lua")
Benchmark("Create ErrorBuilder:", BENCHMARK_COUNT, function()
for i = 1, RENDER_COUNT do
ErrorBuilder:new("benchmark.errorbuilder")
end
end)
print()
end
benchmark.errorbuilder()
-----------------------
-- Placeholder Class --
-----------------------
function benchmark.placeholder()
local Placeholder = import("placeholder.lua")
local key
Benchmark("Create placeholder:", BENCHMARK_COUNT, function()
for i = 1, RENDER_COUNT do
key = Placeholder.index("name")
end
end)
local env = {person = {}}
Benchmark("Set value:\t", BENCHMARK_COUNT, function()
for i = 1, RENDER_COUNT do
Placeholder.set(env, key, "John Doe")
end
end)
Benchmark("Evaluate placeholder:", BENCHMARK_COUNT, function()
for i = 1, RENDER_COUNT do
Placeholder.evaluate(env, key)
end
end)
Benchmark("Create deep index:", BENCHMARK_COUNT, function()
for i = 1, RENDER_COUNT do
key = Placeholder.index("person").name
end
end)
Benchmark("Set deep value:\t", BENCHMARK_COUNT, function()
for i = 1, RENDER_COUNT do
Placeholder.set(env, key, "John")
end
end)
Benchmark("Evaluate deep index:", BENCHMARK_COUNT, function()
for i = 1, RENDER_COUNT do
Placeholder.index(env, key)
end
end)
print()
end
benchmark.placeholder()
---------------------
-- Variation Class --
---------------------
function benchmark.variation()
local FormspecManager = mock.FormspecManager
local Variation = import("formspec/element/variation.lua")
local parent = FormspecManager:new("variation_benchmark")
local Example
Benchmark("Create variation:", BENCHMARK_COUNT, function()
for i = 1, RENDER_COUNT do
Example = Variation:new(parent, "variation_benchmark", variation_fields)
end
end)
local populated
Benchmark("Insert def:\t", BENCHMARK_COUNT, function()
for i = 1, RENDER_COUNT do
populated = Example { x = 0, y = 0, name = "test" }
end
end)
Benchmark("Render variation:", BENCHMARK_COUNT, function()
for i = 1, RENDER_COUNT do
populated:render(shared_form)
end
end)
print()
end
benchmark.variation()
-------------------
-- Element Class --
-------------------
function benchmark.element()
local FormspecManager = mock.FormspecManager
local Element = import("formspec/element/element.lua")
local parent = FormspecManager:new("benchmark")
local Example
Benchmark("Initialize element:", BENCHMARK_COUNT, function()
Example = Element:new(parent, "benchmark_spec")
end)
Benchmark("Add variation:\t", 1, function()
Example:add_variation(variation_fields)
end)
local populated
Benchmark("Insert def:\t", BENCHMARK_COUNT, function()
for i = 1, RENDER_COUNT do
populated = Example { x = 0, y = 0, name = "test" }
end
end)
Benchmark("Render element:\t", BENCHMARK_COUNT, function()
for i = 1, RENDER_COUNT do
populated:render(shared_form)
end
end)
print()
end
benchmark.element()
-------------------------
-- Elements Generation --
-------------------------
local Elements
function benchmark.elements()
local manager = mock.FormspecManager:new("benchmark_elements")
local ElementsClass = import("formspec/elements.lua")
Benchmark("Generate elements:", 1, function()
Elements = ElementsClass(manager)
end)
manager.elements = Elements
setmetatable(manager.elements, { __index = _G })
print()
end
benchmark.elements()
---------------------------------
-- Complex Elements Benchmarks --
---------------------------------
-- Container Element
function benchmark.container_element()
local populated
Benchmark("Prepare container:", BENCHMARK_COUNT, function()
populated = Elements.container { x = 0, y = 0 } {
ui.text { x = 0, y = 0, text = "Hello world!" }
}
end)
Benchmark("Render container:", BENCHMARK_COUNT, function()
for i = 1, RENDER_COUNT do
populated:render(shared_form)
end
end)
print()
end
benchmark.container_element()
----------------
-- Form Class --
----------------
function benchmark.form()
local manager = mock.FormspecManager:new("benchmark")
local Model = import("formspec/model.lua")
local Form = import("formspec/form.lua")
local form_elements
Benchmark("Prepare elements:", BENCHMARK_COUNT, function()
form_elements = {}
for i = 1, RENDER_COUNT do
form_elements[#form_elements + 1] = Elements["text"] { x = 0, y = 0, text = "Hello!" }
end
end)
local Example
Benchmark("Initialize form:", BENCHMARK_COUNT, function()
Example = Form:new(manager, "form_bench", { w = 5, h = 5 }, form_elements, Model:new {})
end)
Benchmark("Render form:\t", BENCHMARK_COUNT, function()
Example:render(shared_form)
end)
print()
end
benchmark.form()
---------------------------
-- FormspecManager Class --
---------------------------
function benchmark.formspecmanager()
local parent = require("tests/mock").UIXInstance:new("benchmark")
local FormspecManager = import("formspec/manager.lua")
local instance
Benchmark("Initialize manager:", BENCHMARK_COUNT, function()
instance = FormspecManager:new(parent)
end)
local form_elements
Benchmark("Prepare elements:", BENCHMARK_COUNT, function()
form_elements = {}
for i = 1, RENDER_COUNT do
form_elements[#form_elements + 1] = Elements["text"] { x = 0, y = 0, text = "Hello!" }
end
end)
Benchmark("Add form:\t", BENCHMARK_COUNT, function()
instance("bench_form") { w = 5, h = 5 } (form_elements) ({})
end)
local form = instance:get("bench_form")
Benchmark("Render form:\t", BENCHMARK_COUNT, function()
form:render(shared_form)
end)
print()
end
benchmark.formspecmanager()
---------
-- API --
---------
function benchmark.api()
import("init.lua")
local uix
Benchmark("Initialize Everything:", BENCHMARK_COUNT / 4, function()
uix = libuix("benchmark_api")
end)
Benchmark("Add form:\t", BENCHMARK_COUNT / 4, function()
local form_elements = {}
for i = 1, RENDER_COUNT do
form_elements[#form_elements + 1] = Elements["text"] { x = 0, y = 0, text = "Hello!" }
end
uix.formspec("bench_form") { w = 5, h = 5 } (form_elements) ({})
end)
local form = uix.formspec:get("bench_form")
Benchmark("Render form:\t", BENCHMARK_COUNT, function()
form:render(shared_form)
end)
end
benchmark.api()