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

  • Committer: John Arbash Meinel
  • Date: 2006-08-14 23:49:01 UTC
  • mto: This revision was merged to the branch mainline in revision 1923.
  • Revision ID: john@arbash-meinel.com-20060814234901-a0e0ee48197703e1
Change caching logic. Don't cache at all without --cache-dir being supplied

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
    CACHE_ROOT = None
37
37
 
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.
40
40
 
 
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?
 
44
        """
41
45
        if Benchmark.CACHE_ROOT is None:
42
 
            Benchmark.CACHE_ROOT = osutils.pathjoin(self.TEST_ROOT, 'CACHE')
 
46
            return None, False
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)
47
51
 
48
52
    def make_kernel_like_tree(self, url=None, root='.',
49
 
                              hardlink_working=False):
 
53
                              link_working=False):
50
54
        """Setup a temporary tree roughly like a kernel tree.
51
55
        
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
57
61
        """
63
67
        else:
64
68
            tree = bzrdir.BzrDir.create_standalone_workingtree(root)
65
69
 
66
 
        self._link_or_copy_kernel_files(root=root, do_link=hardlink_working)
 
70
        self._link_or_copy_kernel_files(root=root, do_link=link_working)
67
71
        return tree
68
72
 
69
73
    def _make_kernel_files(self, root='.'):
88
92
    def _cache_kernel_like_tree(self):
89
93
        """Create the kernel_like_tree cache dir if it doesn't exist"""
90
94
        cache_dir, is_cached = self.get_cache_dir('kernel_like_tree')
 
95
        if cache_dir is None:
 
96
            return None
91
97
        if is_cached:
92
98
            return cache_dir
93
99
        os.mkdir(cache_dir)
112
118
            return
113
119
 
114
120
        cache_dir = self._cache_kernel_like_tree()
 
121
        if cache_dir is None:
 
122
            self._make_kernel_files(root=root)
 
123
            return
115
124
 
116
125
        # Hardlinking the target directory is *much* faster (7s => <1s).
117
126
        osutils.copy_tree(cache_dir, root,
179
188
                if kind == 'file':
180
189
                    os.chmod(abspath, 0440)
181
190
 
182
 
    def _cache_kernel_like_added_tree(self):
183
 
        cache_dir, is_cached = self.get_cache_dir('kernel_like_added_tree')
184
 
        if is_cached:
185
 
            return cache_dir
 
191
    def _create_kernel_like_added_tree(self, root, in_cache=False):
 
192
        """Create a kernel-like tree with the all files added
186
193
 
 
194
        :param root: The root directory to create the files
 
195
        :param in_cache: Is this being created in the cache dir?
 
196
        """
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()
192
202
        try:
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)
 
204
            if in_cache:
 
205
                self._protect_files(root+'/.bzr')
195
206
        finally:
196
207
            tree.unlock()
197
 
 
198
 
        return cache_dir
 
208
        return tree
199
209
 
200
210
    def make_kernel_like_added_tree(self, root='.',
201
 
                                    hardlink_working=True,
 
211
                                    link_working=True,
202
212
                                    hot_cache=True):
203
213
        """Make a kernel like tree, with all files added
204
214
 
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:
 
228
            # Not caching
 
229
            return self._create_kernel_like_added_tree(root, in_cache=False)
 
230
 
 
231
        if not is_cached:
 
232
            self._create_kernel_like_added_tree(root=cache_dir,
 
233
                                                in_cache=True)
217
234
 
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)
221
238
 
222
 
    def _cache_kernel_like_committed_tree(self):
223
 
        cache_dir, is_cached = self.get_cache_dir('kernel_like_committed_tree')
224
 
        if is_cached:
225
 
            return cache_dir
226
 
 
 
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,
230
 
                                                hot_cache=False)
 
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')
232
246
 
233
 
        self._protect_files(cache_dir+'/.bzr')
234
 
        return cache_dir
 
247
        if in_cache:
 
248
            self._protect_files(root+'/.bzr')
 
249
        return tree
235
250
 
236
251
    def make_kernel_like_committed_tree(self, root='.',
237
 
                                    hardlink_working=True,
238
 
                                    hardlink_bzr=False,
 
252
                                    link_working=True,
 
253
                                    link_bzr=False,
239
254
                                    hot_cache=True):
240
255
        """Make a kernel like tree, with all files added and committed
