Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I profile Lua scripts running in Redis?

I have a cluster app that uses a distributed Redis back-end, with dynamically generated Lua scripts dispatched to the redis instances. The Lua component scripts can get fairly complex and have a significant runtime, and I'd like to be able to profile them to find the hot spots.

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

The redis EVAL docs are clear that redis does not export any timekeeping functions to lua, which makes it seem like this might be a lost cause.

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

EDIT I took Doug's suggestion and used debug.sethook - here's the hook routine 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
like image 669
Drew Shafer Avatar asked May 04 '13 03:05

Drew Shafer


1 Answers

If your scripts are processing bound (not I/O bound), then you may be able to use the debug.sethook function with a count hook:

The count hook: is called after the interpreter executes every count instructions. (This event only happens while Lua is executing a Lua function.)

You'll have to build a profiler based on the counts you receive in your callback.

The PepperfishProfiler would be a good place to start. It uses os.clock which you don't have, but you could just use hook counts for a very crude approximation.

This is also covered in PiL 23.3 – Profiles

like image 160
Doug Currie Avatar answered Oct 06 '22 07:10

Doug Currie