-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.lua
206 lines (165 loc) · 4.78 KB
/
test.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
local DataFactory = require("core.DataFactory")
local function clone(object)
local lookup_table = {}
local function _copy(object)
if type(object) ~= "table" then
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local newObject = {}
lookup_table[object] = newObject
for key, value in pairs(object) do
newObject[_copy(key)] = _copy(value)
end
return setmetatable(newObject, getmetatable(object))
end
return _copy(object)
end
local function getTime()
-- return os.time()
return os.clock()
end
local function test_livedata()
-- local N = 100 * 10000 -- 差不多上限了 0.22s左右
local N = 10000 -- 带print差不多上限了 0.125s左右
local dList = {}
local obList = {}
local t = getTime()
for i = 1, N do
local d = DataFactory:newLiveData({ value = i }, i)
dList[#dList + 1] = d
local ob = {
---@param data LiveData
onLiveUpdate = function(sender, data)
print(data._ldname, data.value)
end
}
obList[#obList + 1] = ob
DataFactory:bindObserver(ob, d)
-- print(i)
end
local t1 = getTime()
print("---dt1", t1 - t)
for i, v in ipairs(dList) do
if i % 2 == 0 then
v.value = i / 2
end
end
local t2 = getTime()
print("---dt2", t2 - t1)
DataFactory:doCheckNotifyList()
local t3 = getTime()
print("---dt3", t3 - t2)
DataFactory:doCheckNotifyList()
local t4 = getTime()
print("---dt4", t4 - t3)
end
local function test_livedata_supper()
local N = 10 * 10000 -- 差不多上限了 0.22s左右
-- local N = 1000 -- 带print差不多上限了 0.15s左右
local dsList = {}
local dList = {}
local obsList = {}
local obList = {}
for i = 1, N / 10 do
local d = DataFactory:newLiveData({ value = i }, i)
dsList[#dsList + 1] = d
dList[#dList + 1] = d
local ob = {
---@param data LiveData
onLiveUpdate = function(sender, data)
-- print(data._ldname .. "sss", data.value)
end
}
obsList[#obsList + 1] = ob
DataFactory:bindObserver(ob, d)
end
local t = getTime()
local floor = math.floor
local ceil = math.ceil
for i = 1, N do
local is = ceil(i / 10)
local d = DataFactory:newLiveData({ value = i }, i)
dList[is].vd = d
dList[#dList + 1] = d
local ob = {
---@param data LiveData
onLiveUpdate = function(sender, data)
-- print(data._ldname, data.value)
end
}
obList[#obList + 1] = ob
DataFactory:bindObserver(ob, d)
-- print(i)
end
local t1 = getTime()
print("---dt1", t1 - t)
for i, v in ipairs(dList) do
if i % 2 == 0 then
v.value = i / 2
end
end
local t2 = getTime()
print("---dt2", t2 - t1)
DataFactory:doCheckNotifyList()
local t3 = getTime()
print("---dt3", t3 - t2)
DataFactory:doCheckNotifyList()
local t4 = getTime()
print("---dt4", t4 - t3)
end
---测试多重父类关联
local function test_livedata_multi_supper()
local da = DataFactory:newLiveData({}, "a")
local oba = {
---@param data LiveData
onLiveUpdate = function(sender, data)
print(data._ldname, data.value)
end
}
DataFactory:bindObserver(oba, da)
local db = DataFactory:newLiveData({}, "b")
local obb = {
---@param data LiveData
onLiveUpdate = function(sender, data)
print(data._ldname, data.value)
end
}
DataFactory:bindObserver(obb, db)
local dc = DataFactory:newLiveData({}, "c")
local obc = {
---@param data LiveData
onLiveUpdate = function(sender, data)
print(data._ldname, data.value)
end
}
DataFactory:bindObserver(obc, dc)
da.child = db
db.child = dc
print("=====check 1")
DataFactory:doCheckNotifyList()
dc.value = 1
print("=====check 2")
DataFactory:doCheckNotifyList()
-- dc.child = da -- assert LiveData has loop refrence
print("=====check 3")
local dd = DataFactory:newLiveData(clone(dc), "dd") -- 深拷贝出来的数据, 如果要继续监听值变化, 在套一层LiveData数据壳
dd.value = "#d"
DataFactory:bindObserver(obc, dd)
DataFactory:doCheckNotifyList()
end
---测试print输出耗时
local function test_print()
local t = getTime()
print("----")
print("dt", getTime() - t)
-- body
end
local function main()
-- test_print()
-- test_livedata()
-- test_livedata_supper()
test_livedata_multi_supper()
end
main()