3 # Copyright 2004-2006 Zuza Software Foundation
5 # This file is part of translate.
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.
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.
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
21 from Pootle
import pootlefile
22 from Pootle
import projects
23 from Pootle
import potree
24 from Pootle
import pootle
25 from Pootle
import users
26 from translate
.storage
import po
27 from translate
.search
import indexer
32 class PootleBenchmarker
:
33 """class to aid in benchmarking pootle"""
34 StoreClass
= pootlefile
.pootlefile
35 UnitClass
= pootlefile
.pootleunit
36 def __init__(self
, test_dir
):
37 """sets up benchmarking on the test directory"""
38 self
.test_dir
= os
.path
.abspath(test_dir
)
39 self
.project_dir
= os
.path
.join(self
.test_dir
, "benchmark")
40 self
.po_dir
= os
.path
.join(self
.project_dir
, "zxx")
42 def clear_test_dir(self
):
43 """removes the given directory"""
44 if os
.path
.exists(self
.test_dir
):
45 for dirpath
, subdirs
, filenames
in os
.walk(self
.test_dir
, topdown
=False):
46 for name
in filenames
:
47 os
.remove(os
.path
.join(dirpath
, name
))
49 os
.rmdir(os
.path
.join(dirpath
, name
))
50 if os
.path
.exists(self
.test_dir
): os
.rmdir(self
.test_dir
)
51 assert not os
.path
.exists(self
.test_dir
)
53 def setup_server(self
):
54 """gets a pootle server"""
55 cwd
= os
.path
.abspath(os
.path
.curdir
)
56 parser
= pootle
.PootleOptionParser()
57 prefsfile
= os
.path
.join(self
.test_dir
, "pootle.prefs")
59 importmodules.pootleserver = 'Pootle.pootle'
61 serverclass = pootleserver.PootleServer
64 userprefs = "users.prefs"
67 fullname = "Benchmark"
68 description = "Benchmark auto-created files"
69 checkstyle = "standard"
71 languages.zxx.fullname = "Test Language"
73 open(prefsfile
, "w").write(pootleprefsstr
)
74 userprefsfile
= os
.path
.join(self
.test_dir
, "users.prefs")
75 open(userprefsfile
, "w").write("testuser.activated=1\ntestuser.passwdhash = 'dd82c1882969461de74b46427961ea2c'\n")
76 options
, args
= parser
.parse_args(["--prefsfile=%s" % prefsfile
])
77 options
.servertype
= "dummy"
78 self
.server
= parser
.getserver(options
)
82 def get_session(self
):
83 """gets a new session object"""
84 return users
.PootleSession(self
.server
.sessioncache
, self
.server
)
86 def create_sample_files(self
, num_dirs
, files_per_dir
, strings_per_file
, source_words_per_string
, target_words_per_string
):
87 """creates sample files for benchmarking"""
88 if not os
.path
.exists(self
.test_dir
):
89 os
.mkdir(self
.test_dir
)
90 if not os
.path
.exists(self
.project_dir
):
91 os
.mkdir(self
.project_dir
)
92 if not os
.path
.exists(self
.po_dir
):
94 for dirnum
in range(num_dirs
):
96 dirname
= os
.path
.join(self
.po_dir
, "sample_%d" % dirnum
)
97 if not os
.path
.exists(dirname
):
100 dirname
= self
.po_dir
101 for filenum
in range(files_per_dir
):
102 sample_file
= self
.StoreClass(pofilename
=os
.path
.join(dirname
, "file_%d.po" % filenum
))
103 for stringnum
in range(strings_per_file
):
104 source_string
= " ".join(["word%d" % i
for i
in range(source_words_per_string
)])
105 sample_unit
= sample_file
.addsourceunit(source_string
)
106 sample_unit
.target
= " ".join(["drow%d" % i
for i
in range(target_words_per_string
)])
107 sample_file
.savepofile()
109 def parse_po_files(self
):
110 """parses all the po files in the test directory into memory"""
112 for dirpath
, subdirs
, filenames
in os
.walk(self
.po_dir
, topdown
=False):
113 for name
in filenames
:
114 pofilename
= os
.path
.join(dirpath
, name
)
115 parsedfile
= po
.pofile(open(pofilename
, 'r'))
116 count
+= len(parsedfile
.units
)
117 print "counted %d units" % count
119 def parse_and_create_stats(self
):
120 """parses all the po files in the test directory into memory, using pootlefile, which creates Stats"""
122 indexer
.HAVE_INDEXER
= False
123 for dirpath
, subdirs
, filenames
in os
.walk(self
.po_dir
, topdown
=False):
124 for name
in filenames
:
125 pofilename
= os
.path
.join(dirpath
, name
)
126 parsedfile
= pootlefile
.pootlefile(pofilename
=pofilename
)
127 count
+= len(parsedfile
.units
)
128 print "stats on %d units" % count
130 def parse_and_create_index(self
):
131 """parses all the po files in the test directory into memory, using pootlefile, and allow index creation"""
133 indexer
.HAVE_INDEXER
= True
134 self
.server
.potree
.projectcache
.clear()
135 project
= self
.server
.potree
.getproject("zxx", "benchmark")
136 for name
in project
.browsefiles():
137 count
+= len(project
.getpofile(name
).units
)
138 print "indexed %d units" % count
139 assert os
.path
.exists(os
.path
.join(self
.po_dir
, ".poindex-%s-%s" % (project
.projectcode
, project
.languagecode
)))
141 def generate_main_page(self
):
142 """tests generating the main page"""
143 session
= self
.get_session()
144 page
= self
.server
.getpage(["index.html"], session
, {})
145 print page
.templatevars
147 def generate_projectindex_page(self
):
148 """tests generating the index page for the project"""
149 session
= self
.get_session()
150 assert self
.server
.potree
.haslanguage("zxx")
151 assert self
.server
.potree
.hasproject("zxx", "benchmark")
152 page
= self
.server
.getpage(["zxx", "benchmark"], session
, {})
153 print page
.templatevars
155 def generate_translation_page(self
):
156 """tests generating the translation page for the file"""
157 session
= self
.get_session()
158 page
= self
.server
.getpage(["zxx", "benchmark", "translate.html"], session
, {})
159 print page
.templatevars
161 def submit_translation_change(self
):
162 """tests generating the translation page for the file"""
163 session
= self
.get_session()
164 project
= self
.server
.potree
.getproject("zxx", "benchmark")
165 project
.setrights(None, ["view", "translate"])
166 pofilename
= project
.browsefiles()[0]
167 args
= {"pofilename": pofilename
, "submit0": "true", "trans0": "changed"}
168 page
= self
.server
.getpage(["zxx", "benchmark", "translate.html"], session
, args
)
169 pofile
= project
.getpofile(pofilename
)
170 print str(pofile
.getitem(0))
171 # assert fails because of multistring
172 # assert pofile.getitem(0).unquotedmsgstr == "changed"
173 print page
.templatevars
175 if __name__
== "__main__":
176 for sample_file_sizes
in [
177 # num_dirs, files_per_dir, strings_per_file, source_words_per_string, target_words_per_string
180 # (1, 5, 10, 10, 10),
183 # (5, 10, 100, 20, 20),
184 # (10, 20, 100, 10, 10),
186 benchmarker
= PootleBenchmarker("BenchmarkDir")
187 benchmarker
.clear_test_dir()
188 benchmarker
.create_sample_files(*sample_file_sizes
)
189 benchmarker
.setup_server()
190 methods
= ["parse_po_files", "parse_and_create_stats", "parse_and_create_index",
191 "generate_main_page", "generate_projectindex_page", "generate_translation_page",
192 "submit_translation_change",
194 for methodname
in methods
:
195 print methodname
, "%d dirs, %d files, %d strings, %d/%d words" % sample_file_sizes
196 print "_______________________________________________________"
197 statsfile
= methodname
+ '_%d_%d_%d_%d_%d.stats' % sample_file_sizes
198 profile
.run('benchmarker.%s()' % methodname
, statsfile
)
199 stats
= pstats
.Stats(statsfile
)
200 stats
.sort_stats('cumulative').print_stats(20)
201 print "_______________________________________________________"
202 benchmarker
.clear_test_dir()