Skip to content

Commit 0470f10

Browse files
committed
updating benchmarks[docs]
1 parent d142345 commit 0470f10

13 files changed

+176
-134
lines changed

benchmarks/benchmark_decode.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import sys
2+
from os import path
3+
from subprocess import run
4+
from time import perf_counter
5+
6+
import matplotlib.pyplot as plt
7+
from cpuinfo import get_cpu_info
8+
9+
VERSIONS = ["0.1.6", "0.1.11", "0.2.1", "0.2.5", "0.3.2", "0.4.0", "0.5.1", "0.6.0", "0.7.0", "0.7.2", "0.8.0", "0.9.0"]
10+
N_ITER_SMALL = 100
11+
N_ITER_LARGE = 50
12+
13+
14+
def measure_decode(image, n_iterations, decode_threads=4):
15+
measure_file = path.join(path.dirname(path.abspath(__file__)), "measure_decode.py")
16+
cmd = f"{sys.executable} {measure_file} {n_iterations} {image} {decode_threads}".split()
17+
start_time = perf_counter()
18+
run(cmd, check=True)
19+
total_time = perf_counter() - start_time
20+
return total_time / n_iterations
21+
22+
23+
def plot_large_by_threads(clr, n_threads):
24+
plt.plot(
25+
"0.9.0",
26+
measure_decode("image_large.heic", N_ITER_LARGE, decode_threads=n_threads),
27+
color=clr,
28+
label=f"n_threads={n_threads}",
29+
marker="x",
30+
)
31+
32+
33+
if __name__ == "__main__":
34+
tests_images_path = path.join(path.dirname(path.dirname(path.abspath(__file__))), "tests/images/heif_other")
35+
arrow_image_path = path.join(tests_images_path, "arrow.heic")
36+
cat_image_path = path.join(tests_images_path, "cat.hif")
37+
arrow_image_results = []
38+
cat_image_results = []
39+
small_image_results = []
40+
large_image_results = []
41+
for i, v in enumerate(VERSIONS):
42+
run(f"{sys.executable} -m pip install pillow-heif=={v}".split(), check=True)
43+
arrow_image_results.append(measure_decode(arrow_image_path, N_ITER_SMALL))
44+
cat_image_results.append(measure_decode(cat_image_path, N_ITER_SMALL))
45+
small_image_results.append(measure_decode("image_small.heic", N_ITER_SMALL))
46+
large_image_results.append(measure_decode("image_large.heic", N_ITER_LARGE))
47+
fig, ax = plt.subplots()
48+
ax.plot(VERSIONS, arrow_image_results, label="arrow image")
49+
ax.plot(VERSIONS, cat_image_results, label="cat image")
50+
ax.plot(VERSIONS, small_image_results, label="small image")
51+
ax.plot(VERSIONS, large_image_results, label="large image")
52+
colour = plt.gca().lines[-1].get_color()
53+
plot_large_by_threads(colour, 1)
54+
plot_large_by_threads(colour, 2)
55+
plot_large_by_threads(colour, 8)
56+
plt.ylabel("time to decode(s)")
57+
if sys.platform.lower() == "darwin":
58+
_os = "macOS"
59+
elif sys.platform.lower() == "win32":
60+
_os = "Windows"
61+
else:
62+
_os = "Linux"
63+
plt.xlabel(f"{_os} - {get_cpu_info()['brand_raw']}")
64+
ax.legend()
65+
plt.savefig(f"results_decode_{_os}.png")

benchmarks/benchmark_encode.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import sys
2+
from os import path
3+
from subprocess import run
4+
from time import perf_counter
5+
6+
import matplotlib.pyplot as plt
7+
from cpuinfo import get_cpu_info
8+
9+
VERSIONS = ["0.2.1", "0.2.5", "0.3.2", "0.4.0", "0.5.1", "0.6.0", "0.7.0", "0.7.2", "0.8.0", "0.9.0"]
10+
N_ITER = 25
11+
12+
13+
def measure_encode(image, n_iterations):
14+
measure_file = path.join(path.dirname(path.abspath(__file__)), "measure_encode.py")
15+
cmd = f"{sys.executable} {measure_file} {n_iterations} {image}".split()
16+
start_time = perf_counter()
17+
run(cmd, check=True)
18+
total_time = perf_counter() - start_time
19+
return total_time / n_iterations
20+
21+
22+
if __name__ == "__main__":
23+
rgba_image_results = []
24+
rgb_image_results = []
25+
l_image_results = []
26+
for i, v in enumerate(VERSIONS):
27+
run(f"{sys.executable} -m pip install pillow-heif=={v}".split(), check=True)
28+
rgba_image_results.append(measure_encode("RGBA", N_ITER))
29+
rgb_image_results.append(measure_encode("RGB", N_ITER))
30+
l_image_results.append(measure_encode("L", N_ITER))
31+
fig, ax = plt.subplots()
32+
ax.plot(VERSIONS, rgba_image_results, label="RGBA image")
33+
ax.plot(VERSIONS, rgb_image_results, label="RGB image")
34+
ax.plot(VERSIONS, l_image_results, label="L image")
35+
plt.ylabel("time to encode(s)")
36+
if sys.platform.lower() == "darwin":
37+
_os = "macOS"
38+
elif sys.platform.lower() == "win32":
39+
_os = "Windows"
40+
else:
41+
_os = "Linux"
42+
plt.xlabel(f"{_os} - {get_cpu_info()['brand_raw']}")
43+
ax.legend()
44+
plt.savefig(f"results_encode_{_os}.png")

benchmarks/image_large.heic

8.26 MB
Binary file not shown.

benchmarks/image_small.heic

1.39 MB
Binary file not shown.

benchmarks/measure_decode.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import sys
2+
3+
from PIL import Image
4+
5+
from pillow_heif import __version__, register_heif_opener
6+
7+
if __name__ == "__main__":
8+
_args = {}
9+
if __version__ != "0.1.6":
10+
_args["decode_threads"] = int(sys.argv[3])
11+
register_heif_opener(**_args)
12+
for i in range(int(sys.argv[1])):
13+
im = Image.open(sys.argv[2])
14+
im.load()
15+
sys.exit(0)

benchmarks/measure_encode.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import sys
2+
from io import BytesIO
3+
4+
from PIL import Image
5+
6+
from pillow_heif import __version__, register_heif_opener
7+
8+
L_IMAGE = Image.effect_mandelbrot((4096, 4096), (-3, -2.5, 2, 2.5), 100)
9+
RGB_IMAGE = Image.merge("RGB", [L_IMAGE, L_IMAGE.transpose(Image.ROTATE_90), L_IMAGE.transpose(Image.ROTATE_180)])
10+
RGBA_IMAGE = Image.merge(
11+
"RGBA",
12+
[
13+
L_IMAGE,
14+
L_IMAGE.transpose(Image.ROTATE_90),
15+
L_IMAGE.transpose(Image.ROTATE_180),
16+
L_IMAGE.transpose(Image.ROTATE_270),
17+
],
18+
)
19+
20+
21+
if __name__ == "__main__":
22+
_args = {}
23+
register_heif_opener(**_args)
24+
if sys.argv[2] == "RGBA":
25+
img = RGBA_IMAGE
26+
elif sys.argv[2] == "RGB":
27+
img = RGB_IMAGE
28+
else:
29+
if __version__ == "0.2.1":
30+
img = RGB_IMAGE
31+
else:
32+
img = L_IMAGE
33+
for i in range(int(sys.argv[1])):
34+
buf = BytesIO()
35+
img.save(buf, format="HEIF")
36+
sys.exit(0)
50.5 KB
Loading
44.2 KB
Loading
28.8 KB
Loading
29.3 KB
Loading

0 commit comments

Comments
 (0)