]> arthur.barton.de Git - bup.git/blob - cmd/memtest-cmd.py
cmd/memtest: print per-cycle and total times.
[bup.git] / cmd / memtest-cmd.py
1 #!/usr/bin/env python
2 import sys, re, struct, mmap, time
3 from bup import git, options
4 from bup.helpers import *
5
6 handle_ctrl_c()
7
8 def s_from_bytes(bytes):
9     clist = [chr(b) for b in bytes]
10     return ''.join(clist)
11
12
13 last = start = 0
14 def report(count):
15     global last, start
16     fields = ['VmSize', 'VmRSS', 'VmData', 'VmStk', 'ms']
17     d = {}
18     for line in open('/proc/self/status').readlines():
19         l = re.split(r':\s*', line.strip(), 1)
20         d[l[0]] = l[1]
21     now = time.time()
22     d['ms'] = int((now - last) * 1000)
23     if count >= 0:
24         e1 = count
25         fields = [d[k] for k in fields]
26     else:
27         e1 = ''
28         start = now
29     print ('%9s  ' + ('%10s ' * len(fields))) % tuple([e1] + fields)
30     sys.stdout.flush()
31     last = time.time()
32
33
34 optspec = """
35 bup memtest [-n elements] [-c cycles]
36 --
37 n,number=  number of objects per cycle [10000]
38 c,cycles=  number of cycles to run [100]
39 ignore-midx  ignore .midx files, use only .idx files
40 """
41 o = options.Options('bup memtest', optspec)
42 (opt, flags, extra) = o.parse(sys.argv[1:])
43
44 if extra:
45     o.fatal('no arguments expected')
46
47 git.ignore_midx = opt.ignore_midx
48
49 git.check_repo_or_die()
50 m = git.PackIdxList(git.repo('objects/pack'))
51
52 report(-1)
53 f = open('/dev/urandom')
54 a = mmap.mmap(-1, 20)
55 report(0)
56 for c in xrange(opt.cycles):
57     for n in xrange(opt.number):
58         b = f.read(3)
59         if 0:
60             bytes = list(struct.unpack('!BBB', b)) + [0]*17
61             bytes[2] &= 0xf0
62             bin = struct.pack('!20s', s_from_bytes(bytes))
63         else:
64             a[0:2] = b[0:2]
65             a[2] = chr(ord(b[2]) & 0xf0)
66             bin = str(a[0:20])
67         #print bin.encode('hex')
68         m.exists(bin)
69     report((c+1)*opt.number)
70
71 print 'Total time: %.3fs' % (time.time() - start)