~bzr-pqm/bzr/bzr.dev

4371.3.38 by John Arbash Meinel
Add a failing test for handling nodes that are in the same linear chain.
1
#!/usr/bin/env python
2
import random
3
import os
4
import time
5
import sys
6
import optparse
7
from bzrlib import (
8
    branch,
9
    commands,
10
    graph,
11
    ui,
12
    trace,
13
    _known_graph_py,
14
    _known_graph_pyx,
15
    )
16
from bzrlib.ui import text
17
18
p = optparse.OptionParser()
19
p.add_option('--max-combinations', default=500, type=int)
20
p.add_option('--lsprof', default=None, type=str)
21
opts, args = p.parse_args(sys.argv[1:])
22
trace.enable_default_logging()
23
ui.ui_factory = text.TextUIFactory()
24
25
if len(args) >= 1:
26
    b = branch.Branch.open(args[0])
27
else:
28
    b = branch.Branch.open('.')
29
b.lock_read()
30
try:
31
    g = b.repository.get_graph()
32
    parent_map = dict(p for p in g.iter_ancestry([b.last_revision()])
33
                         if p[1] is not None)
34
finally:
35
    b.unlock()
36
37
print 'Found %d nodes' % (len(parent_map),)
38
39
def all_heads_comp(g, combinations):
40
    h = []
41
    pb = ui.ui_factory.nested_progress_bar()
42
    try:
43
        for idx, combo in enumerate(combinations):
44
            if idx & 0x1f == 0:
45
                pb.update('proc', idx, len(combinations))
46
            h.append(g.heads(combo))
47
    finally:
48
        pb.finished()
49
    return h
50
combinations = []
51
# parents = parent_map.keys()
52
# for p1 in parents:
53
#     for p2 in random.sample(parents, 10):
54
#         combinations.append((p1, p2))
55
# Times for random sampling of 10x1150 of bzrtools
56
#   Graph        KnownGraph
57
#   96.1s   vs   25.7s  :)
58
# Times for 500 'merge parents' from bzr.dev
59
#   25.6s   vs   45.0s  :(
60
61
for revision_id, parent_ids in parent_map.iteritems():
62
    if parent_ids is not None and len(parent_ids) > 1:
63
        combinations.append(parent_ids)
64
if opts.max_combinations > 0 and len(combinations) > opts.max_combinations:
65
    combinations = random.sample(combinations, opts.max_combinations)
66
67
print '      %d combinations' % (len(combinations),)
68
t1 = time.clock()
69
known_g = _known_graph_py.KnownGraph(parent_map)
70
if opts.lsprof is not None:
71
    h_known = commands.apply_lsprofiled(opts.lsprof,
72
        all_heads_comp, known_g, combinations)
73
else:
74
    h_known = all_heads_comp(known_g, combinations)
75
t2 = time.clock()
76
print "Known: %.3fs" % (t2-t1,)
77
print "  %s" % (graph._counters,)
78
t1 = time.clock()
79
known_g = _known_graph_pyx.KnownGraph(parent_map)
80
if opts.lsprof is not None:
81
    h_known = commands.apply_lsprofiled(opts.lsprof,
82
        all_heads_comp, known_g, combinations)
83
else:
84
    h_known = all_heads_comp(known_g, combinations)
85
t2 = time.clock()
86
print "Known (pyx): %.3fs" % (t2-t1,)
87
print "  %s" % (graph._counters,)
88
simple_g = graph.Graph(graph.DictParentsProvider(parent_map))
89
graph._counters[1] = 0
90
graph._counters[2] = 0
91
h_simple = all_heads_comp(simple_g, combinations)
92
t3 = time.clock()
93
print "Orig: %.3fs" % (t3-t2,)
94
print "  %s" % (graph._counters,)
95
if h_simple != h_known:
96
    import pdb; pdb.set_trace()
97
print 'ratio: %.3fs' % ((t2-t1) / (t3-t2))