Daniel Wagner | 611b541 | 2019-07-22 11:24:20 +0200 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # SPDX-License-Identifier: MIT |
| 3 | # |
| 4 | # MIT License |
| 5 | # |
| 6 | # Copyright (c) Daniel Wagner |
| 7 | # |
| 8 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | # of this software and associated documentation files (the "Software"), to deal |
| 10 | # in the Software without restriction, including without limitation the rights |
| 11 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | # copies of the Software, and to permit persons to whom the Software is |
| 13 | # furnished to do so, subject to the following conditions: |
| 14 | # |
| 15 | # The above copyright notice and this permission notice shall be included in |
| 16 | # all copies or substantial portions of the Software. |
| 17 | # |
| 18 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | # SOFTWARE. |
| 25 | # |
| 26 | |
| 27 | import os |
| 28 | import re |
| 29 | import sys |
| 30 | |
| 31 | |
| 32 | def print_res(res, key, thr): |
| 33 | val = res[key] |
| 34 | label = 'pass' |
| 35 | if key == 'max': |
| 36 | if int(val) >= int(thr): |
| 37 | label = 'fail' |
| 38 | print('t{}-{}-latency {} {} us'.format(res['t'], key, label, val)) |
| 39 | |
| 40 | |
| 41 | def get_lastlines(filename): |
| 42 | # Start reading from the end of the file until ESC is found |
| 43 | with open(filename, 'rb') as f: |
| 44 | try: |
| 45 | f.seek(-2, os.SEEK_END) |
| 46 | while f.read(1) != b'\x1b': |
| 47 | f.seek(-2, os.SEEK_CUR) |
| 48 | return f.readlines() |
| 49 | except IOError: |
| 50 | # No ESC found |
| 51 | f.seek(0, os.SEEK_SET) |
| 52 | return f.readlines() |
| 53 | return [] |
| 54 | |
| 55 | |
| 56 | def parse_cyclictest(filename, thr): |
| 57 | fields = ['t', 'min', 'avg', 'max'] |
| 58 | |
| 59 | r = re.compile('[ :\n]+') |
| 60 | for line in get_lastlines(filename): |
| 61 | if not line.startswith('T:'): |
| 62 | continue |
| 63 | |
| 64 | data = [x.lower() for x in r.split(line)] |
| 65 | res = {} |
| 66 | it = iter(data) |
| 67 | for e in it: |
| 68 | if e in fields: |
| 69 | res[e] = next(it) |
| 70 | |
| 71 | print_res(res, 'min', thr) |
| 72 | print_res(res, 'avg', thr) |
| 73 | print_res(res, 'max', thr) |
| 74 | |
| 75 | |
| 76 | def parse_pmqtest(filename, thr): |
| 77 | fields = ['min', 'avg', 'max'] |
| 78 | |
| 79 | rl = re.compile('[ ,:\n]+') |
| 80 | rt = re.compile('[ ,#]+') |
| 81 | for line in get_lastlines(filename): |
| 82 | data = [x.lower() for x in rl.split(line)] |
| 83 | res = {} |
| 84 | it = iter(data) |
| 85 | for e in it: |
| 86 | if e in fields: |
| 87 | res[e] = next(it) |
| 88 | |
| 89 | if not res: |
| 90 | continue |
| 91 | |
| 92 | # The id is constructed from the '#FROM -> #TO' output, e.g. |
| 93 | # #1 -> #0, Min 1, Cur 3, Avg 4, Max 119 |
| 94 | data = rt.split(line) |
| 95 | res['t'] = '{}-{}'.format(data[1], data[3]) |
| 96 | |
| 97 | print_res(res, 'min', thr) |
| 98 | print_res(res, 'avg', thr) |
| 99 | print_res(res, 'max', thr) |
| 100 | |
| 101 | |
| 102 | def main(): |
| 103 | tool = sys.argv[1] |
| 104 | if tool in ['cyclictest', 'signaltest']: |
| 105 | parse_cyclictest(sys.argv[2], int(sys.argv[3])) |
| 106 | elif tool in ['pmqtest']: |
| 107 | parse_pmqtest(sys.argv[2], int(sys.argv[3])) |
| 108 | |
| 109 | |
| 110 | if __name__ == '__main__': |
| 111 | main() |