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 | |
| 27 | class IPerf3Server(log.Origin): |
| 28 | |
| 29 | def __init__(self, suite_run, ip_address): |
| 30 | super().__init__(log.C_RUN, 'iperf3-srv_%s' % ip_address.get('addr')) |
| 31 | self.run_dir = None |
| 32 | self.config_file = None |
| 33 | self.process = None |
| 34 | self.suite_run = suite_run |
| 35 | self.ip_address = ip_address |
| 36 | self._port = DEFAULT_SRV_PORT |
| 37 | |
| 38 | def start(self): |
| 39 | self.log('Starting iperf3-srv') |
| 40 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
| 41 | |
| 42 | pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None, |
| 43 | 'host %s and port not 22' % self.addr()) |
| 44 | |
| 45 | self.log_file = self.run_dir.new_file('iperf3_srv.json') |
| 46 | self.process = process.Process(self.name(), self.run_dir, |
| 47 | ('iperf3', '-s', '-B', self.addr(), |
| 48 | '-p', str(self._port), '-J', |
| 49 | '--logfile', os.path.abspath(self.log_file)), |
| 50 | env={}) |
| 51 | self.suite_run.remember_to_stop(self.process) |
| 52 | self.process.launch() |
| 53 | |
| 54 | def stop(self): |
| 55 | self.suite_run.stop_process(self.process) |
| 56 | |
| 57 | def get_results(self): |
| 58 | with open(self.log_file) as f: |
| 59 | data = json.load(f) |
| 60 | return data |
| 61 | |
| 62 | def addr(self): |
| 63 | return self.ip_address.get('addr') |
| 64 | |
| 65 | def port(self): |
| 66 | return self._port |
| 67 | |
| 68 | def running(self): |
| 69 | return not self.process.terminated() |
| 70 | |
| 71 | def create_client(self): |
| 72 | return IPerf3Client(self.suite_run, self) |
| 73 | |
| 74 | class IPerf3Client(log.Origin): |
| 75 | |
| 76 | def __init__(self, suite_run, iperf3srv): |
| 77 | super().__init__(log.C_RUN, 'iperf3-cli_%s' % iperf3srv.addr()) |
| 78 | self.run_dir = None |
| 79 | self.config_file = None |
| 80 | self.process = None |
| 81 | self.server = iperf3srv |
| 82 | self.suite_run = suite_run |
| 83 | |
| 84 | def run_test(self, netns=None): |
| 85 | self.log('Starting iperf3-client connecting to %s:%d' % (self.server.addr(), self.server.port())) |
| 86 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
| 87 | |
| 88 | pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None, |
| 89 | 'host %s and port not 22' % self.server.addr(), netns) |
| 90 | |
| 91 | self.log_file = self.run_dir.new_file('iperf3_cli.json') |
| 92 | popen_args = ('iperf3', '-c', self.server.addr(), |
| 93 | '-p', str(self.server.port()), '-J', |
| 94 | '--logfile', os.path.abspath(self.log_file)) |
| 95 | if netns: |
| 96 | self.process = process.NetNSProcess(self.name(), self.run_dir, netns, popen_args, env={}) |
| 97 | else: |
| 98 | self.process = process.Process(self.name(), self.run_dir, popen_args, env={}) |
| 99 | process.run_proc_sync(self.process) |
| 100 | return self.get_results() |
| 101 | |
| 102 | def get_results(self): |
| 103 | with open(self.log_file) as f: |
| 104 | data = json.load(f) |
| 105 | return data |
| 106 | |
| 107 | # vim: expandtab tabstop=4 shiftwidth=4 |