/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 bzrlib/benchmarks/bench_bundle.py

  • Committer: Carl Friedrich Bolz
  • Date: 2006-08-11 16:02:36 UTC
  • mto: (1908.3.5 bench_usecases)
  • mto: This revision was merged to the branch mainline in revision 2068.
  • Revision ID: cfbolz@gmx.de-20060811160236-0ce4b4864fbdb46a
(cfbolz, hpk): Add caching mechanism and add benchmark for bundle-reading.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 by Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License version 2 as published by
 
5
# the Free Software Foundation.
 
6
#
 
7
# This program is distributed in the hope that it will be useful,
 
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
# GNU General Public License for more details.
 
11
#
 
12
# You should have received a copy of the GNU General Public License
 
13
# along with this program; if not, write to the Free Software
 
14
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
 
 
16
"""Tests for bzr bundle performance."""
 
17
 
 
18
 
 
19
import os
 
20
import shutil
 
21
from StringIO import StringIO
 
22
 
 
23
from bzrlib.benchmarks import Benchmark
 
24
from bzrlib.workingtree import WorkingTree
 
25
from bzrlib.branch import Branch
 
26
from bzrlib.bundle.serializer import write_bundle
 
27
from bzrlib.bundle import read_bundle
 
28
from bzrlib.revisionspec import RevisionSpec
 
29
 
 
30
# If the CACHEDIR flag is set, make_parametrized_tree below will cache the tree
 
31
# it creates in the .bazaar/temp dir.
 
32
CACHEDIR = os.path.expanduser("~/.bazaar/temp")
 
33
 
 
34
 
 
35
class BundleBenchmark(Benchmark):
 
36
    """
 
37
    The bundle tests should (also) be done at a lower level with
 
38
    direct call to the bzrlib."""
 
39
    
 
40
 
 
41
    def test_create_bundle_known_kernel_like_tree(self):
 
42
        """
 
43
        Create a bundle for a kernel sized tree with no ignored, unknowns,
 
44
        or added and one commit.""" 
 
45
        self.make_kernel_like_tree()
 
46
        self.run_bzr('add')
 
47
        self.run_bzr('commit', '-m', 'initial import')
 
48
        self.time(self.run_bzr, 'bundle', '--revision', '..-1')
 
49
 
 
50
    def test_create_bundle_many_commit_tree (self):
 
51
        """
 
52
        Create a bundle for a tree with many commits but no changes.""" 
 
53
        self.make_many_commit_tree()
 
54
        self.time(self.run_bzr, 'bundle', '--revision', '..-1')
 
55
 
 
56
    def test_create_bundle_heavily_merged_tree(self):
 
57
        """
 
58
        Create a bundle for a heavily merged tree.""" 
 
59
        self.make_heavily_merged_tree()
 
60
        self.time(self.run_bzr, 'bundle', '--revision', '..-1')
 
61
        
 
62
    def test_apply_bundle_known_kernel_like_tree(self):
 
63
        """
 
64
        Create a bundle for a kernel sized tree with no ignored, unknowns,
 
65
        or added and one commit.""" 
 
66
        self.make_kernel_like_tree()
 
67
        self.run_bzr('add')
 
68
        self.run_bzr('commit', '-m', 'initial import')
 
69
        self.run_bzr('branch', '.', '../branch_a')
 
70
        self.run_bzr('bundle', '--revision', '..-1')
 
71
        f = file('../bundle', 'wb')
 
72
        try:
 
73
            f.write(self.run_bzr('bundle', '--revision', '..-1')[0])
 
74
        finally:
 
75
            f.close()
 
76
        os.chdir('../branch_a')
 
77
        self.time(self.run_bzr, 'merge', '../bundle')
 
78
 
 
79
 
 
80
class BundleLibraryLevelBenchmark(Benchmark):
 
81
 
 
82
    def make_parametrized_tree(self, num_files, num_revisions,
 
83
                               num_files_in_bundle):
 
