Dayz Explorer 1.29.162510
Loading...
Searching...
No Matches
missionbenchmark.c
Go to the documentation of this file.
1// Struct for individual benchmark locations
2class 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
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 AddLocation(loc);
66 }
67
69 {
70 BenchmarkLocation loc = new BenchmarkLocation("Teleport");
71 loc.SetDummyTeleport();
72 AddLocation(loc);
73 }
74
75 void AddWait()
76 {
77 BenchmarkLocation loc = new BenchmarkLocation("Wait");
78 loc.SetDummyWait();
79 AddLocation(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 m_StepDistance = vector.Distance(m_CurrentLocation.m_StartPos, target);
261 float camSpeedAdjust = m_CurrentLocation.m_CamSpeedMultiplier * 5 * m_TimeCounter * 1/m_StepDistance;
262 float distanceMulti = 1/Math.Max(0.001, m_StepDistance);
263
264 lerpX = Math.Lerp(m_CurrentLocation.m_StartPos[0], target[0], camSpeedAdjust);
265 lerpZ = Math.Lerp(m_CurrentLocation.m_StartPos[1], target[1], camSpeedAdjust);
266 lerpY = Math.Lerp(m_CurrentLocation.m_StartPos[2], target[2], camSpeedAdjust);
267
268 if (camSpeedAdjust >= 1 || m_NextLocation.m_IsDummyWait)
269 {
271 return;
272 }
273
274 FreeDebugCamera.GetInstance().SetPosition( Vector(lerpX, lerpZ, lerpY) );
275
276 target = m_NextLocation.m_StartLookAtPos;
277
278 lerpX = Math.Lerp(m_CurrentLocation.m_StartLookAtPos[0], target[0], camSpeedAdjust);
279 //lerpZ = Math.Lerp(m_CurrentLocation.m_StartLookAtPos[1], target[1], camSpeedAdjust); // ignored as it causes issues with lerping between look at points
280 lerpY = Math.Lerp(m_CurrentLocation.m_StartLookAtPos[2], target[2], camSpeedAdjust);
281 FreeDebugCamera.GetInstance().LookAt( Vector(lerpX, lerpZ, lerpY) );
282 }
283
284 protected void OnLocationSwitch()
285 {
286 if (m_LocIndex >= (m_Config.m_Locations.Count() - 1))
287 {
288 OnBenchmarkEnd("Test finished!");
289 return;
290 }
291
292 m_MeasureStepTimer = 1; // tick first measurement straight after preload
293 m_SumFPS = 0;
294 m_MeasuringStep = 0;
295 m_TimeCounter = 0;
297 m_NextLocation = m_Config.m_Locations[m_LocIndex+1];
298
299 if (!GetGame().GetPlayer())
300 {
301 CreatePlayer();
303 }
304
305 if (m_NextLocation.m_IsDummyTeleport) // flycam teleport
306 {
307 m_LocIndex += 2;
308 if (m_LocIndex >= (m_Config.m_Locations.Count() - 1))
309 {
310 OnBenchmarkEnd("Test finished!");
311 return;
312 }
313 else
314 {
316 m_NextLocation = m_Config.m_Locations[m_LocIndex+1];
317 m_StepDistance = vector.Distance(m_CurrentLocation.m_StartPos, m_NextLocation.m_StartPos);
319 }
320 }
321
322 if (m_NextLocation.m_IsDummyWait)
323 {
324 m_NextLocation.m_Name = m_CurrentLocation.m_Name + " Wait";
325 m_NextLocation.m_StartPos = m_CurrentLocation.m_StartPos;
326 m_NextLocation.m_StartLookAtPos = m_CurrentLocation.m_StartLookAtPos;
327 }
328
329 if (m_Config.m_DoDevPrints)
330 {
331 Print(string.Format("================"));
332 Print(string.Format("%1 test begin" , m_CurrentLocation.m_Name + " -> " + m_NextLocation.m_Name));
333 }
334 }
335
337 {
338 FreeDebugCamera.GetInstance().SetPosition(loc.m_StartPos);
339 vector lookAtPos = loc.m_StartLookAtPos;
340 lookAtPos[1] = loc.m_StartPos[1];
341 FreeDebugCamera.GetInstance().LookAt(lookAtPos);
342
343 GetGame().GetPlayer().SetPosition(m_CurrentLocation.m_StartPos - "0 2.5 0");
344 m_IsPreloading = true;
345 }
346
347 protected void OnBenchmarkEnd(string reason)
348 {
349 if (!m_Config.m_LogToRPT)
350 {
352
353 if (m_Config.m_DoDevPrints)
354 Print( "Benchmark CSV file closed" );
355 }
356
357 if (m_Config.m_DoDevPrints)
358 Print(string.Format("%1", reason));
359
360 FreeDebugCamera.GetInstance().SetActive(false);
361 GetGame().RequestExit( IDC_MAIN_QUIT ); // does not work on consoles ?
362 }
363
364 protected void CreatePlayer()
365 {
366 Entity playerEnt = GetGame().CreatePlayer(null, "SurvivorF_Eva", m_CurrentLocation.m_StartPos - "0 2.5 0", 0, "NONE");
367 PlayerBase player = PlayerBase.Cast(playerEnt);
368 GetGame().SelectPlayer(NULL, player);
369
370 player.GetStatWater().Set(3000);
371 player.GetStatEnergy().Set(3000);
372 player.SetModifiers(false);
373 player.SetAllowDamage(false);
374 player.SetCanBeDestroyed(false);
375 player.DisableSimulation(true);
376
377 FreeDebugCamera.GetInstance().SetFOV(0.72);
378 FreeDebugCamera.GetInstance().SetActive(true);
379 }
380
381 protected void CreateCSVLog()
382 {
383 string fileName = "benchmark";
384 if (m_Config.m_CSVName != string.Empty)
385 fileName = m_Config.m_CSVName;
386
387 m_CSVLog = OpenFile("$profile:" + fileName + ".csv", FileMode.WRITE);
388 if ( m_CSVLog == 0 )
389 OnBenchmarkEnd("Failed to create benchmark .csv");
390
391 if (m_Config.m_DoDevPrints)
392 Print("Benchmark .csv created");
393
394 FPrintln(m_CSVLog, "location,FPS,time");
395 }
396
397 protected void FPSLog( string position, float frames )
398 {
399 int year, month, day, hour, minute, second;
400 GetYearMonthDay(year, month, day);
401 GetHourMinuteSecondUTC(hour, minute, second);
402 string timestamp = string.Format( "%1-%2-%3-%4-%5-%6", year, month, day, hour, minute, second );
403
404 if (m_Config.m_LogToRPT)
405 PrintToRPT("Average FPS: " + frames);
406 else
407 FPrintln( m_CSVLog, position + "," + frames + "," + timestamp );
408 }
409}
PlayerSpawnPresetDiscreteItemSetSlotData name
one set for cargo
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.
DayZGame GetGame()
Definition gameplay.c:636
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()