Skip to content

Commit fe9d49e

Browse files
committed
[GR-71751] Fix startup benchmark
PullRequest: graalpython/4135
2 parents 0c1791a + fb10b0e commit fe9d49e

File tree

3 files changed

+81
-27
lines changed

3 files changed

+81
-27
lines changed

graalpython/com.oracle.graal.python.benchmarks/python/micro/startup.py

Lines changed: 79 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,87 @@
3636
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
39+
import os
40+
import subprocess
41+
import sys
42+
import tempfile
43+
from pathlib import Path
3944

45+
# Use a C runner to spawn the subprocesses to avoid counting subprocess module overhead into the benchmark
46+
RUNNER_CODE = '''
47+
#include <stdio.h>
48+
#include <stdlib.h>
49+
#include <unistd.h>
50+
#include <sys/wait.h>
4051
41-
import subprocess, sys
52+
int main(int argc, char *argv[]) {
53+
if (argc < 3) {
54+
return 1;
55+
}
56+
int n = atoi(argv[1]);
57+
if (n <= 0) {
58+
return 1;
59+
}
60+
char **cmd_argv = &argv[2];
61+
for (int i = 0; i < n; ++i) {
62+
pid_t pid = fork();
63+
if (pid < 0) {
64+
perror("fork");
65+
return 1;
66+
} else if (pid == 0) {
67+
execvp(cmd_argv[0], cmd_argv);
68+
perror("execvp");
69+
exit(127); // If exec fails
70+
} else {
71+
int status;
72+
if (waitpid(pid, &status, 0) < 0) {
73+
perror("waitpid");
74+
return 1;
75+
}
76+
}
77+
}
78+
return 0;
79+
}
80+
'''
81+
82+
TMPDIR = tempfile.TemporaryDirectory()
83+
RUNNER_EXE = None
84+
ORIG_ARGV = None
85+
86+
87+
def __setup__(*args):
88+
global RUNNER_EXE
89+
tmpdir = Path(TMPDIR.name)
90+
runner_c = tmpdir / 'runner.c'
91+
runner_c.write_text(RUNNER_CODE)
92+
RUNNER_EXE = tmpdir / 'runner'
93+
subprocess.check_call([os.environ.get('CC', 'gcc'), runner_c, '-O2', '-o', RUNNER_EXE])
94+
95+
global ORIG_ARGV
96+
ORIG_ARGV = sys.orig_argv
97+
for i, arg in enumerate(ORIG_ARGV):
98+
if arg.endswith('.py'):
99+
ORIG_ARGV = ORIG_ARGV[:i]
100+
break
101+
try:
102+
ORIG_ARGV.remove('-snapshot-startup')
103+
except ValueError:
104+
pass
105+
106+
107+
def __teardown__():
108+
TMPDIR.cleanup()
42109

43110

44111
def __benchmark__(num=1000000):
45-
while num > 0:
46-
num -= 1
47-
orig_vm_argv = sys.orig_argv
48-
for i, arg in enumerate(orig_vm_argv):
49-
if arg.endswith('.py'):
50-
orig_vm_argv = orig_vm_argv[:i]
51-
break
52-
subprocess.check_call([
53-
*orig_vm_argv,
54-
"-I", # isolate from environment
55-
"-S", # do not import site
56-
"-Wignore", # ignore all warnings
57-
"-B", # do not attempt to write pyc files
58-
"-u", # do not add buffering wrappers around output streams
59-
"-c",
60-
"1"
61-
])
112+
subprocess.check_call([
113+
str(RUNNER_EXE),
114+
str(num),
115+
*ORIG_ARGV,
116+
"-I", # isolate from environment
117+
"-S", # do not import site
118+
"-B", # do not attempt to write pyc files
119+
"-u", # do not add buffering wrappers around output streams
120+
"-c",
121+
"1"
122+
])

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/GraalPythonMain.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -762,14 +762,6 @@ protected void launch(Builder contextBuilder) {
762762
}
763763
}
764764

765-
if (relaunchArgs != null) {
766-
Iterator<String> it = origArgs.iterator();
767-
while (it.hasNext()) {
768-
if (relaunchArgs.contains(it.next())) {
769-
it.remove();
770-
}
771-
}
772-
}
773765
origArgs.add(0, toAbsolutePath(executable));
774766
contextBuilder.option("python.OrigArgv", String.join(STRING_LIST_DELIMITER, origArgs));
775767

graalpython/lib-python/3/test/test_sys.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,8 @@ def test_orig_argv(self):
12001200
code = textwrap.dedent('''
12011201
import sys
12021202
print(sys.argv)
1203-
print(sys.orig_argv)
1203+
# GraalPy change: remove options implicitly passed down by our posix module magic
1204+
print([arg for arg in sys.orig_argv if not arg.startswith(('--experimental-options', '--vm.', '--python.'))])
12041205
''')
12051206
args = [sys.executable, '-I', '-X', 'utf8', '-c', code, 'arg']
12061207
proc = subprocess.run(args, check=True, capture_output=True, text=True, env=env)

0 commit comments

Comments
 (0)