missionbenchmark.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // Struct for individual benchmark locations
  2. class BenchmarkLocation
  3. {
  4. bool m_IsDummyTeleport;
  5. bool m_IsDummyWait;
  6. float m_CamSpeedMultiplier = 1;
  7. string m_Name;
  8. vector m_StartPos;
  9. vector m_StartLookAtPos;
  10. void BenchmarkLocation(string name)
  11. {
  12. m_Name = name;
  13. }
  14. void SetPosition(vector start)
  15. {
  16. m_StartPos = start + "0 2.5 0"; // raise to head level
  17. }
  18. // note that vector[1] (height) is ignored during interpolation and setting it has no effect
  19. void SetLookAtPosition(vector start)
  20. {
  21. m_StartLookAtPos = start;
  22. }
  23. void SetCameraSpeedMultiplier(float multiplier)
  24. {
  25. m_CamSpeedMultiplier = multiplier;
  26. }
  27. void SetDummyTeleport()
  28. {
  29. m_IsDummyTeleport = true;
  30. }
  31. void SetDummyWait()
  32. {
  33. m_IsDummyWait = true;
  34. }
  35. }
  36. // Global settings of benchmark
  37. class BenchmarkConfig
  38. {
  39. bool m_LogToRPT;
  40. bool m_DoDevPrints;
  41. int m_PreloadLength = 5;
  42. float m_TimeMultiplier = 1;
  43. string m_CSVName;
  44. ref array<ref BenchmarkLocation> m_Locations = new array<ref BenchmarkLocation>();
  45. void AddLocation(notnull BenchmarkLocation loc)
  46. {
  47. m_Locations.Insert(loc);
  48. }
  49. void AddQuickLocation(string name, vector pos, vector lookAtPos)
  50. {
  51. BenchmarkLocation loc = new BenchmarkLocation(name);
  52. loc.SetPosition(pos);
  53. loc.SetLookAtPosition(lookAtPos);
  54. m_Locations.Insert(loc);
  55. }
  56. void AddTeleport()
  57. {
  58. BenchmarkLocation loc = new BenchmarkLocation("Teleport");
  59. loc.SetDummyTeleport();
  60. m_Locations.Insert(loc);
  61. }
  62. void AddWait()
  63. {
  64. BenchmarkLocation loc = new BenchmarkLocation("Wait");
  65. loc.SetDummyWait();
  66. m_Locations.Insert(loc);
  67. }
  68. // false = csv, true = rpt
  69. void LogToRPT(bool logRPT)
  70. {
  71. m_LogToRPT = logRPT;
  72. }
  73. void DoDevPrints(bool doPrints)
  74. {
  75. m_DoDevPrints = doPrints;
  76. }
  77. // Preload time of individual locations after teleport
  78. void SetPreloadLengthTime(int seconds)
  79. {
  80. m_PreloadLength = seconds;
  81. }
  82. // Time multiplier for quickly testing flypath
  83. void SetTestTimeMultiplier(float multiplier)
  84. {
  85. m_TimeMultiplier = multiplier;
  86. }
  87. void SetCSVName(string name)
  88. {
  89. m_CSVName = name;
  90. }
  91. }
  92. class MissionBenchmark : MissionGameplay
  93. {
  94. protected const int INITIAL_PRELOAD = 5; // seconds, extra preload seconds to compensate for the game launching
  95. protected const float STEP_INTERVAL = 1; // seconds, how much time passes between steps (fps measurements)
  96. protected bool m_InitialLoadDone; // extra time is added to first preload to make up for intial loading of the game
  97. protected bool m_IsPreloading; // preload time between location measurements
  98. protected bool m_LocationDone; // finished measuring current location
  99. protected int m_LocIndex;
  100. protected int m_MeasuringStep;
  101. protected float m_MeasureStepTimer = 1;
  102. protected float m_SumFPS;
  103. protected float m_TimeCounter;
  104. protected float m_MeasureLength;
  105. protected float m_StepDistance;
  106. protected FileHandle m_CSVLog;
  107. protected BenchmarkLocation m_CurrentLocation;
  108. protected BenchmarkLocation m_NextLocation;
  109. protected ref BenchmarkConfig m_Config;
  110. protected static MissionBenchmark m_Instance;
  111. void MissionBenchmark()
  112. {
  113. m_Instance = this;
  114. }
  115. void ~MissionBenchmark()
  116. {
  117. m_Instance = null;
  118. }
  119. static MissionBenchmark GetInstance()
  120. {
  121. return m_Instance;
  122. }
  123. BenchmarkConfig GetConfig()
  124. {
  125. if (!m_Config)
  126. m_Config = new BenchmarkConfig();
  127. return m_Config;
  128. }
  129. void Start()
  130. {
  131. DisableWeatherChange();
  132. if (!m_Config || m_Config.m_Locations.Count() <= 1)
  133. {
  134. OnBenchmarkEnd("Not enough locations defined");
  135. return;
  136. }
  137. if (!m_Config.m_LogToRPT)
  138. CreateCSVLog();
  139. m_IsPreloading = true;
  140. OnLocationSwitch();
  141. }
  142. override void OnUpdate(float timeslice)
  143. {
  144. super.OnUpdate(timeslice);
  145. m_TimeCounter += timeslice * m_Config.m_TimeMultiplier;
  146. if (!m_CurrentLocation)
  147. return;
  148. else if (m_IsPreloading)
  149. PreloadUpdate();
  150. else
  151. MeasureUpdate(timeslice);
  152. }
  153. // Update logic which runs when location is being preloaded
  154. protected void PreloadUpdate()
  155. {
  156. if (!m_InitialLoadDone)
  157. {
  158. if (m_TimeCounter >= (m_Config.m_PreloadLength + INITIAL_PRELOAD)) // end preload
  159. m_InitialLoadDone = true;
  160. }
  161. else if (m_TimeCounter >= m_Config.m_PreloadLength * (1 / m_Config.m_TimeMultiplier)) // end preload
  162. {
  163. m_TimeCounter = 0;
  164. m_IsPreloading = false;
  165. }
  166. }
  167. // Update logic which runs when measurement is in progress
  168. protected void MeasureUpdate(float timeSlice)
  169. {
  170. m_MeasureStepTimer += timeSlice;
  171. if (m_MeasureStepTimer >= STEP_INTERVAL)
  172. {
  173. float avgFps = GetGame().GetFps();
  174. if (m_Config.m_DoDevPrints)
  175. Print( string.Format("Measure step: %1 | FPS: %2" , m_MeasuringStep + 1, 1/avgFps) );
  176. /*if (m_MeasuringStep >= m_MeasureLength) // end of steps
  177. { }
  178. else*/ // next step
  179. m_MeasureStepTimer = 0;
  180. m_SumFPS += ( 1/avgFps );
  181. m_MeasuringStep++;
  182. GetGame().GetPlayer().SetPosition(FreeDebugCamera.GetInstance().GetPosition() - "0 2.5 0");
  183. }
  184. LerpCamera();
  185. }
  186. protected void DisableWeatherChange()
  187. {
  188. Weather weather = GetGame().GetWeather();
  189. weather.SetDynVolFogHeightBias(0,0);
  190. weather.SetDynVolFogHeightDensity(0,0);
  191. weather.SetDynVolFogDistanceDensity(0,0);
  192. weather.SetWeatherUpdateFreeze(true);
  193. }
  194. protected void AdvanceLocation()
  195. {
  196. if (m_NextLocation.m_IsDummyWait && m_MeasuringStep < 5)
  197. return;
  198. string locationName = m_NextLocation.m_Name;
  199. FPSLog(locationName, m_SumFPS/m_MeasuringStep);
  200. if (m_Config.m_DoDevPrints)
  201. Print( string.Format("%1 | Measurements: %2 | Average FPS: %3 | Elapsed time: %4 seconds" ,locationName, m_MeasuringStep, m_SumFPS/m_MeasuringStep ,m_TimeCounter) );
  202. m_LocIndex++;
  203. OnLocationSwitch();
  204. }
  205. // logic for interpolating camera between location points
  206. protected void LerpCamera()
  207. {
  208. float lerpX, lerpZ, lerpY;
  209. vector target = m_NextLocation.m_StartPos;
  210. float camSpeedAdjust = m_CurrentLocation.m_CamSpeedMultiplier * 5 * m_TimeCounter * 1/m_StepDistance;
  211. lerpX = Math.Lerp(m_CurrentLocation.m_StartPos[0], target[0], camSpeedAdjust);
  212. lerpZ = Math.Lerp(m_CurrentLocation.m_StartPos[1], target[1], camSpeedAdjust);
  213. lerpY = Math.Lerp(m_CurrentLocation.m_StartPos[2], target[2], camSpeedAdjust);
  214. if (camSpeedAdjust >= 1 || m_NextLocation.m_IsDummyWait)
  215. {
  216. AdvanceLocation();
  217. return;
  218. }
  219. FreeDebugCamera.GetInstance().SetPosition( Vector(lerpX, lerpZ, lerpY) );
  220. target = m_NextLocation.m_StartLookAtPos;
  221. lerpX = Math.Lerp(m_CurrentLocation.m_StartLookAtPos[0], target[0], camSpeedAdjust);
  222. //lerpZ = Math.Lerp(m_CurrentLocation.m_StartLookAtPos[1], target[1], camSpeedAdjust); // ignored as it causes issues with lerping between look at points
  223. lerpY = Math.Lerp(m_CurrentLocation.m_StartLookAtPos[2], target[2], camSpeedAdjust);
  224. FreeDebugCamera.GetInstance().LookAt( Vector(lerpX, lerpZ, lerpY) );
  225. }
  226. protected void OnLocationSwitch()
  227. {
  228. if (m_LocIndex >= (m_Config.m_Locations.Count() - 1))
  229. {
  230. OnBenchmarkEnd("Test finished!");
  231. return;
  232. }
  233. m_MeasureStepTimer = 1; // tick first measurement straight after preload
  234. m_SumFPS = 0;
  235. m_MeasuringStep = 0;
  236. m_TimeCounter = 0;
  237. m_CurrentLocation = m_Config.m_Locations[m_LocIndex];
  238. m_NextLocation = m_Config.m_Locations[m_LocIndex+1];
  239. m_StepDistance = vector.Distance(m_CurrentLocation.m_StartPos, m_NextLocation.m_StartPos);
  240. if (!GetGame().GetPlayer())
  241. {
  242. CreatePlayer();
  243. TeleportToPos(m_CurrentLocation);
  244. }
  245. if (m_NextLocation.m_IsDummyTeleport) // flycam teleport
  246. {
  247. m_LocIndex += 2;
  248. if (m_LocIndex >= (m_Config.m_Locations.Count() - 1))
  249. {
  250. OnBenchmarkEnd("Test finished!");
  251. return;
  252. }
  253. else
  254. {
  255. m_CurrentLocation = m_Config.m_Locations[m_LocIndex];
  256. m_NextLocation = m_Config.m_Locations[m_LocIndex+1];
  257. m_StepDistance = vector.Distance(m_CurrentLocation.m_StartPos, m_NextLocation.m_StartPos);
  258. TeleportToPos(m_CurrentLocation);
  259. }
  260. }
  261. if (m_NextLocation.m_IsDummyWait)
  262. {
  263. m_NextLocation.m_Name = m_CurrentLocation.m_Name + " Wait";
  264. m_NextLocation.m_StartPos = m_CurrentLocation.m_StartPos;
  265. m_NextLocation.m_StartLookAtPos = m_CurrentLocation.m_StartLookAtPos;
  266. }
  267. if (m_Config.m_DoDevPrints)
  268. {
  269. Print(string.Format("================"));
  270. Print(string.Format("%1 test begin" , m_CurrentLocation.m_Name + " -> " + m_NextLocation.m_Name));
  271. }
  272. }
  273. protected void TeleportToPos(BenchmarkLocation loc)
  274. {
  275. FreeDebugCamera.GetInstance().SetPosition(loc.m_StartPos);
  276. vector lookAtPos = loc.m_StartLookAtPos;
  277. lookAtPos[1] = loc.m_StartPos[1];
  278. FreeDebugCamera.GetInstance().LookAt(lookAtPos);
  279. GetGame().GetPlayer().SetPosition(m_CurrentLocation.m_StartPos - "0 2.5 0");
  280. m_IsPreloading = true;
  281. }
  282. protected void OnBenchmarkEnd(string reason)
  283. {
  284. if (!m_Config.m_LogToRPT)
  285. {
  286. CloseFile( m_CSVLog );
  287. if (m_Config.m_DoDevPrints)
  288. Print( "Benchmark CSV file closed" );
  289. }
  290. if (m_Config.m_DoDevPrints)
  291. Print(string.Format("%1", reason));
  292. FreeDebugCamera.GetInstance().SetActive(false);
  293. GetGame().RequestExit( IDC_MAIN_QUIT ); // does not work on consoles ?
  294. }
  295. protected void CreatePlayer()
  296. {
  297. Entity playerEnt = GetGame().CreatePlayer(NULL, "SurvivorF_Eva", m_CurrentLocation.m_StartPos - "0 2.5 0", 0, "NONE");
  298. PlayerBase player = PlayerBase.Cast(playerEnt);
  299. GetGame().SelectPlayer(NULL, player);
  300. player.GetStatWater().Set(3000);
  301. player.GetStatEnergy().Set(3000);
  302. player.SetAllowDamage(false);
  303. player.SetCanBeDestroyed(false);
  304. player.DisableSimulation(true);
  305. FreeDebugCamera.GetInstance().SetFOV(0.72);
  306. FreeDebugCamera.GetInstance().SetActive(true);
  307. }
  308. protected void CreateCSVLog()
  309. {
  310. string fileName = "benchmark";
  311. if (m_Config.m_CSVName != string.Empty)
  312. fileName = m_Config.m_CSVName;
  313. m_CSVLog = OpenFile("$profile:" + fileName + ".csv", FileMode.WRITE);
  314. if ( m_CSVLog == 0 )
  315. OnBenchmarkEnd("Failed to create benchmark .csv");
  316. if (m_Config.m_DoDevPrints)
  317. Print("Benchmark .csv created");
  318. FPrintln(m_CSVLog, "Location,FPS,Time");
  319. }
  320. protected void FPSLog( string position, float frames )
  321. {
  322. int year, month, day, hour, minute, second;
  323. GetYearMonthDay(year, month, day);
  324. GetHourMinuteSecondUTC(hour, minute, second);
  325. string timestamp = string.Format( "%1-%2-%3-%4-%5-%6", year, month, day, hour, minute, second );
  326. if (m_Config.m_LogToRPT)
  327. PrintToRPT("Average FPS: " + frames);
  328. else
  329. FPrintln( m_CSVLog, position + "," + frames + "," + timestamp );
  330. }
  331. }