-
Notifications
You must be signed in to change notification settings - Fork 22
/
test.gd
318 lines (229 loc) · 7.39 KB
/
test.gd
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
extends SceneTree
# To run this script
# godot -s test.gd
const NUMBER_TEST_FOR_COLLISION = 100000
const NUMBER_OF_TESTS = 50000
const NUMBER_OF_OBJECTS = 50000 # enough to test and to not run out of memory
var UUID = preload('addons/uuid/uuid.gd')
func benchmark_raw():
print('Benchmarking ...')
var begin = Time.get_unix_time_from_system()
var index = 0
while index < NUMBER_OF_TESTS:
UUID.v4()
index += 1
var duration = 1.0 * Time.get_unix_time_from_system() - begin
print('uuid/sec: %.02f avg time: %.4fus total time: %.2fs' % [
NUMBER_OF_TESTS / duration,
(duration / NUMBER_OF_TESTS) * 1000000,
duration
])
print('Benchmark done')
func benchmark_raw_rng():
print('Benchmarking ...')
var rng = RandomNumberGenerator.new()
var begin = Time.get_unix_time_from_system()
var index = 0
while index < NUMBER_OF_TESTS:
UUID.v4_rng(rng)
index += 1
var duration = 1.0 * Time.get_unix_time_from_system() - begin
print('uuid/sec: %.02f avg time: %.4fus total time: %.2fs' % [
NUMBER_OF_TESTS / duration,
(duration / NUMBER_OF_TESTS) * 1000000,
duration
])
print('Benchmark done')
func benchmark_obj():
print('Benchmarking ...')
var begin = Time.get_unix_time_from_system()
var index = 0
while index < NUMBER_OF_TESTS:
UUID.new().free() # immediately freeing does not seem to add much overhead
index += 1
var duration = 1.0 * Time.get_unix_time_from_system() - begin
print('uuid/sec: %.02f avg time: %.4fus total time: %.2fs' % [
NUMBER_OF_TESTS / duration,
(duration / NUMBER_OF_TESTS) * 1000000,
duration
])
print('Benchmark done')
func benchmark_obj_rng():
print('Benchmarking ...')
var rng = RandomNumberGenerator.new()
var begin = Time.get_unix_time_from_system()
var index = 0
while index < NUMBER_OF_TESTS:
UUID.new(rng).free() # immediately freeing does not seem to add much overhead
index += 1
var duration = 1.0 * Time.get_unix_time_from_system() - begin
print('uuid/sec: %.02f avg time: %.4fus total time: %.2fs' % [
NUMBER_OF_TESTS / duration,
(duration / NUMBER_OF_TESTS) * 1000000,
duration
])
print('Benchmark done')
func benchmark_obj_to_dict():
print('Setting up benchmark ...')
var uuids = []
var index = 0
while index < NUMBER_OF_OBJECTS:
uuids.push_back(UUID.new())
index += 1
print('Benchmarking ...')
var begin = Time.get_unix_time_from_system()
for uuid in uuids:
uuid.as_dict()
var duration = 1.0 * Time.get_unix_time_from_system() - begin
print('uuid/sec: %.02f avg time: %.4fus total time: %.2fs' % [
NUMBER_OF_OBJECTS / duration,
(duration / NUMBER_OF_OBJECTS) * 1000000,
duration
])
print('Cleaning up ...')
for uuid in uuids:
uuid.free()
print('Benchmark done')
func benchmark_obj_to_str():
print('Setting up benchmark ...')
var uuids = []
var index = 0
while index < NUMBER_OF_OBJECTS:
uuids.push_back(UUID.new())
index += 1
print('Benchmarking ...')
var begin = Time.get_unix_time_from_system()
for uuid in uuids:
uuid.as_string()
var duration = 1.0 * Time.get_unix_time_from_system() - begin
print('uuid/sec: %.02f avg time: %.4fus total time: %.2fs' % [
NUMBER_OF_OBJECTS / duration,
(duration / NUMBER_OF_OBJECTS) * 1000000,
duration
])
print('Cleaning up ...')
for uuid in uuids:
uuid.free()
print('Benchmark done')
func benchmark_comp_raw():
print('Setting up benchmark ...')
var uuids = []
var index = 0
while index < NUMBER_OF_OBJECTS:
uuids.push_back(UUID.v4())
index += 1
index = 0
var collisions = 0
print('Benchmarking ...')
var begin = Time.get_unix_time_from_system()
while index < (NUMBER_OF_OBJECTS - 1):
var uuid1 = uuids[index]
var sub_index = index + 1
while sub_index < NUMBER_OF_OBJECTS:
if uuid1 == uuids[sub_index]:
# Don't print anything since it slows down the benchmark
collisions += 1
sub_index += 1
index += 1
var duration = 1.0 * Time.get_unix_time_from_system() - begin
print("%s collisions detected" % [collisions])
print("%s total comparison operations" % [NUMBER_OF_OBJECTS])
print('uuid/sec: %.02f avg time: %.4fus total time: %.2fs' % [
NUMBER_OF_OBJECTS / duration,
(duration / NUMBER_OF_OBJECTS) * 1000000,
duration
])
print('Benchmark done')
func benchmark_comp_obj():
print('Setting up benchmark ...')
var uuids = []
var index = 0
while index < NUMBER_OF_OBJECTS:
uuids.push_back(UUID.new())
index += 1
index = 0
var collisions = 0
print('Benchmarking ...')
var begin = Time.get_unix_time_from_system()
while index < (NUMBER_OF_OBJECTS - 1):
var uuid1 = uuids[index]
var sub_index = index + 1
while sub_index < NUMBER_OF_OBJECTS:
if uuid1.is_equal(uuids[sub_index]):
# Don't print anything since it slows down the benchmark
collisions += 1
sub_index += 1
index += 1
var duration = 1.0 * Time.get_unix_time_from_system() - begin
print("%s collisions detected" % [collisions])
print("%s total comparison operations" % [NUMBER_OF_OBJECTS])
print('uuid/sec: %.02f avg time: %.4fus total time: %.2fs' % [
NUMBER_OF_OBJECTS / duration,
(duration / NUMBER_OF_OBJECTS) * 1000000,
duration
])
print('Cleaning up ...')
for uuid in uuids:
uuid.free()
print('Benchmark done')
func detect_collision():
print('Detecting collision ...')
var number_of_collision = 0
var generated_uuid = {}
var index = 0
while index < NUMBER_TEST_FOR_COLLISION:
var key = UUID.v4()
if generated_uuid.has(key):
number_of_collision += 1
else:
generated_uuid[key] = true
index += 1
print('Number of collision: %s' % [ number_of_collision ])
print('Collision detection done')
func detect_collision_with_rng():
print('Detecting collision with rng ...')
var rng = RandomNumberGenerator.new()
var number_of_collision = 0
var generated_uuid = {}
var index = 0
while index < NUMBER_TEST_FOR_COLLISION:
var key = UUID.v4_rng(rng)
if generated_uuid.has(key):
number_of_collision += 1
else:
generated_uuid[key] = true
index += 1
print('Number of collision: %s' % [ number_of_collision ])
print('Collision detection done')
func test_is_equal():
var uuid_1 = UUID.new()
var uuid_2 = UUID.new()
var uuid_3 = UUID.new()
uuid_3._uuid = uuid_1.as_array()
print('Testing is_equal function')
if uuid_2.is_equal(uuid_1):
print('"is_equal" ain\'t working correctly (different uuid are identicals)')
elif not uuid_3.is_equal(uuid_1):
print('"is_equal" ain\'t working correctly (duplicated array not identical)')
print('Done.')
func _init():
test_is_equal()
detect_collision()
detect_collision_with_rng()
print("\n---------------- Raw ----------------")
benchmark_raw()
print("\n---------------- Raw with rng ----------------")
benchmark_raw_rng()
print("\n---------------- Simple object ----------------")
benchmark_obj()
print("\n---------------- Obj with rng ----------------")
benchmark_obj_rng()
print("\n---------------- Obj to dict ----------------")
benchmark_obj_to_dict()
print("\n---------------- Obj to string ----------------")
benchmark_obj_to_str()
print("\n---------------- Compare raw ----------------")
benchmark_comp_raw()
print("\n---------------- Compare obj ----------------")
benchmark_comp_obj()
quit()