|
| 1 | +#!/usr/bin/python3 |
| 2 | +""" |
| 3 | +Author: Vertigo Designs, Ryan Summers |
| 4 | +
|
| 5 | +Description: Implements HITL testing of Stabilizer data livestream capabilities. |
| 6 | +""" |
| 7 | +import asyncio |
| 8 | +import sys |
| 9 | +import argparse |
| 10 | +import logging |
| 11 | +import socket |
| 12 | +import time |
| 13 | + |
| 14 | +from miniconf import Miniconf |
| 15 | +from stabilizer.stream import StabilizerStream |
| 16 | + |
| 17 | +# The duration to receive frames for. |
| 18 | +STREAM_TEST_DURATION_SECS = 5.0 |
| 19 | + |
| 20 | +# The minimum efficiency of the stream in frame transfer to pass testing. Represented as |
| 21 | +# (received_frames / transmitted_frames). |
| 22 | +MIN_STREAM_EFFICIENCY = 0.95 |
| 23 | + |
| 24 | +def _get_ip(broker): |
| 25 | + """ Get the IP of the local device. |
| 26 | +
|
| 27 | + Args: |
| 28 | + broker: The broker IP of the test. Used to select an interface to get the IP of. |
| 29 | +
|
| 30 | + Returns: |
| 31 | + The IP as an array of integers. |
| 32 | + """ |
| 33 | + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 34 | + try: |
| 35 | + sock.connect((broker, 1883)) |
| 36 | + address = sock.getsockname()[0] |
| 37 | + finally: |
| 38 | + sock.close() |
| 39 | + |
| 40 | + return list(map(int, address.split('.'))) |
| 41 | + |
| 42 | + |
| 43 | +def sequence_delta(previous_sequence, next_sequence): |
| 44 | + """ Check the number of items between two sequence numbers. """ |
| 45 | + if previous_sequence is None: |
| 46 | + return 0 |
| 47 | + |
| 48 | + delta = next_sequence - (previous_sequence + 1) |
| 49 | + return delta & 0xFFFFFFFF |
| 50 | + |
| 51 | + |
| 52 | +def main(): |
| 53 | + """ Main program entry point. """ |
| 54 | + parser = argparse.ArgumentParser(description='Loopback tests for Stabilizer HITL testing',) |
| 55 | + parser.add_argument('prefix', type=str, |
| 56 | + help='The MQTT topic prefix of the target') |
| 57 | + parser.add_argument('--broker', '-b', default='mqtt', type=str, |
| 58 | + help='The MQTT broker address') |
| 59 | + parser.add_argument('--port', '-p', default=2000, type=int, |
| 60 | + help='The UDP port to use for streaming') |
| 61 | + |
| 62 | + args = parser.parse_args() |
| 63 | + |
| 64 | + async def test(): |
| 65 | + """ The actual testing being completed. """ |
| 66 | + local_ip = _get_ip(args.broker) |
| 67 | + interface = await Miniconf.create(args.prefix, args.broker) |
| 68 | + stream = StabilizerStream(args.port, timeout=0.5) |
| 69 | + |
| 70 | + # Configure the stream |
| 71 | + print(f'Configuring stream to target {".".join(map(str, local_ip))}:{args.port}') |
| 72 | + print('') |
| 73 | + await interface.command('stream_target', {'ip': local_ip, 'port': args.port}, retain=False) |
| 74 | + await interface.command('telemetry_period', 10, retain=False) |
| 75 | + |
| 76 | + # Verify frame reception |
| 77 | + print('Testing stream reception') |
| 78 | + print('') |
| 79 | + last_sequence = None |
| 80 | + |
| 81 | + # Sample frames over a set time period and verify that no drops are encountered. |
| 82 | + stop = time.time() + STREAM_TEST_DURATION_SECS |
| 83 | + dropped_frames = 0 |
| 84 | + total_frames = 0 |
| 85 | + |
| 86 | + while time.time() < stop: |
| 87 | + for (seqnum, _data) in stream.read_frame(): |
| 88 | + num_dropped = sequence_delta(last_sequence, seqnum) |
| 89 | + total_frames += 1 + num_dropped |
| 90 | + |
| 91 | + if num_dropped: |
| 92 | + dropped_frames += num_dropped |
| 93 | + logging.warning('Frame drop detected: 0x%08X -> 0x%08X (%d frames)', |
| 94 | + last_sequence, seqnum, num_dropped) |
| 95 | + |
| 96 | + last_sequence = seqnum |
| 97 | + |
| 98 | + assert total_frames, 'Stream did not receive any frames' |
| 99 | + stream_efficiency = 1.0 - (dropped_frames / total_frames) |
| 100 | + |
| 101 | + print(f'Stream Reception Rate: {stream_efficiency * 100:.2f} %') |
| 102 | + print(f'Received {total_frames} frames') |
| 103 | + print(f'Lost {dropped_frames} frames') |
| 104 | + |
| 105 | + assert stream_efficiency > MIN_STREAM_EFFICIENCY, \ |
| 106 | + f'Stream dropped too many packets. Reception rate: {stream_efficiency * 100:.2f} %' |
| 107 | + |
| 108 | + # Disable the stream. |
| 109 | + print('Closing stream') |
| 110 | + print('') |
| 111 | + await interface.command('stream_target', {'ip': [0, 0, 0, 0], 'port': 0}, retain=False) |
| 112 | + stream.clear() |
| 113 | + |
| 114 | + print('Verifying no further data is received') |
| 115 | + try: |
| 116 | + for _ in stream.read_frame(): |
| 117 | + raise Exception('Unexpected data encountered on stream') |
| 118 | + except socket.timeout: |
| 119 | + pass |
| 120 | + print('PASS') |
| 121 | + |
| 122 | + |
| 123 | + loop = asyncio.get_event_loop() |
| 124 | + sys.exit(loop.run_until_complete(test())) |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == '__main__': |
| 128 | + main() |
0 commit comments