#include "..\script_component.hpp"

[] spawn {
	// warning: while loop without suspension executes multiple times per frame
	private _counter = 0;
	private _endTime = diag_tickTime + 5;
	private _frameNo = diag_frameNo;
	while { diag_tickTime < _endTime } do
	{
		_counter = _counter + 1;
	};

	private _rawCPS = _counter / (diag_frameNo - _frameNo);
	missionNamespace setVariable [QGVAR(server_raw_cps), _rawCPS];

	// with suspension
	private _counter = 0;
	private _endTime = diag_tickTime + 5;
	private _frameNo = diag_frameNo;
	while { diag_tickTime < _endTime } do
	{
		_counter = _counter + 1;
		uiSleep 0.001; // waits at least 1 frame
	};

	private _serverCPS = _counter / (diag_frameNo - _frameNo);
	missionNamespace setVariable [QGVAR(server_cps), _counter / (diag_frameNo - _frameNo)];

	// log to RPT
	[
		
		{_this call EFUNC(common,log);},
		[
			LEVEL_INFO,
			QUOTE(COMPONENT),
			format ["Average Server Execution: %1 times per frame", _serverCPS],
			[
				["serverRawCPS", _rawCPS],
				["serverCPS", _serverCPS]
			]
		]
	] call CBA_fnc_directCall;

	[QGVARMAIN(serverEfficiency), [
		[
			["float", QGVARMAIN(raw_cps), missionNamespace getVariable [QGVAR(server_raw_cps), -1]],
			["float", QGVARMAIN(cps), missionNamespace getVariable [QGVAR(server_cps), -1]]
		]
	]] call CBA_fnc_localEvent;
};

nil;