-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbenchmark_async.lua
More file actions
255 lines (211 loc) · 6.83 KB
/
benchmark_async.lua
File metadata and controls
255 lines (211 loc) · 6.83 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
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
-- LuaAsync Performance Benchmark Suite
-- Measures performance characteristics and checks for memory leaks
require("async")
local Benchmark = {}
Benchmark.results = {}
function Benchmark.measure(name, func, iterations)
-- Warm-up
for i = 1, math.min(100, iterations) do
func()
end
-- Measure
collectgarbage("collect")
local memBefore = collectgarbage("count")
local start = os.clock()
for i = 1, iterations do
func()
end
local elapsed = os.clock() - start
local memAfter = collectgarbage("count")
local memUsed = memAfter - memBefore
local opsPerSecond = iterations / elapsed
local avgTime = (elapsed / iterations) * 1000 -- in ms
table.insert(Benchmark.results, {
name = name,
iterations = iterations,
elapsed = elapsed,
opsPerSecond = opsPerSecond,
avgTimeMs = avgTime,
memUsedKB = memUsed
})
return opsPerSecond, avgTime, memUsed
end
function Benchmark.printResults()
print("\n" .. string.rep("=", 90))
print("PERFORMANCE BENCHMARK RESULTS")
print(string.rep("=", 90))
print(string.format("%-40s %12s %12s %12s %12s",
"Benchmark", "Iterations", "Ops/sec", "Avg (ms)", "Memory (KB)"))
print(string.rep("-", 90))
for _, result in ipairs(Benchmark.results) do
print(string.format("%-40s %12d %12.0f %12.4f %12.2f",
result.name,
result.iterations,
result.opsPerSecond,
result.avgTimeMs,
result.memUsedKB))
end
print(string.rep("=", 90))
end
-- ============================================================================
-- BENCHMARKS
-- ============================================================================
print("Initializing LuaAsync...")
Async.Init()
-- Benchmark 1: Task Creation Overhead
print("\n[1/8] Benchmarking Task creation overhead...")
Benchmark.measure("Task Creation (empty function)", function()
local task = Task.new(function() end, nil)
task:Start()
end, 10000)
-- Benchmark 2: Task Scheduling Throughput
print("[2/8] Benchmarking Task scheduling throughput...")
Benchmark.measure("Task.Run (simple yield)", function()
Task.Run(function()
coroutine.yield()
end, nil)
end, 10000)
-- Run the queued tasks
for i = 1, 2000 do
Async.Update(0.016)
end
-- Benchmark 3: Task.Delay Performance
print("[3/8] Benchmarking Task.Delay performance...")
Async.Init()
local delayCount = 0
local delayWaitables = {}
Benchmark.measure("Task.Delay (100ms)", function()
table.insert(delayWaitables, Task.Delay(100, nil))
delayCount = delayCount + 1
if delayCount % 100 == 0 then
-- Process some frames to prevent queue overflow
for j = 1, 10 do
Async.Update(0.016)
end
end
end, 1000)
-- Process remaining tasks
for i = 1, 5000 do
Async.Update(0.016)
end
-- Benchmark 4: Task.FromResult Performance
print("[4/8] Benchmarking Task.FromResult performance...")
Async.Init()
Benchmark.measure("Task.FromResult", function()
local task = Task.FromResult("test")
end, 100000)
-- Benchmark 5: WhenAll Performance
print("[5/8] Benchmarking WhenAll performance...")
Async.Init()
Benchmark.measure("Task.WhenAll (10 tasks)", function()
local tasks = {}
for i = 1, 10 do
table.insert(tasks, Task.new(function()
coroutine.yield()
return i
end, nil))
end
local waitable = Task.WhenAll(nil, table.unpack(tasks))
waitable() -- Start tasks
for j = 1, 3 do
Async.Update(0.016)
end
end, 1000)
-- Benchmark 6: Nested Tasks (Sub-tasks)
print("[6/8] Benchmarking nested task performance...")
Async.Init()
Benchmark.measure("Nested Tasks (3 levels)", function()
Task.Run(function()
Task.RunAsSub(function()
Task.RunAsSub(function()
coroutine.yield()
end, nil)
coroutine.yield()
end, nil)
coroutine.yield()
end, nil)
for j = 1, 5 do
Async.Update(0.016)
end
end, 1000)
-- Benchmark 7: Memory Leak Test - Long Running
print("[7/8] Testing for memory leaks (long-running)...")
Async.Init()
collectgarbage("collect")
local memStart = collectgarbage("count")
local tasks = {}
for i = 1, 1000 do
local task = Task.Run(function()
Await(Task.Delay(10, nil))
end, nil)
table.insert(tasks, task)
if i % 100 == 0 then
-- Process some frames
for j = 1, 20 do
Async.Update(0.016)
end
end
end
-- Process all remaining tasks
for i = 1, 10000 do
Async.Update(0.016)
if #Async._pendingTaskQueue == 0 then
break
end
end
collectgarbage("collect")
local memEnd = collectgarbage("count")
local memLeaked = memEnd - memStart
print(string.format(" Memory before: %.2f KB", memStart))
print(string.format(" Memory after: %.2f KB", memEnd))
print(string.format(" Memory leaked: %.2f KB", memLeaked))
if memLeaked > 100 then
print(" ⚠️ WARNING: Possible memory leak detected!")
else
print(" ✓ Memory usage is acceptable")
end
-- Benchmark 8: Cancellation Performance
print("[8/8] Benchmarking cancellation performance...")
Async.Init()
Benchmark.measure("Task Cancellation", function()
local cts = CancellationTokenSource.new()
local task = Task.Run(function()
Await(Task.Delay(1000, cts:GetToken()))
end, cts:GetToken())
cts:Cancel()
Async.Update(0.016)
end, 10000)
-- ============================================================================
-- RESULTS
-- ============================================================================
Benchmark.printResults()
-- ============================================================================
-- SUMMARY
-- ============================================================================
print("\n" .. string.rep("=", 90))
print("PERFORMANCE ANALYSIS")
print(string.rep("=", 90))
-- Find key metrics
local creationTime = Benchmark.results[1].avgTimeMs
local schedulingOps = Benchmark.results[2].opsPerSecond
local fromResultOps = Benchmark.results[4].opsPerSecond
print(string.format("\n✓ Task Creation: %.4f ms per task", creationTime))
print(string.format("✓ Scheduling Throughput: %.0f tasks/second", schedulingOps))
print(string.format("✓ FromResult Speed: %.0f tasks/second", fromResultOps))
if creationTime < 0.1 then
print(" ✓ Excellent - Task creation is very fast")
elseif creationTime < 1.0 then
print(" ✓ Good - Task creation overhead is acceptable")
else
print(" ⚠️ Warning - Task creation may be slow")
end
if schedulingOps > 10000 then
print(" ✓ Excellent - High scheduling throughput")
elseif schedulingOps > 1000 then
print(" ✓ Good - Reasonable scheduling throughput")
else
print(" ⚠️ Warning - Low scheduling throughput")
end
print("\n" .. string.rep("=", 90))
print("Benchmark complete!")
print(string.rep("=", 90) .. "\n")