How to write a custom tracer/profiler?

A

AK@SYD

Hi, all,

Just wanna look for some suggestions on how to appoarch this
problem...

I have some code ("main logic") which has a dependency on a library.
Let's call it DS. ("data source").

The main logic will issue a number of calls to the DS in order to get
the data it needs and then carry out some calculations.

I want to have a non-intrusive way to find out all the DS data
requested by the main logic.

One thing to note, the DS is a big, messy bit of code. So putting
logger everywhere within DS and main logic is not a reliably,
satisfactory solution.

I have thought about using somehting like aspect. But seems to me
there is no mature, widely-used aspect lib out there.

Another idea is: let's roll a custom tracer/profiler. Whenever any
method/attributes in the DS package are called, the return values will
be logged.

I have taken a quick look at profile.py in python 2.4, It seems sys
module will pass a frames to the profile class. Can I access the
return value of a function via these frame objects?

Cheers, Anthony
 
G

Gabriel Genellina

The main logic will issue a number of calls to the DS in order to get
the data it needs and then carry out some calculations.

I want to have a non-intrusive way to find out all the DS data
requested by the main logic.

One thing to note, the DS is a big, messy bit of code. So putting
logger everywhere within DS and main logic is not a reliably,
satisfactory solution.

I have thought about using somehting like aspect. But seems to me
there is no mature, widely-used aspect lib out there.

Another idea is: let's roll a custom tracer/profiler. Whenever any
method/attributes in the DS package are called, the return values will
be logged.

I have taken a quick look at profile.py in python 2.4, It seems sys
module will pass a frames to the profile class. Can I access the
return value of a function via these frame objects?

You don't have to use a profiler; just replace the original DS functions
that you're interested in monitoring, with a "wrapped" version that logs
its parameters and then calls the original code. A skeleton example
(suppose DS.open is one function you want to monitor):

import DS
original_open = DS.open

def wrapped_open(*args, **kw):
trace("open", *args, **kw)
return original_open(*args, **kw)

DS.open = wrapped_open

This approach has some problems (it does not preserve the original
function signature, by example). You may overcome this (and other
problems) using the decorator module from M. Simionato; see
<http://www.phyast.pitt.edu/~micheles/python/documentation.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,991
Messages
2,570,212
Members
46,800
Latest member
Tobi1987

Latest Threads

Top