Skip to content

Commit

Permalink
add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
mgineer85 committed Nov 4, 2024
1 parent 8b23916 commit aaa63d1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
78 changes: 78 additions & 0 deletions tests/benchmarks/benchmark_encode_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import io
import logging
from multiprocessing import Process
from threading import Thread

import numpy
import pytest
from PIL import Image

# from turbojpeg import TurboJPEG

# turbojpeg = TurboJPEG()
logger = logging.getLogger(name=None)


def encode_fun(frame):
for _ in range(20):
byte_io = io.BytesIO()
img = Image.fromarray(frame.astype("uint8"), "RGB")
img.save(byte_io, "jpeg")


def multiprocess_encode(frame_from_camera):
_encode_process: Process = Process(
target=encode_fun,
name="encode_process",
args=(frame_from_camera,),
daemon=True,
)

_encode_process.start()

# wait until shutdown finished
if _encode_process and _encode_process.is_alive():
_encode_process.join()
_encode_process.close()


def threading_encode(frame_from_camera):
_encode_process: Thread = Thread(
target=encode_fun,
name="encode_thread",
args=(frame_from_camera,),
daemon=True,
)

_encode_process.start()

# wait until shutdown finished
if _encode_process and _encode_process.is_alive():
_encode_process.join()


@pytest.fixture(
params=[
"multiprocess_encode",
"threading_encode",
]
)
def library(request):
# yield fixture instead return to allow for cleanup:
yield request.param

# cleanup
# os.remove(request.param)


@pytest.fixture()
def image_hires():
imarray = numpy.random.rand(2500, 2500, 3) * 255
yield imarray


# needs pip install pytest-benchmark
@pytest.mark.benchmark(group="encode_hires")
def test_libraries_encode_hires(library, image_hires, benchmark):
benchmark(eval(library), frame_from_camera=image_hires)
assert True
3 changes: 2 additions & 1 deletion wigglecam/services/jobservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ def _jobprocessor_fun(self):
assert len(frames) == self._current_job.request.number_captures

# step 2:
# convert to jpg once got all, maybe this can be done in a multiporcessing worker via
# convert to jpg once got all, maybe this can be done in a separate thread worker via
# tx/rx queue to speed up process and reduce memory consumption due to keeping all images in an array
# see benchmarks to check which method to implement later...
for frame in frames:
filename = Path(f"img_{frame.captured_time}_{frame.seq:>03}").with_suffix(".jpg")
filepath = PATH_ORIGINAL / filename
Expand Down

0 comments on commit aaa63d1

Please sign in to comment.