Skip to content

Commit dc31800

Browse files
authored
bpo-40669: Install PEG benchmarking dependencies in a venv (pythonGH-20183)
Create a `make venv` target, that creates a virtual environment and installs the dependency in that venv. `make time` and all the related targets are changed to use the virtual environment python. Automerge-Triggered-By: @pablogsal
1 parent 2135e10 commit dc31800

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

Tools/peg_generator/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
peg_extension/parse.c
22
data/xxl.py
3+
venv/
34
@data

Tools/peg_generator/Makefile

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ endif
55
ifeq ($(UNAME_S),Darwin)
66
PYTHON ?= ../../python.exe
77
endif
8-
8+
VENVDIR ?= ./venv
9+
VENVPYTHON ?= $(VENVDIR)/bin/python
910
CPYTHON ?= ../../Lib
1011
MYPY ?= mypy
1112

@@ -27,6 +28,7 @@ peg_extension/parse.c: $(GRAMMAR) $(TOKENS) pegen/*.py peg_extension/peg_extensi
2728
clean:
2829
-rm -f peg_extension/*.o peg_extension/*.so peg_extension/parse.c
2930
-rm -f data/xxl.py
31+
-rm -rf $(VENVDIR)
3032

3133
dump: peg_extension/parse.c
3234
cat -n $(TESTFILE)
@@ -41,6 +43,12 @@ regen-metaparser: pegen/metagrammar.gram pegen/*.py
4143

4244
.PHONY: test
4345

46+
venv:
47+
$(PYTHON) -m venv $(VENVDIR)
48+
$(VENVPYTHON) -m pip install -U pip setuptools
49+
$(VENVPYTHON) -m pip install -U memory_profiler
50+
@echo "The venv has been created in the $(VENVDIR) directory"
51+
4452
test: run
4553

4654
run: peg_extension/parse.c
@@ -61,22 +69,22 @@ stats: peg_extension/parse.c data/xxl.py
6169

6270
time: time_compile
6371

64-
time_compile: peg_extension/parse.c data/xxl.py
65-
$(PYTHON) scripts/benchmark.py --parser=pegen --target=xxl compile
72+
time_compile: venv peg_extension/parse.c data/xxl.py
73+
$(VENVPYTHON) scripts/benchmark.py --parser=pegen --target=xxl compile
6674

67-
time_parse: peg_extension/parse.c data/xxl.py
68-
$(PYTHON) scripts/benchmark.py --parser=pegen --target=xxl parse
75+
time_parse: venv peg_extension/parse.c data/xxl.py
76+
$(VENVPYTHON) scripts/benchmark.py --parser=pegen --target=xxl parse
6977

70-
time_check: peg_extension/parse.c data/xxl.py
71-
$(PYTHON) scripts/benchmark.py --parser=pegen --target=xxl check
78+
time_check: venv peg_extension/parse.c data/xxl.py
79+
$(VENVPYTHON) scripts/benchmark.py --parser=pegen --target=xxl check
7280

7381
time_stdlib: time_stdlib_compile
7482

75-
time_stdlib_compile: data/xxl.py
76-
$(PYTHON) scripts/benchmark.py --parser=cpython --target=xxl compile
83+
time_stdlib_compile: venv peg_extension/parse.c data/xxl.py
84+
$(VENVPYTHON) scripts/benchmark.py --parser=cpython --target=xxl compile
7785

78-
time_stdlib_parse: data/xxl.py
79-
$(PYTHON) scripts/benchmark.py --parser=cpython --target=xxl parse
86+
time_stdlib_parse: venv peg_extension/parse.c data/xxl.py
87+
$(VENVPYTHON) scripts/benchmark.py --parser=cpython --target=xxl parse
8088

8189
test_local:
8290
$(PYTHON) scripts/test_parse_directory.py \
@@ -105,8 +113,8 @@ mypy: regen-metaparser
105113
format-python:
106114
black pegen scripts
107115

108-
bench:
109-
$(PYTHON) scripts/benchmark.py --parser=pegen --target=stdlib check
116+
bench: venv
117+
$(VENVPYTHON) scripts/benchmark.py --parser=pegen --target=stdlib check
110118

111119
format: format-python
112120

Tools/peg_generator/scripts/benchmark.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
#!/usr/bin/env python3.9
1+
#!/usr/bin/env python3
22

33
import argparse
44
import ast
55
import sys
66
import os
77
from time import time
88

9-
import memory_profiler
9+
try:
10+
import memory_profiler
11+
except ModuleNotFoundError:
12+
print("Please run `make venv` to create a virtual environment and install"
13+
" all the dependencies, before running this script.")
14+
sys.exit(1)
1015

1116
sys.path.insert(0, os.getcwd())
1217
from peg_extension import parse

0 commit comments

Comments
 (0)