1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
#!/usr/bin/env python
import os
import time
import string
import StringIO
import tempfile
import numpy
import sqlite3
from matplotlib import pyplot
from matplotlib import pylab
class Statistics:
def __init__(self):
print("Initializing %s" % (self.__class__))
self.results = {}
pass
def store(self, key, value):
try:
self.results[key] = value
except:
return False
return True
def append(self, key, value):
try:
self.results[key].append(value)
except:
return False
return True
def retrieve(self):
return self.results[self.name]
def get_numpy(self, key):
data = self.results[key]
xlist = []
ylist = []
for x, y in data:
xlist.append(x)
ylist.append(y)
x = numpy.array(xlist)
y = numpy.array(ylist)
return x, y
def plot(self):
pass
class Stopwatch:
def __init__(self):
self.start_time = 0.0
self.stop_time = 0.0
def start(self):
self.start_time = time.time()
def stop(self):
self.stop_time = time.time()
def now(self):
return (time.time() - self.start_time)
def result(self):
return (self.stop_time - self.start_time)
class Sequencial_test(Statistics):
def __init__(self, size=1):
'''
size in megabytes
bufsiz in bytes
mode is 'buffered' or 'unbuffered'
async is True or False
'''
Statistics.__init__(self)
Statistics.store(self, 'buffered_sync', [])
Statistics.store(self, 'buffered_async', [])
Statistics.store(self, 'unbuffered_sync', [])
Statistics.store(self, 'unbuffered_async', [])
self.MEGABYTE = 1024000
self.size = size
self.timer = Stopwatch()
self.random_source = os.urandom(self.MEGABYTE * 10)
self.prefix='sequencial_test_'
self.path_tests = os.path.join(os.curdir, 'tests')
self.path_results = os.path.join(os.curdir, 'results')
if not os.path.exists(self.path_tests):
os.mkdir(self.path_tests)
if not os.path.exists(self.path_results):
os.mkdir(self.path_results)
def __del__(self):
if os.path.exists(self.filename):
print('deleting %s' % (self.filename))
os.remove(os.path.join(self.filename))
def stress_test_wrapper(self, iter_max):
self.stress_buffered_sync(iter_max)
self.stress_buffered_async(iter_max)
self.stress_unbuffered_sync(iter_max)
self.stress_unbuffered_async(iter_max)
def stress_buffered_sync(self, iter_max=1):
print("Synchronous buffered I/O")
for i in range(iter_max):
print("\tTest: %d of %d" % (i+1, iter_max))
self.buffered_sync()
def stress_buffered_async(self, iter_max=1):
print("Asynchronous buffered I/O")
for i in range(iter_max):
print("\tTest: %d of %d" % (i+1, iter_max))
self.buffered_async()
def stress_unbuffered_sync(self, iter_max=1):
print("Synchronous unbuffered I/O")
for i in range(iter_max):
print("\tTest: %d of %d" % (i+1, iter_max))
self.unbuffered_sync()
def stress_unbuffered_async(self, iter_max=1):
print("Asynchronous unbuffered I/O")
for i in range(iter_max):
print("\tTest: %d of %d" % (i+1, iter_max))
self.unbuffered_async()
def buffered_sync(self):
Statistics.append(self,'buffered_sync', self.write_out(size=self.size))
def buffered_async(self):
Statistics.append(self, 'buffered_async', self.write_out(size=self.size, async=True))
def buffered_stepping(self):
pass
def unbuffered_sync(self):
Statistics.append(self, 'unbuffered_sync', self.write_out(size=self.size, mode='unbuffered'))
def unbuffered_async(self):
Statistics.append(self, 'unbuffered_async', self.write_out(size=self.size, mode='unbuffered', async=True))
def unbuffered_stepping(self):
pass
def write_out(self, size, bufsiz=1024000, mode='buffered', async=False):
size = size * self.MEGABYTE
temp = tempfile.NamedTemporaryFile(dir=self.path_tests, prefix=self.prefix, delete=False)
self.filename = temp.name
outfile = file(self.filename, 'wb')
total_bytes = 0
self.timer.start()
while total_bytes <= size:
try:
source = StringIO.StringIO(self.random_source)
if mode == 'buffered':
buf = source.read(bufsiz)
else:
buf = source.read()
outfile.write(buf)
if not async:
outfile.flush()
os.fsync(outfile)
total_bytes += len(buf)
rate_current = (total_bytes / self.timer.now() / 1024 / 1024)
source.close()
'''
if mode == 'buffered':
print("Buf:%d Sz:%d R:%0.2f Te:%0.4f\r" % (bufsiz, size, rate_current, self.timer.now())),
else:
print("Sz:%d R:%0.2f Te:%0.4f\r" % (size, rate_current, self.timer.now())),
'''
except:
print('Failed')
self.timer.stop()
return False
outfile.close()
self.timer.stop()
rate = (total_bytes / self.timer.result() / 1024 / 1024)
elapsed = self.timer.result()
#print('TR:%0.4fs R:%0.2f MB/s' % (self.timer.result(), rate))
os.remove(self.filename)
return rate, elapsed
def read_in(self):
pass
if __name__ == "__main__":
size_steps = [ 1, 4, 8 ]
buffer_steps = [ 512000, 1024000, 4096000 ]
iter_max=10
for size in size_steps:
print("### %dMB ####" % (size))
sequence_test = Sequencial_test(size)
sequence_test.stress_buffered_sync(iter_max)
sequence_test.stress_buffered_async(iter_max)
sequence_test.stress_unbuffered_sync(iter_max)
sequence_test.stress_unbuffered_async(iter_max)
for k in sequence_test.results:
print('Plotting %s' % (k))
rate, elapsed = sequence_test.get_numpy(k)
rate_mean = numpy.repeat(numpy.mean(rate), rate.size, 0)
#rate_average = numpy.repeat(numpy.average(rate), rate.size, 0)
elapsed_mean = numpy.repeat(numpy.mean(elapsed), elapsed.size, 0)
#elapsed_average = numpy.repeat(numpy.average(elapsed), elapsed.size, 0)
print("Rate mean: %f" % (numpy.mean(rate_mean)))
print("Elapsed mean: %f" % (numpy.mean(elapsed_mean)))
pyplot.figure(1)
pyplot.subplot(211)
pyplot.title(k)
pyplot.grid(True)
pyplot.xlabel('Iteration')
pyplot.ylabel('Rate (MB/s)')
pyplot.plot(rate)
#pyplot.plot(rate_average)
pyplot.plot(rate_mean)
pyplot.subplot(212)
pyplot.grid(True)
pyplot.xlabel('Iteration')
pyplot.ylabel('Time to Write')
pyplot.plot(elapsed)
#pyplot.plot(elapsed_average)
pyplot.plot(elapsed_mean)
pyplot.savefig(os.path.join(sequence_test.path_results, string.join([k, str(sequence_test.size)], sep='_')))
pyplot.close()
|