1
+ from __future__ import with_statement , print_function
2
+
1
3
from .lib import TestBase , FileCreator
2
4
3
5
from smmap .mman import *
12
14
from copy import copy
13
15
14
16
class TestMMan (TestBase ):
15
-
17
+
16
18
def test_cursor (self ):
17
19
fc = FileCreator (self .k_window_test_size , "cursor_test" )
18
-
20
+
19
21
man = SlidingWindowMapManager ()
20
22
ci = WindowCursor (man ) # invalid cursor
21
23
assert not ci .is_valid ()
22
24
assert not ci .is_associated ()
23
25
assert ci .size () == 0 # this is cached, so we can query it in invalid state
24
-
26
+
25
27
cv = man .make_cursor (fc .path )
26
28
assert not cv .is_valid () # no region mapped yet
27
29
assert cv .is_associated ()# but it know where to map it from
28
30
assert cv .file_size () == fc .size
29
31
assert cv .path () == fc .path
30
-
32
+
31
33
# copy module
32
34
cio = copy (cv )
33
35
assert not cio .is_valid () and cio .is_associated ()
34
-
36
+
35
37
# assign method
36
38
assert not ci .is_associated ()
37
39
ci .assign (cv )
38
40
assert not ci .is_valid () and ci .is_associated ()
39
-
41
+
40
42
# unuse non-existing region is fine
41
43
cv .unuse_region ()
42
44
cv .unuse_region ()
43
-
45
+
44
46
# destruction is fine (even multiple times)
45
47
cv ._destroy ()
46
48
WindowCursor (man )._destroy ()
47
-
49
+
48
50
def test_memory_manager (self ):
49
51
slide_man = SlidingWindowMapManager ()
50
52
static_man = StaticWindowMapManager ()
51
-
53
+
52
54
for man in (static_man , slide_man ):
53
55
assert man .num_file_handles () == 0
54
56
assert man .num_open_files () == 0
@@ -59,15 +61,15 @@ def test_memory_manager(self):
59
61
assert man .window_size () > winsize_cmp_val
60
62
assert man .mapped_memory_size () == 0
61
63
assert man .max_mapped_memory_size () > 0
62
-
64
+
63
65
# collection doesn't raise in 'any' mode
64
66
man ._collect_lru_region (0 )
65
67
# doesn't raise if we are within the limit
66
68
man ._collect_lru_region (10 )
67
-
68
- # doesn't fail if we overallocate
69
+
70
+ # doesn't fail if we overallocate
69
71
assert man ._collect_lru_region (sys .maxsize ) == 0
70
-
72
+
71
73
# use a region, verify most basic functionality
72
74
fc = FileCreator (self .k_window_test_size , "manager_test" )
73
75
fd = os .open (fc .path , os .O_RDONLY )
@@ -77,8 +79,9 @@ def test_memory_manager(self):
77
79
assert c .use_region (10 , 10 ).is_valid ()
78
80
assert c .ofs_begin () == 10
79
81
assert c .size () == 10
80
- assert c .buffer ()[:] == open (fc .path , 'rb' ).read (20 )[10 :]
81
-
82
+ with open (fc .path , 'rb' ) as fp :
83
+ assert c .buffer ()[:] == fp .read (20 )[10 :]
84
+
82
85
if isinstance (item , int ):
83
86
self .assertRaises (ValueError , c .path )
84
87
else :
@@ -87,38 +90,39 @@ def test_memory_manager(self):
87
90
#END for each input
88
91
os .close (fd )
89
92
# END for each manager type
90
-
93
+
91
94
def test_memman_operation (self ):
92
95
# test more access, force it to actually unmap regions
93
96
fc = FileCreator (self .k_window_test_size , "manager_operation_test" )
94
- data = open (fc .path , 'rb' ).read ()
97
+ with open (fc .path , 'rb' ) as fp :
98
+ data = fp .read ()
95
99
fd = os .open (fc .path , os .O_RDONLY )
96
100
max_num_handles = 15
97
- #small_size =
101
+ #small_size =
98
102
for mtype , args in ( (StaticWindowMapManager , (0 , fc .size // 3 , max_num_handles )),
99
103
(SlidingWindowMapManager , (fc .size // 100 , fc .size // 3 , max_num_handles )),):
100
104
for item in (fc .path , fd ):
101
105
assert len (data ) == fc .size
102
-
106
+
103
107
# small windows, a reasonable max memory. Not too many regions at once
104
108
man = mtype (window_size = args [0 ], max_memory_size = args [1 ], max_open_handles = args [2 ])
105
109
c = man .make_cursor (item )
106
-
110
+
107
111
# still empty (more about that is tested in test_memory_manager()
108
112
assert man .num_open_files () == 0
109
113
assert man .mapped_memory_size () == 0
110
-
114
+
111
115
base_offset = 5000
112
116
# window size is 0 for static managers, hence size will be 0. We take that into consideration
113
117
size = man .window_size () // 2
114
118
assert c .use_region (base_offset , size ).is_valid ()
115
119
rr = c .region_ref ()
116
120
assert rr ().client_count () == 2 # the manager and the cursor and us
117
-
121
+
118
122
assert man .num_open_files () == 1
119
123
assert man .num_file_handles () == 1
120
124
assert man .mapped_memory_size () == rr ().size ()
121
-
125
+
122
126
#assert c.size() == size # the cursor may overallocate in its static version
123
127
assert c .ofs_begin () == base_offset
124
128
assert rr ().ofs_begin () == 0 # it was aligned and expanded
@@ -127,9 +131,9 @@ def test_memman_operation(self):
127
131
else :
128
132
assert rr ().size () == fc .size
129
133
#END ignore static managers which dont use windows and are aligned to file boundaries
130
-
131
- assert c .buffer ()[:] == data [base_offset :base_offset + (size or c .size ())]
132
-
134
+
135
+ assert c .buffer ()[:] == data [base_offset :base_offset + (size or c .size ())]
136
+
133
137
# obtain second window, which spans the first part of the file - it is a still the same window
134
138
nsize = (size or fc .size ) - 10
135
139
assert c .use_region (0 , nsize ).is_valid ()
@@ -138,7 +142,7 @@ def test_memman_operation(self):
138
142
assert c .size () == nsize
139
143
assert c .ofs_begin () == 0
140
144
assert c .buffer ()[:] == data [:nsize ]
141
-
145
+
142
146
# map some part at the end, our requested size cannot be kept
143
147
overshoot = 4000
144
148
base_offset = fc .size - (size or c .size ()) + overshoot
@@ -156,23 +160,23 @@ def test_memman_operation(self):
156
160
assert rr ().ofs_begin () < c .ofs_begin () # it should have extended itself to the left
157
161
assert rr ().ofs_end () <= fc .size # it cannot be larger than the file
158
162
assert c .buffer ()[:] == data [base_offset :base_offset + (size or c .size ())]
159
-
163
+
160
164
# unising a region makes the cursor invalid
161
165
c .unuse_region ()
162
166
assert not c .is_valid ()
163
167
if man .window_size ():
164
- # but doesn't change anything regarding the handle count - we cache it and only
168
+ # but doesn't change anything regarding the handle count - we cache it and only
165
169
# remove mapped regions if we have to
166
170
assert man .num_file_handles () == 2
167
171
#END ignore this for static managers
168
-
172
+
169
173
# iterate through the windows, verify data contents
170
174
# this will trigger map collection after a while
171
175
max_random_accesses = 5000
172
176
num_random_accesses = max_random_accesses
173
177
memory_read = 0
174
178
st = time ()
175
-
179
+
176
180
# cache everything to get some more performance
177
181
includes_ofs = c .includes_ofs
178
182
max_mapped_memory_size = man .max_mapped_memory_size ()
@@ -182,7 +186,7 @@ def test_memman_operation(self):
182
186
while num_random_accesses :
183
187
num_random_accesses -= 1
184
188
base_offset = randint (0 , fc .size - 1 )
185
-
189
+
186
190
# precondition
187
191
if man .window_size ():
188
192
assert max_mapped_memory_size >= mapped_memory_size ()
@@ -192,19 +196,20 @@ def test_memman_operation(self):
192
196
csize = c .size ()
193
197
assert c .buffer ()[:] == data [base_offset :base_offset + csize ]
194
198
memory_read += csize
195
-
199
+
196
200
assert includes_ofs (base_offset )
197
201
assert includes_ofs (base_offset + csize - 1 )
198
202
assert not includes_ofs (base_offset + csize )
199
203
# END while we should do an access
200
204
elapsed = max (time () - st , 0.001 ) # prevent zero divison errors on windows
201
205
mb = float (1000 * 1000 )
202
- sys .stderr .write ("%s: Read %i mb of memory with %i random on cursor initialized with %s accesses in %fs (%f mb/s)\n "
203
- % (mtype , memory_read / mb , max_random_accesses , type (item ), elapsed , (memory_read / mb )/ elapsed ))
204
-
206
+ print ("%s: Read %i mb of memory with %i random on cursor initialized with %s accesses in %fs (%f mb/s)\n "
207
+ % (mtype , memory_read / mb , max_random_accesses , type (item ), elapsed , (memory_read / mb )/ elapsed ),
208
+ file = sys .stderr )
209
+
205
210
# an offset as large as the size doesn't work !
206
211
assert not c .use_region (fc .size , size ).is_valid ()
207
-
212
+
208
213
# collection - it should be able to collect all
209
214
assert man .num_file_handles ()
210
215
assert man .collect ()
0 commit comments