How to write a custom tracer/profiler?

Discussion in 'Python' started by AK@SYD, Jul 15, 2008.

  1. AK@SYD

    AK@SYD Guest

    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
     
    AK@SYD, Jul 15, 2008
    #1
    1. Advertising

  2. En Mon, 14 Jul 2008 23:48:20 -0300, AK@SYD <>
    escribi�:

    > 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>

    --
    Gabriel Genellina
     
    Gabriel Genellina, Jul 15, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Debashish
    Replies:
    3
    Views:
    1,819
    Mike Treseler
    Aug 8, 2003
  2. Samy
    Replies:
    0
    Views:
    507
  3. Replies:
    0
    Views:
    327
  4. Jon Harrop

    Optimise my ray tracer

    Jon Harrop, Jun 2, 2005, in forum: Java
    Replies:
    85
    Views:
    2,392
    Roedy Green
    Jun 26, 2005
  5. potatosoftware
    Replies:
    0
    Views:
    663
    potatosoftware
    Nov 4, 2009
Loading...

Share This Page