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