3
# (C) 2005 Canonical Ltd
 
 
5
"""Benchmark for basicio
 
 
7
Tries serializing an inventory to basic_io repeatedly.
 
 
16
from timeit import Timer
 
 
17
from tempfile import TemporaryFile, NamedTemporaryFile
 
 
19
from bzrlib.branch import Branch
 
 
20
from bzrlib.xml5 import serializer_v5
 
 
21
from bzrlib.basicio import write_inventory, BasicWriter, \
 
 
23
from bzrlib.inventory import Inventory, InventoryEntry, InventoryFile, ROOT_ID
 
 
25
## b = Branch.open('.')
 
 
26
## inv = b.get_inventory(b.last_revision())
 
 
34
    for i in range(NFILES):
 
 
35
        ie = InventoryFile('%08d-id' % i, '%08d-file' % i, ROOT_ID)
 
 
36
        ie.text_sha1='1212121212121212121212121212121212121212'
 
 
41
inv = make_inventory()
 
 
43
bio_tmp = NamedTemporaryFile()
 
 
44
xml_tmp = NamedTemporaryFile()
 
 
48
    w = BasicWriter(bio_tmp)
 
 
49
    write_inventory(w, inv)
 
 
51
    new_inv = read_inventory(bio_tmp)
 
 
55
    serializer_v5.write_inventory(inv, xml_tmp)
 
 
57
    new_inv = serializer_v5.read_inventory(xml_tmp)
 
 
59
def run_benchmark(function_name, tmp_file):
 
 
60
    t = Timer(function_name + '()', 
 
 
61
              'from __main__ import ' + function_name)
 
 
62
    times = t.repeat(nrepeats, ntimes)
 
 
64
    size = tmp_file.tell()
 
 
65
    print 'wrote inventory to %10s %5d times, each %6d bytes, total %6dkB' \
 
 
66
            % (function_name, ntimes, size, (size * ntimes)>>10), 
 
 
67
    each = (min(times)/ntimes*1000)
 
 
68
    print 'in %.1fms each' % each 
 
 
72
    import hotshot, hotshot.stats
 
 
73
    prof_f = NamedTemporaryFile()
 
 
74
    prof = hotshot.Profile(prof_f.name)
 
 
77
    stats = hotshot.stats.load(prof_f.name)
 
 
79
    stats.sort_stats('time')
 
 
80
    ## XXX: Might like to write to stderr or the trace file instead but
 
 
81
    ## print_stats seems hardcoded to stdout
 
 
84
if '-p' in sys.argv[1:]:
 
 
87
    bio_each = run_benchmark('bio_test', bio_tmp)
 
 
88
    xml_each = run_benchmark('xml_test', xml_tmp)
 
 
89
    print 'so bio is %.1f%% faster' % (100 * ((xml_each / bio_each) - 1))
 
 
91
# make sure it was a fair comparison
 
 
92
# assert 'cElementTree' in sys.modules