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