1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | # $Id: cgiprofiling.py 62484 2016-07-22 18:35:33Z vboxsync $
|
---|
4 |
|
---|
5 | """
|
---|
6 | Debug - CGI Profiling.
|
---|
7 | """
|
---|
8 |
|
---|
9 | __copyright__ = \
|
---|
10 | """
|
---|
11 | Copyright (C) 2012-2016 Oracle Corporation
|
---|
12 |
|
---|
13 | This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
14 | available from http://www.virtualbox.org. This file is free software;
|
---|
15 | you can redistribute it and/or modify it under the terms of the GNU
|
---|
16 | General Public License (GPL) as published by the Free Software
|
---|
17 | Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
18 | VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
19 | hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
20 |
|
---|
21 | The contents of this file may alternatively be used under the terms
|
---|
22 | of the Common Development and Distribution License Version 1.0
|
---|
23 | (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
24 | VirtualBox OSE distribution, in which case the provisions of the
|
---|
25 | CDDL are applicable instead of those of the GPL.
|
---|
26 |
|
---|
27 | You may elect to license modified versions of this file under the
|
---|
28 | terms and conditions of either the GPL or the CDDL or both.
|
---|
29 | """
|
---|
30 | __version__ = "$Revision: 62484 $"
|
---|
31 |
|
---|
32 |
|
---|
33 | def profileIt(fnMain, sAppendToElement = 'main', sSort = 'time'):
|
---|
34 | """
|
---|
35 | Profiles a main() type function call (no parameters, returns int) and
|
---|
36 | outputs a hacky HTML section.
|
---|
37 | """
|
---|
38 |
|
---|
39 | #
|
---|
40 | # Execute it.
|
---|
41 | #
|
---|
42 | import cProfile;
|
---|
43 | oProfiler = cProfile.Profile();
|
---|
44 | rc = oProfiler.runcall(fnMain);
|
---|
45 |
|
---|
46 | #
|
---|
47 | # Output HTML to stdout (CGI assumption).
|
---|
48 | #
|
---|
49 | print('<div id="debug2"><br>\n' # Lazy BR-layouting!!
|
---|
50 | ' <h2>Profiler Output</h2>\n'
|
---|
51 | ' <pre>');
|
---|
52 | try:
|
---|
53 | oProfiler.print_stats(sort = sSort);
|
---|
54 | except Exception, oXcpt:
|
---|
55 | print('<p><pre>%s</pre></p>\n' % (oXcpt,));
|
---|
56 | else:
|
---|
57 | print('</pre>\n');
|
---|
58 | oProfiler = None;
|
---|
59 | print('</div>\n');
|
---|
60 |
|
---|
61 | #
|
---|
62 | # Trick to move the section in under the SQL trace.
|
---|
63 | #
|
---|
64 | print('<script lang="script/javascript">\n'
|
---|
65 | 'var oMain = document.getElementById(\'%s\');\n'
|
---|
66 | 'if (oMain) {\n'
|
---|
67 | ' oMain.appendChild(document.getElementById(\'debug2\'));\n'
|
---|
68 | '}\n'
|
---|
69 | '</script>\n'
|
---|
70 | % (sAppendToElement, ) );
|
---|
71 |
|
---|
72 | return rc;
|
---|
73 |
|
---|