~bzr-pqm/bzr/bzr.dev

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
#! /usr/bin/env python

# (C) 2005 Canonical Ltd

"""Benchmark for basicio

Tries serializing an inventory to basic_io repeatedly.
"""

if True:
    import psyco
    psyco.full()


import sys
from timeit import Timer
from tempfile import TemporaryFile, NamedTemporaryFile

from bzrlib.branch import Branch
from bzrlib.xml5 import serializer_v5
from bzrlib.basicio import write_inventory, BasicWriter, \
        read_inventory
from bzrlib.inventory import Inventory, InventoryEntry, InventoryFile, ROOT_ID

## b = Branch.open('.')
## inv = b.get_inventory(b.last_revision())

nrepeats = 3
ntimes = 5
NFILES = 30000

def make_inventory():
    inv = Inventory()
    for i in range(NFILES):
        ie = InventoryFile('%08d-id' % i, '%08d-file' % i, ROOT_ID)
        ie.text_sha1='1212121212121212121212121212121212121212'
        ie.text_size=12312
        inv.add(ie)
    return inv

inv = make_inventory()

bio_tmp = NamedTemporaryFile()
xml_tmp = NamedTemporaryFile()

def bio_test():
    bio_tmp.seek(0)
    w = BasicWriter(bio_tmp)
    write_inventory(w, inv)
    bio_tmp.seek(0)
    new_inv = read_inventory(bio_tmp)

def xml_test():
    xml_tmp.seek(0)
    serializer_v5.write_inventory(inv, xml_tmp)
    xml_tmp.seek(0)
    new_inv = serializer_v5.read_inventory(xml_tmp)

def run_benchmark(function_name, tmp_file):
    t = Timer(function_name + '()', 
              'from __main__ import ' + function_name)
    times = t.repeat(nrepeats, ntimes)
    tmp_file.seek(0, 2)
    size = tmp_file.tell()
    print 'wrote inventory to %10s %5d times, each %6d bytes, total %6dkB' \
            % (function_name, ntimes, size, (size * ntimes)>>10), 
    each = (min(times)/ntimes*1000)
    print 'in %.1fms each' % each 
    return each

def profileit(fn): 
    import hotshot, hotshot.stats
    prof_f = NamedTemporaryFile()
    prof = hotshot.Profile(prof_f.name)
    prof.runcall(fn) 
    prof.close()
    stats = hotshot.stats.load(prof_f.name)
    #stats.strip_dirs()
    stats.sort_stats('time')
    ## XXX: Might like to write to stderr or the trace file instead but
    ## print_stats seems hardcoded to stdout
    stats.print_stats(20)

if '-p' in sys.argv[1:]:
    profileit(bio_test)
else:
    bio_each = run_benchmark('bio_test', bio_tmp)
    xml_each = run_benchmark('xml_test', xml_tmp)
    print 'so bio is %.1f%% faster' % (100 * ((xml_each / bio_each) - 1))

# make sure it was a fair comparison
# assert 'cElementTree' in sys.modules