Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: specifics for running an iperf3 client and server |
| 2 | # |
| 3 | # Copyright (C) 2018 by sysmocom - s.f.m.c. GmbH |
| 4 | # |
| 5 | # Author: Pau Espin Pedrol <pespin@sysmocom.de> |
| 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as |
| 9 | # published by the Free Software Foundation, either version 3 of the |
| 10 | # License, or (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import os |
| 21 | import json |
| 22 | |
| 23 | from . import log, util, process, pcap_recorder |
| 24 | |
| 25 | DEFAULT_SRV_PORT = 5003 |
| 26 | |
Pau Espin Pedrol | 64f0b1b | 2018-11-09 17:44:10 +0100 | [diff] [blame] | 27 | |
| 28 | def iperf3_result_to_json(file): |
| 29 | with open(file) as f: |
| 30 | # Sometimes iperf3 provides 2 dictionaries, the 2nd one being an error about being interrupted (by us). |
| 31 | # json parser doesn't support (raises exception) parsing several dictionaries at a time (not a valid json object). |
| 32 | # We are only interested in the first dictionary, the regular results one: |
| 33 | d = f.read().split("\n}\n")[0] + "\n}\n" |
| 34 | data = json.loads(d) |
| 35 | return data |
| 36 | |
| 37 | |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 38 | class IPerf3Server(log.Origin): |
| 39 | |
| 40 | def __init__(self, suite_run, ip_address): |
| 41 | super().__init__(log.C_RUN, 'iperf3-srv_%s' % ip_address.get('addr')) |
| 42 | self.run_dir = None |
| 43 | self.config_file = None |
| 44 | self.process = None |
| 45 | self.suite_run = suite_run |
| 46 | self.ip_address = ip_address |
| 47 | self._port = DEFAULT_SRV_PORT |
| 48 | |
| 49 | def start(self): |
| 50 | self.log('Starting iperf3-srv') |
| 51 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
| 52 | |
| 53 | pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None, |
| 54 | 'host %s and port not 22' % self.addr()) |
| 55 | |
| 56 | self.log_file = self.run_dir.new_file('iperf3_srv.json') |
| 57 | self.process = process.Process(self.name(), self.run_dir, |
| 58 | ('iperf3', '-s', '-B', self.addr(), |
| 59 | '-p', str(self._port), '-J', |
| 60 | '--logfile', os.path.abspath(self.log_file)), |
| 61 | env={}) |
| 62 | self.suite_run.remember_to_stop(self.process) |
| 63 | self.process.launch() |
| 64 | |
| 65 | def stop(self): |
| 66 | self.suite_run.stop_process(self.process) |
| 67 | |
| 68 | def get_results(self): |
Pau Espin Pedrol | 64f0b1b | 2018-11-09 17:44:10 +0100 | [diff] [blame] | 69 | return iperf3_result_to_json(self.log_file) |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 70 | |
| 71 | def addr(self): |
| 72 | return self.ip_address.get('addr') |
| 73 | |
| 74 | def port(self): |
| 75 | return self._port |
| 76 | |
| 77 | def running(self): |
| 78 | return not self.process.terminated() |
| 79 | |
| 80 | def create_client(self): |
| 81 | return IPerf3Client(self.suite_run, self) |
| 82 | |
| 83 | class IPerf3Client(log.Origin): |
| 84 | |
| 85 | def __init__(self, suite_run, iperf3srv): |
| 86 | super().__init__(log.C_RUN, 'iperf3-cli_%s' % iperf3srv.addr()) |
| 87 | self.run_dir = None |
| 88 | self.config_file = None |
| 89 | self.process = None |
| 90 | self.server = iperf3srv |
| 91 | self.suite_run = suite_run |
| 92 | |
| 93 | def run_test(self, netns=None): |
| 94 | self.log('Starting iperf3-client connecting to %s:%d' % (self.server.addr(), self.server.port())) |
| 95 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
| 96 | |
| 97 | pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None, |
| 98 | 'host %s and port not 22' % self.server.addr(), netns) |
| 99 | |
| 100 | self.log_file = self.run_dir.new_file('iperf3_cli.json') |
| 101 | popen_args = ('iperf3', '-c', self.server.addr(), |
| 102 | '-p', str(self.server.port()), '-J', |
| 103 | '--logfile', os.path.abspath(self.log_file)) |
| 104 | if netns: |
| 105 | self.process = process.NetNSProcess(self.name(), self.run_dir, netns, popen_args, env={}) |
| 106 | else: |
| 107 | self.process = process.Process(self.name(), self.run_dir, popen_args, env={}) |
Pau Espin Pedrol | 79df739 | 2018-11-12 18:15:30 +0100 | [diff] [blame] | 108 | self.process.launch_sync() |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 109 | return self.get_results() |
| 110 | |
| 111 | def get_results(self): |
Pau Espin Pedrol | 64f0b1b | 2018-11-09 17:44:10 +0100 | [diff] [blame] | 112 | return iperf3_result_to_json(self.log_file) |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 113 | |
| 114 | # vim: expandtab tabstop=4 shiftwidth=4 |