Skip to content

Commit c9e4215

Browse files
committed
test: add multi benchmark
1 parent ccdd539 commit c9e4215

1 file changed

Lines changed: 188 additions & 0 deletions

File tree

benches/multi.luau

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
local Charm = require("../packages/charm/src")
2+
local bench = require("./bench")
3+
4+
type Atom<T> = Charm.Atom<T>
5+
6+
local signal = Charm.atom
7+
local computed = Charm.computed
8+
local effect = Charm.effect
9+
10+
local function busy(): number
11+
local a = 0
12+
for i = 1, 100 do
13+
a += 1
14+
end
15+
return a
16+
end
17+
18+
do
19+
local head = signal(0)
20+
local computed1 = computed(function()
21+
return head()
22+
end)
23+
local computed2 = computed(function()
24+
computed1()
25+
return 0
26+
end)
27+
local computed3 = computed(function()
28+
busy() -- heavy computation
29+
return computed2() + 1
30+
end)
31+
local computed4 = computed(function()
32+
return computed3() + 2
33+
end)
34+
local computed5 = computed(function()
35+
return computed4() + 3
36+
end)
37+
effect(function()
38+
computed5()
39+
busy() -- heavy side effect
40+
end)
41+
42+
bench.run("avoidablePropagation", function()
43+
head(1)
44+
assert(computed5() == 6)
45+
for i = 0, 999 do
46+
head(i)
47+
end
48+
assert(computed5() == 6)
49+
end)
50+
end
51+
52+
do
53+
local head = signal(0)
54+
local double = computed(function()
55+
return head() * 2
56+
end)
57+
local inverse = computed(function()
58+
return -head()
59+
end)
60+
local current = computed(function()
61+
local result = 0
62+
for i = 0, 19 do
63+
result += if head() % 2 == 1 then double() else inverse()
64+
end
65+
return result
66+
end)
67+
68+
local callCounter = 0
69+
effect(function()
70+
current()
71+
callCounter += 1
72+
end)
73+
74+
bench.run("unstable", function()
75+
head(1)
76+
assert(current() == 40)
77+
callCounter = 0
78+
for i = 0, 99 do
79+
head(i)
80+
end
81+
assert(callCounter == 100)
82+
end)
83+
end
84+
85+
do
86+
local head = signal(0)
87+
local double = computed(function()
88+
return head() * 2
89+
end)
90+
local inverse = computed(function()
91+
return -head()
92+
end)
93+
local current = computed(function()
94+
local result = 0
95+
for i = 0, 19 do
96+
result += if head() % 2 == 1 and i % 2 == 1 then double() else inverse()
97+
end
98+
return result
99+
end)
100+
101+
local callCounter = 0
102+
effect(function()
103+
current()
104+
callCounter += 1
105+
end)
106+
107+
bench.run("unstable 50%", function()
108+
head(1)
109+
assert(current() == 10)
110+
callCounter = 0
111+
for i = 0, 99 do
112+
head(i)
113+
end
114+
assert(callCounter == 100)
115+
end)
116+
end
117+
118+
do
119+
local head = signal(0)
120+
local last: () -> number = head
121+
local callCounter = 0
122+
123+
for i = 0, 49 do
124+
local current = computed(function()
125+
return head() + i
126+
end)
127+
local current2 = computed(function()
128+
return current() + 1
129+
end)
130+
effect(function()
131+
current2()
132+
callCounter += 1
133+
end)
134+
last = current2
135+
end
136+
137+
bench.run("broadPropagation", function()
138+
head(1)
139+
callCounter = 0
140+
for i = 0, 49 do
141+
head(i)
142+
assert(last() == i + 50)
143+
end
144+
assert(callCounter == 50 * 50)
145+
end)
146+
end
147+
148+
do
149+
local heads = {}
150+
for index = 0, 99 do
151+
heads[index] = signal(0)
152+
end
153+
154+
local mux = computed(function()
155+
local result = {}
156+
for index = 0, 99 do
157+
result[index] = heads[index]()
158+
end
159+
return result
160+
end)
161+
162+
local splited = {}
163+
for index = 0, 99 do
164+
local x = computed(function()
165+
return mux()[index]
166+
end)
167+
splited[index] = computed(function()
168+
return x() + 1
169+
end)
170+
end
171+
172+
for _, x in splited do
173+
effect(function()
174+
x()
175+
end)
176+
end
177+
178+
bench.run("mux", function()
179+
for i = 0, 9 do
180+
heads[i](i)
181+
assert(splited[i]() == i + 1)
182+
end
183+
for i = 0, 9 do
184+
heads[i](i * 2)
185+
assert(splited[i]() == i * 2 + 1)
186+
end
187+
end)
188+
end

0 commit comments

Comments
 (0)