Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
687 changes: 687 additions & 0 deletions _doc/notebooks/profiling.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion _doc/sphinxdoc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pyquickhelper: various automations in python
:alt: Build Status Linux

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

.. image:: https://circleci.com/gh/sdpython/pyquickhelper/tree/master.svg?style=svg
Expand Down
120 changes: 116 additions & 4 deletions _unittests/ut_cli/test_cli_profile.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
"""
@brief test tree node (time=7s)
"""


import os
import unittest
from io import StringIO

import time
from pyquickhelper.loghelper import fLOG, BufferedPrint
from pyquickhelper.pycode import ExtTestCase
from pyquickhelper.__main__ import main
from pyquickhelper.pycode.profiling import profile


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

def test_profile_stat_help(self):
st = BufferedPrint()
main(args=['profile_stat', '--help'], fLOG=st.fprint)
res = str(st)
self.assertIn("usage: profile_stat", res)

def test_profile_stat(self):

def f0(t):
time.sleep(t)

def f1(t):
time.sleep(t)

def f2():
f1(0.1)
f1(0.01)

def f3():
f0(0.2)
f1(0.5)

def f4():
f2()
f3()

ps = profile(f4)[0] # pylint: disable=W0632
ps.dump_stats("temp_stat.prof")

with self.subTest(calls=False, output=None):
st = BufferedPrint()
main(args=['profile_stat', '-f', "temp_stat.prof",
'--calls', '0'], fLOG=st.fprint)
self.assertIn('percall', str(st))

with self.subTest(calls=False, output="txt"):
st = BufferedPrint()
main(args=['profile_stat', '-f', "temp_stat.prof",
'--calls', '0', '-o', 'temp_output.txt'], fLOG=st.fprint)
with open("temp_output.txt", "r", encoding='utf-8') as f:
content = f.read()
self.assertIn('percall', str(st))
self.assertIn('percall', content)

with self.subTest(calls=False, output='csv'):
st = BufferedPrint()
main(args=['profile_stat', '-f', "temp_stat.prof",
'--calls', '0', '-o', 'temp_output.csv'], fLOG=st.fprint)
with open("temp_output.csv", "r", encoding='utf-8') as f:
content = f.read()
self.assertIn('percall', str(st))
self.assertIn('ncalls1,', content)

with self.subTest(calls=False, output='xlsx'):
st = BufferedPrint()
main(args=['profile_stat', '-f', "temp_stat.prof",
'--calls', '0', '-o', 'temp_output.xlsx'], fLOG=st.fprint)
self.assertExists('temp_output.xlsx')
self.assertIn('percall', str(st))

def test_profile_stat_gr(self):

def f0(t):
time.sleep(t)

def f1(t):
time.sleep(t)

def f2():
f1(0.1)
f1(0.01)

def f3():
f0(0.2)
f1(0.5)

def f4():
f2()
f3()

ps = profile(f4)[0] # pylint: disable=W0632
ps.dump_stats("temp_gr_stat.prof")

with self.subTest(calls=False, output=None):
st = BufferedPrint()
main(args=['profile_stat', '-f', "temp_gr_stat.prof",
'--calls', '1'], fLOG=st.fprint)
self.assertIn('+++', str(st))

with self.subTest(calls=False, output="txt"):
st = BufferedPrint()
main(args=['profile_stat', '-f', "temp_gr_stat.prof",
'--calls', '1', '-o', 'temp_gr_output.txt'], fLOG=st.fprint)
with open("temp_gr_output.txt", "r", encoding='utf-8') as f:
content = f.read()
self.assertIn('+++', str(st))
self.assertIn('+++', content)

with self.subTest(calls=False, output='csv'):
st = BufferedPrint()
main(args=['profile_stat', '-f', "temp_gr_stat.prof",
'--calls', '1', '-o', 'temp_gr_output.csv'], fLOG=st.fprint)
with open("temp_gr_output.csv", "r", encoding='utf-8') as f:
content = f.read()
self.assertIn('+++', str(st))
self.assertIn(',+', content)

with self.subTest(calls=False, output='xlsx'):
st = BufferedPrint()
main(args=['profile_stat', '-f', "temp_gr_stat.prof",
'--calls', '1', '-o', 'temp_gr_output.xlsx'], fLOG=st.fprint)
self.assertIn('+++', str(st))
self.assertExists('temp_gr_output.xlsx')


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion _unittests/ut_helpgen/test_stat_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_enumerate_notebooks_link(self):
counts["title"] += 1
nbfound.add(rl[1])
self.assertTrue(counts.get("ref", 0) > 0)
self.assertIn(counts.get(None, 0), (0, 11))
self.assertIn(counts.get(None, 0), (0, 12))
self.assertTrue(counts["title"] > 0)
self.assertTrue(len(nbfound) > 8)
# self.assertTrue(counts.get("refn", 0) > 0)
Expand Down
2 changes: 1 addition & 1 deletion _unittests/ut_ipythonhelper/test_notebook_runner_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_notebook_runner_report(self):
if len(cov) <= 9:
raise Exception("too few found notebooks")

if cov.shape[0] != 14:
if cov.shape[0] != 15:
raise AssertionError("NB={0}\n----\n{1}".format(cov.shape, cov))
self.assertIn("last_name", cov.columns)
cols = ['notebooks', 'last_name', 'date', 'etime',
Expand Down
39 changes: 22 additions & 17 deletions _unittests/ut_pycode/test_pip_helper.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
"""
@brief test tree node (time=2s)
"""

import sys
import os
import unittest
import pandas

from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode.pip_helper import get_packages_list, package2dict
from pyquickhelper.pycode import ExtTestCase
from pyquickhelper.pycode.pip_helper import PQPipError
from pyquickhelper.pycode.pip_helper import (
get_packages_list, package2dict, get_package_info,
PQPipError)


class TestPipHelper(ExtTestCase):

def test_exc(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")

exc = PQPipError('cmd', 'out', 'err')
msg = str(exc)
self.assertEqual([msg.replace('\n', '')], [
'CMD:cmdOUT:out[piperror]err'])

def test_pip_list(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")

li = get_packages_list()
dt = package2dict(li[0])
avoid = {'py_version'}
Expand All @@ -43,6 +29,25 @@ def test_pip_list(self):
self.assertEmpty(empty)
self.assertNotEmpty(li)

def test_pip_show(self):
info = get_package_info("pandas")
if "version" not in str(info):
raise AssertionError(str(info))

info = get_package_info("sphinx")
if "version" not in str(info):
raise Exception(str(info))

def test_pip_show_all(self):
info = get_package_info(start=0, end=2)
df = pandas.DataFrame(info)
self.assertNotEmpty(info)

if __name__ == "__main__":
info = get_package_info()
df = pandas.DataFrame(info)
df.to_excel("out_packages.xlsx")


if __name__ == "__main__":
unittest.main()
50 changes: 0 additions & 50 deletions _unittests/ut_pycode/test_pip_helper2.py

This file was deleted.

Loading