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