test_frame_5.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import lz4.frame
  2. import pytest
  3. import gc
  4. MEM_INCREASE_LIMIT = (1024 * 25)
  5. test_data = [
  6. (b'a' * 1024 * 1024),
  7. ]
  8. @pytest.fixture(
  9. params=test_data,
  10. ids=[
  11. 'data' + str(i) for i in range(len(test_data))
  12. ]
  13. )
  14. def data(request):
  15. return request.param
  16. def test_frame_decompress_mem_usage(data):
  17. tracemalloc = pytest.importorskip('tracemalloc')
  18. tracemalloc.start()
  19. compressed = lz4.frame.compress(data)
  20. prev_snapshot = None
  21. for i in range(1000):
  22. decompressed = lz4.frame.decompress(compressed) # noqa: F841
  23. if i % 100 == 0:
  24. gc.collect()
  25. snapshot = tracemalloc.take_snapshot()
  26. if prev_snapshot:
  27. stats = snapshot.compare_to(prev_snapshot, 'lineno')
  28. assert stats[0].size_diff < MEM_INCREASE_LIMIT
  29. prev_snapshot = snapshot
  30. def test_frame_decompress_chunk_mem_usage(data):
  31. tracemalloc = pytest.importorskip('tracemalloc')
  32. tracemalloc.start()
  33. compressed = lz4.frame.compress(data)
  34. prev_snapshot = None
  35. for i in range(1000):
  36. context = lz4.frame.create_decompression_context()
  37. decompressed = lz4.frame.decompress_chunk( # noqa: F841
  38. context, compressed
  39. )
  40. if i % 100 == 0:
  41. gc.collect()
  42. snapshot = tracemalloc.take_snapshot()
  43. if prev_snapshot:
  44. stats = snapshot.compare_to(prev_snapshot, 'lineno')
  45. assert stats[0].size_diff < MEM_INCREASE_LIMIT
  46. prev_snapshot = snapshot
  47. def test_frame_open_decompress_mem_usage(data):
  48. tracemalloc = pytest.importorskip('tracemalloc')
  49. tracemalloc.start()
  50. with lz4.frame.open('test.lz4', 'w') as f:
  51. f.write(data)
  52. prev_snapshot = None
  53. for i in range(1000):
  54. with lz4.frame.open('test.lz4', 'r') as f:
  55. decompressed = f.read() # noqa: F841
  56. if i % 100 == 0:
  57. gc.collect()
  58. snapshot = tracemalloc.take_snapshot()
  59. if prev_snapshot:
  60. stats = snapshot.compare_to(prev_snapshot, 'lineno')
  61. assert stats[0].size_diff < MEM_INCREASE_LIMIT
  62. prev_snapshot = snapshot
  63. # TODO: add many more memory usage tests along the lines of this one
  64. # for other funcs
  65. def test_dummy_always_pass():
  66. # If pytest finds all tests are skipped, then it exits with code 5 rather
  67. # than 0, which tox sees as an error. Here we add a dummy test that always passes.
  68. assert True