46
44
just hardlink the working tree. Tests must request this, because
47
45
they must break links if they want to change the files
50
b = bzrdir.BzrDir.create_branch_convenience(url)
51
d = bzrdir.BzrDir.create(root)
52
bzrlib.branch.BranchReferenceFormat().initialize(d, b)
53
tree = d.create_workingtree()
55
tree = bzrdir.BzrDir.create_standalone_workingtree(root)
57
self._link_or_copy_kernel_files(root=root, do_link=link_working)
60
def _make_kernel_files(self, root='.'):
61
# a kernel tree has ~10000 and 500 directory, with most files around
63
# we simulate this by three levels of dirs named 0-7, givin 512 dirs,
66
for outer in range(8):
67
files.append("%s/" % outer)
68
for middle in range(8):
69
files.append("%s/%s/" % (outer, middle))
70
for inner in range(8):
71
prefix = "%s/%s/%s/" % (outer, middle, inner)
73
files.extend([prefix + str(foo) for foo in range(20)])
74
cwd = osutils.getcwd()
76
self.build_tree(files)
79
def _cache_kernel_like_tree(self):
80
"""Create the kernel_like_tree cache dir if it doesn't exist"""
81
cache_dir, is_cached = self.get_cache_dir('kernel_like_tree')
87
self._make_kernel_files(root=cache_dir)
88
self._protect_files(cache_dir)
91
def _link_or_copy_kernel_files(self, root, do_link=True):
92
"""Hardlink the kernel files from the cached location.
94
If the platform doesn't correctly support hardlinking files, it
95
reverts to just creating new ones.
98
if not osutils.hardlinks_good() or not do_link:
99
# Turns out that 'shutil.copytree()' is no faster than
100
# just creating them. Probably the python overhead.
101
# Plain _make_kernel_files takes 5s
103
# using hardlinks takes < 1s.
104
self._make_kernel_files(root=root)
107
cache_dir = self._cache_kernel_like_tree()
108
if cache_dir is None:
109
self._make_kernel_files(root=root)
112
# Hardlinking the target directory is *much* faster (7s => <1s).
113
osutils.copy_tree(cache_dir, root,
114
handlers={'file':os.link})
116
def _create_kernel_like_added_tree(self, root, in_cache=False):
117
"""Create a kernel-like tree with the all files added
119
:param root: The root directory to create the files
120
:param in_cache: Is this being created in the cache dir?
122
# Get a basic tree with working files
123
tree = self.make_kernel_like_tree(root=root,
124
link_working=in_cache)
125
# Add everything to it
128
add.smart_add_tree(tree, [root], recurse=True, save=True)
130
self._protect_files(root+'/.bzr')
47
creator = KernelLikeTreeCreator(self, link_working=link_working,
49
return creator.create(root=root)
135
51
def make_kernel_like_added_tree(self, root='.',
136
52
link_working=True,
145
61
the stat-cache is correct. The old way of creating a freshly
146
62
added tree always had a hot cache.
148
# There isn't much underneath .bzr, so we don't support hardlinking
149
# it. Testing showed there wasn't much gain, and there is potentially
150
# a problem if someone modifies something underneath us.
151
cache_dir, is_cached = self.get_cache_dir('kernel_like_added_tree')
152
if cache_dir is None:
154
return self._create_kernel_like_added_tree(root, in_cache=False)
157
self._create_kernel_like_added_tree(root=cache_dir,
160
return self._clone_tree(cache_dir, root,
161
link_working=link_working,
164
def _create_kernel_like_committed_tree(self, root, in_cache=False):
165
"""Create a kernel-like tree with all files added and committed"""
166
# Get a basic tree with working files
167
tree = self.make_kernel_like_added_tree(root=root,
168
link_working=in_cache,
169
hot_cache=(not in_cache))
170
tree.commit('first post', rev_id='r1')
173
self._protect_files(root+'/.bzr')
64
creator = KernelLikeAddedTreeCreator(self, link_working=link_working,
66
return creator.create(root=root)
176
68
def make_kernel_like_committed_tree(self, root='.',
177
69
link_working=True,
186
78
:param link_bzr: Hardlink the .bzr directory. For readonly
187
79
operations this is safe, and shaves off a lot of setup time
189
cache_dir, is_cached = self.get_cache_dir('kernel_like_committed_tree')
190
if cache_dir is None:
191
return self._create_kernel_like_committed_tree(root=root,
195
self._create_kernel_like_committed_tree(root=cache_dir,
198
# Now we have a cached tree, just copy it
199
return self._clone_tree(cache_dir, root,
201
link_working=link_working,
81
creator = KernelLikeCommittedTreeCreator(self,
82
link_working=link_working,
85
return creator.create(root=root)
204
87
def _create_many_commit_tree(self, root, in_cache=False):
205
88
tree = bzrdir.BzrDir.create_standalone_workingtree(root)
294
177
CACHE_ROOT = None
296
def __init__(self, tree_name, link_bzr=False, link_working=False,
179
def __init__(self, test, tree_name,
298
183
"""Instantiate a new creator object, supply the id of the tree"""
185
self._cache_root = TreeCreator.CACHE_ROOT
300
187
self._tree_name = tree_name
301
188
self._link_bzr = link_bzr
302
189
self._link_working = link_working
303
190
self._hot_cache = hot_cache
191
if not osutils.hardlinks_good():
192
self._link_working = self._link_bzr = False
306
def is_caching_enabled():
194
def is_caching_enabled(self):
307
195
"""Will we try to cache the tree we create?"""
308
return TreeCreator.CACHE_ROOT is not None
196
return self._cache_root is not None
310
198
def is_cached(self):
311
199
"""Is this tree already cached?"""
312
200
cache_dir = self._get_cache_dir()
313
201
if cache_dir is None:
315
return os.path.exists(cache_dir):
203
return os.path.exists(cache_dir)
205
def disable_cache(self):
206
"""Do not use the cache"""
207
self._cache_root = None
209
def ensure_cached(self):
210
"""If caching, make sure the cached copy exists"""
211
cache_dir = self._get_cache_dir()
212
if cache_dir is None:
215
if not self.is_cached():
216
self._create_tree(root=cache_dir, in_cache=True)
317
218
def create(self, root):
318
219
"""Create a new tree at 'root'.
412
312
os.chmod(abspath, 0440)
315
class KernelLikeTreeCreator(TreeCreator):
316
"""Create a basic tree with ~10k unversioned files"""
318
def __init__(self, test, link_working=False, url=None):
319
super(KernelLikeTreeCreator, self).__init__(test,
320
tree_name='kernel_like_tree',
321
link_working=link_working,
326
def create(self, root):
327
"""Create all the kernel files in the given location.
329
This is overloaded for compatibility reasons.
331
if self._url is not None:
332
b = bzrdir.BzrDir.create_branch_convenience(self._url)
333
d = bzrdir.BzrDir.create(root)
334
bzrlib.branch.BranchReferenceFormat().initialize(d, b)
335
tree = d.create_workingtree()
337
tree = bzrdir.BzrDir.create_standalone_workingtree(root)
339
if not self._link_working or not self.is_caching_enabled():
340
# Turns out that 'shutil.copytree()' is no faster than
341
# just creating them. Probably the python overhead.
342
# Plain _make_kernel_files takes 3-5s
344
# using hardlinks takes < 1s.
345
self._create_tree(root=root, in_cache=False)
349
cache_dir = self._get_cache_dir()
350
osutils.copy_tree(cache_dir, root,
351
handlers={'file':os.link})
354
def _create_tree(self, root, in_cache=False):
355
# a kernel tree has ~10000 and 500 directory, with most files around
357
# we simulate this by three levels of dirs named 0-7, givin 512 dirs,
360
for outer in range(8):
361
files.append("%s/" % outer)
362
for middle in range(8):
363
files.append("%s/%s/" % (outer, middle))
364
for inner in range(8):
365
prefix = "%s/%s/%s/" % (outer, middle, inner)
367
files.extend([prefix + str(foo) for foo in range(20)])
368
cwd = osutils.getcwd()
370
self._test.build_tree(files)
373
self._protect_files(root)
376
class KernelLikeAddedTreeCreator(TreeCreator):
378
def __init__(self, test, link_working=False, hot_cache=True):
379
super(KernelLikeAddedTreeCreator, self).__init__(test,
380
tree_name='kernel_like_added_tree',
381
link_working=link_working,
385
def _create_tree(self, root, in_cache=False):
386
"""Create a kernel-like tree with the all files added
388
:param root: The root directory to create the files
389
:param in_cache: Is this being created in the cache dir?
391
kernel_creator = KernelLikeTreeCreator(self._test,
392
link_working=in_cache)
393
tree = kernel_creator.create(root=root)
395
# Add everything to it
398
add.smart_add_tree(tree, [root], recurse=True, save=True)
400
self._protect_files(root+'/.bzr')
406
class KernelLikeCommittedTreeCreator(TreeCreator):
408
def __init__(self, test, link_working=False, link_bzr=False,
410
super(KernelLikeCommittedTreeCreator, self).__init__(test,
411
tree_name='kernel_like_committed_tree',
412
link_working=link_working,
416
def _create_tree(self, root, in_cache=False):
417
"""Create a kernel-like tree with the all files added
419
:param root: The root directory to create the files
420
:param in_cache: Is this being created in the cache dir?
422
kernel_creator = KernelLikeAddedTreeCreator(self._test,
423
link_working=in_cache,
424
hot_cache=(not in_cache))
425
tree = kernel_creator.create(root=root)
426
tree.commit('first post', rev_id='r1')
429
self._protect_files(root+'/.bzr')
415
434
def test_suite():
416
435
"""Build and return a TestSuite which contains benchmark tests only."""
417
436
testmod_names = [ \