84
        """Create a tree with given parameters. Always creates 2 levels of
 
85
        directories with the given number of files. Then the given number of
 
86
        revisions are created, changing some lines in one files in each
 
87
        revision. Only num_files_in_bundle files are changed in these
 
88
        revisions.
 
89
 
 
90
        :param num_files: number of files in tree
 
91
        :param num_revisions: number of revisions
 
92
        :param num_files_in_bundle: number of files changed in the revisions
 
93
        """
 
94
        if CACHEDIR is None:
 
95
            return self._make_parametrized_tree(num_files, num_revisions,
 
96
                                                num_files_in_bundle)
 
97
        olddir = os.getcwd()
 
98
        try:
 
99
            if not os.path.exists(CACHEDIR):
 
100
                os.makedirs(CACHEDIR)
 
101
            os.chdir(CACHEDIR)
 
102
            cache_name = "make_parametrized_tree_%s_%s_%s" % (
 
103
                num_files, num_revisions, num_files_in_bundle)
 
104
            if not os.path.exists(cache_name):
 
105
                os.mkdir(cache_name)
 
106
                os.chdir(cache_name)
 
107
                self._make_parametrized_tree(num_files, num_revisions,
 
108
                                             num_files_in_bundle)
 
109
                os.chdir(CACHEDIR)
 
110
            for subdir in os.listdir(cache_name):
 
111
                shutil.copytree(os.path.join(cache_name, subdir),
 
112
                                os.path.join(olddir, subdir))
 
113
        finally:
 
114
            os.chdir(olddir)
 
115
 
 
116
    def _make_parametrized_tree(self, num_files, num_revisions,
 
117
                                num_files_in_bundle):
 
118
        # create files
 
119
        directories = []
 
120
        files = []
 
121
        count = 0
 
122
        for outer in range(num_files // 64 + 1):
 
123
            directories.append("%s/" % outer)
 
124
            for middle in range(8):
 
125
                prefix = "%s/%s/" % (outer, middle)
 
126
                directories.append(prefix)
 
127
                for filename in range(min(8, num_files - count)):
 
128
                    count += 1
 
129
                    files.append(prefix + str(filename))
 
130
        self.run_bzr('init')
 
131
        self.build_tree(directories + files)
 
132
        for d in directories:
 
133
            self.run_bzr('add', d)
 
134
        self.run_bzr('commit', '-m', 'initial repo layout')
 
135
        # create revisions
 
136
        affected_files = files[:num_files_in_bundle]
 
137
        count = 0
 
138
        for changes_file in range(num_revisions // num_files_in_bundle + 1):
 
139
            for f in affected_files:
 
140
                count += 1
 
141
                if count >= num_revisions:
 
142
                    break
 
143
                content = "\n".join([str(i) for i in range(changes_file)] +
 
144
                                    [str(changes_file)] * 5) + "\n"
 
145
                self.build_tree_contents([(f, content)])
 
146
                self.run_bzr("commit", '-m', 'some changes')
 
147
        assert count >= num_revisions
 
148
 
 
149
 
 
150
    for treesize, treesize_h in [(5, "small"), (100, "moderate"),
 
151
                                 (1000, "big")]:
 
152
        for bundlefiles, bundlefiles_h in [(5, "few"), (100, "some"),
 
153
                                           (1000, "many")]:
 
154
            if bundlefiles > treesize:
 
155
                continue
 
156
            for num_revisions in [1, 500, 1000]:
 
157
                code = """
 
158
def test_%s_files_%s_tree_%s_revision(self):
 
159
    self.make_parametrized_tree(%s, %s, %s)
 
160
    branch, _ = Branch.open_containing(".")
 
161
    revision_history = branch.revision_history()
 
162
    bundle_text = StringIO()
 
163
    self.time(write_bundle, branch.repository, revision_history[-1],
 
164
              None, bundle_text)
 
165
    bundle_text.seek(0)
 
166
    self.time(read_bundle, bundle_text)""" % (
 
167
                    bundlefiles_h, treesize_h, num_revisions,
 
168
                    treesize, num_revisions, bundlefiles)
 
169
                exec code
 
170