/usr/share/doc/python-tables-doc/bench/undo_redo.py is in python-tables-doc 3.1.1-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | ###########################################################################
# Benchmark for undo/redo. Run this program without parameters
# for mode of use.
#
# Francesc Alted
# 2005-03-09
###########################################################################
from __future__ import print_function
import numpy
from time import time
import tables
verbose = 0
class BasicBenchmark(object):
def __init__(self, filename, testname, vecsize, nobjects, niter):
self.file = filename
self.test = testname
self.vecsize = vecsize
self.nobjects = nobjects
self.niter = niter
# Initialize the arrays
self.a1 = numpy.arange(0, 1 * self.vecsize)
self.a2 = numpy.arange(1 * self.vecsize, 2 * self.vecsize)
self.a3 = numpy.arange(2 * self.vecsize, 3 * self.vecsize)
def setUp(self):
# Create an HDF5 file
self.fileh = tables.open_file(self.file, mode="w")
# open the do/undo
self.fileh.enable_undo()
def tearDown(self):
self.fileh.disable_undo()
self.fileh.close()
# Remove the temporary file
# os.remove(self.file)
def createNode(self):
"""Checking a undo/redo create_array."""
for i in range(self.nobjects):
# Create a new array
self.fileh.create_array('/', 'array' + str(i), self.a1)
# Put a mark
self.fileh.mark()
# Unwind all marks sequentially
for i in range(self.niter):
t1 = time()
for i in range(self.nobjects):
self.fileh.undo()
if verbose:
print("u", end=' ')
if verbose:
print()
undo = time() - t1
# Rewind all marks sequentially
t1 = time()
for i in range(self.nobjects):
self.fileh.redo()
if verbose:
print("r", end=' ')
if verbose:
print()
redo = time() - t1
print("Time for Undo, Redo (createNode):", undo, "s, ", redo, "s")
def copy_children(self):
"""Checking a undo/redo copy_children."""
# Create a group
self.fileh.create_group('/', 'agroup')
# Create several objects there
for i in range(10):
# Create a new array
self.fileh.create_array('/agroup', 'array' + str(i), self.a1)
# Excercise copy_children
for i in range(self.nobjects):
# Create another group for destination
self.fileh.create_group('/', 'anothergroup' + str(i))
# Copy children from /agroup to /anothergroup+i
self.fileh.copy_children('/agroup', '/anothergroup' + str(i))
# Put a mark
self.fileh.mark()
# Unwind all marks sequentially
for i in range(self.niter):
t1 = time()
for i in range(self.nobjects):
self.fileh.undo()
if verbose:
print("u", end=' ')
if verbose:
print()
undo = time() - t1
# Rewind all marks sequentially
t1 = time()
for i in range(self.nobjects):
self.fileh.redo()
if verbose:
print("r", end=' ')
if verbose:
print()
redo = time() - t1
print(("Time for Undo, Redo (copy_children):", undo, "s, ",
redo, "s"))
def set_attr(self):
"""Checking a undo/redo for setting attributes."""
# Create a new array
self.fileh.create_array('/', 'array', self.a1)
for i in range(self.nobjects):
# Set an attribute
setattr(self.fileh.root.array.attrs, "attr" + str(i), str(self.a1))
# Put a mark
self.fileh.mark()
# Unwind all marks sequentially
for i in range(self.niter):
t1 = time()
for i in range(self.nobjects):
self.fileh.undo()
if verbose:
print("u", end=' ')
if verbose:
print()
undo = time() - t1
# Rewind all marks sequentially
t1 = time()
for i in range(self.nobjects):
self.fileh.redo()
if verbose:
print("r", end=' ')
if verbose:
print()
redo = time() - t1
print("Time for Undo, Redo (set_attr):", undo, "s, ", redo, "s")
def runall(self):
if testname == "all":
tests = [self.createNode, self.copy_children, self.set_attr]
elif testname == "createNode":
tests = [self.createNode]
elif testname == "copy_children":
tests = [self.copy_children]
elif testname == "set_attr":
tests = [self.set_attr]
for meth in tests:
self.setUp()
meth()
self.tearDown()
if __name__ == '__main__':
import sys
import getopt
usage = """usage: %s [-v] [-p] [-t test] [-s vecsize] [-n niter] datafile
-v verbose (total dump of profiling)
-p do profiling
-t {createNode|copy_children|set_attr|all} run the specified test
-s the size of vectors that are undone/redone
-n number of objects in operations
-i number of iterations for reading\n""" % sys.argv[0]
try:
opts, pargs = getopt.getopt(sys.argv[1:], 'vpt:s:n:i:')
except:
sys.stderr.write(usage)
sys.exit(0)
# if we pass too much parameters, abort
if len(pargs) != 1:
sys.stderr.write(usage)
sys.exit(0)
# default options
verbose = 0
profile = 0
testname = "all"
vecsize = 10
nobjects = 1
niter = 1
# Get the options
for option in opts:
if option[0] == '-v':
verbose = 1
elif option[0] == '-p':
profile = 1
elif option[0] == '-t':
testname = option[1]
if testname not in ['createNode', 'copy_children', 'set_attr',
'all']:
sys.stderr.write(usage)
sys.exit(0)
elif option[0] == '-s':
vecsize = int(option[1])
elif option[0] == '-n':
nobjects = int(option[1])
elif option[0] == '-i':
niter = int(option[1])
filename = pargs[0]
bench = BasicBenchmark(filename, testname, vecsize, nobjects, niter)
if profile:
import hotshot
import hotshot.stats
prof = hotshot.Profile("do_undo.prof")
prof.runcall(bench.runall)
prof.close()
stats = hotshot.stats.load("do_undo.prof")
stats.strip_dirs()
stats.sort_stats('time', 'calls')
if verbose:
stats.print_stats()
else:
stats.print_stats(20)
else:
bench.runall()
# Local Variables:
# mode: python
# End:
|