Can I profile Lua scripts in Redis?

I have a cluster application that uses a Redis distributed server with dynamically generated Lua scripts sent to redis instances. Lua component scripts can be quite complex and have significant runtimes, and I would like to be able to profile them to find hot spots.

SLOWLOG is useful for telling me that my scripts are slow and how slow they are, but that is not my problem. I know how slow they are, I would like to find out which parts of them are slow.

Docs redis EVAL it is clear that redis does not export any timing functions to lua, which makes it look like it could be a lost cause.

So, Redis short custom fork , is there any way to tell which parts of my Lua script are slower than others?

EDIT I accepted Doug's suggestion and used debug.sethook - here is the manual procedure that I inserted at the top of my script:

redis.call('del', 'line_sample_count') local function profile() local line = debug.getinfo(2)['currentline'] redis.call('zincrby', 'line_sample_count', 1, line) end debug.sethook(profile, '', 100) 

Then, to see the hottest 10 lines of my script:

 ZREVRANGE line_sample_count 0 9 WITHSCORES 
+7
source share
2 answers

If your scripts are processed related (not related to I / O), then you can use the debug.sethook function with the help of count hook:

count hook : called after the interpreter executes each counter. (This event only occurs when Lua is running Lua.)

You will need to create a profiler based on the counts that you will receive in your callback.

PepperfishProfiler is a good place to start. It uses os.clock , which you don’t have, but you can just use the hook count for a very rough approximation.

This is also covered in PiL 23.3 - Profiles.

+2
source

In standard Lua C you cannot. This is not a built-in function - it returns only seconds. Thus, there are two options: you either write your own Lua DLL extension to return the time in msec, or:

You can perform a basic test using millisecond resolution times. You can access the current millisecond of time using LuaSocket. Although this adds dependency to your project, it is an effective way to do trivial benchmarking.

 require "socket" t = socket.gettime(); 
0
source

All Articles