blob: efb860804389ad7e5ccfae0a89171673c7bc1798 [file] [log] [blame]
José Fonseca299a1b32012-01-26 20:32:59 +00001#!/usr/bin/env python
2##########################################################################
3#
4# Copyright 2012 Jose Fonseca
5# All Rights Reserved.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in
15# all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23# THE SOFTWARE.
24#
25##########################################################################/
26
27'''Sample program for apitrace pickle command.
28
29Run as:
30
31 apitrace pickle foo.trace | python unpickle.py
32
33'''
34
35
José Fonseca447576d2012-01-27 14:27:13 +000036import optparse
José Fonseca299a1b32012-01-26 20:32:59 +000037import cPickle as pickle
38import sys
39import time
40
41
42def main():
José Fonseca447576d2012-01-27 14:27:13 +000043 optparser = optparse.OptionParser(
44 usage="\n\tapitrace pickle trace. %prog [options]")
45 optparser.add_option(
46 '--quiet',
47 action="store_true", dest="quiet", default=False,
48 help="don't dump calls to stdout")
49
50 (options, args) = optparser.parse_args(sys.argv[1:])
51
52 if args:
53 optparser.error('unexpected arguments')
54
José Fonseca299a1b32012-01-26 20:32:59 +000055 calls = 0
56 startTime = time.time()
57 while True:
58 try:
59 call = pickle.load(sys.stdin)
60 except EOFError:
61 break
62 else:
63 callNo, functionName, args, ret = call
José Fonseca447576d2012-01-27 14:27:13 +000064 if not options.quiet:
65 sys.stdout.write('%u ' % callNo)
66 sys.stdout.write(functionName)
67 sys.stdout.write('(' + ', '.join(map(repr, args)) + ')')
68 if ret is not None:
69 sys.stdout.write(' = ')
70 sys.stdout.write(repr(ret))
71 sys.stdout.write('\n')
José Fonseca299a1b32012-01-26 20:32:59 +000072 calls += 1
73 stopTime = time.time()
74 duration = stopTime - startTime
José Fonseca447576d2012-01-27 14:27:13 +000075 sys.stderr.write('%u calls, %.03f secs, %u calls/sec\n' % (calls, duration, calls/duration))
José Fonseca299a1b32012-01-26 20:32:59 +000076
77
78if __name__ == '__main__':
79 main()