/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to tools/biobench.py

  • Committer: Martin Pool
  • Date: 2005-11-01 21:51:17 UTC
  • mfrom: (1185.16.147)
  • mto: (1185.33.49 bzr.dev)
  • mto: This revision was merged to the branch mainline in revision 1512.
  • Revision ID: mbp@sourcefrog.net-20051101215117-e7c33dde397b2350
[merge] main -> bzr.mbp.basic_io

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# (C) 2005 Canonical Ltd
 
2
 
 
3
"""Benchmark for basicio
 
4
 
 
5
Tries serializing an inventory to basic_io repeatedly.
 
6
"""
 
7
 
 
8
if True:
 
9
    import psyco
 
10
    psyco.full()
 
11
 
 
12
 
 
13
import sys
 
14
from timeit import Timer
 
15
from tempfile import TemporaryFile, NamedTemporaryFile
 
16
 
 
17
from bzrlib.branch import Branch
 
18
from bzrlib.xml5 import serializer_v5
 
19
from bzrlib.basicio import write_inventory, BasicWriter, \
 
20
        read_inventory
 
21
from bzrlib.inventory import Inventory, InventoryEntry, InventoryFile, ROOT_ID
 
22
 
 
23
## b = Branch.open('.')
 
24
## inv = b.get_inventory(b.last_revision())
 
25
 
 
26
nrepeats = 3
 
27
ntimes = 5
 
28
NFILES = 30000
 
29
 
 
30
def make_inventory():
 
31
    inv = Inventory()
 
32
    for i in range(NFILES):
 
33
        ie = InventoryFile('%08d-id' % i, '%08d-file' % i, ROOT_ID)
 
34
        ie.text_sha1='1212121212121212121212121212121212121212'
 
35
        ie.text_size=12312
 
36
        inv.add(ie)
 
37
    return inv
 
38
 
 
39
inv = make_inventory()
 
40
 
 
41
bio_tmp = NamedTemporaryFile()
 
42
xml_tmp = NamedTemporaryFile()
 
43
 
 
44
def bio_test():
 
45
    bio_tmp.seek(0)
 
46
    w = BasicWriter(bio_tmp)
 
47
    write_inventory(w, inv)
 
48
    bio_tmp.seek(0)
 
49
    new_inv = read_inventory(bio_tmp)
 
50
 
 
51
def xml_test():
 
52
    xml_tmp.seek(0)
 
53
    serializer_v5.write_inventory(inv, xml_tmp)
 
54
    xml_tmp.seek(0)
 
55
    new_inv = serializer_v5.read_inventory(xml_tmp)
 
56
 
 
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)
 
61
    tmp_file.seek(0, 2)
 
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 
 
67
    return each
 
68
 
 
69
def profileit(fn): 
 
70
    import hotshot, hotshot.stats
 
71
    prof_f = NamedTemporaryFile()
 
72
    prof = hotshot.Profile(prof_f.name)
 
73
    prof.runcall(fn) 
 
74
    prof.close()
 
75
    stats = hotshot.stats.load(prof_f.name)
 
76
    #stats.strip_dirs()
 
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
 
80
    stats.print_stats(20)
 
81
 
 
82
if '-p' in sys.argv[1:]:
 
83
    profileit(bio_test)
 
84
else:
 
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))
 
88
 
 
89
# make sure it was a fair comparison
 
90
# assert 'cElementTree' in sys.modules