]> arthur.barton.de Git - bup.git/blobdiff - cmd/memtest-cmd.py
git.py: keep statistics on how much sha1 searching we had to do.
[bup.git] / cmd / memtest-cmd.py
index 7ad09e558176eceeeafdb2b93a5562a3058daea8..f1ab06b186786d24c45df916cb050a2d9af8d531 100755 (executable)
@@ -1,37 +1,45 @@
 #!/usr/bin/env python
-import sys, re, struct, mmap
+import sys, re, struct, mmap, time
 from bup import git, options
 from bup.helpers import *
 
+handle_ctrl_c()
 
 def s_from_bytes(bytes):
     clist = [chr(b) for b in bytes]
     return ''.join(clist)
 
 
+last = start = 0
 def report(count):
-    fields = ['VmSize', 'VmRSS', 'VmData', 'VmStk']
+    global last, start
+    fields = ['VmSize', 'VmRSS', 'VmData', 'VmStk', 'ms']
     d = {}
     for line in open('/proc/self/status').readlines():
         l = re.split(r':\s*', line.strip(), 1)
         d[l[0]] = l[1]
+    now = time.time()
+    d['ms'] = int((now - last) * 1000)
     if count >= 0:
         e1 = count
         fields = [d[k] for k in fields]
     else:
         e1 = ''
+        start = now
     print ('%9s  ' + ('%10s ' * len(fields))) % tuple([e1] + fields)
     sys.stdout.flush()
+    last = time.time()
 
 
 optspec = """
-memtest [-n elements] [-c cycles]
+bup memtest [-n elements] [-c cycles]
 --
-n,number=  number of objects per cycle
-c,cycles=  number of cycles to run
+n,number=  number of objects per cycle [10000]
+c,cycles=  number of cycles to run [100]
 ignore-midx  ignore .midx files, use only .idx files
+existing   test with existing objects instead of fake ones
 """
-o = options.Options(sys.argv[0], optspec)
+o = options.Options('bup memtest', optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
 
 if extra:
@@ -42,24 +50,37 @@ git.ignore_midx = opt.ignore_midx
 git.check_repo_or_die()
 m = git.PackIdxList(git.repo('objects/pack'))
 
-cycles = opt.cycles or 100
-number = opt.number or 10000
-
 report(-1)
 f = open('/dev/urandom')
 a = mmap.mmap(-1, 20)
 report(0)
-for c in xrange(cycles):
-    for n in xrange(number):
-        b = f.read(3)
-        if 0:
-            bytes = list(struct.unpack('!BBB', b)) + [0]*17
-            bytes[2] &= 0xf0
-            bin = struct.pack('!20s', s_from_bytes(bytes))
+
+if opt.existing:
+    def foreverit(mi):
+        while 1:
+            for e in mi:
+                yield e
+    objit = iter(foreverit(m))
+    
+for c in xrange(opt.cycles):
+    for n in xrange(opt.number):
+        if opt.existing:
+            bin = objit.next()
+            assert(m.exists(bin))
         else:
+            b = f.read(3)
             a[0:2] = b[0:2]
             a[2] = chr(ord(b[2]) & 0xf0)
             bin = str(a[0:20])
-        #print bin.encode('hex')
-        m.exists(bin)
-    report((c+1)*number)
+
+            # technically, a randomly generated object id might exist.
+            # but the likelihood of that is the likelihood of finding
+            # a collision in sha-1 by accident, which is so unlikely that
+            # we don't care.
+            assert(not m.exists(bin))
+    report((c+1)*opt.number)
+
+print ('%d objects searched in %d steps: avg %.3f steps/object' 
+       % (git._total_searches, git._total_steps,
+          git._total_steps*1.0/git._total_searches))
+print 'Total time: %.3fs' % (time.time() - start)