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