~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/benchmarks/bench_dirstate.py

  • Committer: John Arbash Meinel
  • Date: 2007-05-07 17:57:01 UTC
  • mto: This revision was merged to the branch mainline in revision 2643.
  • Revision ID: john@arbash-meinel.com-20070507175701-b8c87exjybq31evq
Change the name of cmp_dirblock_strings to cmp_by_dirs
And refactor the test cases so that we test both the python version and the
C version. Also, add benchmarks for both.
It shows that the C version is approx 10x faster.

Show diffs side-by-side

added added

removed removed

Lines of Context:
182
182
            self.checkOffsets(offsets)
183
183
        finally:
184
184
            state.unlock()
 
185
 
 
186
    def create_path_names(self, layout, base=''):
 
187
        """Create a list of paths with auto-generated names.
 
188
 
 
189
        :param layout: A list of [(num_dirs, num_files)] tuples. For each
 
190
            level, the given number of directories will be created, each
 
191
            containing that many files.
 
192
            So [(2, 5), (3, 4)] will create 2 top level directories, containing
 
193
            5 files, and each top level directory will contain 3 subdirs with 4
 
194
            files.
 
195
        :param base: The base path to prepend to all entries, most callers will
 
196
            pass ''
 
197
        :return: A list of path names.
 
198
        """
 
199
        if not layout:
 
200
            return []
 
201
 
 
202
        paths = []
 
203
        num_dirs, num_files = layout[0]
 
204
        for dnum in xrange(num_dirs):
 
205
            if base:
 
206
                path = '%s/%02d_directory' % (base, dnum)
 
207
            else:
 
208
                path = '%02d_directory' % (dnum,)
 
209
            paths.append(path)
 
210
            for fnum in xrange(num_files):
 
211
                fname = '%s/%02d_filename' % (path, fnum)
 
212
                paths.append(fname)
 
213
            paths.extend(self.create_path_names(layout[1:], base=path))
 
214
        return paths
 
215
 
 
216
    def test_create_path_names(self):
 
217
        names = self.create_path_names([(2, 3), (1, 2)])
 
218
        self.assertEqual(['00_directory',
 
219
                          '00_directory/00_filename',
 
220
                          '00_directory/01_filename',
 
221
                          '00_directory/02_filename',
 
222
                          '00_directory/00_directory',
 
223
                          '00_directory/00_directory/00_filename',
 
224
                          '00_directory/00_directory/01_filename',
 
225
                          '01_directory',
 
226
                          '01_directory/00_filename',
 
227
                          '01_directory/01_filename',
 
228
                          '01_directory/02_filename',
 
229
                          '01_directory/00_directory',
 
230
                          '01_directory/00_directory/00_filename',
 
231
                          '01_directory/00_directory/01_filename',
 
232
                         ], names)
 
233
        names = self.time(self.create_path_names, [(10, 2), (10, 2), (10, 20)])
 
234
        # 20 files + 1 directory name, 10 times, plus 2 filenames and 1 dir, 10
 
235
        # times, and another 2 files + 1 dir, 10 times
 
236
        self.assertEqual(21330, 10*(3 + 10*(3 + 10*(1 + 20))))
 
237
        self.assertEqual(21330, len(names))
 
238
 
 
239
    def compareAllPaths(self, cmp_func, layout):
 
240
        """Compare N^2 paths.
 
241
 
 
242
        Basically, compare every path in the list against every other path.
 
243
        """
 
244
        paths = self.create_path_names(layout)
 
245
        for path1 in paths:
 
246
            for path2 in paths:
 
247
                cmp_func(path1, path2)
 
248
 
 
249
    def test_py_cmp_dirblock_strings(self):
 
250
        """Benchmark 103041 comparisons."""
 
251
        self.time(self.compareAllPaths, dirstate.py_cmp_by_dirs,
 
252
                                        [(3, 1), (3, 1), (3, 1), (3, 2)])
 
253
 
 
254
    def test_c_cmp_dirblock_strings(self):
 
255
        self.requireFeature(CompiledDirstateHelpersFeature)
 
256
        from bzrlib.compiled.dirstate_helpers import c_cmp_by_dirs
 
257
        self.time(self.compareAllPaths, c_cmp_by_dirs,
 
258
                                        [(3, 1), (3, 1), (3, 1), (3, 2)])