missionbenchmark.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. if (!m_Config || m_Config.m_Locations.Count() <= 1)
  132. {
  133. OnBenchmarkEnd("Not enough locations defined");
  134. return;
  135. }
  136. if (!m_Config.m_LogToRPT)
  137. CreateCSVLog();
  138. m_IsPreloading = true;
  139. OnLocationSwitch();
  140. }
  141. override void OnUpdate(float timeslice)
  142. {
  143. super.OnUpdate(timeslice);
  144. m_TimeCounter += timeslice * m_Config.m_TimeMultiplier;
  145. if (!m_CurrentLocation)
  146. return;
  147. else if (m_IsPreloading)
  148. PreloadUpdate();
  149. else
  150. MeasureUpdate(timeslice);
  151. }
  152. // Update logic which runs when location is being preloaded
  153. protected void PreloadUpdate()
  154. {
  155. if (!m_InitialLoadDone)
  156. {
  157. if (m_TimeCounter >= (m_Config.m_PreloadLength + INITIAL_PRELOAD)) // end preload
  158. m_InitialLoadDone = true;
  159. }
  160. else if (m_TimeCounter >= m_Config.m_PreloadLength * (1 / m_Config.m_TimeMultiplier)) // end preload
  161. {
  162. m_TimeCounter = 0;
  163. m_IsPreloading = false;
  164. }
  165. }
  166. // Update logic which runs when measurement is in progress
  167. protected void MeasureUpdate(float timeSlice)
  168. {
  169. m_MeasureStepTimer += timeSlice;
  170. if (m_MeasureStepTimer >= STEP_INTERVAL)
  171. {
  172. float avgFps = GetGame().GetFps();
  173. if (m_Config.m_DoDevPrints)
  174. Print( string.Format("Measure step: %1 | FPS: %2" , m_MeasuringStep + 1, 1/avgFps) );
  175. /*if (m_MeasuringStep >= m_MeasureLength) // end of steps
  176. { }
  177. else*/ // next step
  178. m_MeasureStepTimer = 0;
  179. m_SumFPS += ( 1/avgFps );
  180. m_MeasuringStep++;
  181. GetGame().GetPlayer().SetPosition(FreeDebugCamera.GetInstance().GetPosition() - "0 2.5 0");
  182. }
  183. LerpCamera();
  184. }
  185. protected void AdvanceLocation()
  186. {
  187. if (m_NextLocation.m_IsDummyWait && m_MeasuringStep < 5)
  188. return;
  189. string locationName = m_NextLocation.m_Name;
  190. FPSLog(locationName, m_SumFPS/m_MeasuringStep);
  191. if (m_Config.m_DoDevPrints)
  192. Print( string.Format("%1 | Measurements: %2 | Average FPS: %3 | Elapsed time: %4 seconds" ,locationName, m_MeasuringStep, m_SumFPS/m_MeasuringStep ,m_TimeCounter) );
  193. m_LocIndex++;
  194. OnLocationSwitch();
  195. }
  196. // logic for interpolating camera between location points
  197. protected void LerpCamera()
  198. {
  199. float lerpX, lerpZ, lerpY;
  200. vector target = m_NextLocation.m_StartPos;
  201. float camSpeedAdjust = m_CurrentLocation.m_CamSpeedMultiplier * 5 * m_TimeCounter * 1/m_StepDistance;
  202. lerpX = Math.Lerp(m_CurrentLocation.m_StartPos[0], target[0], camSpeedAdjust);
  203. lerpZ = Math.Lerp(m_CurrentLocation.m_StartPos[1], target[1], camSpeedAdjust);
  204. lerpY = Math.Lerp(m_CurrentLocation.m_StartPos[2], target[2], camSpeedAdjust);
  205. if (camSpeedAdjust >= 1 || m_NextLocation.m_IsDummyWait)
  206. {
  207. AdvanceLocation();
  208. return;
  209. }
  210. FreeDebugCamera.GetInstance().SetPosition( Vector(lerpX, lerpZ, lerpY) );
  211. target = m_NextLocation.m_StartLookAtPos;
  212. lerpX = Math.Lerp(m_CurrentLocation.m_StartLookAtPos[0], target[0], camSpeedAdjust);
  213. //lerpZ = Math.Lerp(m_CurrentLocation.m_StartLookAtPos[1], target[1], camSpeedAdjust); // ignored as it causes issues with lerping between look at points
  214. lerpY = Math.Lerp(m_CurrentLocation.m_StartLookAtPos[2], target[2], camSpeedAdjust);
  215. FreeDebugCamera.GetInstance().LookAt( Vector(lerpX, lerpZ, lerpY) );
  216. }
  217. protected void OnLocationSwitch()
  218. {
  219. if (m_LocIndex >= (m_Config.m_Locations.Count() - 1))
  220. {
  221. OnBenchmarkEnd("Test finished!");
  222. return;
  223. }
  224. m_MeasureStepTimer = 1; // tick first measurement straight after preload
  225. m_SumFPS = 0;
  226. m_MeasuringStep = 0;
  227. m_TimeCounter = 0;
  228. m_CurrentLocation = m_Config.m_Locations[m_LocIndex];
  229. m_NextLocation = m_Config.m_Locations[m_LocIndex+1];
  230. m_StepDistance = vector.Distance(m_CurrentLocation.m_StartPos, m_NextLocation.m_StartPos);
  231. if (!GetGame().GetPlayer())
  232. {
  233. CreatePlayer();
  234. TeleportToPos(m_CurrentLocation);
  235. }
  236. if (m_NextLocation.m_IsDummyTeleport) // flycam teleport
  237. {
  238. m_LocIndex += 2;
  239. if (m_LocIndex >= (m_Config.m_Locations.Count() - 1))
  240. {
  241. OnBenchmarkEnd("Test finished!");
  242. return;
  243. }
  244. else
  245. {
  246. m_CurrentLocation = m_Config.m_Locations[m_LocIndex];
  247. m_NextLocation = m_Config.m_Locations[m_LocIndex+1];
  248. m_StepDistance = vector.Distance(m_CurrentLocation.m_StartPos, m_NextLocation.m_StartPos);
  249. TeleportToPos(m_CurrentLocation);
  250. }
  251. }
  252. if (m_NextLocation.m_IsDummyWait)
  253. {
  254. m_NextLocation.m_Name = m_CurrentLocation.m_Name + " Wait";
  255. m_NextLocation.m_StartPos = m_CurrentLocation.m_StartPos;
  256. m_NextLocation.m_StartLookAtPos = m_CurrentLocation.m_StartLookAtPos;
  257. }
  258. if (m_Config.m_DoDevPrints)
  259. {
  260. Print(string.Format("================"));
  261. Print(string.Format("%1 test begin" , m_CurrentLocation.m_Name + " -> " + m_NextLocation.m_Name));
  262. }
  263. }
  264. protected void TeleportToPos(BenchmarkLocation loc)
  265. {
  266. FreeDebugCamera.GetInstance().SetPosition(loc.m_StartPos);
  267. vector lookAtPos = loc.m_StartLookAtPos;
  268. lookAtPos[1] = loc.m_StartPos[1];
  269. FreeDebugCamera.GetInstance().LookAt(lookAtPos);
  270. GetGame().GetPlayer().SetPosition(m_CurrentLocation.m_StartPos - "0 2.5 0");
  271. m_IsPreloading = true;
  272. }
  273. protected void OnBenchmarkEnd(string reason)
  274. {
  275. if (!m_Config.m_LogToRPT)
  276. {
  277. CloseFile( m_CSVLog );
  278. if (m_Config.m_DoDevPrints)
  279. Print( "Benchmark CSV file closed" );
  280. }
  281. if (m_Config.m_DoDevPrints)
  282. Print(string.Format("%1", reason));
  283. FreeDebugCamera.GetInstance().SetActive(false);
  284. GetGame().RequestExit( IDC_MAIN_QUIT ); // does not work on consoles ?
  285. }
  286. protected void CreatePlayer()
  287. {
  288. Entity playerEnt = GetGame().CreatePlayer(NULL, "SurvivorF_Eva", m_CurrentLocation.m_StartPos - "0 2.5 0", 0, "NONE");
  289. PlayerBase player = PlayerBase.Cast(playerEnt);
  290. GetGame().SelectPlayer(NULL, player);
  291. player.GetStatWater().Set(3000);
  292. player.GetStatEnergy().Set(3000);
  293. player.SetAllowDamage(false);
  294. player.SetCanBeDestroyed(false);
  295. FreeDebugCamera.GetInstance().SetFOV(0.72);
  296. FreeDebugCamera.GetInstance().SetActive(true);
  297. }
  298. protected void CreateCSVLog()
  299. {
  300. string fileName = "benchmark";
  301. if (m_Config.m_CSVName != string.Empty)
  302. fileName = m_Config.m_CSVName;
  303. m_CSVLog = OpenFile("$profile:" + fileName + ".csv", FileMode.WRITE);
  304. if ( m_CSVLog == 0 )
  305. OnBenchmarkEnd("Failed to create benchmark .csv");
  306. if (m_Config.m_DoDevPrints)
  307. Print("Benchmark .csv created");
  308. FPrintln(m_CSVLog, "Location,FPS,Time");
  309. }
  310. protected void FPSLog( string position, float frames )
  311. {
  312. int year, month, day, hour, minute, second;
  313. GetYearMonthDay(year, month, day);
  314. GetHourMinuteSecondUTC(hour, minute, second);
  315. string timestamp = string.Format( "%1-%2-%3-%4-%5-%6", year, month, day, hour, minute, second );
  316. if (m_Config.m_LogToRPT)
  317. PrintToRPT("Average FPS: " + frames);
  318. else
  319. FPrintln( m_CSVLog, position + "," + frames + "," + timestamp );
  320. }
  321. }