forked from graalvm/mx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mx_microbench.py
116 lines (97 loc) · 4.27 KB
/
mx_microbench.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
#
# ----------------------------------------------------------------------------------------------------
#
# Copyright (c) 2016, 2016, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#
# ----------------------------------------------------------------------------------------------------
import mx
from argparse import ArgumentParser
_microbench_executor = None
def set_microbenchmark_executor(ex):
global _microbench_executor
assert _microbench_executor is None, 'cannot override microbenchmark executor twice'
_microbench_executor = ex
def get_microbenchmark_executor():
if not _microbench_executor:
set_microbenchmark_executor(MicrobenchExecutor())
return _microbench_executor
class MicrobenchExecutor(object):
def microbench(self, args):
"""run JMH microbenchmark projects"""
parser = ArgumentParser(prog='mx microbench', description=microbench.__doc__,
usage="%(prog)s [command options|VM options] [-- [JMH options]]")
parser.add_argument('--jar', help='Explicitly specify micro-benchmark location')
self.add_arguments(parser)
known_args, args = parser.parse_known_args(args)
vmArgs, jmhArgs = mx.extract_VM_args(args, useDoubleDash=True)
vmArgs = self.parseVmArgs(vmArgs)
# look for -f in JMH arguments
forking = True
for i in range(len(jmhArgs)):
arg = jmhArgs[i]
if arg.startswith('-f'):
if arg == '-f' and (i+1) < len(jmhArgs):
arg += jmhArgs[i+1]
try:
if int(arg[2:]) == 0:
forking = False
except ValueError:
pass
if known_args.jar:
# use the specified jar
args = ['-jar', known_args.jar]
if not forking:
args += vmArgs
# we do not know the compliance level of the jar - assuming 1.8
self.javaCompliance = mx.JavaCompliance('1.8')
else:
# find all projects with a direct JMH dependency
jmhProjects = []
for p in mx.projects_opt_limit_to_suites():
if 'JMH' in [x.name for x in p.deps]:
jmhProjects.append(p)
# get java compliance - 1.8 is minimum since we build jmh-runner with java 8
self.javaCompliance = max([p.javaCompliance for p in jmhProjects] + [mx.JavaCompliance('1.8')])
cp = mx.classpath([p.name for p in jmhProjects], jdk=mx.get_jdk(self.javaCompliance))
# execute JMH runner
args = ['-cp', cp]
if not forking:
args += vmArgs
args += ['org.openjdk.jmh.Main']
if forking:
def quoteSpace(s):
if " " in s:
return '"' + s + '"'
return s
forkedVmArgs = map(quoteSpace, self.parseForkedVmArgs(vmArgs))
args += ['--jvmArgsPrepend', ' '.join(forkedVmArgs)]
self.run_java(args + jmhArgs)
def add_arguments(self, parser):
pass
def run_java(self, args):
mx.run_java(args, jdk=mx.get_jdk(self.javaCompliance))
def parseVmArgs(self, vmArgs):
return vmArgs
def parseForkedVmArgs(self, vmArgs):
return vmArgs
def microbench(args):
get_microbenchmark_executor().microbench(args)