]> arthur.barton.de Git - bup.git/blob - lib/bup/cmd/memtest.py
test: add pylint and test imports
[bup.git] / lib / bup / cmd / memtest.py
1
2 from __future__ import absolute_import, print_function
3 import re, resource, sys, time
4
5 from bup import git, bloom, midx, options, _helpers
6 from bup.compat import range
7 from bup.io import byte_stream
8
9
10 _linux_warned = 0
11 def linux_memstat():
12     global _linux_warned
13     #fields = ['VmSize', 'VmRSS', 'VmData', 'VmStk', 'ms']
14     d = {}
15     try:
16         f = open(b'/proc/self/status', 'rb')
17     except IOError as e:
18         if not _linux_warned:
19             log('Warning: %s\n' % e)
20             _linux_warned = 1
21         return {}
22     for line in f:
23         # Note that on Solaris, this file exists but is binary.  If that
24         # happens, this split() might not return two elements.  We don't
25         # really need to care about the binary format since this output
26         # isn't used for much and report() can deal with missing entries.
27         t = re.split(br':\s*', line.strip(), 1)
28         if len(t) == 2:
29             k,v = t
30             d[k] = v
31     return d
32
33
34 last = last_u = last_s = start = 0
35 def report(count, out):
36     global last, last_u, last_s, start
37     headers = ['RSS', 'MajFlt', 'user', 'sys', 'ms']
38     ru = resource.getrusage(resource.RUSAGE_SELF)
39     now = time.time()
40     rss = int(ru.ru_maxrss // 1024)
41     if not rss:
42         rss = linux_memstat().get(b'VmRSS', b'??')
43     fields = [rss,
44               ru.ru_majflt,
45               int((ru.ru_utime - last_u) * 1000),
46               int((ru.ru_stime - last_s) * 1000),
47               int((now - last) * 1000)]
48     fmt = '%9s  ' + ('%10s ' * len(fields))
49     if count >= 0:
50         line = fmt % tuple([count] + fields)
51         out.write(line.encode('ascii') + b'\n')
52     else:
53         start = now
54         out.write((fmt % tuple([''] + headers)).encode('ascii') + b'\n')
55     out.flush()
56
57     # don't include time to run report() in usage counts
58     ru = resource.getrusage(resource.RUSAGE_SELF)
59     last_u = ru.ru_utime
60     last_s = ru.ru_stime
61     last = time.time()
62
63
64 optspec = """
65 bup memtest [-n elements] [-c cycles]
66 --
67 n,number=  number of objects per cycle [10000]
68 c,cycles=  number of cycles to run [100]
69 ignore-midx  ignore .midx files, use only .idx files
70 existing   test with existing objects instead of fake ones
71 """
72
73 def main(argv):
74     o = options.Options(optspec)
75     opt, flags, extra = o.parse_bytes(argv[1:])
76
77     if extra:
78         o.fatal('no arguments expected')
79
80     git.check_repo_or_die()
81     m = git.PackIdxList(git.repo(b'objects/pack'), ignore_midx=opt.ignore_midx)
82
83     sys.stdout.flush()
84     out = byte_stream(sys.stdout)
85
86     report(-1, out)
87     _helpers.random_sha()
88     report(0, out)
89
90     if opt.existing:
91         def foreverit(mi):
92             while 1:
93                 for e in mi:
94                     yield e
95         objit = iter(foreverit(m))
96
97     for c in range(opt.cycles):
98         for n in range(opt.number):
99             if opt.existing:
100                 bin = next(objit)
101                 assert(m.exists(bin))
102             else:
103                 bin = _helpers.random_sha()
104
105                 # technically, a randomly generated object id might exist.
106                 # but the likelihood of that is the likelihood of finding
107                 # a collision in sha-1 by accident, which is so unlikely that
108                 # we don't care.
109                 assert(not m.exists(bin))
110         report((c+1)*opt.number, out)
111
112     if bloom._total_searches:
113         out.write(b'bloom: %d objects searched in %d steps: avg %.3f steps/object\n'
114                   % (bloom._total_searches, bloom._total_steps,
115                      bloom._total_steps*1.0/bloom._total_searches))
116     if midx._total_searches:
117         out.write(b'midx: %d objects searched in %d steps: avg %.3f steps/object\n'
118                   % (midx._total_searches, midx._total_steps,
119                      midx._total_steps*1.0/midx._total_searches))
120     if git._total_searches:
121         out.write(b'idx: %d objects searched in %d steps: avg %.3f steps/object\n'
122                   % (git._total_searches, git._total_steps,
123                      git._total_steps*1.0/git._total_searches))
124     out.write(b'Total time: %.3fs\n' % (time.time() - start))