-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding PythonCProfileEvaluation Builtin (#867)
Here is a proposal for a Builtin symbol that produces a Low-level profile of the evaluation.
- Loading branch information
Showing
2 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
Low-level Profiling | ||
Low-level (Python) profile from inside the Mathics interpreter | ||
""" | ||
|
||
import cProfile | ||
import pstats | ||
import sys | ||
from io import StringIO | ||
|
||
from mathics.builtin import Builtin | ||
from mathics.core.attributes import A_HOLD_ALL_COMPLETE, A_PROTECTED | ||
from mathics.core.convert.python import from_python | ||
from mathics.core.evaluation import Evaluation | ||
from mathics.core.expression import Expression | ||
from mathics.core.list import ListExpression | ||
from mathics.core.symbols import SymbolNull | ||
|
||
|
||
class PythonCProfileEvaluation(Builtin): | ||
""" | ||
<url>:Python:https://docs.python.org/3/library/profile.html</url> | ||
<dl> | ||
<dt>'PythonProfileEvaluation[$expr$]' | ||
<dd>profile $expr$ with the Python's cProfiler. | ||
</dl> | ||
>> PythonCProfileEvaluation[a + b + 1] | ||
= ... | ||
""" | ||
|
||
attributes = A_HOLD_ALL_COMPLETE | A_PROTECTED | ||
summary_text = "profile the internal evaluation of an expression" | ||
|
||
def eval(self, expr: Expression, evaluation: Evaluation): | ||
"PythonCProfileEvaluation[expr_]" | ||
profile_result = SymbolNull | ||
textstream = StringIO() | ||
if sys.version_info >= (3, 8): | ||
with cProfile.Profile() as pr: | ||
result = expr.evaluate(evaluation) | ||
stats = pstats.Stats(pr, stream=textstream) | ||
stats.strip_dirs().sort_stats(-1).print_stats() | ||
# TODO: convert the string (or the statistics) | ||
# into something like a WL Table, by splitting the | ||
# rows and the columns. By now, just a string | ||
# is returned. | ||
profile_result = from_python(textstream.getvalue()) | ||
else: | ||
result = expr.evaluate(evaluation) | ||
return ListExpression(result, profile_result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters