forked from dotnet/performance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmarks_ci.py
executable file
·278 lines (233 loc) · 8.64 KB
/
benchmarks_ci.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python3
'''
Additional information:
This script wraps all the logic of how to build/run the .NET micro benchmarks,
acquire tools, gather data into BenchView format and upload it, archive
results, etc.
This is meant to be used on CI runs and available for local runs,
so developers can easily reproduce what runs in the lab.
Note:
The micro benchmarks themselves can be built and run using the DotNet Cli tool.
For more information refer to: benchmarking-workflow.md
../docs/benchmarking-workflow.md
- or -
https://github.com/dotnet/performance/blob/master/docs/benchmarking-workflow.md
'''
from argparse import ArgumentParser, ArgumentTypeError
from datetime import datetime
from logging import getLogger
import os
import platform
import sys
from performance.common import validate_supported_runtime
from performance.logger import setup_loggers
import benchview
import dotnet
import micro_benchmarks
from azcopy import AzCopy
if sys.platform == 'linux' and "linux_distribution" not in dir(platform):
MESSAGE = '''The `linux_distribution` method is missing from ''' \
'''the `platform` module, which is used to find out information ''' \
'''about the OS flavor/version we are using.%s''' \
'''The Python Docs state that `platform.linux_distribution` is ''' \
'''"Deprecated since version 3.5, will be removed in version 3.8: ''' \
'''See alternative like the distro package.%s"''' \
'''Most systems in the lab have Python versions 3.5 and 3.6 ''' \
'''installed, so we are good at the moment.%s''' \
'''If we are hitting this issue, then it might be time to look ''' \
'''into using the `distro` module, and possibly packaing as part ''' \
'''of the dependencies of these scripts/repo.'''
getLogger().error(MESSAGE, os.linesep, os.linesep, os.linesep)
exit(1)
def init_tools(
architecture: str,
dotnet_versions: str,
target_framework_monikers: list,
verbose: bool) -> None:
'''
Install tools used by this repository into the tools folder.
This function writes a semaphore file when tools have been successfully
installed in order to avoid reinstalling them on every rerun.
'''
getLogger().info('Installing tools.')
channels = [
micro_benchmarks.FrameworkAction.get_channel(
target_framework_moniker)
for target_framework_moniker in target_framework_monikers
]
dotnet.install(
architecture=architecture,
channels=channels,
versions=dotnet_versions,
verbose=verbose,
)
benchview.install()
def add_arguments(parser: ArgumentParser) -> ArgumentParser:
'''Adds new arguments to the specified ArgumentParser object.'''
if not isinstance(parser, ArgumentParser):
raise TypeError('Invalid parser.')
# Download DotNet Cli
dotnet.add_arguments(parser)
# Restore/Build/Run functionality for MicroBenchmarks.csproj
micro_benchmarks.add_arguments(parser)
PRODUCT_INFO = [
'init-tools', # Default
'repo',
'cli',
]
parser.add_argument(
'--cli-source-info',
dest='cli_source_info',
required=False,
default=PRODUCT_INFO[0],
choices=PRODUCT_INFO,
help='Specifies where the product information comes from.',
)
parser.add_argument(
'--cli-branch',
dest='cli_branch',
required=False,
type=str,
help='Product branch.'
)
parser.add_argument(
'--cli-commit-sha',
dest='cli_commit_sha',
required=False,
type=str,
help='Product commit sha.'
)
parser.add_argument(
'--cli-repository',
dest='cli_repository',
required=False,
type=str,
help='Product repository.'
)
def __is_valid_datetime(dt: str) -> str:
try:
datetime.strptime(dt, '%Y-%m-%dT%H:%M:%SZ')
return dt
except ValueError:
raise ArgumentTypeError(
'Datetime "{}" is in the wrong format.'.format(dt))
parser.add_argument(
'--cli-source-timestamp',
dest='cli_source_timestamp',
required=False,
type=__is_valid_datetime,
help='''Product timestamp of the soruces used to generate this build
(date-time from RFC 3339, Section 5.6.
"%%Y-%%m-%%dT%%H:%%M:%%SZ").'''
)
parser.add_argument('--upload-to-perflab-container',
dest="upload_to_perflab_container",
required=False,
help="Container to upload perf lab results to."
)
# Generic arguments.
parser.add_argument(
'-q', '--quiet',
required=False,
default=False,
action='store_true',
help='Turns off verbosity.',
)
parser.add_argument(
'--build-only',
dest='build_only',
required=False,
default=False,
action='store_true',
help='Builds the benchmarks but does not run them.',
)
parser.add_argument(
'--run-only',
dest='run_only',
required=False,
default=False,
action='store_true',
help='Attempts to run the benchmarks without building.',
)
# BenchView acquisition, and fuctionality
parser = benchview.add_arguments(parser)
return parser
def __process_arguments(args: list):
parser = ArgumentParser(
description='Tool to run .NET micro benchmarks',
allow_abbrev=False,
# epilog=os.linesep.join(__doc__.splitlines())
epilog=__doc__,
)
add_arguments(parser)
return parser.parse_args(args)
def __main(args: list) -> int:
validate_supported_runtime()
args = __process_arguments(args)
verbose = not args.quiet
setup_loggers(verbose=verbose)
# This validation could be cleaner
if args.generate_benchview_data and not args.benchview_submission_name:
raise RuntimeError("""In order to generate BenchView data,
`--benchview-submission-name` must be provided.""")
target_framework_monikers = micro_benchmarks \
.FrameworkAction \
.get_target_framework_monikers(args.frameworks)
# Acquire necessary tools (dotnet, and BenchView)
init_tools(
architecture=args.architecture,
dotnet_versions=args.dotnet_versions,
target_framework_monikers=target_framework_monikers,
verbose=verbose
)
# WORKAROUND
# The MicroBenchmarks.csproj targets .NET Core 2.0, 2.1, 2.2 and 3.0
# to avoid a build failure when using older frameworks (error NETSDK1045:
# The current .NET SDK does not support targeting .NET Core $XYZ)
# we set the TFM to what the user has provided.
os.environ['PYTHON_SCRIPT_TARGET_FRAMEWORKS'] = ';'.join(
target_framework_monikers
)
# dotnet --info
dotnet.info(verbose=verbose)
BENCHMARKS_CSPROJ = dotnet.CSharpProject(
project=args.csprojfile,
bin_directory=args.bin_directory
)
if not args.run_only:
# .NET micro-benchmarks
# Restore and build micro-benchmarks
micro_benchmarks.build(
BENCHMARKS_CSPROJ,
args.configuration,
target_framework_monikers,
args.incremental,
verbose
)
# Run micro-benchmarks
if not args.build_only:
for framework in args.frameworks:
#Before we run the benchmarks we need to set the CommitDate in the environment
#for the nex reporting tool
if framework != 'net461' and args.generate_benchview_data:
target_framework_moniker = micro_benchmarks.FrameworkAction.get_target_framework_moniker(framework)
commit_sha = dotnet.get_dotnet_sdk(target_framework_moniker, args.cli)
source_timestamp = dotnet.get_commit_date(framework, commit_sha, args.cli_repository)
os.environ['PERFLAB_HASH'] = commit_sha
os.environ['PERFLAB_BUILDTIMESTAMP'] = source_timestamp
# ensure that if we aren't generating data we dont try to go down the perflab reporting path, since we'll be missing data.
if os.getenv('PERFLAB_INLAB') and not args.generate_benchview_data:
os.environ.pop('PERFLAB_INLAB')
micro_benchmarks.run(
BENCHMARKS_CSPROJ,
args.configuration,
framework,
verbose,
args
)
benchview.run_scripts(args, verbose, BENCHMARKS_CSPROJ)
if(args.upload_to_perflab_container):
AzCopy.upload_results(args.upload_to_perflab_container, verbose)
# TODO: Archive artifacts.
if __name__ == "__main__":
__main(sys.argv[1:])