Package translate :: Package storage :: Module benchmark

[hide private]
[frames] | no frames]

# Source Code for Module translate.storage.benchmark

      1  #!/usr/bin/env python
      2  #
      3  # Copyright 2004-2006 Zuza Software Foundation
      4  #
      5  # This file is part of translate.
      6  #
      7  # translate is free software; you can redistribute it and/or modify
      8  # it under the terms of the GNU General Public License as published by
      9  # the Free Software Foundation; either version 2 of the License, or
     10  # (at your option) any later version.
     11  #
     12  # translate is distributed in the hope that it will be useful,
     13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  # GNU General Public License for more details.
     16  #
     17  # You should have received a copy of the GNU General Public License
     18  # along with translate; if not, write to the Free Software
     19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     20
     21  from translate.storage import factory
     22  import os
     23  import cProfile
     24  import pstats
     25  import random
     26  import sys
     27
     28 -class TranslateBenchmarker:
     29      """class to aid in benchmarking Translate Toolkit stores"""
     30 -    def __init__(self, test_dir, storeclass):
     31          """sets up benchmarking on the test directory"""
     32          self.test_dir = os.path.abspath(test_dir)
     33          self.StoreClass = storeclass
     34          self.extension = self.StoreClass.Extensions[0]
     35          self.project_dir = os.path.join(self.test_dir, "benchmark")
     36          self.file_dir = os.path.join(self.project_dir, "zxx")
     37
     38 -    def clear_test_dir(self):
     39          """removes the given directory"""
     40          if os.path.exists(self.test_dir):
     41              for dirpath, subdirs, filenames in os.walk(self.test_dir, topdown=False):
     42                  for name in filenames:
     43                      os.remove(os.path.join(dirpath, name))
     44                  for name in subdirs:
     45                      os.rmdir(os.path.join(dirpath, name))
     46          if os.path.exists(self.test_dir): os.rmdir(self.test_dir)
     47          assert not os.path.exists(self.test_dir)
     48
     49 -    def create_sample_files(self, num_dirs, files_per_dir, strings_per_file, source_words_per_string, target_words_per_string):
     50          """creates sample files for benchmarking"""
     51          if not os.path.exists(self.test_dir):
     52              os.mkdir(self.test_dir)
     53          if not os.path.exists(self.project_dir):
     54              os.mkdir(self.project_dir)
     55          if not os.path.exists(self.file_dir):
     56              os.mkdir(self.file_dir)
     57          for dirnum in range(num_dirs):
     58              if num_dirs > 1:
     59                  dirname = os.path.join(self.file_dir, "sample_%d" % dirnum)
     60                  if not os.path.exists(dirname):
     61                      os.mkdir(dirname)
     62              else:
     63                  dirname = self.file_dir
     64              for filenum in range(files_per_dir):
     65                  sample_file = self.StoreClass()
     66                  for stringnum in range(strings_per_file):
     67                      source_string = " ".join(["word%d" % (random.randint(0, strings_per_file) * i) for i in range(source_words_per_string)])
     68                      sample_unit = sample_file.addsourceunit(source_string)
     69                      sample_unit.target = " ".join(["drow%d" % (random.randint(0, strings_per_file) * i) for i in range(target_words_per_string)])
     70                  sample_file.savefile(os.path.join(dirname, "file_%d.%s" % (filenum, self.extension)))
     71
     72 -    def parse_file(self):
     73          """parses all the files in the test directory into memory"""
     74          count = 0
     75          for dirpath, subdirs, filenames in os.walk(self.file_dir, topdown=False):
     76              for name in filenames:
     77                  pofilename = os.path.join(dirpath, name)
     78                  parsedfile = self.StoreClass(open(pofilename, 'r'))
     79                  count += len(parsedfile.units)
     80          print "counted %d units" % count
     81
     82  if __name__ == "__main__":
     83      storetype = "po"
     84      if len(sys.argv) > 1:
     85          storetype = sys.argv[1]
     86      if storetype in factory.classes:
     87          storeclass = factory.classes[storetype]
     88      else:
     89          print "StoreClass: '%s' is not a base class that the class factory can load" % storetype
     90          sys.exit()
     91      for sample_file_sizes in [
     92        # num_dirs, files_per_dir, strings_per_file, source_words_per_string, target_words_per_string
     93        # (1, 1, 2, 2, 2),
     94        (1, 1, 10000, 5, 10),   # Creat 1 very large file with German like ratios or source to target
     95        # (100, 10, 10, 5, 10),   # Create lots of directories and files with smaller then avarage size
     96        # (1, 5, 10, 10, 10),
     97        # (1, 10, 10, 10, 10),
     98        # (5, 10, 10, 10, 10),
     99        # (5, 10, 100, 20, 20),
    100        # (10, 20, 100, 10, 10),
    101        # (10, 20, 100, 10, 10),
    102        # (100, 2, 140, 3, 3),  # OpenOffice.org approximate ratios
    103        ]:
    104          benchmarker = TranslateBenchmarker("BenchmarkDir", storeclass)
    105          benchmarker.clear_test_dir()
    106          benchmarker.create_sample_files(*sample_file_sizes)
    107          methods = [("create_sample_files", "*sample_file_sizes"), ("parse_file", ""), ]
    108          for methodname, methodparam in methods:
    109              print methodname, "%d dirs, %d files, %d strings, %d/%d words" % sample_file_sizes
    110              print "_______________________________________________________"
    111              statsfile = "%s_%s" % (methodname, storetype) + '_%d_%d_%d_%d_%d.stats' % sample_file_sizes
    112              cProfile.run('benchmarker.%s(%s)' % (methodname, methodparam), statsfile)
    113              stats = pstats.Stats(statsfile)
    114              stats.sort_stats('cumulative').print_stats(20)
    115              print "_______________________________________________________"
    116          #benchmarker.clear_test_dir()
    117