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