Skip to content

Commit c1368d9

Browse files
committed
Add folder for perf profiler files
1 parent 42ee249 commit c1368d9

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import sys
2+
import sysconfig
3+
from math import exp, log
4+
from statistics import mean
5+
6+
from PIL import Image
7+
8+
9+
def check_perf_support():
10+
if sys.version_info < (3, 12):
11+
version = sysconfig.get_python_version()
12+
raise RuntimeError(f"This is Python {version}, not 3.12 or later")
13+
14+
if not sysconfig.get_config_var("PY_HAVE_PERF_TRAMPOLINE"):
15+
raise RuntimeError("Python doesn't support perf on this platform")
16+
17+
if not sys.is_stack_trampoline_active():
18+
raise RuntimeError("Did you forget the '-X perf' option?")
19+
20+
cflags = sysconfig.get_config_var("CONFIGURE_CFLAGS")
21+
if "-fno-omit-frame-pointer" not in cflags:
22+
print("Python compiled without the frame pointer", file=sys.stderr)
23+
24+
25+
def main():
26+
image = Image.open("image.jpg")
27+
print("luminance =", get_average_luminance(image.getdata()))
28+
image.show()
29+
30+
31+
def get_average_luminance(pixels):
32+
return exp(mean(log(luminance(pixel) + 1e-9) for pixel in pixels))
33+
34+
35+
def luminance(pixel):
36+
red, green, blue = tuple(linearize(c) for c in pixel)
37+
return 0.2126 * red + 0.7152 * green + 0.0722 * blue
38+
39+
40+
def linearize(channel, gamma=2.2):
41+
return (channel / 255) ** gamma
42+
43+
44+
if __name__ == "__main__":
45+
try:
46+
check_perf_support()
47+
except RuntimeError as error:
48+
print(error, file=sys.stderr)
49+
else:
50+
main()
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import sys
2+
import sysconfig
3+
from math import exp, log
4+
5+
from PIL import Image
6+
7+
8+
def check_perf_support():
9+
if sys.version_info < (3, 12):
10+
version = sysconfig.get_python_version()
11+
raise RuntimeError(f"This is Python {version}, not 3.12 or later")
12+
13+
if not sysconfig.get_config_var("PY_HAVE_PERF_TRAMPOLINE"):
14+
raise RuntimeError("Python doesn't support perf on this platform")
15+
16+
if not sys.is_stack_trampoline_active():
17+
raise RuntimeError("Did you forget the '-X perf' option?")
18+
19+
cflags = sysconfig.get_config_var("CONFIGURE_CFLAGS")
20+
if "-fno-omit-frame-pointer" not in cflags:
21+
print("Python compiled without the frame pointer", file=sys.stderr)
22+
23+
24+
def main():
25+
image = Image.open("image.jpg")
26+
print("luminance =", get_average_luminance(image.getdata()))
27+
image.show()
28+
29+
30+
def get_average_luminance(pixels):
31+
return exp(mean([log(luminance(pixel) + 1e-9) for pixel in pixels]))
32+
33+
34+
def mean(n):
35+
return sum(n) / len(n)
36+
37+
38+
def luminance(pixel):
39+
red, green, blue = tuple(linearize(c) for c in pixel)
40+
return 0.2126 * red + 0.7152 * green + 0.0722 * blue
41+
42+
43+
def linearize(channel, gamma=2.2):
44+
return (channel / 255) ** gamma
45+
46+
47+
if __name__ == "__main__":
48+
try:
49+
check_perf_support()
50+
except RuntimeError as error:
51+
print(error, file=sys.stderr)
52+
else:
53+
main()

python-312/perf-profiler/censor.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
from argparse import ArgumentParser
3+
4+
5+
def main(args):
6+
censorship = censor(args.modules)
7+
for line in sys.stdin:
8+
stack_trace, num_samples = line.rsplit(maxsplit=1)
9+
symbols = filter(censorship, stack_trace.split(";"))
10+
censored_stack_trace = ";".join(symbols)
11+
if censored_stack_trace:
12+
print(censored_stack_trace, num_samples)
13+
14+
15+
def parse_args():
16+
parser = ArgumentParser()
17+
parser.add_argument(
18+
"-m", "--modules", default=[], type=lambda s: s.split(",")
19+
)
20+
return parser.parse_args()
21+
22+
23+
def censor(modules):
24+
def is_valid(symbol):
25+
if not symbol.startswith("py::"):
26+
return False
27+
if modules:
28+
return any(module in symbol for module in modules)
29+
return True
30+
31+
return is_valid
32+
33+
34+
if __name__ == "__main__":
35+
main(parse_args())

python-312/perf-profiler/image.jpg

630 KB
Loading

python-312/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Pillow==9.5.0

0 commit comments

Comments
 (0)