/[escript]/trunk/escript/py_src/benchmark.py
ViewVC logotype

Diff of /trunk/escript/py_src/benchmark.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 364 by gross, Thu Dec 15 04:38:28 2005 UTC revision 385 by gross, Mon Dec 19 06:12:14 2005 UTC
# Line 1  Line 1 
1  # $Id:$  filter# $Id:$
2    
3  #  #
4  #      COPYRIGHT ACcESS 2004 -  All Rights Reserved  #      COPYRIGHT ACcESS 2004 -  All Rights Reserved
# Line 95  class BenchmarkSuite(object): Line 95  class BenchmarkSuite(object):
95         @type scale: C{int} or C{list} of C{int}s.         @type scale: C{int} or C{list} of C{int}s.
96         """         """
97         self.__scale=scale               self.__scale=scale      
98         for i in range(len(self)): self[i].run(scale)         for i in range(len(self)): self[i].run(scale=scale)
99     def getHTML(self,level=1):     def getHTML(self,filter,level=1):
100         """         """
101         returns the results of the last benchmark run in HTML format.         returns the results of the last benchmark run in HTML format.
102    
103           @param filter: filter to be applied to the results
104           @type filter: L{BenchmarkFilter}
105         @param level: level used in header <H?> tags         @param level: level used in header <H?> tags
106         @type level: C{int}         @type level: C{int}
107         @return: HTML document         @return: HTML document
# Line 116  class BenchmarkSuite(object): Line 118  class BenchmarkSuite(object):
118             out+="<p>platform: %s%s</p>\n"%(socket.gethostname(),m)             out+="<p>platform: %s%s</p>\n"%(socket.gethostname(),m)
119         for i in range(len(self)):         for i in range(len(self)):
120             out+="<p>\n"             out+="<p>\n"
121             out+=self[i].getHTML(min(level+1,self.MAX_LEVEL))             out+=self[i].getHTML(filter=filter,level=min(level+1,self.MAX_LEVEL))
122             out+="<p>\n"             out+="<p>\n"
123         if level==1:         if level==1:
124             out+="<hr><p align=\"center\">by %s at %s</p>\n"%(os.getlogin(),time.strftime('%X %x %Z'))             out+="<hr><p align=\"center\">by %s at %s</p>\n"%(os.getlogin(),time.strftime('%X %x %Z'))
# Line 189  class Benchmark(object): Line 191  class Benchmark(object):
191         if isinstance(scale,list):         if isinstance(scale,list):
192             if len(scale)<len(self.__problems):             if len(scale)<len(self.__problems):
193                raise ValueError,"scale list is too small. must be greater or equal to the number of problems in the benchmark"                raise ValueError,"scale list is too small. must be greater or equal to the number of problems in the benchmark"
194           self.__filter=filter
195         self.__scale=scale         self.__scale=scale
196         self.__results=[]         self.__results=[]
197         c=0         c=0
# Line 199  class Benchmark(object): Line 202  class Benchmark(object):
202               s=scale               s=scale
203            row=[]            row=[]
204            for p in self.__options:            for p in self.__options:
205                os.putenv("OMP_NUM_TREADS",str(s))                os.environ['OMP_NUM_TREADS']=str(s)
206                row.append(r.run(p))                row.append(r.run(p))
207            self.__results.append(row)            self.__results.append(row)
208            c+=1            c+=1
209     def getHTML(self,level=1):     def getHTML(self,filter,level=1):
210         """         """
211         returns the results of the last benchmark run in HTML format.         returns the results of the last benchmark run in HTML format.
212    
213           @param filter: filter to be applied to the results
214           @type filter: L{BenchmarkFilter}
215         @param level: level used in header <H?> tags         @param level: level used in header <H?> tags
216         @type level: C{int}         @type level: C{int}
217         @return: HTML document         @return: HTML document
# Line 225  class Benchmark(object): Line 230  class Benchmark(object):
230         if len(self.__problems)>0:         if len(self.__problems)>0:
231            out+="<TABLE ALIGN=\"center\" BORDER=3 CELLPADDING=5 CELLSPACING=1>\n"            out+="<TABLE ALIGN=\"center\" BORDER=3 CELLPADDING=5 CELLSPACING=1>\n"
232            h1_seg=""            h1_seg=""
233            rn=self.__problems[0].getResultNames()            rn=filter.getResultNames()
234            if len(rn)==0:            if len(rn)==0:
235               h1_seg+="<TD></TD>"               h1_seg+="<TD></TD>"
236            else:            else:
# Line 247  class Benchmark(object): Line 252  class Benchmark(object):
252            for r in range(len(self.__results)):            for r in range(len(self.__results)):
253               out+="<TR><TH ALIGN=\"right\">%s</TH>"%str(self.__problems[r])               out+="<TR><TH ALIGN=\"right\">%s</TH>"%str(self.__problems[r])
254               if isinstance(self.__scale,list): out+="<TD ALIGN=\"right\">%s</TD>"%self.__scale[c]               if isinstance(self.__scale,list): out+="<TD ALIGN=\"right\">%s</TD>"%self.__scale[c]
255               for col in self.__results[r]:               for col in self.__results[r]:
256                     for e in col: out+="<TD ALIGN=\"right\">%s</TD>"%str(e)                     for e in filter(col): out+="<TD ALIGN=\"right\">%s</TD>"%e
257               out+="</TR>\n"               out+="</TR>\n"
258                 c+=1
259            out+="</TABLE>"            out+="</TABLE>"
           c+=1  
