Hello!
On Mon, Jun 16, 2014 at 9:51 AM, Robert Paprocki wrote:
> I am wondering if it is possible to estimate/calculate the usage of an
> ngx.shared.DICT shm; specifically, if it is possible to calculate how many
> key/value pairs could fit in the shm based on it's size. For example, I am
> storing key/value pairs in which the key is a 20 character string, and the
> value is a 15 character string. Is it possible to estimate how many pairs I
> could fit into a shm of, say, 10M? 100M? Is there a way to quickly know how
> different types of values will consume memory in the shm (bool, int,
> string)? Thank you!
>
This actually depends on the underlying architecture. For example, the
entry size is different on x86_64 and i386. The simplest way to find
out the exact number of elements that can be stored in your shared
dict in your system is to write a small Lua script that keeps filling
the store with safe_set() until out of memory. For example,
lua_shared_dict dict 10m;
location = /t {
content_by_lua '
local rep = string.rep
local val = rep("v", 15)
local n = 0
local dict = ngx.shared.dict
while true do
local key = tostring(n)
local len = #key
if len < 20 then
key = key .. rep("k", 20 - len)
end
assert(#key == 20)
local ok, err = dict:safe_set(key, val)
if not ok then
break
end
n = n + 1
end
ngx.say(n, " key/value pairs inserted.")
';
}
The reason to use safe_set() here is to prevent automatic evicting
least recently used items upon memory shortage in set().
Querying /t gives the response body on my Linux x86_64 system:
81375 key/value pairs inserted.
So a 10m store can hold 81375 key/value pairs with 20-byte keys and
15-byte values.
Changing the "dict" store to 100m gives
814367 key/value pairs inserted.
So a 100m store can hold 814367 pairs. It's a linear growth as expected.
Best regards,
-agentzh