/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-15 02:08:38 UTC
  • mto: This revision was merged to the branch mainline in revision 1923.
  • Revision ID: john@arbash-meinel.com-20060815020838-53063d5cf56d9a6e
Hook the bench_bench.py tests up for the new classes

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 
35
35
class Benchmark(ExternalBase):
36
36
 
37
 
    CACHE_ROOT = None
38
 
 
39
37
    def make_kernel_like_tree(self, url=None, root='.',
40
38
                              link_working=False):
41
39
        """Setup a temporary tree roughly like a kernel tree.
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
48
46
        """
49
 
        if url is not None:
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()
54
 
        else:
55
 
            tree = bzrdir.BzrDir.create_standalone_workingtree(root)
56
 
 
57
 
        self._link_or_copy_kernel_files(root=root, do_link=link_working)
58
 
        return tree
59
 
 
60
 
    def _make_kernel_files(self, root='.'):
61
 
        # a kernel tree has ~10000 and 500 directory, with most files around 
62
 
        # 3-4 levels deep. 
63
 
        # we simulate this by three levels of dirs named 0-7, givin 512 dirs,
64
 
        # and 20 files each.
65
 
        files = []
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)
72
 
                    files.append(prefix)
73
 
                    files.extend([prefix + str(foo) for foo in range(20)])
74
 
        cwd = osutils.getcwd()
75
 
        os.chdir(root)
76
 
        self.build_tree(files)
77
 
        os.chdir(cwd)
78
 
 
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')
82
 
        if cache_dir is None:
83
 
            return None
84
 
        if is_cached:
85
 
            return cache_dir
86
 
        os.mkdir(cache_dir)
87
 
        self._make_kernel_files(root=cache_dir)
88
 
        self._protect_files(cache_dir)
89
 
        return cache_dir
90
 
 
91
 
    def _link_or_copy_kernel_files(self, root, do_link=True):
92
 
        """Hardlink the kernel files from the cached location.
93
 
 
94
 
        If the platform doesn't correctly support hardlinking files, it
95
 
        reverts to just creating new ones.
96
 
        """
97
 
 
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
102
 
            # cp -a takes 3s
103
 
            # using hardlinks takes < 1s.
104
 
            self._make_kernel_files(root=root)
105
 
            return
106
 
 
107
 
        cache_dir = self._cache_kernel_like_tree()
108
 
        if cache_dir is None:
109
 
            self._make_kernel_files(root=root)
110
 
            return
111
 
 
112
 
        # Hardlinking the target directory is *much* faster (7s => <1s).
113
 
        osutils.copy_tree(cache_dir, root,
114
 
                          handlers={'file':os.link})
115
 
 
116
 
    def _create_kernel_like_added_tree(self, root, in_cache=False):
117
 
        """Create a kernel-like tree with the all files added
118
 
 
119
 
        :param root: The root directory to create the files
120
 
        :param in_cache: Is this being created in the cache dir?
121
 
        """
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
126
 
        tree.lock_write()
127
 
        try:
128
 
            add.smart_add_tree(tree, [root], recurse=True, save=True)
129
 
            if in_cache:
130
 
                self._protect_files(root+'/.bzr')
131
 
        finally:
132
 
            tree.unlock()
133
 
        return tree
 
47
        creator = KernelLikeTreeCreator(self, link_working=link_working,
 
48
                                        url=url)
 
49
        return creator.create(root=root)
134
50
 
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.
147
63
        """
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:
153
 
            # Not caching
154
 
            return self._create_kernel_like_added_tree(root, in_cache=False)
155
 
 
156
 
        if not is_cached:
157
 
            self._create_kernel_like_added_tree(root=cache_dir,
158
 
                                                in_cache=True)
159
 
 
160
 
        return self._clone_tree(cache_dir, root,
161
 
                                link_working=link_working,
162
 
                                hot_cache=hot_cache)
163
 
 
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')
171
 
 
172
 
        if in_cache:
173
 
            self._protect_files(root+'/.bzr')
174
 
        return tree
 
64
        creator = KernelLikeAddedTreeCreator(self, link_working=link_working,
 
65
                                             hot_cache=hot_cache)
 
66
        return creator.create(root=root)
175
67
 
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
188
80
        """
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,
192
 
                                                           in_cache=False)
193
 
 
194
 
        if not is_cached:
195
 
            self._create_kernel_like_committed_tree(root=cache_dir,
196
 
                                                    in_cache=True)
197
 
 
198
 
        # Now we have a cached tree, just copy it
199
 
        return self._clone_tree(cache_dir, root,
200
 
                                link_bzr=link_bzr,
201
 
                                link_working=link_working,
202
 
                                hot_cache=hot_cache)
 
81
        creator = KernelLikeCommittedTreeCreator(self,
 
82
                                                 link_working=link_working,
 
83
                                                 link_bzr=link_bzr,
 
84
                                                 hot_cache=hot_cache)
 
85
        return creator.create(root=root)
203
86
 
204
87
    def _create_many_commit_tree(self, root, in_cache=False):
205
88
        tree = bzrdir.BzrDir.create_standalone_workingtree(root)
293
176
 
294
177
    CACHE_ROOT = None
295
178
 
