1
# (C) 2005 Canonical Ltd
3
"""Benchmark for basicio
5
Tries serializing an inventory to basic_io repeatedly.
14
from timeit import Timer
15
from tempfile import TemporaryFile, NamedTemporaryFile
17
from bzrlib.branch import Branch
18
from bzrlib.xml5 import serializer_v5
19
from bzrlib.basicio import write_inventory, BasicWriter, \
21
from bzrlib.inventory import Inventory, InventoryEntry, InventoryFile, ROOT_ID
23
## b = Branch.open('.')
24
## inv = b.get_inventory(b.last_revision())
32
for i in range(NFILES):
33
ie = InventoryFile('%08d-id' % i, '%08d-file' % i, ROOT_ID)
34
ie.text_sha1='1212121212121212121212121212121212121212'
39
inv = make_inventory()
41
bio_tmp = NamedTemporaryFile()
42
xml_tmp = NamedTemporaryFile()
46
w = BasicWriter(bio_tmp)
47
write_inventory(w, inv)
49
new_inv = read_inventory(bio_tmp)
53
serializer_v5.write_inventory(inv, xml_tmp)
55
new_inv = serializer_v5.read_inventory(xml_tmp)
57
def run_benchmark(function_name, tmp_file):
58
t = Timer(function_name + '()',
59
'from __main__ import ' + function_name)
60
times = t.repeat(nrepeats, ntimes)
62
size = tmp_file.tell()
63
print 'wrote inventory to %10s %5d times, each %6d bytes, total %6dkB' \
64
% (function_name, ntimes, size, (size * ntimes)>>10),
65
each = (min(times)/ntimes*1000)
66
print 'in %.1fms each' % each
70
import hotshot, hotshot.stats
71
prof_f = NamedTemporaryFile()
72
prof = hotshot.Profile(prof_f.name)
75
stats = hotshot.stats.load(prof_f.name)
77
stats.sort_stats('time')
78
## XXX: Might like to write to stderr or the trace file instead but
79
## print_stats seems hardcoded to stdout
82
if '-p' in sys.argv[1:]:
85
bio_each = run_benchmark('bio_test', bio_tmp)
86
xml_each = run_benchmark('xml_test', xml_tmp)
87
print 'so bio is %.1f%% faster' % (100 * ((xml_each / bio_each) - 1))
89
# make sure it was a fair comparison
90
# assert 'cElementTree' in sys.modules