summaryrefslogtreecommitdiff
path: root/scripts/perf/profile_csv.py
blob: 7a6b2b338e1bf24f47e5883f30669f4f6f711e93 (plain)
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
#!/usr/bin/env python3
"""\
@file   profile_csv.py
@author Nat Goodspeed
@date   2024-09-12
@brief  Convert a JSON file from Develop -> Render Tests -> Frame Profile to CSV

$LicenseInfo:firstyear=2024&license=viewerlgpl$
Copyright (c) 2024, Linden Research, Inc.
$/LicenseInfo$
"""

import json
from logsdir import Error, latest_file, logsdir
import sys

def convert(path, totals=True, unused=True, file=sys.stdout):
    with open(path) as inf:
        data = json.load(inf)
    # print path to sys.stderr in case user is redirecting stdout
    print(path, file=sys.stderr)

    print('"name", "file1", "file2", "time", "binds", "samples", "triangles"', file=file)

    if totals:
        t = data['totals']
        print(f'"totals", "", "", {t["time"]}, {t["binds"]}, {t["samples"]}, {t["triangles"]}',
              file=file)

    for sh in data['shaders']:
        print(f'"{sh["name"]}", "{sh["files"][0]}", "{sh["files"][1]}", '
              f'{sh["time"]}, {sh["binds"]}, {sh["samples"]}, {sh["triangles"]}', file=file)

    if unused:
        for u in data['unused']:
            print(f'"{u}", "", "", 0, 0, 0, 0', file=file)

def main(*raw_args):
    from argparse import ArgumentParser
    parser = ArgumentParser(description="""
%(prog)s converts a JSON file from Develop -> Render Tests -> Frame Profile to
a more-or-less equivalent CSV file. It expands the totals stats and unused
shaders list to full shaders lines.
""")
    parser.add_argument('-t', '--totals', action='store_false', default=True,
                        help="""omit totals from CSV file""")
    parser.add_argument('-u', '--unused', action='store_false', default=True,
                        help="""omit unused shaders from CSV file""")
    parser.add_argument('path', nargs='?',
                        help="""profile filename to convert (default is most recent)""")

    args = parser.parse_args(raw_args)
    convert(args.path or latest_file(logsdir(), 'profile.*.json'),
            totals=args.totals, unused=args.unused)

if __name__ == "__main__":
    try:
        sys.exit(main(*sys.argv[1:]))
    except (Error, OSError, json.JSONDecodeError) as err:
        sys.exit(str(err))