Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Add common pytests #517

Merged
merged 6 commits into from
Jul 1, 2023
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
53 changes: 53 additions & 0 deletions python/pyfury/tests/benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import timeit
import pickle
import pytest
import pyfury as fury
from pyfury.tests.record import create_foo, foo_schema

iter_nums = 100000


@pytest.mark.skip(reason="take too long")
def test_encode():
# print("schema", foo_schema())
encoder = fury.create_row_encoder(foo_schema())
foo = create_foo()
row = encoder.to_row(foo)
assert foo == encoder.from_row(row)

t1 = timeit.timeit(lambda: encoder.to_row(foo), number=iter_nums)
print(
"encoder take {0} for {1} times, avg: {2}".format(t1, iter_nums, t1 / iter_nums)
)
t2 = timeit.timeit(lambda: pickle.dumps(foo), number=iter_nums)
print(
"pickle take {0} for {1} times, avg: {2}".format(t2, iter_nums, t2 / iter_nums)
)


@pytest.mark.skip(reason="take too long")
def test_decode():
# print(foo_schema()
encoder = fury.create_row_encoder(foo_schema())
foo = create_foo()

row = encoder.to_row(foo)
assert foo == encoder.from_row(row)
t1 = timeit.timeit(lambda: encoder.from_row(row), number=iter_nums)
print(
"encoder take {0} for {1} times, avg: {2}, size {3}".format(
t1, iter_nums, t1 / iter_nums, row.size_bytes()
)
)
pickled_data = pickle.dumps(foo)
t2 = timeit.timeit(lambda: pickle.loads(pickled_data), number=iter_nums)
print(
"pickle take {0} for {1} times, avg: {2}, size {3}".format(
t2, iter_nums, t2 / iter_nums, len(pickled_data)
)
)


if __name__ == "__main__":
test_encode()
test_decode()
31 changes: 31 additions & 0 deletions python/pyfury/tests/profiling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
add `# cython: profile=True` to cython file to compile first
"""

import cProfile
import pstats

import pyfury as fury
import pytest
import pyximport
from pyfury.tests.record import create_foo, foo_schema


@pytest.mark.skip(reason="take too long")
def test_encode():
"""add enable cython profile directive to lib.pxi for profiling"""
encoder = fury.create_row_encoder(foo_schema())
foo = create_foo()
iter_nums = 100000
for _ in range(iter_nums):
encoder.to_row(foo)


if __name__ == "__main__":
pyximport.install()
cProfile.runctx("test_encode()", globals(), locals(), "Profile.prof")

s = pstats.Stats("Profile.prof")
s.strip_dirs().sort_stats("time").print_stats()
s.print_callers()
test_encode()
33 changes: 33 additions & 0 deletions python/pyfury/tests/test_codegen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os
import textwrap
import uuid

from pyfury import codegen


def test_debug_compiled():
code = textwrap.dedent(
"""
def _debug_compiled(x):
print(x)
print(x)
return x
"""
)[1:]
unique_filename = f"_debug_compiled_{uuid.uuid4()}.py"
with open(unique_filename, "w") as f:
f.write(code)
compiled = compile(code, unique_filename, "exec")
context = {}
exec(compiled, context, context)
_debug_compiled = context["_debug_compiled"]
assert _debug_compiled(2) == 2
os.remove(unique_filename)


def test_compile_function():
code, func = codegen.compile_function(
"test_compile_function", ["x"], ["print(1)", "print(2)", "return x"], {}
)
print(code)
assert func(100) == 100
Loading