260         if level==1:         if level==1:
261            out+="<hr><p align=\"center\">by %s at %s</p>\n"%(os.getlogin(),time.strftime('%X %x %Z'))            out+="<hr><p align=\"center\">by %s at %s</p>\n"%(os.getlogin(),time.strftime('%X %x %Z'))
262            out+="</BODY></HTML>\n"            out+="</BODY></HTML>\n"
# Line 274  class BenchmarkProblem(object): Line 279  class BenchmarkProblem(object):
279         else:         else:
280            self.__name=name            self.__name=name
281    
    def getResultNames(self):  
        """  
        return the names of the results produced when run() is called.  
         
        @return: names the list of the names to be used when the results of the run() call are printed  
        @rtype: C{list} of C{str}  
        @remark: this function has to overwritten by a particular problem  
        """  
        raise NotImplementedError  
        return []  
282                
283     def __str__(self):     def __str__(self):
284         """         """
# Line 302  class BenchmarkProblem(object): Line 297  class BenchmarkProblem(object):
297         @param options: the options that are used for the run. Note that the number of OpenMP threads is controlled         @param options: the options that are used for the run. Note that the number of OpenMP threads is controlled
298                         by the L{Benchmark} the problem is run in.                         by the L{Benchmark} the problem is run in.
299         @type options: L{Options}         @type options: L{Options}
300         @return: list of run characteristics         @return: run characteristics
301         @rtype: C{list}         @rtype: any type that can be read by the L{BenchmarkFilter} applied to it.
302         @remark: this function has to overwritten by a particular problem         @remark: this function has to overwritten by a particular problem
303         """         """
304         raise NotImplementedError         raise NotImplementedError
305         return []         return []
306            
307    class BenchmarkFilter(object):
308       """
309       object to filter the characteristcs returned by Bechmark runs.
310      
311       """
312       def __init__(self):
313           """
314           sets up a filter
315           """
316           pass
317    
318    
319       def getResultNames(self):
320           """
321           return the names of the results produced when run() is called.
322          
323           @return: names the list of the names to be used when the results of the run() call are printed
324           @rtype: C{list} of C{str}
325           @remark: this function has to overwritten by a particular problem
326           """
327           raise NotImplementedError
328           return []
329    
330       def __call__(self,result):
331           """
332           filters out values results returned as characteristcs of a problem run
333          
334           @param result: values to be filtered
335           @type result: any type that is produced by the L{BenchmarkProblem} it is applied to
336           @return: a list of strings selected from result
337           @rtype: C{list} of C{str}
338           @remark: this function has to overwritten by a particular problem
339           """
340           raise NotImplementedError
341           return []
342    
343    
344  class Options(object):  class Options(object):
345      """      """
346      defines a set of options to be used to run a L{BenchmarkProblem}      defines a set of options to be used to run a L{BenchmarkProblem}
# Line 322  class Options(object): Line 354  class Options(object):
354         """         """
355         super(Options,self).__init__()         super(Options,self).__init__()
356         if name==None:         if name==None:
357             self.__name=self.__class__.__name__            self.__name=self.__class__.__name__
358         else:         else:
359            self.__name=name            self.__name=name
360      def __str__(self):      def __str__(self):
# Line 341  if __name__=="__main__": Line 373  if __name__=="__main__":
373      class OptionsTest2(Options):      class OptionsTest2(Options):
374          pass          pass
375    
376      class BenchmarkProblemTest(BenchmarkProblem):      class BenchmarkProblemTest1(BenchmarkProblem):
        def __init__(self,name=None):  
            super(BenchmarkProblemTest,self).__init__(name)  
        def getResultNames(self):  
             return ["r0","r1"]    
   
     class BenchmarkProblemTest1(BenchmarkProblemTest):  
377         def __init__(self):         def __init__(self):
378             super(BenchmarkProblemTest1,self).__init__(name="TEST1")             super(BenchmarkProblemTest1,self).__init__(name="TEST1")
379         def run(self,options=None):         def run(self,options=None):
380             import time             import time
381             return time.time(),"A"             return time.time(),"A"
382    
383      class BenchmarkProblemTest2(BenchmarkProblemTest):      class BenchmarkProblemTest2(BenchmarkProblem):
384         def __init__(self):         def __init__(self):
385             super(BenchmarkProblemTest2,self).__init__(name="TEST2")             super(BenchmarkProblemTest2,self).__init__(name="TEST2")
386         def run(self,options=None):         def run(self,options=None):
387             import time             import time
388             return -time.time(),"B"             return -time.time(),"B"
389    
390        class SimpleFilter(BenchmarkFilter):
391           def getResultNames(self):
392                return ["r0","r1"]  
393           def __call__(self,result):
394                return [str(result[0]),str(result[1])]
395    
396      bm=Benchmark("Example")      bm=Benchmark("Example")
397      bm.addProblem(BenchmarkProblemTest1())      bm.addProblem(BenchmarkProblemTest1())
398      bm.addProblem(BenchmarkProblemTest2())      bm.addProblem(BenchmarkProblemTest2())
# Line 371  if __name__=="__main__": Line 403  if __name__=="__main__":
403      bms.addBenchmark(bm)      bms.addBenchmark(bm)
404    
405      bms.run()      bms.run()
406      print bms.getHTML()      print bms.getHTML(filter=SimpleFilter())
407            
408      bms.run(scale=4)      bms.run(scale=4)
409      print bms.getHTML()      print bms.getHTML(filter=SimpleFilter())
410    
411      bms.run(scale=[1,2])      bms.run(scale=[1,2])
412      print bms.getHTML()      print bms.getHTML(filter=SimpleFilter())

Legend:
Removed from v.364  
changed lines
  Added in v.385

  ViewVC Help
Powered by ViewVC 1.1.26