modelx.get_stacktrace#
- get_stacktrace(summarize=False)[source]#
Get stack trace.
If
summarize
is set toFalse
(default), returns the call stack trace. The stack trace is a list of tuples each of which represents one of two types of operations, push(“ENTER”) or pop(“EXIT”) to/from the call stack. The table blow shows data sored in the tuple elements.Index
Content
0
“ENTER” or “EXIT”
1
Stack position
2
Time (Seconds elapsed from the epoch)
3
String to represent Cells object
4
A tuple of arguments to the Cells object
If
summarize
is set toTrue
, returns a summary of the trace. The summary is adict
, whose keys are the representation strings of the called Cells, and whose values are dicts containing the following statistics of the Cells.Key
Value
“calls”
Number of calls to the Cells
“duration”
Total time in seconds elapsed in the Cells
“first_entry_at”
Time (from the epoch) of the first entry to the Cells
“last_exit_at”
Time (from the epoch) of the last exit from the Cells
The call stack trace must be activated by
start_stacktrace()
before usingget_stacktrace()
, otherwise a runtime error is raised. When settingsummarize
toTrue
, make sure thatstart_stacktrace()
was called with its parametermaxlen
being set toNone
, otherwiseget_stacktrace()
may raise an Error because of incomplete trace records.- Returns
A list of tuples each of which is a record of stack history, or a dict containing the summary information.
Example
The sample code below creates and executes a sample model, and stores a trace summary of the execution as Pandas DataFrame:
import time import pandas as pd import modelx as mx m = mx.new_model() m.time = time @mx.defcells def foo(x): time.sleep(0.1) # Waits 0.1 second return foo(x-1) + 1 if x > 0 else bar() @mx.defcells def bar(): time.sleep(0.2) # Waits 0.2 second return 0 mx.start_stacktrace(maxlen=None) foo(5) df = pd.DataFrame.from_dict( mx.get_stacktrace(summarize=True), orient="index") mx.stop_stacktrace()
The DataFrame shows how many times each formula was called, how much time each formula took, time at which the execution enters into each formula for the first time, and time at which the execution leaves each formula for the last.
Cells
calls
duration
first_entry_at
last_exit_at
Model1.Space1.foo(x)
6
0.6097867488861084
1605873067.2099519
1605873068.0203028
Model1.Space1.bar()
1
0.20056414604187012
1605873067.8197386
1605873068.0203028
Changed in version 0.11.0: summarize parameter is added.
New in version 0.0.25.