BenchmarkPack.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import os
  2. import io
  3. from collections import OrderedDict
  4. from Plugin import PluginManager
  5. from Config import config
  6. from util import Msgpack
  7. @PluginManager.registerTo("Actions")
  8. class ActionsPlugin:
  9. def createZipFile(self, path):
  10. import zipfile
  11. test_data = b"Test" * 1024
  12. file_name = b"\xc3\x81rv\xc3\xadzt\xc5\xb1r\xc5\x91%s.txt".decode("utf8")
  13. with zipfile.ZipFile(path, 'w') as archive:
  14. for y in range(100):
  15. zip_info = zipfile.ZipInfo(file_name % y, (1980, 1, 1, 0, 0, 0))
  16. zip_info.compress_type = zipfile.ZIP_DEFLATED
  17. zip_info.create_system = 3
  18. zip_info.flag_bits = 0
  19. zip_info.external_attr = 25165824
  20. archive.writestr(zip_info, test_data)
  21. def testPackZip(self, num_run=1):
  22. """
  23. Test zip file creating
  24. """
  25. yield "x 100 x 5KB "
  26. from Crypt import CryptHash
  27. zip_path = '%s/test.zip' % config.data_dir
  28. for i in range(num_run):
  29. self.createZipFile(zip_path)
  30. yield "."
  31. archive_size = os.path.getsize(zip_path) / 1024
  32. yield "(Generated file size: %.2fkB)" % archive_size
  33. hash = CryptHash.sha512sum(open(zip_path, "rb"))
  34. valid = "cb32fb43783a1c06a2170a6bc5bb228a032b67ff7a1fd7a5efb9b467b400f553"
  35. assert hash == valid, "Invalid hash: %s != %s<br>" % (hash, valid)
  36. os.unlink(zip_path)
  37. def testUnpackZip(self, num_run=1):
  38. """
  39. Test zip file reading
  40. """
  41. yield "x 100 x 5KB "
  42. import zipfile
  43. zip_path = '%s/test.zip' % config.data_dir
  44. test_data = b"Test" * 1024
  45. file_name = b"\xc3\x81rv\xc3\xadzt\xc5\xb1r\xc5\x91".decode("utf8")
  46. self.createZipFile(zip_path)
  47. for i in range(num_run):
  48. with zipfile.ZipFile(zip_path) as archive:
  49. for f in archive.filelist:
  50. assert f.filename.startswith(file_name), "Invalid filename: %s != %s" % (f.filename, file_name)
  51. data = archive.open(f.filename).read()
  52. assert archive.open(f.filename).read() == test_data, "Invalid data: %s..." % data[0:30]
  53. yield "."
  54. os.unlink(zip_path)
  55. def createArchiveFile(self, path, archive_type="gz"):
  56. import tarfile
  57. import gzip
  58. # Monkey patch _init_write_gz to use fixed date in order to keep the hash independent from datetime
  59. def nodate_write_gzip_header(self):
  60. self._write_mtime = 0
  61. original_write_gzip_header(self)
  62. test_data_io = io.BytesIO(b"Test" * 1024)
  63. file_name = b"\xc3\x81rv\xc3\xadzt\xc5\xb1r\xc5\x91%s.txt".decode("utf8")
  64. original_write_gzip_header = gzip.GzipFile._write_gzip_header
  65. gzip.GzipFile._write_gzip_header = nodate_write_gzip_header
  66. with tarfile.open(path, 'w:%s' % archive_type) as archive:
  67. for y in range(100):
  68. test_data_io.seek(0)
  69. tar_info = tarfile.TarInfo(file_name % y)
  70. tar_info.size = 4 * 1024
  71. archive.addfile(tar_info, test_data_io)
  72. def testPackArchive(self, num_run=1, archive_type="gz"):
  73. """
  74. Test creating tar archive files
  75. """
  76. yield "x 100 x 5KB "
  77. from Crypt import CryptHash
  78. hash_valid_db = {
  79. "gz": "92caec5121a31709cbbc8c11b0939758e670b055bbbe84f9beb3e781dfde710f",
  80. "bz2": "b613f41e6ee947c8b9b589d3e8fa66f3e28f63be23f4faf015e2f01b5c0b032d",
  81. "xz": "ae43892581d770959c8d993daffab25fd74490b7cf9fafc7aaee746f69895bcb",
  82. }
  83. archive_path = '%s/test.tar.%s' % (config.data_dir, archive_type)
  84. for i in range(num_run):
  85. self.createArchiveFile(archive_path, archive_type=archive_type)
  86. yield "."
  87. archive_size = os.path.getsize(archive_path) / 1024
  88. yield "(Generated file size: %.2fkB)" % archive_size
  89. hash = CryptHash.sha512sum(open("%s/test.tar.%s" % (config.data_dir, archive_type), "rb"))
  90. valid = hash_valid_db[archive_type]
  91. assert hash == valid, "Invalid hash: %s != %s<br>" % (hash, valid)
  92. if os.path.isfile(archive_path):
  93. os.unlink(archive_path)
  94. def testUnpackArchive(self, num_run=1, archive_type="gz"):
  95. """
  96. Test reading tar archive files
  97. """
  98. yield "x 100 x 5KB "
  99. import tarfile
  100. test_data = b"Test" * 1024
  101. file_name = b"\xc3\x81rv\xc3\xadzt\xc5\xb1r\xc5\x91%s.txt".decode("utf8")
  102. archive_path = '%s/test.tar.%s' % (config.data_dir, archive_type)
  103. self.createArchiveFile(archive_path, archive_type=archive_type)
  104. for i in range(num_run):
  105. with tarfile.open(archive_path, 'r:%s' % archive_type) as archive:
  106. for y in range(100):
  107. assert archive.extractfile(file_name % y).read() == test_data
  108. yield "."
  109. if os.path.isfile(archive_path):
  110. os.unlink(archive_path)
  111. def testPackMsgpack(self, num_run=1):
  112. """
  113. Test msgpack encoding
  114. """
  115. yield "x 100 x 5KB "
  116. binary = b'fqv\xf0\x1a"e\x10,\xbe\x9cT\x9e(\xa5]u\x072C\x8c\x15\xa2\xa8\x93Sw)\x19\x02\xdd\t\xfb\xf67\x88\xd9\xee\x86\xa1\xe4\xb6,\xc6\x14\xbb\xd7$z\x1d\xb2\xda\x85\xf5\xa0\x97^\x01*\xaf\xd3\xb0!\xb7\x9d\xea\x89\xbbh8\xa1"\xa7]e(@\xa2\xa5g\xb7[\xae\x8eE\xc2\x9fL\xb6s\x19\x19\r\xc8\x04S\xd0N\xe4]?/\x01\xea\xf6\xec\xd1\xb3\xc2\x91\x86\xd7\xf4K\xdf\xc2lV\xf4\xe8\x80\xfc\x8ep\xbb\x82\xb3\x86\x98F\x1c\xecS\xc8\x15\xcf\xdc\xf1\xed\xfc\xd8\x18r\xf9\x80\x0f\xfa\x8cO\x97(\x0b]\xf1\xdd\r\xe7\xbf\xed\x06\xbd\x1b?\xc5\xa0\xd7a\x82\xf3\xa8\xe6@\xf3\ri\xa1\xb10\xf6\xd4W\xbc\x86\x1a\xbb\xfd\x94!bS\xdb\xaeM\x92\x00#\x0b\xf7\xad\xe9\xc2\x8e\x86\xbfi![%\xd31]\xc6\xfc2\xc9\xda\xc6v\x82P\xcc\xa9\xea\xb9\xff\xf6\xc8\x17iD\xcf\xf3\xeeI\x04\xe9\xa1\x19\xbb\x01\x92\xf5nn4K\xf8\xbb\xc6\x17e>\xa7 \xbbv'
  117. data = OrderedDict(
  118. sorted({"int": 1024 * 1024 * 1024, "float": 12345.67890, "text": "hello" * 1024, "binary": binary}.items())
  119. )
  120. data_packed_valid = b'\x84\xa6binary\xc5\x01\x00fqv\xf0\x1a"e\x10,\xbe\x9cT\x9e(\xa5]u\x072C\x8c\x15\xa2\xa8\x93Sw)\x19\x02\xdd\t\xfb\xf67\x88\xd9\xee\x86\xa1\xe4\xb6,\xc6\x14\xbb\xd7$z\x1d\xb2\xda\x85\xf5\xa0\x97^\x01*\xaf\xd3\xb0!\xb7\x9d\xea\x89\xbbh8\xa1"\xa7]e(@\xa2\xa5g\xb7[\xae\x8eE\xc2\x9fL\xb6s\x19\x19\r\xc8\x04S\xd0N\xe4]?/\x01\xea\xf6\xec\xd1\xb3\xc2\x91\x86\xd7\xf4K\xdf\xc2lV\xf4\xe8\x80\xfc\x8ep\xbb\x82\xb3\x86\x98F\x1c\xecS\xc8\x15\xcf\xdc\xf1\xed\xfc\xd8\x18r\xf9\x80\x0f\xfa\x8cO\x97(\x0b]\xf1\xdd\r\xe7\xbf\xed\x06\xbd\x1b?\xc5\xa0\xd7a\x82\xf3\xa8\xe6@\xf3\ri\xa1\xb10\xf6\xd4W\xbc\x86\x1a\xbb\xfd\x94!bS\xdb\xaeM\x92\x00#\x0b\xf7\xad\xe9\xc2\x8e\x86\xbfi![%\xd31]\xc6\xfc2\xc9\xda\xc6v\x82P\xcc\xa9\xea\xb9\xff\xf6\xc8\x17iD\xcf\xf3\xeeI\x04\xe9\xa1\x19\xbb\x01\x92\xf5nn4K\xf8\xbb\xc6\x17e>\xa7 \xbbv\xa5float\xcb@\xc8\x1c\xd6\xe61\xf8\xa1\xa3int\xce@\x00\x00\x00\xa4text\xda\x14\x00'
  121. data_packed_valid += b'hello' * 1024
  122. for y in range(num_run):
  123. for i in range(100):
  124. data_packed = Msgpack.pack(data)
  125. yield "."
  126. assert data_packed == data_packed_valid, "%s<br>!=<br>%s" % (repr(data_packed), repr(data_packed_valid))
  127. def testUnpackMsgpack(self, num_run=1):
  128. """
  129. Test msgpack decoding
  130. """
  131. yield "x 5KB "
  132. binary = b'fqv\xf0\x1a"e\x10,\xbe\x9cT\x9e(\xa5]u\x072C\x8c\x15\xa2\xa8\x93Sw)\x19\x02\xdd\t\xfb\xf67\x88\xd9\xee\x86\xa1\xe4\xb6,\xc6\x14\xbb\xd7$z\x1d\xb2\xda\x85\xf5\xa0\x97^\x01*\xaf\xd3\xb0!\xb7\x9d\xea\x89\xbbh8\xa1"\xa7]e(@\xa2\xa5g\xb7[\xae\x8eE\xc2\x9fL\xb6s\x19\x19\r\xc8\x04S\xd0N\xe4]?/\x01\xea\xf6\xec\xd1\xb3\xc2\x91\x86\xd7\xf4K\xdf\xc2lV\xf4\xe8\x80\xfc\x8ep\xbb\x82\xb3\x86\x98F\x1c\xecS\xc8\x15\xcf\xdc\xf1\xed\xfc\xd8\x18r\xf9\x80\x0f\xfa\x8cO\x97(\x0b]\xf1\xdd\r\xe7\xbf\xed\x06\xbd\x1b?\xc5\xa0\xd7a\x82\xf3\xa8\xe6@\xf3\ri\xa1\xb10\xf6\xd4W\xbc\x86\x1a\xbb\xfd\x94!bS\xdb\xaeM\x92\x00#\x0b\xf7\xad\xe9\xc2\x8e\x86\xbfi![%\xd31]\xc6\xfc2\xc9\xda\xc6v\x82P\xcc\xa9\xea\xb9\xff\xf6\xc8\x17iD\xcf\xf3\xeeI\x04\xe9\xa1\x19\xbb\x01\x92\xf5nn4K\xf8\xbb\xc6\x17e>\xa7 \xbbv'
  133. data = OrderedDict(
  134. sorted({"int": 1024 * 1024 * 1024, "float": 12345.67890, "text": "hello" * 1024, "binary": binary}.items())
  135. )
  136. data_packed = b'\x84\xa6binary\xc5\x01\x00fqv\xf0\x1a"e\x10,\xbe\x9cT\x9e(\xa5]u\x072C\x8c\x15\xa2\xa8\x93Sw)\x19\x02\xdd\t\xfb\xf67\x88\xd9\xee\x86\xa1\xe4\xb6,\xc6\x14\xbb\xd7$z\x1d\xb2\xda\x85\xf5\xa0\x97^\x01*\xaf\xd3\xb0!\xb7\x9d\xea\x89\xbbh8\xa1"\xa7]e(@\xa2\xa5g\xb7[\xae\x8eE\xc2\x9fL\xb6s\x19\x19\r\xc8\x04S\xd0N\xe4]?/\x01\xea\xf6\xec\xd1\xb3\xc2\x91\x86\xd7\xf4K\xdf\xc2lV\xf4\xe8\x80\xfc\x8ep\xbb\x82\xb3\x86\x98F\x1c\xecS\xc8\x15\xcf\xdc\xf1\xed\xfc\xd8\x18r\xf9\x80\x0f\xfa\x8cO\x97(\x0b]\xf1\xdd\r\xe7\xbf\xed\x06\xbd\x1b?\xc5\xa0\xd7a\x82\xf3\xa8\xe6@\xf3\ri\xa1\xb10\xf6\xd4W\xbc\x86\x1a\xbb\xfd\x94!bS\xdb\xaeM\x92\x00#\x0b\xf7\xad\xe9\xc2\x8e\x86\xbfi![%\xd31]\xc6\xfc2\xc9\xda\xc6v\x82P\xcc\xa9\xea\xb9\xff\xf6\xc8\x17iD\xcf\xf3\xeeI\x04\xe9\xa1\x19\xbb\x01\x92\xf5nn4K\xf8\xbb\xc6\x17e>\xa7 \xbbv\xa5float\xcb@\xc8\x1c\xd6\xe61\xf8\xa1\xa3int\xce@\x00\x00\x00\xa4text\xda\x14\x00'
  137. data_packed += b'hello' * 1024
  138. for y in range(num_run):
  139. data_unpacked = Msgpack.unpack(data_packed, decode=False)
  140. yield "."
  141. assert data_unpacked == data, "%s<br>!=<br>%s" % (data_unpacked, data)
  142. def testUnpackMsgpackStreaming(self, num_run=1, fallback=False):
  143. """
  144. Test streaming msgpack decoding
  145. """
  146. yield "x 1000 x 5KB "
  147. binary = b'fqv\xf0\x1a"e\x10,\xbe\x9cT\x9e(\xa5]u\x072C\x8c\x15\xa2\xa8\x93Sw)\x19\x02\xdd\t\xfb\xf67\x88\xd9\xee\x86\xa1\xe4\xb6,\xc6\x14\xbb\xd7$z\x1d\xb2\xda\x85\xf5\xa0\x97^\x01*\xaf\xd3\xb0!\xb7\x9d\xea\x89\xbbh8\xa1"\xa7]e(@\xa2\xa5g\xb7[\xae\x8eE\xc2\x9fL\xb6s\x19\x19\r\xc8\x04S\xd0N\xe4]?/\x01\xea\xf6\xec\xd1\xb3\xc2\x91\x86\xd7\xf4K\xdf\xc2lV\xf4\xe8\x80\xfc\x8ep\xbb\x82\xb3\x86\x98F\x1c\xecS\xc8\x15\xcf\xdc\xf1\xed\xfc\xd8\x18r\xf9\x80\x0f\xfa\x8cO\x97(\x0b]\xf1\xdd\r\xe7\xbf\xed\x06\xbd\x1b?\xc5\xa0\xd7a\x82\xf3\xa8\xe6@\xf3\ri\xa1\xb10\xf6\xd4W\xbc\x86\x1a\xbb\xfd\x94!bS\xdb\xaeM\x92\x00#\x0b\xf7\xad\xe9\xc2\x8e\x86\xbfi![%\xd31]\xc6\xfc2\xc9\xda\xc6v\x82P\xcc\xa9\xea\xb9\xff\xf6\xc8\x17iD\xcf\xf3\xeeI\x04\xe9\xa1\x19\xbb\x01\x92\xf5nn4K\xf8\xbb\xc6\x17e>\xa7 \xbbv'
  148. data = OrderedDict(
  149. sorted({"int": 1024 * 1024 * 1024, "float": 12345.67890, "text": "hello" * 1024, "binary": binary}.items())
  150. )
  151. data_packed = b'\x84\xa6binary\xc5\x01\x00fqv\xf0\x1a"e\x10,\xbe\x9cT\x9e(\xa5]u\x072C\x8c\x15\xa2\xa8\x93Sw)\x19\x02\xdd\t\xfb\xf67\x88\xd9\xee\x86\xa1\xe4\xb6,\xc6\x14\xbb\xd7$z\x1d\xb2\xda\x85\xf5\xa0\x97^\x01*\xaf\xd3\xb0!\xb7\x9d\xea\x89\xbbh8\xa1"\xa7]e(@\xa2\xa5g\xb7[\xae\x8eE\xc2\x9fL\xb6s\x19\x19\r\xc8\x04S\xd0N\xe4]?/\x01\xea\xf6\xec\xd1\xb3\xc2\x91\x86\xd7\xf4K\xdf\xc2lV\xf4\xe8\x80\xfc\x8ep\xbb\x82\xb3\x86\x98F\x1c\xecS\xc8\x15\xcf\xdc\xf1\xed\xfc\xd8\x18r\xf9\x80\x0f\xfa\x8cO\x97(\x0b]\xf1\xdd\r\xe7\xbf\xed\x06\xbd\x1b?\xc5\xa0\xd7a\x82\xf3\xa8\xe6@\xf3\ri\xa1\xb10\xf6\xd4W\xbc\x86\x1a\xbb\xfd\x94!bS\xdb\xaeM\x92\x00#\x0b\xf7\xad\xe9\xc2\x8e\x86\xbfi![%\xd31]\xc6\xfc2\xc9\xda\xc6v\x82P\xcc\xa9\xea\xb9\xff\xf6\xc8\x17iD\xcf\xf3\xeeI\x04\xe9\xa1\x19\xbb\x01\x92\xf5nn4K\xf8\xbb\xc6\x17e>\xa7 \xbbv\xa5float\xcb@\xc8\x1c\xd6\xe61\xf8\xa1\xa3int\xce@\x00\x00\x00\xa4text\xda\x14\x00'
  152. data_packed += b'hello' * 1024
  153. for i in range(num_run):
  154. unpacker = Msgpack.getUnpacker(decode=False, fallback=fallback)
  155. for y in range(1000):
  156. unpacker.feed(data_packed)
  157. for data_unpacked in unpacker:
  158. pass
  159. yield "."
  160. assert data == data_unpacked, "%s != %s" % (data_unpacked, data)