Coder Social home page Coder Social logo

pycalltrak's Introduction

pycalltrak

Poor Man's Call Trace Analyzer&Visualizer

It is a bit different than normal callgraph outputs. PyCalltrak will output every function call with different arguments. This means if a function is called multiple times with different arguments, then all of those calls will be visible in the callgraph as distinct nodes.

This project is a toy project and used specifically while developing/trying out/playing with different algorithms on various subjects. I used calltrak summary and visualizer(it still outputs ASCII, a GUI backend can be easily written) to find repetitive calls happening throughout the execution flow. It might help on various situations like these where you would have a complex algorithm(possibly recursive) that is called multiple times. pycalltrak might shine in those kind of situations since you can visualize/get summary on what has happened in runtime.

An example is worth thousand words.

Following is a Travelling Salesman Problem Solver using Top-Down Dynamic Programming algorithm:

import sys
from functools import lru_cache

A = [[11, 10, 15, 20, 3, 7, 16, 21], [10, 1, 35, 25, 3, 7, 16, 21],
     [15, 35, 1, 30, 3, 7, 16, 21], [20, 25, 30, 1, 3, 7, 16, 21],
     [20, 25, 30, 3, 7, 7, 16, 21], [20, 25, 30, 45, 3, 1, 16, 21],
     [20, 25, 30, 5, 3, 7, 3, 21], [20, 25, 30, 66, 3, 7, 16, 1]]
S = frozenset([x for x in range(len(A))]) # use frozenset to make Set hashable for lru_cache

#@lru_cache(maxsize=None)
def tsp(x, S):
    if not len(S):
        return A[x][0]
    r = sys.maxsize
    for y in S:
        r = min(r, A[x][y] + tsp(y, S - {y}))
    return r

import calltrak
calltrak.start()
r = tsp(0, S - {0})
calltrak.stop()
stats = calltrak.get_stats()
print(stats.summary())
#print(stats.to_json())

It will output following:

13700 call(s) in total. 13650(99%) recurring call(s).

If you enable lru_cache decorator which enables memoization on the function and re-run the example:

449 call(s) in total. 0(0%) recurring call(s).

If you would like to visualize the call graph at this point, pycalltrak already have outlined the x,y coords needed to print out the stats. If you want to do it in your own way, then you need to traverse the callgraph and decide the topology of the graph yourself. A simple example using a single coordinate to print out a Left-Aligned call graph:

for stat in stats:
    print((f'{"." * (stat.y-1)} {stat}'))

The output will be as following: (used different input than the example above)

16 call(s) in total. 6(37%) recurring call(s).
 tsp(x=0, S=frozenset({1, 2, 3})) -> 80 [0.00s]
. tsp(x=1, S=frozenset({2, 3})) -> 70 [0.00s]
. tsp(x=2, S=frozenset({1, 3})) -> 65 [0.00s]
. tsp(x=3, S=frozenset({1, 2})) -> 75 [0.00s]
.. tsp(x=2, S=frozenset({3})) -> 50 [0.00s]
.. tsp(x=3, S=frozenset({2})) -> 45 [0.00s]
.. tsp(x=1, S=frozenset({3})) -> 45 [0.00s]
.. tsp(x=3, S=frozenset({1})) -> 35 [0.00s]
.. tsp(x=1, S=frozenset({2})) -> 50 [0.00s]
.. tsp(x=2, S=frozenset({1})) -> 45 [0.00s]
... tsp(x=3, S=frozenset()) -> 20 [0.00s]
... tsp(x=2, S=frozenset()) -> 15 [0.00s]
... tsp(x=3, S=frozenset()) -> 20 [0.00s]
... tsp(x=1, S=frozenset()) -> 10 [0.00s]
... tsp(x=2, S=frozenset()) -> 15 [0.00s]
... tsp(x=1, S=frozenset()) -> 10 [0.00s]

pycalltrak's People

Contributors

sumerc avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.