/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/time_graph.py

  • Committer: Jelmer Vernooij
  • Date: 2017-11-11 13:10:32 UTC
  • mto: This revision was merged to the branch mainline in revision 6804.
  • Revision ID: jelmer@jelmer.uk-20171111131032-31lgi8qmvlz8363d
Fix typos.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
import random
 
3
import os
 
4
import time
 
5
import sys
 
6
import optparse
 
7
from breezy import (
 
8
    branch,
 
9
    commands,
 
10
    graph,
 
11
    ui,
 
12
    trace,
 
13
    _known_graph_py,
 
14
    _known_graph_pyx,
 
15
    )
 
16
from breezy.ui import text
 
17
 
 
18
p = optparse.OptionParser()
 
19
p.add_option('--quick', default=False, action='store_true')
 
20
p.add_option('--max-combinations', default=500, type=int)
 
21
p.add_option('--lsprof', default=None, type=str)
 
22
opts, args = p.parse_args(sys.argv[1:])
 
23
 
 
24
trace.enable_default_logging()
 
25
ui.ui_factory = text.TextUIFactory()
 
26
 
 
27
begin = time.clock()
 
28
if len(args) >= 1:
 
29
    b = branch.Branch.open(args[0])
 
30
else:
 
31
    b = branch.Branch.open('.')
 
32
with b.lock_read():
 
33
    g = b.repository.get_graph()
 
34
    parent_map = dict(p for p in g.iter_ancestry([b.last_revision()])
 
35
                         if p[1] is not None)
 
36
end = time.clock()
 
37
 
 
38
print('Found %d nodes, loaded in %.3fs' % (len(parent_map), end - begin))
 
39
 
 
40
def all_heads_comp(g, combinations):
 
41
    h = []
 
42
    pb = ui.ui_factory.nested_progress_bar()
 
43
    try:
 
44
        for idx, combo in enumerate(combinations):
 
45
            if idx & 0x1f == 0:
 
46
                pb.update('proc', idx, len(combinations))
 
47
            h.append(g.heads(combo))
 
48
    finally:
 
49
        pb.finished()
 
50
    return h
 
51
 
 
52
combinations = []
 
53
# parents = parent_map.keys()
 
54
# for p1 in parents:
 
55
#     for p2 in random.sample(parents, 10):
 
56
#         combinations.append((p1, p2))
 
57
# Times for random sampling of 10x1150 of bzrtools
 
58
#   Graph        KnownGraph
 
59
#   96.1s   vs   25.7s  :)
 
60
# Times for 500 'merge parents' from bzr.dev
 
61
#   25.6s   vs   45.0s  :(
 
62
 
 
63
for revision_id, parent_ids in parent_map.iteritems():
 
64
    if parent_ids is not None and len(parent_ids) > 1:
 
65
        combinations.append(parent_ids)
 
66
# The largest portion of the graph that has to be walked for a heads() check
 
67
# combinations = [('john@arbash-meinel.com-20090312021943-tu6tcog48aiujx4s',
 
68
#                  'john@arbash-meinel.com-20090312130552-09xa2xsitf6rilzc')]
 
69
if opts.max_combinations > 0 and len(combinations) > opts.max_combinations:
 
70
    combinations = random.sample(combinations, opts.max_combinations)
 
71
 
 
72
print('      %d combinations' % (len(combinations),))
 
73
 
 
74
def combi_graph(graph_klass, comb):
 
75
    # DEBUG
 
76
    graph._counters[1] = 0
 
77
    graph._counters[2] = 0
 
78
 
 
79
    begin = time.clock()
 
80
    g = graph_klass(parent_map)
 
81
    if opts.lsprof is not None:
 
82
        heads = commands.apply_lsprofiled(opts.lsprof, all_heads_comp, g, comb)
 
83
    else:
 
84
        heads = all_heads_comp(g, comb)
 
85
    end = time.clock()
 
86
    return dict(elapsed=(end - begin), graph=g, heads=heads)
 
87
 
 
88
def report(name, g):
 
89
    print('%s: %.3fs' % (name, g['elapsed']))
 
90
    counters_used = False
 
91
    for c in graph._counters:
 
92
        if c:
 
93
            counters_used = True
 
94
    if counters_used:
 
95
        print('  %s' % (graph._counters,))
 
96
 
 
97
known_python = combi_graph(_known_graph_py.KnownGraph, combinations)
 
98
report('Known', known_python)
 
99
 
 
100
known_pyrex = combi_graph(_known_graph_pyx.KnownGraph, combinations)
 
101
report('Known (pyx)', known_pyrex)
 
102
 
 
103
def _simple_graph(parent_map):
 
104
    return graph.Graph(graph.DictParentsProvider(parent_map))
 
105
 
 
106
if opts.quick:
 
107
    if known_python['heads'] != known_pyrex['heads']:
 
108
        import pdb; pdb.set_trace()
 
109
    print('ratio: %.1f:1 faster' % (
 
110
        known_python['elapsed'] / known_pyrex['elapsed'],))
 
111
else:
 
112
    orig = combi_graph(_simple_graph, combinations)
 
113
    report('Orig', orig)
 
114
 
 
115
    if orig['heads'] != known_pyrex['heads']:
 
116
        import pdb; pdb.set_trace()
 
117
 
 
118
    print('ratio: %.1f:1 faster' % (
 
119
        orig['elapsed'] / known_pyrex['elapsed'],))