from pstats import Stats, SortKey
from io import StringIO

sortby = SortKey.CUMULATIVE

s = StringIO()

stats = Stats("my_benchmark.prof", stream=s).sort_stats(sortby)

# "my_benchmark.prof" can be a profiler instance instead.

stats.print_stats()

print(s.getvalue())
