38
38
def get_cache_dir(self, extra):
39
"""Get the directory to use for caching the given object."""
39
"""Get the directory to use for caching the given object.
41
:return: (cache_dir, is_cached)
42
cache_dir: The path to use for caching. If None, caching is disabled
43
is_cached: Has the path already been created?
41
45
if Benchmark.CACHE_ROOT is None:
42
Benchmark.CACHE_ROOT = osutils.pathjoin(self.TEST_ROOT, 'CACHE')
43
47
if not os.path.isdir(Benchmark.CACHE_ROOT):
44
48
os.mkdir(Benchmark.CACHE_ROOT)
45
49
cache_dir = osutils.pathjoin(self.CACHE_ROOT, extra)
46
50
return cache_dir, os.path.exists(cache_dir)
48
52
def make_kernel_like_tree(self, url=None, root='.',
49
hardlink_working=False):
50
54
"""Setup a temporary tree roughly like a kernel tree.
52
56
:param url: Creat the kernel like tree as a lightweight checkout
53
57
of a new branch created at url.
54
:param hardlink_working: instead of creating a new copy of all files
58
:param link_working: instead of creating a new copy of all files
55
59
just hardlink the working tree. Tests must request this, because
56
60
they must break links if they want to change the files
179
188
if kind == 'file':
180
189
os.chmod(abspath, 0440)
182
def _cache_kernel_like_added_tree(self):
183
cache_dir, is_cached = self.get_cache_dir('kernel_like_added_tree')
191
def _create_kernel_like_added_tree(self, root, in_cache=False):
192
"""Create a kernel-like tree with the all files added
194
:param root: The root directory to create the files
195
:param in_cache: Is this being created in the cache dir?
187
197
# Get a basic tree with working files
188
tree = self.make_kernel_like_tree(root=cache_dir,
189
hardlink_working=True)
198
tree = self.make_kernel_like_tree(root=root,
199
link_working=in_cache)
190
200
# Add everything to it
191
201
tree.lock_write()
193
add.smart_add_tree(tree, [cache_dir], recurse=True, save=True)
194
self._protect_files(cache_dir+'/.bzr')
203
add.smart_add_tree(tree, [root], recurse=True, save=True)
205
self._protect_files(root+'/.bzr')
200
210
def make_kernel_like_added_tree(self, root='.',
201
hardlink_working=True,
203
213
"""Make a kernel like tree, with all files added
205
215
:param root: Where to create the files
206
:param hardlink_working: Instead of copying all of the working tree
216
:param link_working: Instead of copying all of the working tree
207
217
files, just hardlink them to the cached files. Tests can unlink
208
218
files that they will change.
209
219
:param hot_cache: Run through the newly created tree and make sure
213
223
# There isn't much underneath .bzr, so we don't support hardlinking
214
224
# it. Testing showed there wasn't much gain, and there is potentially
215
225
# a problem if someone modifies something underneath us.
216
cache_dir = self._cache_kernel_like_added_tree()
226
cache_dir, is_cached = self.get_cache_dir('kernel_like_added_tree')
227
if cache_dir is None:
229
return self._create_kernel_like_added_tree(root, in_cache=False)
232
self._create_kernel_like_added_tree(root=cache_dir,
218
235
return self._clone_tree(cache_dir, root,
219
link_working=hardlink_working,
236
link_working=link_working,
220
237
hot_cache=hot_cache)
222
def _cache_kernel_like_committed_tree(self):
223
cache_dir, is_cached = self.get_cache_dir('kernel_like_committed_tree')
239
def _create_kernel_like_committed_tree(self, root, in_cache=False):
240
"""Create a kernel-like tree with all files added and committed"""
227
241
# Get a basic tree with working files
228
tree = self.make_kernel_like_added_tree(root=cache_dir,
229
hardlink_working=True,
242
tree = self.make_kernel_like_added_tree(root=root,
243
link_working=in_cache,
244
hot_cache=(not in_cache))
231
245
tree.commit('first post', rev_id='r1')
233
self._protect_files(cache_dir+'/.bzr')
248
self._protect_files(root+'/.bzr')
236
251
def make_kernel_like_committed_tree(self, root='.',
237
hardlink_working=True,
240
255
"""Make a kernel like tree, with all files added and committed
242
257
:param root: Where to create the files
243
:param hardlink_working: Instead of copying all of the working tree
258
:param link_working: Instead of copying all of the working tree
244
259
files, just hardlink them to the cached files. Tests can unlink
245
260
files that they will change.
246
:param hardlink_bzr: Hardlink the .bzr directory. For readonly
261
:param link_bzr: Hardlink the .bzr directory. For readonly
247
262
operations this is safe, and shaves off a lot of setup time
249
cache_dir = self._cache_kernel_like_committed_tree()
264
cache_dir, is_cached = self.get_cache_dir('kernel_like_committed_tree')
265
if cache_dir is None:
266
return self._create_kernel_like_committed_tree(root=root,
270
self._create_kernel_like_committed_tree(root=cache_dir,
251
273
# Now we have a cached tree, just copy it
252
274
return self._clone_tree(cache_dir, root,
253
link_bzr=hardlink_bzr,
254
link_working=hardlink_working,
276
link_working=link_working,
255
277
hot_cache=hot_cache)
257
def _cache_many_commit_tree(self):
258
cache_dir, is_cached = self.get_cache_dir('many_commit_tree')
262
tree = bzrdir.BzrDir.create_standalone_workingtree(cache_dir)
279
def _create_many_commit_tree(self, root, in_cache=False):
280
tree = bzrdir.BzrDir.create_standalone_workingtree(root)
263
281
tree.lock_write()
264
tree.branch.lock_write()
265
tree.branch.repository.lock_write()
267
283
for i in xrange(1000):
268
284
tree.commit('no-changes commit %d' % i)
272
tree.branch.repository.unlock()
288
self._protect_files(root+'/.bzr')
280
292
def make_many_commit_tree(self, directory_name='.',
284
296
No file changes are included. Not hardlinking the working tree,
285
297
because there are no working tree files.
287
cache_dir = self._cache_many_commit_tree()
299
cache_dir, is_cached = self.get_cache_dir('many_commit_tree')
300
if cache_dir is None:
301
return self._create_many_commit_tree(root=directory_name,
305
self._create_many_commit_tree(root=cache_dir, in_cache=True)
288
307
return self._clone_tree(cache_dir, directory_name,
289
308
link_bzr=hardlink,
292
def _cache_heavily_merged_tree(self):
293
cache_dir, is_cached = self.get_cache_dir('heavily_merged_tree')
311
def _create_heavily_merged_tree(self, root, in_cache=False):
298
313
tree = bzrdir.BzrDir.create_standalone_workingtree(
299
cache_dir + '/tree1')
300
315
tree.lock_write()
302
tree2 = tree.bzrdir.sprout(cache_dir + '/tree2').open_workingtree()
317
tree2 = tree.bzrdir.sprout(root + '/tree2').open_workingtree()
303
318
tree2.lock_write()
305
320
for i in xrange(250):
326
343
tree. Not hardlinking the working tree, because there are no working
329
cache_dir = self._cache_heavily_merged_tree()
346
cache_dir, is_cached = self.get_cache_dir('heavily_merged_tree')
347
if cache_dir is None:
349
return self._create_heavily_merged_tree(root=directory_name,
353
self._create_heavily_merged_tree(root=cache_dir, in_cache=True)
330
355
tree_dir = cache_dir + '/tree1'
331
356
return self._clone_tree(tree_dir, directory_name,
332
357
link_bzr=hardlink,