Dayz Explorer 1.28.160049
Loading...
Searching...
No Matches
missionbenchmark.c
Go to the documentation of this file.
1// Struct for individual benchmark locations
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
11 void BenchmarkLocation(string name)
12 {
13 m_Name = name;
14 }
15
16 void SetPosition(vector start)
17 {
18 m_StartPos = start + "0 2.5 0"; // raise to head level
19 }
20
21 // note that vector[1] (height) is ignored during interpolation and setting it has no effect
22 void SetLookAtPosition(vector start)
23 {
24 m_StartLookAtPos = start;
25 }
26
27 void SetCameraSpeedMultiplier(float multiplier)
28 {
29 m_CamSpeedMultiplier = multiplier;
30 }
31
32 void SetDummyTeleport()
33 {
34 m_IsDummyTeleport = true;
35 }
36
37 void SetDummyWait()
38 {
39 m_IsDummyWait = true;
40 }
41}
42
43// Global settings of benchmark
44class BenchmarkConfig
45{
50 string m_CSVName;
51
53
55 {
56 m_Locations.Insert(loc);
57 }
58
59 void AddQuickLocation(string name, vector pos, vector lookAtPos)
60 {
62 loc.SetPosition(pos);
63 loc.SetLookAtPosition(lookAtPos);
64
65 m_Locations.Insert(loc);
66 }
67
69 {
70 BenchmarkLocation loc = new BenchmarkLocation("Teleport");
71 loc.SetDummyTeleport();
72 m_Locations.Insert(loc);
73 }
74
75 void AddWait()
76 {
77 BenchmarkLocation loc = new BenchmarkLocation("Wait");
78 loc.SetDummyWait();
79 m_Locations.Insert(loc);
80 }
81
82 // false = csv, true = rpt
83 void LogToRPT(bool logRPT)
84 {
85 m_LogToRPT = logRPT;
86 }
87
88 void DoDevPrints(bool doPrints)
89 {
90 m_DoDevPrints = doPrints;
91 }
92
93 // Preload time of individual locations after teleport
94 void SetPreloadLengthTime(int seconds)
95 {
96 m_PreloadLength = seconds;
97 }
98
99 // Time multiplier for quickly testing flypath
100 void SetTestTimeMultiplier(float multiplier)
101 {
102 m_TimeMultiplier = multiplier;
103 }
104
105 void SetCSVName(string name)
106 {
107 m_CSVName = name;
108 }
109}
110
111class MissionBenchmark : MissionGameplay
112{
113 protected const int INITIAL_PRELOAD = 5; // seconds, extra preload seconds to compensate for the game launching
114 protected const float STEP_INTERVAL = 1; // seconds, how much time passes between steps (fps measurements)
115
116 protected bool m_InitialLoadDone; // extra time is added to first preload to make up for intial loading of the game
117 protected bool m_IsPreloading; // preload time between location measurements
118 protected bool m_LocationDone; // finished measuring current location
119 protected int m_LocIndex;
120 protected int m_MeasuringStep;
121 protected float m_MeasureStepTimer = 1;
122 protected float m_SumFPS;
123 protected float m_TimeCounter;
124 protected float m_MeasureLength;
125 protected float m_StepDistance;
126
130
131 protected ref BenchmarkConfig m_Config;
132
133 protected static MissionBenchmark m_Instance;
134
136 {
137 m_Instance = this;
138 }
139
141 {
142 m_Instance = null;
143 }
144
146 {
147 return m_Instance;
148 }
149
150 BenchmarkConfig GetConfig()
151 {
152 if (!m_Config)
153 m_Config = new BenchmarkConfig();
154
155 return m_Config;
156 }
157
158 void Start()
159 {
161
162 if (!m_Config || m_Config.m_Locations.Count() <= 1)
163 {
164 OnBenchmarkEnd("Not enough locations defined");
165 return;
166 }
167
168 if (!m_Config.m_LogToRPT)
169 CreateCSVLog();
170
171 m_IsPreloading = true;
172
174 }
175
176 override void OnUpdate(float timeslice)
177 {
178 super.OnUpdate(timeslice);
179
180 m_TimeCounter += timeslice * m_Config.m_TimeMultiplier;
181
183 return;
184 else if (m_IsPreloading)
186 else
187 MeasureUpdate(timeslice);
188 }
189
190 // Update logic which runs when location is being preloaded
191 protected void PreloadUpdate()
192 {
194 {
195 if (m_TimeCounter >= (m_Config.m_PreloadLength + INITIAL_PRELOAD)) // end preload
196 m_InitialLoadDone = true;
197 }
198 else if (m_TimeCounter >= m_Config.m_PreloadLength * (1 / m_Config.m_TimeMultiplier)) // end preload
199 {
200 m_TimeCounter = 0;
201 m_IsPreloading = false;
202 }
203 }
204
205 // Update logic which runs when measurement is in progress
206 protected void MeasureUpdate(float timeSlice)
207 {
208 m_MeasureStepTimer += timeSlice;
210 {
211 float avgFps = GetGame().GetFps();
212
213 if (m_Config.m_DoDevPrints)
214 Print( string.Format("Measure step: %1 | FPS: %2" , m_MeasuringStep + 1, 1/avgFps) );
215
216 /*if (m_MeasuringStep >= m_MeasureLength) // end of steps
217 { }
218 else*/ // next step
219
221 m_SumFPS += ( 1/avgFps );
223 GetGame().GetPlayer().SetPosition(FreeDebugCamera.GetInstance().GetPosition() - "0 2.5 0");
224 }
225
226 LerpCamera();
227 }
228
229 protected void DisableWeatherChange()
230 {
231 Weather weather = GetGame().GetWeather();
232 weather.SetDynVolFogHeightBias(0,0);
233 weather.SetDynVolFogHeightDensity(0,0);
234 weather.SetDynVolFogDistanceDensity(0,0);
235
236 weather.SetWeatherUpdateFreeze(true);
237 }
238
239 protected void AdvanceLocation()
240 {
241 if (m_NextLocation.m_IsDummyWait && m_MeasuringStep < 5)
242 return;
243
244 string locationName = m_NextLocation.m_Name;
245 FPSLog(locationName, m_SumFPS/m_MeasuringStep);
246
247 if (m_Config.m_DoDevPrints)
248 Print( string.Format("%1 | Measurements: %2 | Average FPS: %3 | Elapsed time: %4 seconds" ,locationName, m_MeasuringStep, m_SumFPS/m_MeasuringStep ,m_TimeCounter) );
249
250 m_LocIndex++;
252 }
253
254 // logic for interpolating camera between location points
255 protected void LerpCamera()
256 {
257 float lerpX, lerpZ, lerpY;
258
259 vector target = m_NextLocation.m_StartPos;
260 float camSpeedAdjust = m_CurrentLocation.m_CamSpeedMultiplier * 5 * m_TimeCounter * 1/m_StepDistance;
261
262 lerpX = Math.Lerp(m_CurrentLocation.m_StartPos[0], target[0], camSpeedAdjust);
263 lerpZ = Math.Lerp(m_CurrentLocation.m_StartPos[1], target[1], camSpeedAdjust);
264 lerpY = Math.Lerp(m_CurrentLocation.m_StartPos[2], target[2], camSpeedAdjust);
265
266 if (camSpeedAdjust >= 1 || m_NextLocation.m_IsDummyWait)
267 {
269 return;
270 }
271
272 FreeDebugCamera.GetInstance().SetPosition( Vector(lerpX, lerpZ, lerpY) );
273
274 target = m_NextLocation.m_StartLookAtPos;
275
276 lerpX = Math.Lerp(m_CurrentLocation.m_StartLookAtPos[0], target[0], camSpeedAdjust);
277 //lerpZ = Math.Lerp(m_CurrentLocation.m_StartLookAtPos[1], target[1], camSpeedAdjust); // ignored as it causes issues with lerping between look at points
278 lerpY = Math.Lerp(m_CurrentLocation.m_StartLookAtPos[2], target[2], camSpeedAdjust);
279 FreeDebugCamera.GetInstance().LookAt( Vector(lerpX, lerpZ, lerpY) );
280 }
281
282 protected void OnLocationSwitch()
283 {
284 if (m_LocIndex >= (m_Config.m_Locations.Count() - 1))
285 {
286 OnBenchmarkEnd("Test finished!");
287 return;
288 }
289
290 m_MeasureStepTimer = 1; // tick first measurement straight after preload
291 m_SumFPS = 0;
292 m_MeasuringStep = 0;
293 m_TimeCounter = 0;
295 m_NextLocation = m_Config.m_Locations[m_LocIndex+1];
296 m_StepDistance = vector.Distance(m_CurrentLocation.m_StartPos, m_NextLocation.m_StartPos);
297
298 if (!GetGame().GetPlayer())
299 {
300 CreatePlayer();
302 }
303
304 if (m_NextLocation.m_IsDummyTeleport) // flycam teleport
305 {
306 m_LocIndex += 2;
307 if (m_LocIndex >= (m_Config.m_Locations.Count() - 1))
308 {
309 OnBenchmarkEnd("Test finished!");
310 return;
311 }
312 else
313 {
315 m_NextLocation = m_Config.m_Locations[m_LocIndex+1];
316 m_StepDistance = vector.Distance(m_CurrentLocation.m_StartPos, m_NextLocation.m_StartPos);
318 }
319 }
320
321 if (m_NextLocation.m_IsDummyWait)
322 {
323 m_NextLocation.m_Name = m_CurrentLocation.m_Name + " Wait";
324 m_NextLocation.m_StartPos = m_CurrentLocation.m_StartPos;
325 m_NextLocation.m_StartLookAtPos = m_CurrentLocation.m_StartLookAtPos;
326 }
327
328 if (m_Config.m_DoDevPrints)
329 {
330 Print(string.Format("================"));
331 Print(string.Format("%1 test begin" , m_CurrentLocation.m_Name + " -> " + m_NextLocation.m_Name));
332 }
333 }
334
336 {
337 FreeDebugCamera.GetInstance().SetPosition(loc.m_StartPos);
338 vector lookAtPos = loc.m_StartLookAtPos;
339 lookAtPos[1] = loc.m_StartPos[1];
340 FreeDebugCamera.GetInstance().LookAt(lookAtPos);
341
342 GetGame().GetPlayer().SetPosition(m_CurrentLocation.m_StartPos - "0 2.5 0");
343 m_IsPreloading = true;
344 }
345
346 protected void OnBenchmarkEnd(string reason)
347 {
348 if (!m_Config.m_LogToRPT)
349 {
351
352 if (m_Config.m_DoDevPrints)
353 Print( "Benchmark CSV file closed" );
354 }
355
356 if (m_Config.m_DoDevPrints)
357 Print(string.Format("%1", reason));
358
359 FreeDebugCamera.GetInstance().SetActive(false);
360 GetGame().RequestExit( IDC_MAIN_QUIT ); // does not work on consoles ?
361 }
362
363 protected void CreatePlayer()
364 {
365 Entity playerEnt = GetGame().CreatePlayer(NULL, "SurvivorF_Eva", m_CurrentLocation.m_StartPos - "0 2.5 0", 0, "NONE");
366 PlayerBase player = PlayerBase.Cast(playerEnt);
367 GetGame().SelectPlayer(NULL, player);
368
369 player.GetStatWater().Set(3000);
370 player.GetStatEnergy().Set(3000);
371 player.SetAllowDamage(false);
372 player.SetCanBeDestroyed(false);
373 player.DisableSimulation(true);
374
375 FreeDebugCamera.GetInstance().SetFOV(0.72);
376 FreeDebugCamera.GetInstance().SetActive(true);
377 }
378
379 protected void CreateCSVLog()
380 {
381 string fileName = "benchmark";
382 if (m_Config.m_CSVName != string.Empty)
383 fileName = m_Config.m_CSVName;
384
385 m_CSVLog = OpenFile("$profile:" + fileName + ".csv", FileMode.WRITE);
386 if ( m_CSVLog == 0 )
387 OnBenchmarkEnd("Failed to create benchmark .csv");
388
389 if (m_Config.m_DoDevPrints)
390 Print("Benchmark .csv created");
391
392 FPrintln(m_CSVLog, "Location,FPS,Time");
393 }
394
395 protected void FPSLog( string position, float frames )
396 {
397 int year, month, day, hour, minute, second;
398 GetYearMonthDay(year, month, day);
399 GetHourMinuteSecondUTC(hour, minute, second);
400 string timestamp = string.Format( "%1-%2-%3-%4-%5-%6", year, month, day, hour, minute, second );
401
402 if (m_Config.m_LogToRPT)
403 PrintToRPT("Average FPS: " + frames);
404 else
405 FPrintln( m_CSVLog, position + "," + frames + "," + timestamp );
406 }
407}
PlayerSpawnPresetDiscreteItemSetSlotData name
one set for cargo
proto native Weather GetWeather()
Returns weather controller object.
Definition camera.c:2
Definition enmath.c:7
const float STEP_INTERVAL
static MissionBenchmark m_Instance
void MeasureUpdate(float timeSlice)
void FPSLog(string position, float frames)
void OnBenchmarkEnd(string reason)
BenchmarkLocation m_CurrentLocation
ref BenchmarkConfig m_Config
override void OnUpdate(float timeslice)
BenchmarkLocation m_NextLocation
BenchmarkConfig GetConfig()
static MissionBenchmark GetInstance()
void TeleportToPos(BenchmarkLocation loc)
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
proto native CGame GetGame()
proto void Print(void var)
Prints content of variable to console/log.
proto void PrintToRPT(void var)
Prints content of variable to RPT file (performance warning - each write means fflush!...
FileMode
Definition ensystem.c:383
proto void CloseFile(FileHandle file)
Close the File.
proto FileHandle OpenFile(string name, FileMode mode)
Opens File.
int[] FileHandle
Definition ensystem.c:390
proto void FPrintln(FileHandle file, void var)
Write to file and add new line.
proto native vector Vector(float x, float y, float z)
Vector constructor from components.
proto void GetYearMonthDay(out int year, out int month, out int day)
Returns system date.
proto void GetHourMinuteSecondUTC(out int hour, out int minute, out int second)
Returns UTC system time.
const int IDC_MAIN_QUIT
Definition constants.c:144
void AddTeleport()
void DoDevPrints(bool doPrints)
string m_CSVName
void AddLocation(notnull BenchmarkLocation loc)
ref array< ref BenchmarkLocation > m_Locations
int m_PreloadLength
void SetPreloadLengthTime(int seconds)
void AddQuickLocation(string name, vector pos, vector lookAtPos)
class BenchmarkLocation m_LogToRPT
void AddWait()
void SetTestTimeMultiplier(float multiplier)
float m_TimeMultiplier
void SetCSVName(string name)
bool m_DoDevPrints
void LogToRPT(bool logRPT)
PlayerBase GetPlayer()
class SyncedValue m_Name