• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 argparse
6import cProfile
7import pstats
8import StringIO
9import inspect
10import sys
11
12
13
14class Bench(object):
15
16  def SetUp(self):
17    pass
18
19  def Run(self):
20    pass
21
22  def TearDown(self):
23    pass
24
25
26def Main(args):
27  parser = argparse.ArgumentParser()
28  parser.add_argument('--repeat-count', type=int, default=10)
29  parser.add_argument('bench_name')
30  args = parser.parse_args(args)
31
32  benches = [g for g in globals().values()
33             if g != Bench and inspect.isclass(g) and
34             Bench in inspect.getmro(g)]
35
36  # pylint: disable=undefined-loop-variable
37  b = [b for b in benches if b.__name__ == args.bench_name]
38  if len(b) != 1:
39    sys.stderr.write('Bench %r not found.' % args.bench_name)
40    return 1
41
42  bench = b[0]()
43  bench.SetUp()
44  try:
45    pr = cProfile.Profile()
46    pr.enable(builtins=False)
47    for _ in range(args.repeat_count):
48      bench.Run()
49    pr.disable()
50    s = StringIO.StringIO()
51
52    sortby = 'cumulative'
53    ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
54    ps.print_stats()
55    print s.getvalue()
56    return 0
57  finally:
58    bench.TearDown()
59
60
61if __name__ == '__main__':
62  sys.exit(Main(sys.argv[1:]))
63