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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
#!/usr/bin/python
# (c) Alexander Belchenko, 2007, 2009
"""Benchmarking.
Run each test 3 times and get median value.
Using 10K array as base test time.
Each other test compared with base with next formula::
Tc * Nb
q = ---------
Tb * Nc
Here:
* Tc - execution time of current test
* Tb - execution time of base
* Nb - array size of base (10K)
* Nc - array size of current test
If resulting value is ``q <= 1.0`` it's the best possible result,
i.e. time increase proportionally to array size.
"""
from cStringIO import StringIO
import gc
import sys
import time
import intelhex
def median(values):
"""Return median value for the list of values.
@param values: list of values for processing.
@return: median value.
"""
values.sort()
n = int(len(values) / 2)
return values[n]
def run_test(func, fobj):
"""Run func with argument fobj and measure execution time.
@param func: function for test
@param fobj: data for test
@return: execution time
"""
gc.disable()
try:
begin = time.time()
func(fobj)
end = time.time()
finally:
gc.enable()
return end - begin
def run_readtest_N_times(func, hexstr, n):
"""Run each test N times.
@param func: function for test
@param hexstr: string with content of hex file to read
@param n: times to repeat.
@return: (median time, times list)
"""
assert n > 0
times = []
for i in xrange(n):
sio = StringIO(hexstr)
times.append(run_test(func, sio))
sio.close()
t = median(times)
return t, times
def run_writetest_N_times(func, n):
"""Run each test N times.
@param func: function for test
@param n: times to repeat.
@return: (median time, times list)
"""
assert n > 0
times = []
for i in xrange(n):
sio = StringIO()
times.append(run_test(func, sio))
sio.close()
t = median(times)
return t, times
def time_coef(tc, nc, tb, nb):
"""Return time coefficient relative to base numbers.
@param tc: current test time
@param nc: current test data size
@param tb: base test time
@param nb: base test data size
@return: time coef.
"""
tc = float(tc)
nc = float(nc)
tb = float(tb)
nb = float(nb)
q = (tc * nb) / (tb * nc)
return q
def get_test_data(n1, offset, n2):
"""Create test data on given pattern.
@param n1: size of first part of array at base address 0.
@param offset: offset for second part of array.
@param n2: size of second part of array at given offset.
@return: (overall size, hex file, IntelHex object)
"""
# make IntelHex object
ih = intelhex.IntelHex()
addr = 0
for i in xrange(n1):
ih[addr] = addr % 256
addr += 1
addr += offset
for i in xrange(n2):
ih[addr] = addr % 256
addr += 1
# make hex file
sio = StringIO()
ih.write_hex_file(sio)
hexstr = sio.getvalue()
sio.close()
#
return n1+n2, hexstr, ih
def get_base_10K():
"""Base 10K"""
return get_test_data(10000, 0, 0)
def get_100K():
return get_test_data(100000, 0, 0)
def get_100K_100K():
return get_test_data(100000, 1000000, 100000)
def get_0_100K():
return get_test_data(0, 1000000, 100000)
def get_1M():
return get_test_data(1000000, 0, 0)
class Measure(object):
"""Measure execution time helper."""
data_set = [
# (data name, getter)
('base 10K', get_base_10K), # first should be base numbers
('100K', get_100K),
('1M', get_1M),
('100K+100K', get_100K_100K),
('0+100K', get_0_100K),
]
def __init__(self, n=3, read=True, write=True):
self.n = n
self.read = read
self.write = write
self.results = []
def measure_one(self, data):
"""Do measuring of read and write operations.
@param data: 3-tuple from get_test_data
@return: (time readhex, time writehex)
"""
_unused, hexstr, ih = data
tread, twrite = 0.0, 0.0
if self.read:
tread = run_readtest_N_times(intelhex.IntelHex, hexstr, self.n)[0]
if self.write:
twrite = run_writetest_N_times(ih.write_hex_file, self.n)[0]
return tread, twrite
def measure_all(self):
for name, getter in self.data_set:
data = getter()
times = self.measure_one(data)
self.results.append((name, times, data[0]))
def print_report(self, to_file=None):
if to_file is None:
to_file = sys.stdout
base_title, base_times, base_n = self.results[0]
base_read, base_write = base_times
read_report = ['%-10s\t%7.3f' % (base_title, base_read)]
write_report = ['%-10s\t%7.3f' % (base_title, base_write)]
for item in self.results[1:]:
cur_title, cur_times, cur_n = item
cur_read, cur_write = cur_times
if self.read:
qread = time_coef(cur_read, cur_n,
base_read, base_n)
read_report.append('%-10s\t%7.3f\t%7.3f' % (cur_title,
cur_read,
qread))
if self.write:
qwrite = time_coef(cur_write, cur_n,
base_write, base_n)
write_report.append('%-10s\t%7.3f\t%7.3f' % (cur_title,
cur_write,
qwrite))
if self.read:
to_file.write('Read operation:\n')
to_file.write('\n'.join(read_report))
to_file.write('\n\n')
if self.write:
to_file.write('Write operation:\n')
to_file.write('\n'.join(write_report))
to_file.write('\n\n')
HELP = """\
Usage: python _bench.py [OPTIONS]
Options:
-h this help
-n N repeat tests N times
-r run only tests for read operation
-w run only tests for write operation
If option -r or -w is not specified then all tests will be run.
"""
def main(argv=None):
"""Main function to run benchmarks.
@param argv: command-line arguments.
@return: exit code (0 is OK).
"""
import getopt
# default values
test_read = None
test_write = None
n = 3 # number of repeat
if argv is None:
argv = sys.argv[1:]
try:
opts, args = getopt.getopt(argv, 'hn:rw', [])
for o,a in opts:
if o == '-h':
print(HELP)
return 0
elif o == '-n':
n = int(a)
elif o == '-r':
test_read = True
elif o == '-w':
test_write = True
if args:
raise getopt.GetoptError('Arguments are not used.')
except getopt.GetoptError, msg:
txt = str(msg)
print(txt)
return 1
if (test_read, test_write) == (None, None):
test_read = test_write = True
m = Measure(n, test_read, test_write)
m.measure_all()
m.print_report()
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
"""
Some Results
************
21/04/2007 revno.40
Python 2.5 @ Windows XP, Intel Celeron M CPU 430 @ 1.73GHz
Read operation:
base 10K 0.031
100K 0.360 1.161
1M 3.500 1.129
100K+100K 0.719 1.160
0+100K 0.360 1.161
Write operation:
base 10K 0.031
100K 0.297 0.958
1M 2.953 0.953
100K+100K 1.328 2.142
0+100K 0.312 1.006
21/04/2007 revno.46
Python 2.5 @ Windows XP, Intel Celeron M CPU 430 @ 1.73GHz
Read operation:
base 10K 0.016
100K 0.203 1.269
1M 2.000 1.250
100K+100K 0.422 1.319
0+100K 0.203 1.269
Write operation:
base 10K 0.031
100K 0.297 0.958
1M 2.969 0.958
100K+100K 1.328 2.142
0+100K 0.312 1.006
22/04/2007 revno.48
Python 2.5 @ Windows XP, Intel Celeron M CPU 430 @ 1.73GHz
Read operation:
base 10K 0.016
100K 0.187 1.169
1M 1.891 1.182
100K+100K 0.406 1.269
0+100K 0.188 1.175
Write operation:
base 10K 0.031
100K 0.296 0.955
1M 2.969 0.958
100K+100K 1.328 2.142
0+100K 0.312 1.006
19/08/2008 revno.72
Python 2.5.2 @ Windows XP, Intel Celeron M CPU 430 @ 1.73GHz
Read operation:
base 10K 0.016
100K 0.171 1.069
1M 1.734 1.084
100K+100K 0.375 1.172
0+100K 0.172 1.075
Write operation:
base 10K 0.016
100K 0.156 0.975
1M 1.532 0.957
100K+100K 0.344 1.075
0+100K 0.156 0.975
"""
|