@@ -25,19 +25,23 @@ def get_file_sizes():
25
25
size = int (size ) * {'KiB' : 1024 , 'MiB' : 1024 ** 2 }[unit ]
26
26
yield s .replace (' ' , '' ), size
27
27
28
+
28
29
def get_binary_files ():
29
30
return ((name + ".bin" , size ) for name , size in get_file_sizes ())
30
31
32
+
31
33
def get_text_files ():
32
34
return (("%s-%s-%s.txt" % (name , TEXT_ENCODING , NEWLINES ), size )
33
35
for name , size in get_file_sizes ())
34
36
37
+
35
38
def with_open_mode (mode ):
36
39
def decorate (f ):
37
40
f .file_open_mode = mode
38
41
return f
39
42
return decorate
40
43
44
+
41
45
def with_sizes (* sizes ):
42
46
def decorate (f ):
43
47
f .file_sizes = sizes
@@ -55,6 +59,7 @@ def read_bytewise(f):
55
59
while f .read (1 ):
56
60
pass
57
61
62
+
58
63
@with_open_mode ("r" )
59
64
@with_sizes ("medium" )
60
65
def read_small_chunks (f ):
@@ -63,6 +68,7 @@ def read_small_chunks(f):
63
68
while f .read (20 ):
64
69
pass
65
70
71
+
66
72
@with_open_mode ("r" )
67
73
@with_sizes ("medium" )
68
74
def read_big_chunks (f ):
@@ -71,6 +77,7 @@ def read_big_chunks(f):
71
77
while f .read (4096 ):
72
78
pass
73
79
80
+
74
81
@with_open_mode ("r" )
75
82
@with_sizes ("small" , "medium" , "large" )
76
83
def read_whole_file (f ):
@@ -79,6 +86,7 @@ def read_whole_file(f):
79
86
while f .read ():
80
87
pass
81
88
89
+
82
90
@with_open_mode ("rt" )
83
91
@with_sizes ("medium" )
84
92
def read_lines (f ):
@@ -87,6 +95,7 @@ def read_lines(f):
87
95
for line in f :
88
96
pass
89
97
98
+
90
99
@with_open_mode ("r" )
91
100
@with_sizes ("medium" )
92
101
def seek_forward_bytewise (f ):
@@ -97,6 +106,7 @@ def seek_forward_bytewise(f):
97
106
for i in range (0 , size - 1 ):
98
107
f .seek (i , 0 )
99
108
109
+
100
110
@with_open_mode ("r" )
101
111
@with_sizes ("medium" )
102
112
def seek_forward_blockwise (f ):
@@ -107,6 +117,7 @@ def seek_forward_blockwise(f):
107
117
for i in range (0 , size - 1 , 1000 ):
108
118
f .seek (i , 0 )
109
119
120
+
110
121
@with_open_mode ("rb" )
111
122
@with_sizes ("medium" )
112
123
def read_seek_bytewise (f ):
@@ -115,6 +126,7 @@ def read_seek_bytewise(f):
115
126
while f .read (1 ):
116
127
f .seek (1 , 1 )
117
128
129
+
118
130
@with_open_mode ("rb" )
119
131
@with_sizes ("medium" )
120
132
def read_seek_blockwise (f ):
@@ -131,20 +143,23 @@ def write_bytewise(f, source):
131
143
for i in range (0 , len (source )):
132
144
f .write (source [i :i + 1 ])
133
145
146
+
134
147
@with_open_mode ("w" )
135
148
@with_sizes ("medium" )
136
149
def write_small_chunks (f , source ):
137
150
""" write 20 units at a time """
138
151
for i in range (0 , len (source ), 20 ):
139
152
f .write (source [i :i + 20 ])
140
153
154
+
141
155
@with_open_mode ("w" )
142
156
@with_sizes ("medium" )
143
157
def write_medium_chunks (f , source ):
144
158
""" write 4096 units at a time """
145
159
for i in range (0 , len (source ), 4096 ):
146
160
f .write (source [i :i + 4096 ])
147
161
162
+
148
163
@with_open_mode ("w" )
149
164
@with_sizes ("large" )
150
165
def write_large_chunks (f , source ):
@@ -161,6 +176,7 @@ def modify_bytewise(f, source):
161
176
for i in range (0 , len (source )):
162
177
f .write (source [i :i + 1 ])
163
178
179
+
164
180
@with_open_mode ("w+" )
165
181
@with_sizes ("medium" )
166
182
def modify_small_chunks (f , source ):
@@ -169,6 +185,7 @@ def modify_small_chunks(f, source):
169
185
for i in range (0 , len (source ), 20 ):
170
186
f .write (source [i :i + 20 ])
171
187
188
+
172
189
@with_open_mode ("w+" )
173
190
@with_sizes ("medium" )
174
191
def modify_medium_chunks (f , source ):
@@ -177,6 +194,7 @@ def modify_medium_chunks(f, source):
177
194
for i in range (0 , len (source ), 4096 ):
178
195
f .write (source [i :i + 4096 ])
179
196
197
+
180
198
@with_open_mode ("wb+" )
181
199
@with_sizes ("medium" )
182
200
def modify_seek_forward_bytewise (f , source ):
@@ -186,6 +204,7 @@ def modify_seek_forward_bytewise(f, source):
186
204
f .write (source [i :i + 1 ])
187
205
f .seek (i + 2 )
188
206
207
+
189
208
@with_open_mode ("wb+" )
190
209
@with_sizes ("medium" )
191
210
def modify_seek_forward_blockwise (f , source ):
@@ -195,6 +214,7 @@ def modify_seek_forward_blockwise(f, source):
195
214
f .write (source [i :i + 1000 ])
196
215
f .seek (i + 2000 )
197
216
217
+
198
218
# XXX the 2 following tests don't work with py3k's text IO
199
219
@with_open_mode ("wb+" )
200
220
@with_sizes ("medium" )
@@ -205,6 +225,7 @@ def read_modify_bytewise(f, source):
205
225
f .read (1 )
206
226
f .write (source [i + 1 :i + 2 ])
207
227
228
+
208
229
@with_open_mode ("wb+" )
209
230
@with_sizes ("medium" )
210
231
def read_modify_blockwise (f , source ):
@@ -233,6 +254,7 @@ def read_modify_blockwise(f, source):
233
254
read_modify_bytewise , read_modify_blockwise ,
234
255
]
235
256
257
+
236
258
def run_during (duration , func ):
237
259
_t = time .time
238
260
n = 0
@@ -248,6 +270,7 @@ def run_during(duration, func):
248
270
real = (end [4 ] if start [4 ] else time .time ()) - real_start
249
271
return n , real , sum (end [0 :2 ]) - sum (start [0 :2 ])
250
272
273
+
251
274
def warm_cache (filename ):
252
275
with open (filename , "rb" ) as f :
253
276
f .read ()
@@ -322,6 +345,7 @@ def run_test_family(tests, mode_filter, files, open_func, *make_args):
322
345
# Binary writes
323
346
if "b" in options and "w" in options :
324
347
print ("\n ** Binary append **\n " )
348
+
325
349
def make_test_source (name , size ):
326
350
with open (name , "rb" ) as f :
327
351
return f .read ()
@@ -331,6 +355,7 @@ def make_test_source(name, size):
331
355
# Text writes
332
356
if "t" in options and "w" in options :
333
357
print ("\n ** Text append **\n " )
358
+
334
359
def make_test_source (name , size ):
335
360
with text_open (name , "r" ) as f :
336
361
return f .read ()
@@ -340,6 +365,7 @@ def make_test_source(name, size):
340
365
# Binary overwrites
341
366
if "b" in options and "w" in options :
342
367
print ("\n ** Binary overwrite **\n " )
368
+
343
369
def make_test_source (name , size ):
344
370
with open (name , "rb" ) as f :
345
371
return f .read ()
@@ -349,6 +375,7 @@ def make_test_source(name, size):
349
375
# Text overwrites
350
376
if "t" in options and "w" in options :
351
377
print ("\n ** Text overwrite **\n " )
378
+
352
379
def make_test_source (name , size ):
353
380
with text_open (name , "r" ) as f :
354
381
return f .read ()
@@ -398,6 +425,7 @@ def prepare_files():
398
425
f .write (head )
399
426
f .write (tail )
400
427
428
+
401
429
def main ():
402
430
global TEXT_ENCODING , NEWLINES
403
431
@@ -455,6 +483,7 @@ def main():
455
483
prepare_files ()
456
484
run_all_tests (test_options )
457
485
486
+
458
487
if __name__ == "__main__" :
459
488
main ()
460
489
0 commit comments