72_lz4.py 698 B

12345678910111213141516171819202122232425262728293031
  1. try:
  2. import lz4
  3. except ImportError:
  4. print('lz4 is not enabled, skipping test...')
  5. exit()
  6. def test(data: bytes):
  7. compressed = lz4.compress(data)
  8. decompressed = lz4.decompress(compressed)
  9. assert data == decompressed
  10. return len(compressed) / len(data)
  11. test(b'')
  12. test(b'hello world')
  13. import random
  14. def gen_data():
  15. values = []
  16. for i in range(random.randint(0, 10000)):
  17. values.append(i % 256)
  18. return bytes(values)
  19. for i in range(100):
  20. ratio = test(gen_data())
  21. # print(f'compression ratio: {ratio:.2f}')
  22. # test 64MB random data (require 1GB list[int] buffer)
  23. rnd = [random.randint(0, 255) for _ in range(1024*1024*1024//16)]
  24. test(bytes(rnd))