-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmemory_test.lua
More file actions
116 lines (98 loc) · 3.08 KB
/
memory_test.lua
File metadata and controls
116 lines (98 loc) · 3.08 KB
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
-- LuaAsync Memory Leak Test
-- Thorough test for memory leaks
require("async")
print("=" .. string.rep("=", 79))
print("MEMORY LEAK TEST")
print("=" .. string.rep("=", 79))
function testMemoryLeak(testName, taskFunc)
collectgarbage("collect")
collectgarbage("collect")
local memBefore = collectgarbage("count")
-- Create and complete many tasks
for i = 1, 1000 do
Async.Init()
taskFunc()
-- Process until all tasks complete
local frameCount = 0
for j = 1, 1000 do
Async.Update(0.016)
frameCount = frameCount + 1
if #Async._pendingTaskQueue == 0 then
break
end
end
end
-- Force garbage collection
collectgarbage("collect")
collectgarbage("collect")
local memAfter = collectgarbage("count")
local memLeaked = memAfter - memBefore
print(string.format("\n%s:", testName))
print(string.format(" Memory before: %.2f KB", memBefore))
print(string.format(" Memory after: %.2f KB (1000 iterations)", memAfter))
print(string.format(" Memory leaked: %.2f KB", memLeaked))
if memLeaked < 50 then
print(" ✓ PASS - No significant memory leak")
return true
elseif memLeaked < 200 then
print(" ⚠️ WARN - Minor memory growth (may be acceptable)")
return true
else
print(" ✗ FAIL - Significant memory leak detected!")
return false
end
end
-- Test 1: Simple tasks
local allPass = true
allPass = testMemoryLeak("Test 1: Simple yield tasks", function()
Task.Run(function()
coroutine.yield()
end, nil)
end) and allPass
-- Test 2: Task.Delay
allPass = testMemoryLeak("Test 2: Task.Delay tasks", function()
Task.Run(function()
Await(Task.Delay(1, nil))
end, nil)
end) and allPass
-- Test 3: Nested tasks
allPass = testMemoryLeak("Test 3: Nested tasks", function()
Task.Run(function()
Task.RunAsSub(function()
coroutine.yield()
end, nil)
coroutine.yield()
end, nil)
end) and allPass
-- Test 4: WhenAll
allPass = testMemoryLeak("Test 4: WhenAll tasks", function()
local tasks = {}
for i = 1, 5 do
table.insert(tasks, Task.new(function()
coroutine.yield()
return i
end, nil))
end
local waitable = Task.WhenAll(nil, table.unpack(tasks))
waitable() -- Start all tasks
end) and allPass
-- Test 5: Task.FromResult
allPass = testMemoryLeak("Test 5: Task.FromResult", function()
local task = Task.FromResult("test")
task:OnCompleted(function() end)
end) and allPass
-- Test 6: Cancelled tasks
allPass = testMemoryLeak("Test 6: Cancelled tasks", function()
local cts = CancellationTokenSource.new()
Task.Run(function()
Await(Task.Delay(1000, cts:GetToken()))
end, cts:GetToken())
cts:Cancel()
end) and allPass
print("\n" .. string.rep("=", 80))
if allPass then
print("✓ ALL MEMORY TESTS PASSED - No significant leaks detected")
else
print("✗ SOME MEMORY TESTS FAILED - Review memory management")
end
print(string.rep("=", 80) .. "\n")