296
 
    def __init__(self, tree_name, link_bzr=False, link_working=False,
 
179
    def __init__(self, test, tree_name,
 
180
                 link_bzr=False,
 
181
                 link_working=False,
297
182
                 hot_cache=True):
298
183
        """Instantiate a new creator object, supply the id of the tree"""
299
184
 
 
185
        self._cache_root = TreeCreator.CACHE_ROOT
 
186
        self._test = test
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
304
193
 
305
 
    @staticmethod
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
309
197
 
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:
314
202
            return False
315
 
        return os.path.exists(cache_dir):
 
203
        return os.path.exists(cache_dir)
316
204
        
 
205
    def disable_cache(self):
 
206
        """Do not use the cache"""
 
207
        self._cache_root = None
 
208
 
 
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:
 
213
            return
 
214
 
 
215
        if not self.is_cached():
 
216
            self._create_tree(root=cache_dir, in_cache=True)
 
217
 
317
218
    def create(self, root):
318
219
        """Create a new tree at 'root'.
319
220
 
324
225
            # Not caching
325
226
            return self._create_tree(root, in_cache=False)
326
227
 
327
 
        if not self.is_cached():
328
 
            self._create_tree(root=cache_dir, in_cache=True)
 
228
        self.ensure_cached()
329
229
 
330
230
        return self._clone_cached_tree(root)
331
231
 
334
234
 
335
235
        :return: The path to use for caching. If None, caching is disabled
336
236
        """
337
 
        if self.CACHE_ROOT is None:
 
237
        if self._cache_root is None:
338
238
            return None
339
 
        return osutils.pathjoin(self.CACHE_ROOT, extra)
 
239
        return osutils.pathjoin(self._cache_root, self._tree_name)
340
240
 
341
241
    def _create_tree(self, root, in_cache=False):
342
242
        """Create the desired tree in the given location.
412
312
                    os.chmod(abspath, 0440)
413
313
 
414
314
 
 
315
class KernelLikeTreeCreator(TreeCreator):
 
316
    """Create a basic tree with ~10k unversioned files""" 
 
317
 
 
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,
 
322
            link_bzr=False)
 
323
 
 
324
        self._url = url
 
325
 
 
326
    def create(self, root):
 
327
        """Create all the kernel files in the given location.
 
328
 
 
329
        This is overloaded for compatibility reasons.
 
330
        """
 
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()
 
336
        else:
 
337
            tree = bzrdir.BzrDir.create_standalone_workingtree(root)
 
338
 
 
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
 
343
            # cp -a takes 3s
 
344
            # using hardlinks takes < 1s.
 
345
            self._create_tree(root=root, in_cache=False)
 
346
            return tree
 
347
 
 
348
        self.ensure_cached()
 
349
        cache_dir = self._get_cache_dir()
 
350
        osutils.copy_tree(cache_dir, root,
 
351
                          handlers={'file':os.link})
 
352
        return tree
 
353
 
 
354
    def _create_tree(self, root, in_cache=False):
 
355
        # a kernel tree has ~10000 and 500 directory, with most files around 
 
356
        # 3-4 levels deep. 
 
357
        # we simulate this by three levels of dirs named 0-7, givin 512 dirs,
 
358
        # and 20 files each.
 
359
        files = []
 
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)
 
366
                    files.append(prefix)
 
367
                    files.extend([prefix + str(foo) for foo in range(20)])
 
368
        cwd = osutils.getcwd()
 
369
        os.chdir(root)
 
370
        self._test.build_tree(files)
 
371
        os.chdir(cwd)
 
372
        if in_cache:
 
373
            self._protect_files(root)
 
374
 
 
375
 
 
376
class KernelLikeAddedTreeCreator(TreeCreator):
 
377
 
 
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,
 
382
            link_bzr=False,
 
383
            hot_cache=hot_cache)
 
384
 
 
385
    def _create_tree(self, root, in_cache=False):
 
386
        """Create a kernel-like tree with the all files added
 
387
 
 
388
        :param root: The root directory to create the files
 
389
        :param in_cache: Is this being created in the cache dir?
 
390
        """
 
391
        kernel_creator = KernelLikeTreeCreator(self._test,
 
392
                                               link_working=in_cache)
 
393
        tree = kernel_creator.create(root=root)
 
394
 
 
395
        # Add everything to it
 
396
        tree.lock_write()
 
397
        try:
 
398
            add.smart_add_tree(tree, [root], recurse=True, save=True)
 
399
            if in_cache:
 
400
                self._protect_files(root+'/.bzr')
 
401
        finally:
 
402
            tree.unlock()
 
403
        return tree
 
404
 
 
405
 
 
406
class KernelLikeCommittedTreeCreator(TreeCreator):
 
407
 
 
408
    def __init__(self, test, link_working=False, link_bzr=False,
 
409
                 hot_cache=True):
 
410
        super(KernelLikeCommittedTreeCreator, self).__init__(test,
 
411
            tree_name='kernel_like_committed_tree',
 
412
            link_working=link_working,
 
413
            link_bzr=link_bzr,
 
414
            hot_cache=hot_cache)
 
415
 
 
416
    def _create_tree(self, root, in_cache=False):
 
417
        """Create a kernel-like tree with the all files added
 
418
 
 
419
        :param root: The root directory to create the files
 
420
        :param in_cache: Is this being created in the cache dir?
 
421
        """
 
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')
 
427
 
 
428
        if in_cache:
 
429
            self._protect_files(root+'/.bzr')
 
430
        return tree
 
431
 
 
432
 
 
433
 
415
434
def test_suite():
416
435
    """Build and return a TestSuite which contains benchmark tests only."""
417
436
    testmod_names = [ \