241
256
 
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
248
263
        """
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,
 
267
                                                           in_cache=False)
 
268
 
 
269
        if not is_cached:
 
270
            self._create_kernel_like_committed_tree(root=cache_dir,
 
271
                                                    in_cache=True)
250
272
 
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,
 
275
                                link_bzr=link_bzr,
 
276
                                link_working=link_working,
255
277
                                hot_cache=hot_cache)
256
278
 
257
 
    def _cache_many_commit_tree(self):
258
 
        cache_dir, is_cached = self.get_cache_dir('many_commit_tree')
259
 
        if is_cached:
260
 
            return cache_dir
261
 
 
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()
266
282
        try:
267
283
            for i in xrange(1000):
268
284
                tree.commit('no-changes commit %d' % i)
269
285
        finally:
270
 
            try:
271
 
                try:
272
 
                    tree.branch.repository.unlock()
273
 
                finally:
274
 
                    tree.branch.unlock()
275
 
            finally:
276
 
                tree.unlock()
 
286
            tree.unlock()
 
287
        if in_cache:
 
288
            self._protect_files(root+'/.bzr')
277
289
 
278
 
        return cache_dir
 
290
        return tree
279
291
 
280
292
    def make_many_commit_tree(self, directory_name='.',
281
293
                              hardlink=False):
284
296
        No file changes are included. Not hardlinking the working tree, 
285
297
        because there are no working tree files.
286
298
        """
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,
 
302
                                                 in_cache=False)
 
303
 
 
304
        if not is_cached:
 
305
            self._create_many_commit_tree(root=cache_dir, in_cache=True)
 
306
 
288
307
        return self._clone_tree(cache_dir, directory_name,
289
308
                                link_bzr=hardlink,
290
309
                                hot_cache=True)
291
310
 
292
 
    def _cache_heavily_merged_tree(self):
293
 
        cache_dir, is_cached = self.get_cache_dir('heavily_merged_tree')
294
 
        if is_cached:
295
 
            return cache_dir
296
 
 
297
 
        os.mkdir(cache_dir)
 
311
    def _create_heavily_merged_tree(self, root, in_cache=False):
 
312
        os.mkdir(root)
298
313
        tree = bzrdir.BzrDir.create_standalone_workingtree(
299
 
                cache_dir + '/tree1')
 
314
                root + '/tree1')
300
315
        tree.lock_write()
301
316
        try:
302
 
            tree2 = tree.bzrdir.sprout(cache_dir + '/tree2').open_workingtree()
 
317
            tree2 = tree.bzrdir.sprout(root + '/tree2').open_workingtree()
303
318
            tree2.lock_write()
304
319
            try:
305
320
                for i in xrange(250):
314
329
                tree2.unlock()
315
330
        finally:
316
331
            tree.unlock()
317
 
        return cache_dir
 
332
        if in_cache:
 
333
            self._protect_files(root+'/tree1/.bzr')
 
334
        return tree
318
335
 
319
336
    def make_heavily_merged_tree(self, directory_name='.',
320
337
                                 hardlink=False):
326
343
        tree.  Not hardlinking the working tree, because there are no working 
327
344
        tree files.
328
345
        """
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:
 
348
            # Not caching
 
349
            return self._create_heavily_merged_tree(root=directory_name,
 
350
                                                    in_cache=False)
 
351
 
 
352
        if not is_cached:
 
353
            self._create_heavily_merged_tree(root=cache_dir, in_cache=True)
 
354
 
330
355
        tree_dir = cache_dir + '/tree1'
331
356
        return self._clone_tree(tree_dir, directory_name,
332
357
                                link_bzr=hardlink,