forked from dotnet/performance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicro_benchmarks.py
executable file
·424 lines (364 loc) · 12.6 KB
/
micro_benchmarks.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env python3
'''
Builds the Benchmarks
'''
from argparse import ArgumentParser
from argparse import ArgumentTypeError
from argparse import SUPPRESS
from io import StringIO
from logging import getLogger
from os import path
from subprocess import CalledProcessError
from traceback import format_exc
from typing import Tuple
import csv
import sys
from performance.common import get_repo_root_path
from performance.common import get_artifacts_directory
from performance.common import get_packages_directory
from performance.common import remove_directory
from performance.common import validate_supported_runtime
from performance.logger import setup_loggers
from channel_map import ChannelMap
import dotnet
def get_supported_configurations() -> list:
'''
The configuration to use for building the project. The default for most
projects is 'Release'
'''
return ['Release', 'Debug']
def add_arguments(parser: ArgumentParser) -> ArgumentParser:
'''
Adds new arguments to the specified ArgumentParser object.
'''
def __dotnet_configuration(configuration: str) -> str:
for config in get_supported_configurations():
is_valid = config.casefold() == configuration.casefold()
if is_valid:
return config
raise ArgumentTypeError(
'Unknown configuration: {}.'.format(configuration))
supported_configurations = get_supported_configurations()
parser.add_argument(
'-c', '--configuration',
required=False,
default=supported_configurations[0],
choices=supported_configurations,
type=__dotnet_configuration,
help=SUPPRESS,
)
parser.add_argument(
'-f', '--frameworks',
required=False,
choices=ChannelMap.get_supported_frameworks(),
nargs='+',
help='''The framework to build/run for. '''
'''The target framework must also be specified in the project '''
'''file.''',
)
parser.add_argument(
'--incremental',
required=False,
default='yes',
choices=['yes', 'no'],
type=str,
help='''Controls whether previous packages/bin/obj folders should '''
'''be kept or removed before the dotnet restore/build/run are '''
'''executed (Default yes).''',
)
# BenchmarkDotNet
parser.add_argument(
'--enable-hardware-counters',
dest='enable_pmc',
required=False,
default=False,
action='store_true',
help='''Enables the following performance metric counters: '''
'''BranchMispredictions+CacheMisses+InstructionRetired''',
)
parser.add_argument(
'--filter',
required=False,
nargs='+',
help='Glob patterns to execute benchmarks that match.',
)
def __valid_file_path(file_path: str) -> str:
'''Verifies that specified file path exists.'''
file_path = path.abspath(file_path)
if not path.isfile(file_path):
raise ArgumentTypeError('{} does not exist.'.format(file_path))
return file_path
parser.add_argument(
'--corerun',
dest='corerun',
required=False,
nargs='+',
type=__valid_file_path,
help='Full path to CoreRun.exe (corerun on Unix)',
)
parser.add_argument(
'--cli',
dest='cli',
required=False,
type=__valid_file_path,
help='Full path to dotnet.exe',
)
def __get_bdn_arguments(user_input: str) -> list:
file = StringIO(user_input)
reader = csv.reader(file, delimiter=' ')
for args in reader:
return args
return []
parser.add_argument(
'--wasm',
dest='wasm',
required=False,
default=False,
action='store_true',
help='Tests should be run with the wasm runtime'
)
parser.add_argument(
'--bdn-arguments',
dest='bdn_arguments',
required=False,
type=__get_bdn_arguments,
help='''Command line arguments to be passed to the BenchmarkDotNet '''
'''harness.''',
)
parser.add_argument(
'--bdn-artifacts',
dest='bdn_artifacts',
required=False,
type=str,
help='''Path to artifacts directory to be passed to the BenchmarkDotNet '''
'''harness.''',
)
parser.add_argument(
'--run-isolated',
dest='run_isolated',
required=False,
default=False,
action='store_true',
help='Move the binaries to a different directory for running',
)
def __valid_dir_path(file_path: str) -> str:
'''Verifies that specified file path exists.'''
file_path = path.abspath(file_path)
if not path.isdir(file_path):
raise ArgumentTypeError('{} does not exist.'.format(file_path))
return file_path
def __csproj_file_path(file_path: str) -> dotnet.CSharpProjFile:
file_path = __valid_file_path(file_path)
return dotnet.CSharpProjFile(
file_name=file_path,
working_directory=path.dirname(file_path)
)
microbenchmarks_csproj = path.join(
get_repo_root_path(), 'src', 'benchmarks', 'micro',
'MicroBenchmarks.csproj'
)
parser.add_argument(
'--csproj',
dest='csprojfile',
required=False,
type=__csproj_file_path,
default=dotnet.CSharpProjFile(
file_name=microbenchmarks_csproj,
working_directory=path.dirname(microbenchmarks_csproj)
),
help='''C# project file name with the benchmarks to build/run. '''
'''The default project is the MicroBenchmarks.csproj'''
)
def __absolute_path(file_path: str) -> str:
'''
Return a normalized absolutized version of the specified file_path
path.
'''
return path.abspath(file_path)
parser.add_argument(
'--bin-directory',
dest='bin_directory',
required=False,
default=path.join(get_repo_root_path(), 'artifacts', 'bin'),
type=__absolute_path,
help='Root of the bin directory',
)
return parser
def __process_arguments(args: list) -> Tuple[list, bool]:
parser = ArgumentParser(
description="Builds the benchmarks.",
allow_abbrev=False)
parser.add_argument(
'-v', '--verbose',
required=False,
default=False,
action='store_true',
help='Turns on verbosity (default "False")',
)
parser = add_arguments(parser)
return parser.parse_args(args)
def __get_benchmarkdotnet_arguments(framework: str, args: tuple) -> list:
run_args = []
if args.corerun:
run_args += ['--coreRun'] + args.corerun
if args.cli:
run_args += ['--cli', args.cli]
if args.enable_pmc:
run_args += [
'--counters',
'BranchMispredictions+CacheMisses+InstructionRetired',
]
if args.filter:
run_args += ['--filter'] + args.filter
if args.resume:
run_args += ['--resume']
# Extra BenchmarkDotNet cli arguments.
if args.bdn_arguments:
run_args += args.bdn_arguments
if args.bdn_artifacts:
run_args += ['--artifacts', args.bdn_artifacts]
# we need to tell BenchmarkDotNet where to restore the packages
# if we don't it's gonna restore to default global folder
run_args += ['--packages', get_packages_directory()]
# Required for WASM and NativeAOT where:
# host process framework != benchmark process framework
if framework.startswith("nativeaot"):
run_args += ['--runtimes', framework]
if args.wasm:
if framework == "net5.0" or framework == "net6.0":
run_args += ['--runtimes', 'wasm']
else:
run_args += ['--runtimes', 'wasmnet70']
# Increase default 2 min build timeout to accommodate slow (or even very slow) hardware
if not args.bdn_arguments or '--buildTimeout' not in args.bdn_arguments:
run_args += ['--buildTimeout', '1200']
return run_args
def get_bin_dir_to_use(csprojfile: dotnet.CSharpProjFile, bin_directory: str, run_isolated: bool) -> str:
'''
Gets the bin_directory, which might be different if run_isolate=True
'''
if run_isolated:
return path.join(bin_directory, 'for-running', dotnet.get_project_name(csprojfile.file_name))
else:
return bin_directory
def build(
BENCHMARKS_CSPROJ: dotnet.CSharpProject,
configuration: str,
target_framework_monikers: list,
incremental: str,
run_isolated: bool,
verbose: bool) -> None:
'''Restores and builds the benchmarks'''
packages = get_packages_directory()
if incremental == 'no':
__log_script_header("Removing packages, bin and obj folders.")
binary_folders = [
packages,
path.join(BENCHMARKS_CSPROJ.bin_path),
]
for binary_folder in binary_folders:
remove_directory(path=binary_folder)
# dotnet restore
__log_script_header("Restoring .NET micro benchmarks")
BENCHMARKS_CSPROJ.restore(packages_path=packages, verbose=verbose)
# dotnet build
build_title = "Building .NET micro benchmarks for '{}'".format(
' '.join(target_framework_monikers))
__log_script_header(build_title)
BENCHMARKS_CSPROJ.build(
configuration=configuration,
target_framework_monikers=target_framework_monikers,
output_to_bindir=run_isolated,
verbose=verbose,
packages_path=packages)
# When running isolated, artifacts/obj/{project_name} will still be
# there, and would interfere with any subsequent builds. So, remove
# that
if run_isolated:
objDir = path.join(get_artifacts_directory(), 'obj', BENCHMARKS_CSPROJ.project_name)
remove_directory(objDir)
def run(
BENCHMARKS_CSPROJ: dotnet.CSharpProject,
configuration: str,
framework: str,
run_isolated: bool,
verbose: bool,
*args) -> None:
'''Runs the benchmarks'''
__log_script_header("Running .NET micro benchmarks for '{}'".format(
framework
))
# dotnet exec
run_args = __get_benchmarkdotnet_arguments(framework, *args)
target_framework_moniker = dotnet.FrameworkAction.get_target_framework_moniker(
framework
)
if run_isolated:
runDir = BENCHMARKS_CSPROJ.bin_path
asm_path=dotnet.get_main_assembly_path(runDir, BENCHMARKS_CSPROJ.project_name)
dotnet.exec(asm_path, verbose, *run_args)
else:
# This is needed for `dotnet run`, but not for `dotnet exec`
run_args = ['--'] + run_args
BENCHMARKS_CSPROJ.run(
configuration,
target_framework_moniker,
verbose,
*run_args
)
def __log_script_header(message: str):
getLogger().info('-' * len(message))
getLogger().info(message)
getLogger().info('-' * len(message))
def __main(args: list) -> int:
try:
validate_supported_runtime()
args = __process_arguments(args)
configuration = args.configuration
frameworks = args.frameworks
incremental = args.incremental
verbose = args.verbose
target_framework_monikers = dotnet.FrameworkAction. \
get_target_framework_monikers(frameworks)
setup_loggers(verbose=verbose)
# dotnet --info
dotnet.info(verbose)
bin_dir_to_use=micro_benchmarks.get_bin_dir_to_use(args.csprojfile, args.bin_directory, args.run_isolated)
BENCHMARKS_CSPROJ = dotnet.CSharpProject(
project=args.csprojfile,
bin_directory=bin_dir_to_use
)
# dotnet build
build(
BENCHMARKS_CSPROJ,
configuration,
target_framework_monikers,
incremental,
args.run_isolated,
verbose
)
for framework in frameworks:
# dotnet run
run(
BENCHMARKS_CSPROJ,
configuration,
framework,
args.run_isolated,
verbose,
args
)
return 0
except CalledProcessError as ex:
getLogger().error(
'Command: "%s", exited with status: %s', ex.cmd, ex.returncode)
except IOError as ex:
getLogger().error(
"I/O error (%s): %s: %s", ex.errno, ex.strerror, ex.filename)
except SystemExit: # Argparse throws this exception when it exits.
pass
except Exception:
getLogger().error('Unexpected error: %s', sys.exc_info()[0])
getLogger().error(format_exc())
return 1
if __name__ == "__main__":
exit(__main(sys.argv[1:]))