Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Commit 005835a

Browse files
authored
Implements new rendering for a profiling (#352) (show call stack, json output)
* Implements new rendring for a profiling
1 parent 9de029d commit 005835a

File tree

15 files changed

+1624
-93
lines changed

15 files changed

+1624
-93
lines changed

_doc/notebooks/profiling.ipynb

Lines changed: 687 additions & 0 deletions
Large diffs are not rendered by default.

_doc/sphinxdoc/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pyquickhelper: various automations in python
77
:alt: Build Status Linux
88

99
.. image:: https://ci.appveyor.com/api/projects/status/t2g9olcgqgdvqq3l?svg=true
10-
:target: https://app.travis-ci.com/github/sdpython/pyquickhelper/
10+
:target: https://ci.appveyor.com/project/sdpython/pyquickhelper
1111
:alt: Build Status Windows
1212

1313
.. image:: https://circleci.com/gh/sdpython/pyquickhelper/tree/master.svg?style=svg

_unittests/ut_cli/test_cli_profile.py

Lines changed: 116 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
"""
22
@brief test tree node (time=7s)
33
"""
4-
5-
64
import os
75
import unittest
8-
from io import StringIO
9-
6+
import time
107
from pyquickhelper.loghelper import fLOG, BufferedPrint
118
from pyquickhelper.pycode import ExtTestCase
129
from pyquickhelper.__main__ import main
10+
from pyquickhelper.pycode.profiling import profile
1311

1412

1513
def to_profile(args):
@@ -25,6 +23,120 @@ def test_profile(self):
2523
prof = self.profile(lambda: to_profile(["clean_files", "--help"]))[1]
2624
self.assertNotIn("bokeh", prof.lower())
2725

26+
def test_profile_stat_help(self):
27+
st = BufferedPrint()
28+
main(args=['profile_stat', '--help'], fLOG=st.fprint)
29+
res = str(st)
30+
self.assertIn("usage: profile_stat", res)
31+
32+
def test_profile_stat(self):
33+
34+
def f0(t):
35+
time.sleep(t)
36+
37+
def f1(t):
38+
time.sleep(t)
39+
40+
def f2():
41+
f1(0.1)
42+
f1(0.01)
43+
44+
def f3():
45+
f0(0.2)
46+
f1(0.5)
47+
48+
def f4():
49+
f2()
50+
f3()
51+
52+
ps = profile(f4)[0] # pylint: disable=W0632
53+
ps.dump_stats("temp_stat.prof")
54+
55+
with self.subTest(calls=False, output=None):
56+
st = BufferedPrint()
57+
main(args=['profile_stat', '-f', "temp_stat.prof",
58+
'--calls', '0'], fLOG=st.fprint)
59+
self.assertIn('percall', str(st))
60+
61+
with self.subTest(calls=False, output="txt"):
62+
st = BufferedPrint()
63+
main(args=['profile_stat', '-f', "temp_stat.prof",
64+
'--calls', '0', '-o', 'temp_output.txt'], fLOG=st.fprint)
65+
with open("temp_output.txt", "r", encoding='utf-8') as f:
66+
content = f.read()
67+
self.assertIn('percall', str(st))
68+
self.assertIn('percall', content)
69+
70+
with self.subTest(calls=False, output='csv'):
71+
st = BufferedPrint()
72+
main(args=['profile_stat', '-f', "temp_stat.prof",
73+
'--calls', '0', '-o', 'temp_output.csv'], fLOG=st.fprint)
74+
with open("temp_output.csv", "r", encoding='utf-8') as f:
75+
content = f.read()
76+
self.assertIn('percall', str(st))
77+
self.assertIn('ncalls1,', content)
78+
79+
with self.subTest(calls=False, output='xlsx'):
80+
st = BufferedPrint()
81+
main(args=['profile_stat', '-f', "temp_stat.prof",
82+
'--calls', '0', '-o', 'temp_output.xlsx'], fLOG=st.fprint)
83+
self.assertExists('temp_output.xlsx')
84+
self.assertIn('percall', str(st))
85+
86+
def test_profile_stat_gr(self):
87+
88+
def f0(t):
89+
time.sleep(t)
90+
91+
def f1(t):
92+
time.sleep(t)
93+
94+
def f2():
95+
f1(0.1)
96+
f1(0.01)
97+
98+
def f3():
99+
f0(0.2)
100+
f1(0.5)
101+
102+
def f4():
103+
f2()
104+
f3()
105+
106+
ps = profile(f4)[0] # pylint: disable=W0632
107+
ps.dump_stats("temp_gr_stat.prof")
108+
109+
with self.subTest(calls=False, output=None):
110+
st = BufferedPrint()
111+
main(args=['profile_stat', '-f', "temp_gr_stat.prof",
112+
'--calls', '1'], fLOG=st.fprint)
113+
self.assertIn('+++', str(st))
114+
115+
with self.subTest(calls=False, output="txt"):
116+
st = BufferedPrint()
117+
main(args=['profile_stat', '-f', "temp_gr_stat.prof",
118+
'--calls', '1', '-o', 'temp_gr_output.txt'], fLOG=st.fprint)
119+
with open("temp_gr_output.txt", "r", encoding='utf-8') as f:
120+
content = f.read()
121+
self.assertIn('+++', str(st))
122+
self.assertIn('+++', content)
123+
124+
with self.subTest(calls=False, output='csv'):
125+
st = BufferedPrint()
126+
main(args=['profile_stat', '-f', "temp_gr_stat.prof",
127+
'--calls', '1', '-o', 'temp_gr_output.csv'], fLOG=st.fprint)
128+
with open("temp_gr_output.csv", "r", encoding='utf-8') as f:
129+
content = f.read()
130+
self.assertIn('+++', str(st))
131+
self.assertIn(',+', content)
132+
133+
with self.subTest(calls=False, output='xlsx'):
134+
st = BufferedPrint()
135+
main(args=['profile_stat', '-f', "temp_gr_stat.prof",
136+
'--calls', '1', '-o', 'temp_gr_output.xlsx'], fLOG=st.fprint)
137+
self.assertIn('+++', str(st))
138+
self.assertExists('temp_gr_output.xlsx')
139+
28140

29141
if __name__ == "__main__":
30142
unittest.main()

_unittests/ut_helpgen/test_stat_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_enumerate_notebooks_link(self):
3030
counts["title"] += 1
3131
nbfound.add(rl[1])
3232
self.assertTrue(counts.get("ref", 0) > 0)
33-
self.assertIn(counts.get(None, 0), (0, 11))
33+
self.assertIn(counts.get(None, 0), (0, 12))
3434
self.assertTrue(counts["title"] > 0)
3535
self.assertTrue(len(nbfound) > 8)
3636
# self.assertTrue(counts.get("refn", 0) > 0)

_unittests/ut_ipythonhelper/test_notebook_runner_report.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_notebook_runner_report(self):
1818
if len(cov) <= 9:
1919
raise Exception("too few found notebooks")
2020

21-
if cov.shape[0] != 14:
21+
if cov.shape[0] != 15:
2222
raise AssertionError("NB={0}\n----\n{1}".format(cov.shape, cov))
2323
self.assertIn("last_name", cov.columns)
2424
cols = ['notebooks', 'last_name', 'date', 'etime',
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,23 @@
11
"""
22
@brief test tree node (time=2s)
33
"""
4-
5-
import sys
6-
import os
74
import unittest
85
import pandas
9-
10-
from pyquickhelper.loghelper import fLOG
11-
from pyquickhelper.pycode.pip_helper import get_packages_list, package2dict
126
from pyquickhelper.pycode import ExtTestCase
13-
from pyquickhelper.pycode.pip_helper import PQPipError
7+
from pyquickhelper.pycode.pip_helper import (
8+
get_packages_list, package2dict, get_package_info,
9+
PQPipError)
1410

1511

1612
class TestPipHelper(ExtTestCase):
1713

1814
def test_exc(self):
19-
fLOG(
20-
__file__,
21-
self._testMethodName,
22-
OutputPrint=__name__ == "__main__")
23-
2415
exc = PQPipError('cmd', 'out', 'err')
2516
msg = str(exc)
2617
self.assertEqual([msg.replace('\n', '')], [
2718
'CMD:cmdOUT:out[piperror]err'])
2819

2920
def test_pip_list(self):
30-
fLOG(
31-
__file__,
32-
self._testMethodName,
33-
OutputPrint=__name__ == "__main__")
34-
3521
li = get_packages_list()
3622
dt = package2dict(li[0])
3723
avoid = {'py_version'}
@@ -43,6 +29,25 @@ def test_pip_list(self):
4329
self.assertEmpty(empty)
4430
self.assertNotEmpty(li)
4531

32+
def test_pip_show(self):
33+
info = get_package_info("pandas")
34+
if "version" not in str(info):
35+
raise AssertionError(str(info))
36+
37+
info = get_package_info("sphinx")
38+
if "version" not in str(info):
39+
raise Exception(str(info))
40+
41+
def test_pip_show_all(self):
42+
info = get_package_info(start=0, end=2)
43+
df = pandas.DataFrame(info)
44+
self.assertNotEmpty(info)
45+
46+
if __name__ == "__main__":
47+
info = get_package_info()
48+
df = pandas.DataFrame(info)
49+
df.to_excel("out_packages.xlsx")
50+
4651

4752
if __name__ == "__main__":
4853
unittest.main()

_unittests/ut_pycode/test_pip_helper2.py

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)