How to determine the runtime of a Lua script in Redis?

I have a small Lua script to run in Redis, and I am interested in getting the runtime.

Due to the nature of Redis and its Lua implementation, I cannot use the TIME function at the script's start / return points and include this information in the opposite for processing (see http://redis.io/commands/eval - Scripts as pure functions). This leads to the error: (error) ERR Error running script (call to f_a49ed2fea72f1f529843d6024d1515e76e69bcbd): Write commands not allowed after non deterministic commands

I was looking for a function for a function / call that I could do that would return the execution time of the last run of the script, but nothing was found yet.

I am using PHP and the Predis Library. Although I can check the runtime on the part of PHP, I want to remove the transfer overhead and find out how long the Lua script will block access to the database. I have successfully returned the time if I do not need to change any data stored in Redis, and that was about 1/10 time when PHP reports.

How to determine the runtime of a Lua script in Redis, and not through PHP?

+2
source share
4 answers

You can activate the Redis slow log feature by changing the parameter slowlog-log-slower-than to 0. It will record the execution time of ALL commands (including Lua scripts and regardless of execution time).

A slow log is queued for memory, which you must regularly upload to collect data. Depending on the amount of traffic you may need to increase slowlog-max-len to catch up on the runtime that interests you.

You can use the slowlog get command to flush the slow log. You can filter the results that you do not need. AFAIK, there is no way to filter during data collection (to save only Lua statistics).

+4
source

You can use os.clock () inside the script if you enable the os module when creating Redis.

Change #if 0 to #if 1 on line 484 in https://github.com/antirez/redis/blob/unstable/src/scripting.c :

 void luaLoadLibraries(lua_State *lua) { luaLoadLib(lua, "", luaopen_base); luaLoadLib(lua, LUA_TABLIBNAME, luaopen_table); luaLoadLib(lua, LUA_STRLIBNAME, luaopen_string); luaLoadLib(lua, LUA_MATHLIBNAME, luaopen_math); luaLoadLib(lua, LUA_DBLIBNAME, luaopen_debug); luaLoadLib(lua, "cjson", luaopen_cjson); luaLoadLib(lua, "struct", luaopen_struct); luaLoadLib(lua, "cmsgpack", luaopen_cmsgpack); #if 0 /* Stuff that we don't load currently, for sandboxing concerns. */ luaLoadLib(lua, LUA_LOADLIBNAME, luaopen_package); luaLoadLib(lua, LUA_OSLIBNAME, luaopen_os); #endif } 
+2
source

EDIT : rld is deprecated.

You can use rld ( https://github.com/RedisLabs/redis-lua-debugger ) and print it in the log - the log has time information (although rld will lead to a script to work slower, naturally).

0
source

Like Redis v3.2, you can replicate commands instead of scripts with a call to the new redis.replicate_commands() API. This will allow you, among other things, to call TIME in scripts that perform write operations without causing a non-determinism error.

0
source

All Articles