File: run_benchmarks.py

package info (click to toggle)
ignition-cmake 2.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,132 kB
  • sloc: xml: 35,671; python: 3,768; javascript: 2,308; sh: 172; ansic: 109; cpp: 64; makefile: 7
file content (119 lines) | stat: -rwxr-xr-x 4,134 bytes parent folder | download | duplicates (2)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python3
# Copyright 2019 Open Source Robotics Foundation, Inc.
#
# 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 argparse
import datetime
import logging
import os
import shutil
import subprocess
import sys
import tarfile

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('run_benchmarks')


def run_benchmark(target, output_dir):
    """Run a specified benchmark target."""
    basename = os.path.basename(target)
    name = basename.split('BENCHMARK_')[1]
    filename = os.path.join(output_dir, name + '.json')

    logger.info('Running benchmark: {name}'.format_map(locals()))

    cmd = [
        target,
        '--benchmark_out={filename}'.format_map(locals()),
        '--benchmark_out_format=json'
    ]

    logger.debug(' '.join(cmd))
    try:
        benchmark = subprocess.call(cmd,
                                    stdout=sys.stdout,
                                    stderr=sys.stderr)
    except (OSError, subprocess.CalledProcessError) as exception:
        logger.error('Exception occurred: ' + str(exception))
        logger.error('Benchmark failed')
    else:
        logger.info('Finished benchmark: {name}'.format_map(locals()))


def run_benchmarks(targets, output_dir):
    """Run all benchmark targets in a list."""
    for target in targets:
        run_benchmark(target, output_dir)


def create_results_dir(project_name, time, root, version_file):
    """Create a directory to store benchmark results in."""
    time_str = time.strftime('%Y-%m-%d_%H-%M-%S')

    folder_name = '{project_name}_{time_str}'.format_map(locals())
    results_dir = os.path.join(root, folder_name)
    logger.info('Creating results dir: {results_dir}'.format_map(locals()))

    # Create output paths as needed.
    if not os.path.exists(root):
        os.mkdir(root)
    if not os.path.exists(results_dir):
        os.mkdir(results_dir)

    # Copy version information over
    shutil.copy(version_file, os.path.join(results_dir, 'version_info.json'))
    return results_dir


def collect_results(project_name, results_dir):
    """Collect results from a given execution into a tar file."""
    path, basename = os.path.split(results_dir)
    tarname = basename + '.tar.gz'

    with tarfile.open(os.path.join(path, tarname), 'w:gz') as tar:
        tar.add(results_dir, arcname=basename)
    logger.info('Benchmark results collected in: ' +
                os.path.join(path, tarname))


if __name__ == '__main__':
    parser = argparse.ArgumentParser('Run and aggregate available benchmarks')
    parser.add_argument('--project-name', help='Name of the Ignition project')
    parser.add_argument('--version-file',
                        help='Generated file containing version information')
    parser.add_argument('--benchmark-targets', help='Targets to be executed')
    parser.add_argument('--results-root',
                        help='Root directory to store results')

    args = parser.parse_args()

    # Targets will be semicolon delimited from CMake.

    if not os.path.exists(args.benchmark_targets):
        print('Invalid Targets File: {f}'.format(f=args.benchmark_targets))
        parser.exit()

    targets = []
    with open(args.benchmark_targets, 'r') as f:
        for line in f.readlines():
            targets.extend(line.split(';'))

    results_dir = create_results_dir(args.project_name,
                                     datetime.datetime.utcnow(),
                                     args.results_root,
                                     args.version_file)

    run_benchmarks(targets, results_dir)
    collect_results(args.project_name, results_dir)