Holger Hans Peter Freyther | 574e62f | 2018-06-20 09:15:15 +0100 | [diff] [blame] | 1 | # osmo_ms_driver: Locationg Update Test |
| 2 | # Create MS's and wait for the Location Update to succeed. |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 3 | # |
| 4 | # Copyright (C) 2018 by Holger Hans Peter Freyther |
| 5 | # |
| 6 | # This program is free software: you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as |
| 8 | # published by the Free Software Foundation, either version 3 of the |
| 9 | # License, or (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | |
Holger Hans Peter Freyther | 99a6a41 | 2018-08-29 04:24:38 +0100 | [diff] [blame] | 19 | from copy import copy |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 20 | from osmo_gsm_tester import log |
| 21 | from .starter import OsmoVirtPhy, OsmoMobile |
Holger Hans Peter Freyther | 574e62f | 2018-06-20 09:15:15 +0100 | [diff] [blame] | 22 | from .test_support import imsi_ki_gen, Results |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 23 | |
| 24 | from datetime import timedelta |
| 25 | |
| 26 | import time |
| 27 | |
Holger Hans Peter Freyther | 574e62f | 2018-06-20 09:15:15 +0100 | [diff] [blame] | 28 | class LUResult(Results): |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 29 | |
| 30 | def __init__(self, name): |
Holger Hans Peter Freyther | 574e62f | 2018-06-20 09:15:15 +0100 | [diff] [blame] | 31 | super().__init__(name) |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 32 | self._time_of_lu = None |
| 33 | |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 34 | def set_lu_time(self, time): |
| 35 | assert self._time_of_lu is None |
| 36 | self._time_of_lu = time |
| 37 | |
Holger Hans Peter Freyther | 0f6e410 | 2018-06-23 15:52:25 +0100 | [diff] [blame] | 38 | def has_lu_time(self): |
| 39 | return self._time_of_lu is not None |
| 40 | |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 41 | def lu_time(self): |
| 42 | return self._time_of_lu or 0 |
| 43 | |
| 44 | def lu_delay(self): |
| 45 | return self.lu_time() - self.start_time() |
| 46 | |
| 47 | class MassUpdateLocationTest(log.Origin): |
| 48 | """ |
| 49 | A test to launch a configurable amount of MS and make them |
| 50 | execute a Location Updating Procedure. |
| 51 | |
| 52 | Configure the number of MS to be tested and a function that |
| 53 | decides how quickly to start them and a timeout. |
| 54 | """ |
| 55 | |
| 56 | TEMPLATE_LUA = "osmo-mobile-lu.lua" |
| 57 | TEMPLATE_CFG = "osmo-mobile.cfg" |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 58 | |
| 59 | def __init__(self, name, number_of_ms, cdf_function, event_server, tmp_dir): |
| 60 | super().__init__(log.C_RUN, name) |
| 61 | self._number_of_ms = number_of_ms |
| 62 | self._cdf = cdf_function |
| 63 | self._cdf.set_target(number_of_ms) |
| 64 | self._unstarted = [] |
Holger Hans Peter Freyther | 99a6a41 | 2018-08-29 04:24:38 +0100 | [diff] [blame] | 65 | self._mobiles = [] |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 66 | self._phys = [] |
| 67 | self._results = {} |
| 68 | imsi_gen = imsi_ki_gen() |
| 69 | |
Holger Hans Peter Freyther | 0f6e410 | 2018-06-23 15:52:25 +0100 | [diff] [blame] | 70 | self._outstanding = number_of_ms |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 71 | for i in range(0, number_of_ms): |
| 72 | ms_name = "%.5d" % i |
| 73 | |
| 74 | phy = OsmoVirtPhy(ms_name, tmp_dir) |
| 75 | self._phys.append(phy) |
| 76 | |
| 77 | launcher = OsmoMobile(ms_name, tmp_dir, self.TEMPLATE_LUA, |
| 78 | self.TEMPLATE_CFG, imsi_gen, |
| 79 | phy.phy_filename(), |
| 80 | event_server.server_path()) |
Holger Hans Peter Freyther | 574e62f | 2018-06-20 09:15:15 +0100 | [diff] [blame] | 81 | self._results[ms_name] = LUResult(ms_name) |
Holger Hans Peter Freyther | 99a6a41 | 2018-08-29 04:24:38 +0100 | [diff] [blame] | 82 | self._mobiles.append(launcher) |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 83 | self._event_server = event_server |
| 84 | self._event_server.register(self.handle_msg) |
Holger Hans Peter Freyther | 99a6a41 | 2018-08-29 04:24:38 +0100 | [diff] [blame] | 85 | self._unstarted = copy(self._mobiles) |
Holger Hans Peter Freyther | b697b3b | 2018-08-29 04:26:20 +0100 | [diff] [blame] | 86 | self._started = [] |
Holger Hans Peter Freyther | 99a6a41 | 2018-08-29 04:24:38 +0100 | [diff] [blame] | 87 | |
| 88 | def mobiles(self): |
| 89 | return self._mobiles |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 90 | |
| 91 | def pre_launch(self, loop): |
| 92 | """ |
| 93 | We need the virtphy's be ready when the lua script in the |
| 94 | mobile comes and kicks-off the test. In lua we don't seem to |
| 95 | be able to just stat/check if a file/socket exists so we need |
| 96 | to do this from here. |
| 97 | """ |
| 98 | self.log("Pre-launching all virtphy's") |
| 99 | for phy in self._phys: |
| 100 | phy.start(loop) |
| 101 | |
| 102 | self.log("Checking if sockets are in the filesystem") |
| 103 | for phy in self._phys: |
| 104 | phy.verify_ready() |
| 105 | |
Holger Hans Peter Freyther | 6926c57 | 2018-06-23 19:58:38 +0100 | [diff] [blame] | 106 | def prepare(self, loop): |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 107 | self.log("Starting testcase") |
| 108 | |
| 109 | self.pre_launch(loop) |
| 110 | |
| 111 | self._start_time = time.clock_gettime(time.CLOCK_MONOTONIC) |
| 112 | self._end_time = self._start_time + \ |
| 113 | self._cdf.duration().total_seconds() + \ |
| 114 | timedelta(seconds=120).total_seconds() |
| 115 | |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 116 | self._started = [] |
Holger Hans Peter Freyther | 6926c57 | 2018-06-23 19:58:38 +0100 | [diff] [blame] | 117 | self._too_slow = 0 |
| 118 | |
| 119 | def step_once(self, loop, current_time): |
| 120 | if len(self._unstarted) <= 0: |
| 121 | return current_time, None |
| 122 | |
| 123 | step_size = self._cdf.step_size().total_seconds() |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 124 | |
| 125 | # Start |
| 126 | self._cdf.step_once() |
| 127 | |
Holger Hans Peter Freyther | 6926c57 | 2018-06-23 19:58:38 +0100 | [diff] [blame] | 128 | # Check for timeout |
| 129 | # start pending MS |
| 130 | while len(self._started) < self._cdf.current_scaled_value() and len(self._unstarted) > 0: |
| 131 | ms = self._unstarted.pop(0) |
| 132 | ms.start(loop) |
| 133 | launch_time = time.clock_gettime(time.CLOCK_MONOTONIC) |
| 134 | self._results[ms.name_number()].set_launch_time(launch_time) |
| 135 | self._started.append(ms) |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 136 | |
Holger Hans Peter Freyther | 6926c57 | 2018-06-23 19:58:38 +0100 | [diff] [blame] | 137 | now_time = time.clock_gettime(time.CLOCK_MONOTONIC) |
| 138 | sleep_time = (current_time + step_size) - now_time |
| 139 | if sleep_time <= 0: |
| 140 | self.log("Starting too slowly. Moving on", |
| 141 | target=(current_time + step_size), now=now_time, sleep=sleep_time) |
| 142 | self._too_slow += 1 |
| 143 | sleep_time = 0 |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 144 | |
Holger Hans Peter Freyther | 6926c57 | 2018-06-23 19:58:38 +0100 | [diff] [blame] | 145 | if len(self._unstarted) == 0: |
| 146 | end_time = time.clock_gettime(time.CLOCK_MONOTONIC) |
| 147 | self.log("All started...", too_slow=self._too_slow, duration=end_time - self._start_time) |
| 148 | return current_time, None |
| 149 | |
| 150 | return current_time + step_size, sleep_time |
| 151 | |
Holger Hans Peter Freyther | 1dc9a04 | 2018-06-23 22:27:23 +0100 | [diff] [blame] | 152 | def run_test(self, loop, test_duration): |
Holger Hans Peter Freyther | 6926c57 | 2018-06-23 19:58:38 +0100 | [diff] [blame] | 153 | self.prepare(loop) |
| 154 | |
Holger Hans Peter Freyther | 1dc9a04 | 2018-06-23 22:27:23 +0100 | [diff] [blame] | 155 | to_complete_time = self._start_time + test_duration.total_seconds() |
Holger Hans Peter Freyther | 6926c57 | 2018-06-23 19:58:38 +0100 | [diff] [blame] | 156 | tick_time = self._start_time |
| 157 | |
| 158 | while not self.all_completed(): |
| 159 | tick_time, sleep_time = self.step_once(loop, tick_time) |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 160 | now_time = time.clock_gettime(time.CLOCK_MONOTONIC) |
Holger Hans Peter Freyther | 6926c57 | 2018-06-23 19:58:38 +0100 | [diff] [blame] | 161 | if sleep_time is None: |
| 162 | sleep_time = to_complete_time - now_time |
| 163 | if sleep_time < 0: |
| 164 | break |
| 165 | loop.schedule_timeout(sleep_time) |
| 166 | loop.select() |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 167 | |
| 168 | def stop_all(self): |
| 169 | for launcher in self._started: |
| 170 | launcher.kill() |
| 171 | |
Holger Hans Peter Freyther | 05895a9 | 2018-06-17 19:31:46 +0100 | [diff] [blame] | 172 | def handle_msg(self, _data, addr, time): |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 173 | import json |
| 174 | data = json.loads(_data.decode()) |
| 175 | |
| 176 | if data['type'] == 'register': |
| 177 | ms = self._results[data['ms']] |
| 178 | ms.set_start_time(time) |
| 179 | launch_delay = ms.start_time() - ms.launch_time() |
| 180 | self.log("MS start registered ", ms=ms, at=time, delay=launch_delay) |
| 181 | elif data['type'] == 'event': |
| 182 | if data['data']['lu_done'] == 1: |
| 183 | ms = self._results[data['ms']] |
Holger Hans Peter Freyther | 0f6e410 | 2018-06-23 15:52:25 +0100 | [diff] [blame] | 184 | if not ms.has_lu_time(): |
| 185 | self._outstanding = self._outstanding - 1 |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 186 | ms.set_lu_time(time) |
| 187 | self.log("MS performed LU ", ms=ms, at=time, lu_delay=ms.lu_delay()) |
| 188 | else: |
| 189 | print(time, data) |
| 190 | raise Exception("Unknown event type..:" + _data.decode()) |
| 191 | |
| 192 | |
Holger Hans Peter Freyther | 0f6e410 | 2018-06-23 15:52:25 +0100 | [diff] [blame] | 193 | def all_completed(self): |
| 194 | return self._outstanding == 0 |
| 195 | |
Holger Hans Peter Freyther | af522fd | 2018-06-23 21:35:30 +0100 | [diff] [blame] | 196 | def results_min_max(self, results): |
| 197 | min_value = max_value = None |
| 198 | for result in results: |
| 199 | if min_value is None or result.lu_delay() < min_value: |
| 200 | min_value = result.lu_delay() |
| 201 | if max_value is None or result.lu_delay() > max_value: |
| 202 | max_value = result.lu_delay() |
| 203 | return min_value, max_value |
| 204 | |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 205 | def print_stats(self): |
Holger Hans Peter Freyther | 0f6e410 | 2018-06-23 15:52:25 +0100 | [diff] [blame] | 206 | all_completed = self.all_completed() |
Holger Hans Peter Freyther | af522fd | 2018-06-23 21:35:30 +0100 | [diff] [blame] | 207 | min_value, max_value = self.results_min_max(filter(lambda x: x.has_lu_time(), self._results.values())) |
Holger Hans Peter Freyther | 30cc021 | 2018-02-25 21:34:35 +0000 | [diff] [blame] | 208 | |
| 209 | self.log("Tests done", all_completed=all_completed, |
Holger Hans Peter Freyther | af522fd | 2018-06-23 21:35:30 +0100 | [diff] [blame] | 210 | min=min_value, max=max_value) |