]> arthur.barton.de Git - bup.git/blob - cmd/memtest-cmd.py
0005e67d3dc78fde985918e1e4289fee92e61aa8
[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 existing   test with existing objects instead of fake ones
41 """
42 o = options.Options('bup memtest', optspec)
43 (opt, flags, extra) = o.parse(sys.argv[1:])
44
45 if extra:
46     o.fatal('no arguments expected')
47
48 git.ignore_midx = opt.ignore_midx
49
50 git.check_repo_or_die()
51 m = git.PackIdxList(git.repo('objects/pack'))
52
53 report(-1)
54 f = open('/dev/urandom')
55 a = mmap.mmap(-1, 20)
56 report(0)
57
58 if opt.existing:
59     def foreverit(mi):
60         while 1:
61             for e in mi:
62                 yield e
63     objit = iter(foreverit(m))
64     
65 for c in xrange(opt.cycles):
66     for n in xrange(opt.number):
67         if opt.existing:
68             bin = objit.next()
69             assert(m.exists(bin))
70         else:
71             b = f.read(3)
72             a[0:2] = b[0:2]
73             a[2] = chr(ord(b[2]) & 0xf0)
74             bin = str(a[0:20])
75
76             # technically, a randomly generated object id might exist.
77             # but the likelihood of that is the likelihood of finding
78             # a collision in sha-1 by accident, which is so unlikely that
79             # we don't care.
80             assert(not m.exists(bin))
81     report((c+1)*opt.number)
82
83 print 'Total time: %.3fs' % (time.time() - start)