1# Copyright (c) 2012 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import cProfile 6import pstats 7import StringIO 8import inspect 9import optparse 10import sys 11import os 12 13tracing_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 14 '..', '..')) 15if tracing_path not in sys.path: 16 sys.path.append(tracing_path) 17 18from tracing import tracing_project 19from tvcm import generate 20from tvcm import project 21 22 23class Bench(object): 24 25 def SetUp(self): 26 pass 27 28 def Run(self): 29 pass 30 31 def TearDown(self): 32 pass 33 34 35def Main(args): 36 parser = optparse.OptionParser() 37 parser.add_option('--repeat-count', type='int', 38 default=10) 39 options, args = parser.parse_args(args) 40 41 benches = [g for g in globals().values() 42 if g != Bench and inspect.isclass(g) and 43 Bench in inspect.getmro(g)] 44 if len(args) != 1: 45 sys.stderr.write('\n'.join([b.__name__ for b in benches])) 46 return 1 47 48 b = [b for b in benches if b.__name__ == args[0]] 49 if len(b) != 1: 50 sys.stderr.write('Oops') 51 return 1 52 53 bench = b[0]() 54 bench.SetUp() 55 try: 56 pr = cProfile.Profile() 57 pr.enable(builtins=False) 58 for i in range(options.repeat_count): 59 bench.Run() 60 pr.disable() 61 s = StringIO.StringIO() 62 63 sortby = 'cumulative' 64 ps = pstats.Stats(pr, stream=s).sort_stats(sortby) 65 ps.print_stats() 66 print s.getvalue() 67 return 0 68 finally: 69 bench.TearDown() 70 71 72if __name__ == '__main__': 73 sys.exit(Main(sys.argv[1:])) 74