/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-07 20:42:15 UTC
  • mto: (1908.4.6 commit-perf)
  • mto: This revision was merged to the branch mainline in revision 1923.
  • Revision ID: john@arbash-meinel.com-20060807204215-91925a46be1a4001
Support caching a committed kernel-like tree, and mark hardlinked trees as readonly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
 
36
36
    _cached_kernel_like_tree = None
37
37
    _cached_kernel_like_added_tree = None
 
38
    _cached_kernel_like_committed_tree = None
38
39
 
39
40
    def make_kernel_like_tree(self, url=None, root='.',
40
41
                              hardlink_working=False):
97
98
                                         'cached_kernel_like_tree')
98
99
            os.mkdir(cache_dir)
99
100
            self._make_kernel_files(root=cache_dir)
 
101
            self._protect_files(cache_dir)
100
102
            Benchmark._cached_kernel_like_tree = cache_dir
101
103
 
102
104
        # Hardlinking the target directory is *much* faster (7s => <1s).
103
105
        osutils.copy_tree(Benchmark._cached_kernel_like_tree, root,
104
106
                          handlers={'file':os.link})
105
107
 
 
108
    def _clone_tree(self, source, dest, link_bzr=False, link_working=True):
 
109
        """Copy the contents from a given location to another location.
 
110
        Optionally hardlink certain pieces of the tree.
 
111
 
 
112
        :param source: The directory to copy
 
113
        :param dest: The destination
 
114
        :param link_bzr: Should the .bzr/ files be hardlinked?
 
115
        :param link_working: Should the working tree be hardlinked?
 
116
        """
 
117
        # We use shutil.copyfile so that we don't copy permissions
 
118
        # because most of our source trees are marked readonly to
 
119
        # prevent modifying in the case of hardlinks
 
120
        handlers = {'file':shutil.copyfile}
 
121
        if osutils.hardlinks_good():
 
122
            if link_working:
 
123
                if link_bzr:
 
124
                    handlers = {'file':os.link}
 
125
                else:
 
126
                    # Don't hardlink files inside bzr
 
127
                    def file_handler(source, dest):
 
128
                        if '.bzr/' in source:
 
129
                            shutil.copyfile(source, dest)
 
130
                        else:
 
131
                            os.link(source, dest)
 
132
                    handlers = {'file':file_handler}
 
133
            elif link_bzr:
 
134
                # Only link files inside .bzr/
 
135
                def file_handler(source, dest):
 
136
                    if '.bzr/' in source:
 
137
                        os.link(source, dest)
 
138
                    else:
 
139
                        shutil.copyfile(source, dest)
 
140
                handlers = {'file':file_handler}
 
141
        osutils.copy_tree(source, dest, handlers=handlers)
 
142
 
 
143
    def _protect_files(self, root):
 
144
        """Chmod all files underneath 'root' to prevent writing
 
145
 
 
146
        :param root: The base directory to modify
 
147
        """
 
148
        for dirinfo, entries in osutils.walkdirs(root):
 
149
            for relpath, name, kind, st, abspath in entries:
 
150
                if kind == 'file':
 
151
                    os.chmod(abspath, 0440)
 
152
 
106
153
    def make_kernel_like_added_tree(self, root='.',
107
154
                                    hardlink_working=True):
108
155
        """Make a kernel like tree, with all files added
112
159
            files, just hardlink them to the cached files. Tests can unlink
113
160
            files that they will change.
114
161
        """
 
162
        # There isn't much underneath .bzr, so we don't support hardlinking
 
163
        # it. Testing showed there wasn't much gain, and there is potentially
 
164
        # a problem if someone modifies something underneath us.
115
165
        if Benchmark._cached_kernel_like_added_tree is None:
116
166
            cache_dir = osutils.pathjoin(self.TEST_ROOT,
117
167
                                         'cached_kernel_like_added_tree')
120
170
                                              hardlink_working=True)
121
171
            # Add everything to it
122
172
            add.smart_add_tree(tree, [cache_dir], recurse=True, save=True)
 
173
 
 
174
            self._protect_files(cache_dir+'/.bzr')
123
175
            Benchmark._cached_kernel_like_added_tree = cache_dir
124
176
 
 
177
        self._clone_tree(Benchmark._cached_kernel_like_added_tree, root,
 
178
                         link_working=hardlink_working)
 
179
        return workingtree.WorkingTree.open(root)
 
180
 
 
181
    def make_kernel_like_committed_tree(self, root='.',
 
182
                                    hardlink_working=True,
 
183
                                    hardlink_bzr=False):
 
184
        """Make a kernel like tree, with all files added and committed
 
185
 
 
186
        :param root: Where to create the files
 
187
        :param hardlink_working: Instead of copying all of the working tree
 
188
            files, just hardlink them to the cached files. Tests can unlink
 
189
            files that they will change.
 
190
        :param hardlink_bzr: Hardlink the .bzr directory. For readonly 
 
191
            operations this is safe, and shaves off a lot of setup time
 
192
        """
 
193
        if Benchmark._cached_kernel_like_committed_tree is None:
 
194
            cache_dir = osutils.pathjoin(self.TEST_ROOT,
 
195
                                         'cached_kernel_like_committed_tree')
 
196
            # Get a basic tree with working files
 
197
            tree = self.make_kernel_like_added_tree(root=cache_dir,
 
198
                                                    hardlink_working=True)
 
199
            tree.commit('first post', rev_id='r1')
 
200
 
 
201
            self._protect_files(cache_dir+'/.bzr')
 
202
            Benchmark._cached_kernel_like_committed_tree = cache_dir
 
203
 
125
204
        # Now we have a cached tree, just copy it
126
 
        handlers = {} # Copy all files
127
 
        if osutils.hardlinks_good() and hardlink_working:
128
 
            # Hardlink only working files (not files underneath .bzr)
129
 
            def file_handler(source, dest):
130
 
                if '.bzr/' in source:
131
 
                    shutil.copy2(source, dest)
132
 
                else:
133
 
                    os.link(source, dest)
134
 
            handlers = {'file':file_handler}
135
 
 
136
 
        osutils.copy_tree(Benchmark._cached_kernel_like_added_tree, root,
137
 
                          handlers=handlers)
 
205
        self._clone_tree(Benchmark._cached_kernel_like_committed_tree, root,
 
206
                         link_bzr=hardlink_bzr,
 
207
                         link_working=hardlink_working)
138
208
        return workingtree.WorkingTree.open(root)
139
209
 
140
210
    def make_many_commit_tree(self, directory_name='.'):