You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
5.3KB

  1. from __future__ import print_function
  2. from .lib import TestBase, FileCreator
  3. from smmap.mman import (
  4. SlidingWindowMapManager,
  5. StaticWindowMapManager
  6. )
  7. from smmap.buf import SlidingWindowMapBuffer
  8. from random import randint
  9. from time import time
  10. import sys
  11. import os
  12. man_optimal = SlidingWindowMapManager()
  13. man_worst_case = SlidingWindowMapManager(
  14. window_size=TestBase.k_window_test_size // 100,
  15. max_memory_size=TestBase.k_window_test_size // 3,
  16. max_open_handles=15)
  17. static_man = StaticWindowMapManager()
  18. class TestBuf(TestBase):
  19. def test_basics(self):
  20. with FileCreator(self.k_window_test_size, "buffer_test") as fc:
  21. # invalid paths fail upon construction
  22. c = man_optimal.make_cursor(fc.path)
  23. self.assertRaises(ValueError, SlidingWindowMapBuffer, type(c)()) # invalid cursor
  24. self.assertRaises(ValueError, SlidingWindowMapBuffer, c, fc.size) # offset too large
  25. buf = SlidingWindowMapBuffer() # can create uninitailized buffers
  26. assert buf.cursor() is None
  27. # can call end access any time
  28. buf.end_access()
  29. buf.end_access()
  30. assert len(buf) == 0
  31. # begin access can revive it, if the offset is suitable
  32. offset = 100
  33. assert buf.begin_access(c, fc.size) == False
  34. assert buf.begin_access(c, offset) == True
  35. assert len(buf) == fc.size - offset
  36. assert buf.cursor().is_valid()
  37. # empty begin access keeps it valid on the same path, but alters the offset
  38. assert buf.begin_access() == True
  39. assert len(buf) == fc.size
  40. assert buf.cursor().is_valid()
  41. # simple access
  42. with open(fc.path, 'rb') as fp:
  43. data = fp.read()
  44. assert data[offset] == buf[0]
  45. assert data[offset:offset * 2] == buf[0:offset]
  46. # negative indices, partial slices
  47. assert buf[-1] == buf[len(buf) - 1]
  48. assert buf[-10:] == buf[len(buf) - 10:len(buf)]
  49. # end access makes its cursor invalid
  50. buf.end_access()
  51. assert not buf.cursor().is_valid()
  52. assert buf.cursor().is_associated() # but it remains associated
  53. # an empty begin access fixes it up again
  54. assert buf.begin_access() == True and buf.cursor().is_valid()
  55. del(buf) # ends access automatically
  56. del(c)
  57. assert man_optimal.num_file_handles() == 1
  58. # PERFORMANCE
  59. # blast away with random access and a full mapping - we don't want to
  60. # exaggerate the manager's overhead, but measure the buffer overhead
  61. # We do it once with an optimal setting, and with a worse manager which
  62. # will produce small mappings only !
  63. max_num_accesses = 100
  64. fd = os.open(fc.path, os.O_RDONLY)
  65. for item in (fc.path, fd):
  66. for manager, man_id in ((man_optimal, 'optimal'),
  67. (man_worst_case, 'worst case'),
  68. (static_man, 'static optimal')):
  69. buf = SlidingWindowMapBuffer(manager.make_cursor(item))
  70. assert manager.num_file_handles() == 1
  71. for access_mode in range(2): # single, multi
  72. num_accesses_left = max_num_accesses
  73. num_bytes = 0
  74. fsize = fc.size
  75. st = time()
  76. buf.begin_access()
  77. while num_accesses_left:
  78. num_accesses_left -= 1
  79. if access_mode: # multi
  80. ofs_start = randint(0, fsize)
  81. ofs_end = randint(ofs_start, fsize)
  82. d = buf[ofs_start:ofs_end]
  83. assert len(d) == ofs_end - ofs_start
  84. assert d == data[ofs_start:ofs_end]
  85. num_bytes += len(d)
  86. del d
  87. else:
  88. pos = randint(0, fsize)
  89. assert buf[pos] == data[pos]
  90. num_bytes += 1
  91. # END handle mode
  92. # END handle num accesses
  93. buf.end_access()
  94. assert manager.num_file_handles()
  95. assert manager.collect()
  96. assert manager.num_file_handles() == 0
  97. elapsed = max(time() - st, 0.001) # prevent zero division errors on windows
  98. mb = float(1000 * 1000)
  99. mode_str = (access_mode and "slice") or "single byte"
  100. print("%s: Made %i random %s accesses to buffer created from %s reading a total of %f mb in %f s (%f mb/s)"
  101. % (man_id, max_num_accesses, mode_str, type(item), num_bytes / mb, elapsed, (num_bytes / mb) / elapsed),
  102. file=sys.stderr)
  103. # END handle access mode
  104. del buf
  105. # END for each manager
  106. # END for each input
  107. os.close(fd)