1
# Copyright (C) 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tree creators for kernel-like trees"""
29
from bzrlib.benchmarks.tree_creator import TreeCreator
32
class KernelLikeTreeCreator(TreeCreator):
33
"""Create a basic tree with ~10k unversioned files"""
35
def __init__(self, test, link_working=False, url=None):
36
super(KernelLikeTreeCreator, self).__init__(test,
37
tree_name='kernel_like_tree',
38
link_working=link_working,
43
def create(self, root):
44
"""Create all the kernel files in the given location.
46
This is overloaded for compatibility reasons.
48
if self._url is not None:
49
b = bzrdir.BzrDir.create_branch_convenience(self._url)
50
d = bzrdir.BzrDir.create(root)
51
bzrlib.branch.BranchReferenceFormat().initialize(d, b)
52
tree = d.create_workingtree()
54
tree = bzrdir.BzrDir.create_standalone_workingtree(root)
56
if not self._link_working or not self.is_caching_enabled():
57
# Turns out that 'shutil.copytree()' is no faster than
58
# just creating them. Probably the python overhead.
59
# Plain _make_kernel_files takes 3-5s
61
# using hardlinks takes < 1s.
62
self._create_tree(root=root, in_cache=False)
66
cache_dir = self._get_cache_dir()
67
osutils.copy_tree(cache_dir, root,
68
handlers={'file':os.link})
71
def _create_tree(self, root, in_cache=False):
72
# a kernel tree has ~10000 and 500 directory, with most files around
74
# we simulate this by three levels of dirs named 0-7, givin 512 dirs,
77
for outer in range(8):
78
files.append("%s/" % outer)
79
for middle in range(8):
80
files.append("%s/%s/" % (outer, middle))
81
for inner in range(8):
82
prefix = "%s/%s/%s/" % (outer, middle, inner)
84
files.extend([prefix + str(foo) for foo in range(20)])
85
cwd = osutils.getcwd()
89
if e.errno not in (errno.EEXIST,):
92
self._test.build_tree(files)
95
self._protect_files(root)
98
class KernelLikeAddedTreeCreator(TreeCreator):
99
"""Create a tree with ~10k versioned but not committed files"""
101
def __init__(self, test, link_working=False, hot_cache=True):
102
super(KernelLikeAddedTreeCreator, self).__init__(test,
103
tree_name='kernel_like_added_tree',
104
link_working=link_working,
108
def _create_tree(self, root, in_cache=False):
109
"""Create a kernel-like tree with the all files added
111
:param root: The root directory to create the files
112
:param in_cache: Is this being created in the cache dir?
114
kernel_creator = KernelLikeTreeCreator(self._test,
115
link_working=in_cache)
116
tree = kernel_creator.create(root=root)
118
# Add everything to it
121
tree.smart_add([root], recurse=True, save=True)
123
self._protect_files(root+'/.bzr')
129
class KernelLikeCommittedTreeCreator(TreeCreator):
130
"""Create a tree with ~10K files, and a single commit adding all of them"""
132
def __init__(self, test, link_working=False, link_bzr=False,
134
super(KernelLikeCommittedTreeCreator, self).__init__(test,
135
tree_name='kernel_like_committed_tree',
136
link_working=link_working,
140
def _create_tree(self, root, in_cache=False):
141
"""Create a kernel-like tree with all files committed
143
:param root: The root directory to create the files
144
:param in_cache: Is this being created in the cache dir?
146
kernel_creator = KernelLikeAddedTreeCreator(self._test,
147
link_working=in_cache,
148
hot_cache=(not in_cache))
149
tree = kernel_creator.create(root=root)
150
tree.commit('first post', rev_id='r1')
153
self._protect_files(root+'/.bzr')
157
class KernelLikeInventoryCreator(TreeCreator):
158
"""Return just the memory representation of a committed kernel-like tree"""
160
def __init__(self, test):
161
super(KernelLikeInventoryCreator, self).__init__(test,
162
tree_name='kernel_like_inventory',
167
def ensure_cached(self):
168
"""Make sure we have a cached version of the kernel-like inventory"""
169
cache_dir = self._get_cache_dir()
170
if cache_dir is None:
176
committed_creator = KernelLikeCommittedTreeCreator(self._test,
180
committed_creator.ensure_cached()
181
committed_cache_dir = committed_creator._get_cache_dir()
182
committed_tree = workingtree.WorkingTree.open(committed_cache_dir)
183
rev_tree = committed_tree.basis_tree()
185
f = open(cache_dir+'/inventory', 'wb')
187
xml5.serializer_v5.write_inventory(rev_tree.inventory, f)
191
def create(self, root=None):
192
"""Create a kernel like inventory
194
:param root: Exists to mimic the base class, but this class
195
returns only an in-memory Inventory, so it should always be None.
196
:return: An Inventory object.
198
cache_dir = self._get_cache_dir()
199
if cache_dir is None:
200
return self._create_and_return()
203
return self._open_cached(cache_dir)
205
def _create_and_return(self):
206
"""Create a kernel-like tree, and return its inventory"""
207
creator = KernelLikeCommittedTreeCreator(self._test,
211
tree = creator.create('.')
212
basis = tree.basis_tree()
215
return basis.inventory
219
def _open_cached(self, cache_dir):
220
f = open(cache_dir + '/inventory', 'rb')
222
return xml5.serializer_v5.read_inventory(f)