|
| 1 | +import subprocess |
| 2 | +import pytest |
| 3 | +import re |
| 4 | + |
| 5 | +@pytest.mark.d457 |
| 6 | +@pytest.mark.parametrize("frames", {150}) |
| 7 | +@pytest.mark.parametrize("device", {'0', '2'}) |
| 8 | +def test_fps(device, frames): |
| 9 | + try: |
| 10 | + print(f"\nDevice: {device}") |
| 11 | + formats = get_formats(device) |
| 12 | + for w, h in formats: |
| 13 | + print(f"Format: {w}x{h}") |
| 14 | + for FPS in formats[(w, h)]: |
| 15 | + cmd = [ "v4l2-ctl", |
| 16 | + f"-d{device}", |
| 17 | + f"--set-fmt-video=width={w},height={h}", |
| 18 | + ] |
| 19 | + subprocess.check_call(cmd) |
| 20 | + print(f"FPS/{FPS}:", end=' ') |
| 21 | + cmd = [ "v4l2-ctl", |
| 22 | + f"-d{device}", |
| 23 | + "-p", |
| 24 | + f"{FPS}", |
| 25 | + ] |
| 26 | + subprocess.check_call(cmd, stdout=subprocess.DEVNULL) |
| 27 | + cmd = [ "v4l2-ctl", |
| 28 | + f"-d{device}", |
| 29 | + "--stream-mmap", |
| 30 | + "--stream-count", |
| 31 | + f"{frames}", |
| 32 | + "--verbose", |
| 33 | + ] |
| 34 | + timeout = 4.0 * frames / FPS |
| 35 | + output = subprocess.run(cmd, |
| 36 | + check=True, |
| 37 | + text=True, |
| 38 | + capture_output=True, |
| 39 | + timeout=timeout).stderr.splitlines() |
| 40 | + last = None |
| 41 | + skip = True # skip first FPS measurement |
| 42 | + kpi = 5 # [%] |
| 43 | + count = 0 |
| 44 | + for line in output: |
| 45 | + m = re.search(r"cap dqbuf:.*seq:\s*(\d*) bytesused:", line) |
| 46 | + if m: |
| 47 | + count += 1 |
| 48 | + frame = int(m.group(1)) |
| 49 | + # print(f"{frame}", end='') |
| 50 | + if last: |
| 51 | + assert frame > last, f"Repeated frame: {frame}" |
| 52 | + assert (frame - last) < 3, f"Frames dropped between: {last} and {frame}" |
| 53 | + m = re.search(r"delta:\s*(\d+\.\d+) ms", line) |
| 54 | + if m: |
| 55 | + fps = 1000 / float(m.group(1)) |
| 56 | + # print(f"/{fps:.2f}", end='') |
| 57 | + if not skip: |
| 58 | + assert fps > FPS * (1 - kpi/100), f"FPS too low: {fps:.2f}/{FPS}" |
| 59 | + assert fps < FPS * (1 + kpi/100), f"FPS too high: {fps:.2f}/{FPS}" |
| 60 | + else: |
| 61 | + # print('?', end='') |
| 62 | + skip = False |
| 63 | + # print(end=',') |
| 64 | + last = frame |
| 65 | + print() |
| 66 | + assert last, "No frames arrived" |
| 67 | + assert count == frames, f"Missing frames: {count} < {frames}" |
| 68 | + except subprocess.TimeoutExpired: |
| 69 | + assert False, "No frames arrived" |
| 70 | + |
| 71 | +def get_formats(device): |
| 72 | + cmd = [ "v4l2-ctl", |
| 73 | + "-d" + device, |
| 74 | + "--list-formats-ext", |
| 75 | + ] |
| 76 | + output = subprocess.run(cmd, |
| 77 | + check=True, |
| 78 | + text=True, |
| 79 | + capture_output=True |
| 80 | + ).stdout.splitlines() |
| 81 | + formats = {} |
| 82 | + last = None |
| 83 | + for line in output: |
| 84 | + m = re.search(r"\s*Size: Discrete\s*(\d+)x(\d+)", line) |
| 85 | + if m: |
| 86 | + w = int(m.group(1)) |
| 87 | + h = int(m.group(2)) |
| 88 | + last = (w, h) |
| 89 | + if not last in formats: |
| 90 | + formats[last] = set() |
| 91 | + continue |
| 92 | + m = re.search(r"\s*Interval: Discrete.*\((\d+\.\d+)\s+fps\)", line) |
| 93 | + if m: |
| 94 | + fps = float(m.group(1)) |
| 95 | + if last: |
| 96 | + formats[last].add(fps) |
| 97 | + return formats |
0 commit comments