2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-27 00:02:19 +08:00

Fix memory usage lua script

This commit is contained in:
Ken Hibino 2022-03-26 14:33:03 -07:00
parent 0cfa7f47ba
commit bc27126670

View File

@ -292,30 +292,25 @@ for i=3,6 do
memusg = memusg + m memusg = memusg + m
end end
end end
local group_names = redis.call("SRANDMEMBER", KEYS[7], tonumber(ARGV[3])) local groups = redis.call("SMEMBERS", KEYS[7])
if (table.getn(group_names) > 0) then if table.getn(groups) > 0 then
local group_sample_total = 0 local agg_task_count = 0
for _, gname in ipairs(group_names) do local agg_task_sample_total = 0
local agg_task_sample_size = 0
for i, gname in ipairs(groups) do
local group_key = ARGV[4] .. gname local group_key = ARGV[4] .. gname
agg_task_count = agg_task_count + redis.call("ZCARD", group_key)
if i <= tonumber(ARGV[3]) then
local ids = redis.call("ZRANGE", group_key, 0, sample_size - 1) local ids = redis.call("ZRANGE", group_key, 0, sample_size - 1)
local sample_total = 0
if (table.getn(ids) > 0) then
for _, id in ipairs(ids) do for _, id in ipairs(ids) do
local bytes = redis.call("MEMORY", "USAGE", ARGV[1] .. id) local bytes = redis.call("MEMORY", "USAGE", ARGV[1] .. id)
sample_total = sample_total + bytes agg_task_sample_total = agg_task_sample_total + bytes
end agg_task_sample_size = agg_task_sample_size + 1
local n = redis.call("ZCARD", group_key)
local avg = sample_total / table.getn(ids)
group_sample_total = group_sample_total + (avg * n)
end
local m = redis.call("MEMORY", "USAGE", group_key)
if (m) then
group_sample_total = group_sample_total + m
end end
end end
local group_size = redis.call("SCARD", KEYS[7]) end
local group_memusg_avg = group_sample_total / table.getn(group_names) local avg = agg_task_sample_total / agg_task_sample_size
memusg = memusg + (group_memusg_avg * group_size) memusg = memusg + (avg * agg_task_count)
end end
return memusg return memusg
`) `)