File: rank_profiled_methods.py

package info (click to toggle)
mistral 21.0.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,140 kB
  • sloc: python: 54,052; sh: 701; makefile: 58
file content (99 lines) | stat: -rw-r--r-- 2,931 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Copyright 2019 - Nokia Networks
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

"""
"""

import sys


def _print_help():
    print("\nUsage: <script_name> <input_file_name> <output_file_name>\n")
    print(
        'The script processes a Mistral profiler log file (<input_file_name)\n'
        'and generates a report into a file (<output_file_name>) that\n'
        'contains statistics about each profiler trace: \n'
        '-------------------------------------------------------------\n'
        ' Total time | Max time | Avg time | Occurrences | Trace name \n'
        '-------------------------------------------------------------\n'
        ' ...          ...        ...        ...           ...\n'
    )


def main():
    try:
        in_file_name = str(sys.argv[1])
        out_file_name = str(sys.argv[2])
    except:
        _print_help()

        return "Failed to parse arguments."

    print('Ranking profiled methods...')

    in_f = open(in_file_name, 'r')
    out_f = open(out_file_name, 'w')

    # {trace_name: [total_time, max_time, occurrences]}
    d = dict()

    with in_f:
        for line in in_f:
            tokens = line.split()

            # Skip all "-start" lines that don't contain a duration in seconds.
            # Processing only "-stop" lines.
            if len(tokens[1]) > 10:
                continue

            trace_name = tokens[5]
            trace_name = trace_name[0:len(trace_name) - 5]

            duration = float(tokens[1])

            if trace_name not in d:
                d[trace_name] = [duration, duration, 1]
            else:
                l = d[trace_name]

                l[0] = l[0] + duration
                l[2] = l[2] + 1

                if duration > l[1]:
                    l[1] = duration

    result = sorted(d.items(), key=lambda x: x[1][0], reverse=True)

    out_f.write('Total time | Max time | Avg time | Occurrences | Trace name\n')
    out_f.write('-' * 90)
    out_f.write('-\n')

    for item in result:
        out_f.write(
            '{0:<12.3f} {1:<10.3f} {2:<10.3f} {3:<13d} {4}\n'.format(
                item[1][0],
                item[1][1],
                item[1][0] / item[1][2],
                item[1][2],
                item[0]
            )
        )

    out_f.close()

    print("Ranking file was successfully created: %s" % out_file_name)


if __name__ == '__main__':
    sys.exit(main())