[213] | 1 | #!/usr/bin/env python3 |
---|
[197] | 2 | |
---|
| 3 | import sys |
---|
[213] | 4 | import gc |
---|
[215] | 5 | import time |
---|
[197] | 6 | import pyregfi |
---|
| 7 | |
---|
[213] | 8 | def usage(): |
---|
| 9 | sys.stderr.write("USAGE: pyregfi-smoketest.py hive1 [hive2 ...]\n") |
---|
[197] | 10 | |
---|
| 11 | |
---|
[215] | 12 | # helper function |
---|
| 13 | def getCurrentPath(key): |
---|
| 14 | if key == None: |
---|
| 15 | return '' |
---|
| 16 | path = [] |
---|
| 17 | p = key |
---|
| 18 | while p != None: |
---|
| 19 | path.append(p.name) |
---|
| 20 | if p.is_root(): |
---|
| 21 | break |
---|
| 22 | else: |
---|
| 23 | p = p.get_parent() |
---|
| 24 | path.reverse() |
---|
| 25 | del path[0] |
---|
| 26 | |
---|
| 27 | return path |
---|
| 28 | |
---|
| 29 | |
---|
[213] | 30 | # Uses the HiveIterator to walk all keys |
---|
| 31 | # Gathers various (meaningless) statistics to exercise simple attribute access |
---|
| 32 | # and to hopefully smoke out any bugs that can be identified by changing stats |
---|
[216] | 33 | def iterTallyNames(hive): |
---|
[213] | 34 | key_count = 0 |
---|
| 35 | key_lens = 0 |
---|
| 36 | key_rawlens = 0 |
---|
| 37 | value_count = 0 |
---|
| 38 | value_lens = 0 |
---|
| 39 | value_rawlens = 0 |
---|
[197] | 40 | |
---|
[213] | 41 | for k in hive: |
---|
| 42 | key_count += 1 |
---|
| 43 | if k.name != None: |
---|
| 44 | key_lens += len(k.name) |
---|
| 45 | if k.name_raw != None: |
---|
| 46 | key_rawlens += len(k.name_raw) |
---|
[199] | 47 | |
---|
[213] | 48 | for v in k.values: |
---|
| 49 | value_count += 1 |
---|
| 50 | if v.name != None: |
---|
| 51 | value_lens += len(v.name) |
---|
| 52 | if v.name_raw != None: |
---|
| 53 | value_rawlens += len(v.name_raw) |
---|
[200] | 54 | |
---|
[214] | 55 | print(" Counts: keys=%d, values=%d" % (key_count, value_count)) |
---|
| 56 | print(" Total name length: keys=%d, values=%d" % (key_lens, value_lens)) |
---|
| 57 | print(" Total raw name lengths: keys=%d, values=%d" % (key_rawlens, value_rawlens)) |
---|
[200] | 58 | |
---|
[209] | 59 | |
---|
[215] | 60 | # For each key in the hive, this traverses the parent links up to the root, |
---|
| 61 | # recording the path as it goes, and then uses the subtree/descend method |
---|
| 62 | # to find the same key again, verifying it is the same. This test is currently |
---|
| 63 | # very slow because no key caching is used. |
---|
| 64 | def iterParentWalk(hive): |
---|
| 65 | i = 1 |
---|
| 66 | for k in hive: |
---|
| 67 | path = getCurrentPath(k) |
---|
| 68 | try: |
---|
| 69 | hive_iter = hive.subtree(path) |
---|
| 70 | if hive_iter.current_key() != k: |
---|
| 71 | print("WARNING: k != current_key for path '%s'." % path) |
---|
| 72 | else: |
---|
| 73 | i += 1 |
---|
| 74 | except Exception as e: |
---|
| 75 | print("WARNING: Could not decend to path '%s'.\nError:\n %s\n%s" % (path,e.args,e)) |
---|
| 76 | print(" Successfully tested paths on %d keys." % i) |
---|
[209] | 77 | |
---|
[215] | 78 | |
---|
[216] | 79 | # Uses the HiveIterator to walk all keys |
---|
| 80 | # Gathers various (meaningless) statistics about data/data_raw attributes |
---|
| 81 | def iterTallyData(hive): |
---|
| 82 | data_stat = 0.0 |
---|
| 83 | dataraw_stat = 0.0 |
---|
| 84 | |
---|
| 85 | for k in hive: |
---|
| 86 | for v in k.values: |
---|
| 87 | d = v.data |
---|
| 88 | if d == None: |
---|
| 89 | data_stat += 0.1 |
---|
| 90 | elif hasattr(d, "__len__"): |
---|
| 91 | data_stat += len(d) |
---|
| 92 | else: |
---|
| 93 | data_stat += d/2.0**64 |
---|
| 94 | |
---|
| 95 | d = v.data_raw |
---|
| 96 | if d == None: |
---|
| 97 | dataraw_stat += 0.1 |
---|
| 98 | else: |
---|
| 99 | dataraw_stat += len(d) |
---|
| 100 | |
---|
| 101 | print(" Data stat: %f" % data_stat) |
---|
| 102 | print(" Raw data stat: %f" % dataraw_stat) |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | |
---|
[213] | 106 | if len(sys.argv) < 2: |
---|
| 107 | usage() |
---|
| 108 | sys.exit(1) |
---|
[209] | 109 | |
---|
[215] | 110 | |
---|
[216] | 111 | #tests = [("iterTallyNames",iterTallyNames),("iterParentWalk",iterParentWalk),] |
---|
| 112 | tests = [("iterTallyData",iterTallyData),] |
---|
[215] | 113 | |
---|
[213] | 114 | files = [] |
---|
| 115 | for f in sys.argv[1:]: |
---|
| 116 | files.append((f, open(f,"r+b"))) |
---|
| 117 | |
---|
| 118 | |
---|
[215] | 119 | start_time = time.time() |
---|
[213] | 120 | for hname,fh in files: |
---|
| 121 | hive = pyregfi.Hive(fh) |
---|
| 122 | for tname,t in tests: |
---|
[215] | 123 | teststart = time.time() |
---|
[213] | 124 | tstr = "'%s' on '%s'" % (tname,hname) |
---|
| 125 | print("##BEGIN %s:" % tstr) |
---|
| 126 | t(hive) |
---|
[215] | 127 | print("##END %s; runtime=%f; messages:" % (tstr, time.time() - teststart)) |
---|
[213] | 128 | print(pyregfi.GetLogMessages()) |
---|
| 129 | print |
---|
[215] | 130 | sys.stdout.flush() |
---|
[213] | 131 | |
---|
[215] | 132 | hive = None |
---|
[213] | 133 | files = None |
---|
| 134 | tests = None |
---|
| 135 | gc.collect() |
---|
[215] | 136 | print("### Tests Completed, runtime: %f ###" % (time.time() - start_time)) |
---|
[214] | 137 | #print(gc.garbage) |
---|