module BTS_Tests_perf {

/* Performance Tests for OsmoBTS
 * (C) 2020 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
 * All rights reserved.
 *
 * Released under the terms of GNU General Public License, Version 2 or
 * (at your option) any later version.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * This test suite tests performance of OsmoBTS under different heavy load scenarios.
 */

import from Misc_Helpers all;
import from General_Types all;
import from Osmocom_Types all;
import from GSM_Types all;
import from L1CTL_PortType all;
import from L1CTL_Types all;
import from LAPDm_Types all;
import from IPA_Emulation all;
import from GSM_RR_Types all;

import from RSL_Types all;
import from RSL_Emulation all;

import from PCUIF_Types all;
import from PCUIF_CodecPort all;

import from Osmocom_VTY_Functions all;

import from BTS_Tests all;

/***********************************************************************
 * Performance tests. Expect osmo-bts to be configured with all TRX as TCH/H.
 ***********************************************************************/

modulepar {
	float mp_wait_time := 10.0;
}


/* This test requires BTS with 1 TRX to be configured with following timeslots: TS[0]=CCCH+SDCCH4, TS[1..7]: TCH/H
 * One can simply take the osmo-bsc.cfg in the same dir and change TS1..7, that's all needed.
 * It will activate TS1..7 TCH/Hchannels (2 TCH/H per TS, that's 14 channels)
 * and wait for requested time. This test is useful to bring the BTS (+BTS-TRX)
 * into a high channel load state to check that the system it runs on can keep
 * on with full load.
 */
function f_TC_highchanload_tchh(charstring id) runs on ConnHdlr {
	var ChannelNrs chan_nr := { /* TS 1..7: TCH/H */
		valueof(ts_RslChanNr_Lm(1,0)), valueof(ts_RslChanNr_Lm(1,1)),
		valueof(ts_RslChanNr_Lm(2,0)), valueof(ts_RslChanNr_Lm(2,1)),
		valueof(ts_RslChanNr_Lm(3,0)), valueof(ts_RslChanNr_Lm(3,1)),
		valueof(ts_RslChanNr_Lm(4,0)), valueof(ts_RslChanNr_Lm(4,1)),
		valueof(ts_RslChanNr_Lm(5,0)), valueof(ts_RslChanNr_Lm(5,1)),
		valueof(ts_RslChanNr_Lm(6,0)), valueof(ts_RslChanNr_Lm(6,1)),
		valueof(ts_RslChanNr_Lm(7,0)), valueof(ts_RslChanNr_Lm(7,1))
		};

	log("Started");
	for (var integer i := 0; i < sizeof(chan_nr); i := i+1) {
		log("Registering ", chan_nr[i]);
		f_rslem_register(0, chan_nr[i]);
	}
	log("Registered");
	for (var integer i := 0; i < sizeof(chan_nr); i := i+1) {
		f_rsl_transceive(ts_RSL_CHAN_ACT(chan_nr[i],
				 ts_RSL_ChanMode(RSL_CHRT_TCH_H, RSL_CMOD_SP_GSM3 /* AMR*/)),
				 tr_RSL_CHAN_ACT_ACK(chan_nr[i]),
				 log2str("RSL CHAN ACT [", i, "]"));
	}
	log("Activated, now waiting ", mp_wait_time, " seconds");

	f_sleep(mp_wait_time);
	log("sleep done, deactivating");

	for (var integer i := 0; i < sizeof(chan_nr); i := i+1) {
		f_rsl_transceive(ts_RSL_RF_CHAN_REL(chan_nr[i]),
				 tr_RSL_RF_CHAN_REL_ACK(chan_nr[i]),
				 log2str("RF CHAN REL [", i, "]"),
				 true);
	}
	setverdict(pass);
}
testcase TC_highchanload_tchh() runs on test_CT {
	var ConnHdlr vc_conn; /* 1..7 * 2 */
	var ConnHdlrPars pars := valueof(t_Pars(t_RslChanNr_Bm(1), ts_RSL_ChanMode_SIGN,
						t_guard := 60.0 + mp_wait_time));

	f_init();

	vc_conn := f_start_handler(refers(f_TC_highchanload_tchh), pars);
	vc_conn.done;
}

control {
	execute( TC_highchanload_tchh() );
}


}
