DayZ 1.24
Loading...
Searching...
No Matches
EnProfilerTests.c
Go to the documentation of this file.
1class EnProfilerTests : TestFramework
2{
5
6 //---------------------------------------------------------------------------
7 // Ctor - Decides the tests to run
8 //---------------------------------------------------------------------------
10 {
12
13 AddInitTest("TestToggling");
14 AddInitTest("TestTogglingImmediate");
15 AddInitTest("TestSetFlags");
16 AddInitTest("TestClearFlags");
17 AddInitTest("TestAddFlags");
18 AddInitTest("TestModule");
19 AddInitTest("TestClassTimeData");
20 AddInitTest("TestClassCountData");
21 AddInitTest("TestFuncTimeData");
22 AddInitTest("TestFuncCountData");
23 }
24
25 //---------------------------------------------------------------------------
26 // Dtor
27 //---------------------------------------------------------------------------
32
33 //---------------------------------------------------------------------------
34 // Tests
35 //---------------------------------------------------------------------------
36 // Test toggling state
49
50 //---------------------------------------------------------------------------
51 // Test toggling immediate state
64
65 //---------------------------------------------------------------------------
66 // Test SetFlags/GetFlags
68 {
70
71 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
72 {
74 EnProfiler.SetFlags(flags);
75
76 if (!Assert(EnProfiler.GetFlags() == flags))
77 {
79 return NTFR(TFR.FAIL);
80 }
81
82 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
83 {
85 EnProfiler.SetFlags(flags);
86
87 if (!Assert(EnProfiler.GetFlags() == flags))
88 {
90 return NTFR(TFR.FAIL);
91 }
92 }
93 }
94
95 // Let's test some bogus
96 EnProfiler.SetFlags(-333);
99 if (!Assert(bogusFlags == 0))
100 {
102 return NTFR(TFR.FAIL);
103 }
104
107 if (!Assert(bogusFlags == 0))
108 {
110 return NTFR(TFR.FAIL);
111 }
112
113 // Reset
115 return NTFR(TFR.SUCCESS);
116 }
117
118 //---------------------------------------------------------------------------
119 // Test removing of flags
121 {
123
125
127 {
129 return NTFR(TFR.FAIL);
130 }
131
134
136 {
138 return NTFR(TFR.FAIL);
139 }
140
143
145 {
147 return NTFR(TFR.FAIL);
148 }
149
150 // Reset
152 return NTFR(TFR.SUCCESS);
153 }
154
155 //---------------------------------------------------------------------------
156 // Test adding of flags
158 {
160
162
163 // Return should match resulting flags
165 {
167 return NTFR(TFR.FAIL);
168 }
169
170 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
171 {
173 return NTFR(TFR.FAIL);
174 }
175
176 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
177 {
179 return NTFR(TFR.FAIL);
180 }
181
182 // Reset
184 return NTFR(TFR.SUCCESS);
185 }
186
187 //---------------------------------------------------------------------------
188 // Test module
190 {
191 // File lives in Game, use it while testing
193
194 // This was added at the same time as this API, so check if it works as well
195 string nameOfCurrentModule = Type().GetModule();
196 if (!Assert(nameOfCurrentModule != ""))
197 return NTFR(TFR.FAIL);
198
199 // We know we are in Game, so use it as a test
202 return NTFR(TFR.FAIL);
203
205 return NTFR(TFR.FAIL);
206
207 // Test if setting and getting works
210
212 {
214 return NTFR(TFR.FAIL);
215 }
216
217 // Data to restore
220
221 // Make sure we are only profiling Game and that the data is clean
222 // Only valid for the Get...Per... methods, as they need to be sorted
224
225 // GetTickTime() returns in seconds, so gather the results in seconds too
228
229 // Time to sleeb
230 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
231 float timeSlept = Sleep(0.3);
232 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
234
235 // Restore
237
238 // We called the function, so it must have some time
239 if (!Assert(postTime > 0))
240 {
242
243 if (!wasEnabled)
244 EnProfiler.Enable(false, true);
245
247
248 return NTFR(TFR.FAIL);
249 }
250
251 if (!Assert(diff < 0.00001))
252 {
254
255 if (!wasEnabled)
256 EnProfiler.Enable(false, true);
257
259
260 return NTFR(TFR.FAIL);
261 }
262
263 // Clean the session
265
266 // Something from a different module should not get sorted, so just fire something from a different module
267 for (int i = 0; i < 1000; ++i)
268 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
269
270 // Sort and gather the data and validate if it is correct
274
275 Debug.TFLog("Game fncs:", this, "TestModule");
276
277 int funcCount = timePerFunc.Count();
278 for (int j = 0; j < funcCount; ++j)
279 {
281 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
282 // We are currently profiling Game, so this Core function shouldn't be present
283 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
284 {
286
287 if (!wasEnabled)
288 EnProfiler.Enable(false, true);
289
291
292 return NTFR(TFR.FAIL);
293 }
294 }
295
298
299 int classCount = timePerClass.Count();
300 for (int k = 0; k < classCount; ++k)
301 {
302 typename type = timePerClass[k].param2;
304 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
305 {
307
308 if (!wasEnabled)
309 EnProfiler.Enable(false, true);
310
312
313 return NTFR(TFR.FAIL);
314 }
315
316 // Only classes from Game should be present
318 {
320
321 if (!wasEnabled)
322 EnProfiler.Enable(false, true);
323
325
326 return NTFR(TFR.FAIL);
327 }
328 }
329
330 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
333 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
335
336 bool found = false;
337
338 Debug.TFLog("Core fncs:", this, "TestModule");
339
340 funcCount = timePerFunc.Count();
341 for (int l = 0; l < funcCount; ++l)
342 {
344 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
345 // We are currently profiling Core, so this Core function should be present
346 if (tfpc.param2 == "EnumTools::StringToEnum")
347 {
348 found = true;
349 break;
350 }
351 }
352
353 Assert(found);
354
355 // Test some bogus
357 EnProfiler.SetModule(-333);
359 EnProfiler.SetModule(6003);
361
364
365 if (!wasEnabled)
366 EnProfiler.Enable(false, true);
367
368 return BTFR(success && found);
369 }
370
371 //---------------------------------------------------------------------------
372 // Test to see if class time data is correct
374 {
375 // We should restore this when done
378
379 // GetTickTime() returns in seconds, so gather the results in seconds too
381
382 // Create the classes
384
385 // Time to stress
386 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
387 float timeStressed = clss.DoEverything();
388 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
389 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
392
393 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
394
395 // Restore
397 if (!wasEnabled)
398 EnProfiler.Enable(false, true);
399
400 // We called the function, so it must have some time
401 if (!Assert(postTime > 0))
402 return NTFR(TFR.FAIL);
403
405 return NTFR(TFR.FAIL);
406
407 if (!Assert(diff < 0.001))
408 return NTFR(TFR.FAIL);
409
410 return NTFR(TFR.SUCCESS);
411 }
412
413 //---------------------------------------------------------------------------
414 // Test to see if class count data is correct
416 {
417 const int allocAmount = 9;
418 const int releaseAmount = 6;
420
421 // We should restore this when done
423
424 // Time to test
427
429 for (int i = 0; i < allocAmount; ++i)
430 instanceArr.Insert(new EPTHelperClass());
431
432 for (int j = 0; j < releaseAmount; ++j)
433 delete instanceArr[j];
434
435 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
437
440
441 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
442
443 // Restore
444 if (!wasEnabled)
445 EnProfiler.Enable(false, true);
446
447 // Time to check
448 if (!Assert(alloced == allocAmount))
449 return NTFR(TFR.FAIL);
450
452 return NTFR(TFR.FAIL);
453
454 return NTFR(TFR.SUCCESS);
455 }
456
457 //---------------------------------------------------------------------------
458 // Test to see if func time data is correct
460 {
461 // We should restore this when done
464
465 // GetTickTime() returns in seconds, so gather the results in seconds too
467
468 // Time to stress
469 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
470 float timeStressed = StringFormat();
471 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
474
475 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
476 float timeStressed2 = StringConcat();
477 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
480
481 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
482
483 // Restore
485 if (!wasEnabled)
486 EnProfiler.Enable(false, true);
487
488 // We called the function, so it must have some time
489 if (!Assert(postTime > 0))
490 return NTFR(TFR.FAIL);
491
492 if (!Assert(diff < 0.001))
493 return NTFR(TFR.FAIL);
494
495 if (!Assert(postTime2 > 0))
496 return NTFR(TFR.FAIL);
497
498 if (!Assert(diff2 < 0.001))
499 return NTFR(TFR.FAIL);
500
501 // I know that string.Format is faster than additive concatenation
503 return NTFR(TFR.FAIL);
504
505 return NTFR(TFR.SUCCESS);
506 }
507
508 //---------------------------------------------------------------------------
509 // Test to see if func count data is correct
511 {
512 // We should restore this when done
514
515 // Time to count
516
517 // - CallFunction
518 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
519 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
520 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
521
523
524 // - CallFunctionParams
525 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
526 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
527 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
528
530
531 // - Regular call
532 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
534 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
535
537
538 // - Call
539 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
540 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
541 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
542
544
545 // - Garbage
546 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
547
548 // - Static
549 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
551 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
552
554
555 // - Global
556 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
557 GetDayZGame();
558 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
559
561
562 // - Global proto
563 // Not tracked, so don't need to compare before and after, should always be 0
564 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
565 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
566
567 // - Static proto
568 // Not tracked, so don't need to compare before and after, should always be 0
569 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
570
571 // - proto
572 // Not tracked, so don't need to compare before and after, should always be 0
573 GetGame().GetHostName();
574 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
575
576 // - proto native
577 // Not tracked, so don't need to compare before and after, should always be 0
578 GetGame().IsServer();
579 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
580
581 // - static proto native
582 // Not tracked, so don't need to compare before and after, should always be 0
584 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
585
586 // Restore
587 if (!wasEnabled)
588 EnProfiler.Enable(false, true);
589
590 // Do the checks
591
592 // - CallFunction
593 if (!Assert(callCountCF == 1))
594 return NTFR(TFR.FAIL);
595
596 // - CallFunctionParams
597 if (!Assert(callCountCFP == 1))
598 return NTFR(TFR.FAIL);
599
600 // - Regular call
601 if (!Assert(callCountRG == 1))
602 return NTFR(TFR.FAIL);
603
604 // - Call
605 if (!Assert(callCountC == 1))
606 return NTFR(TFR.FAIL);
607
608 // - Garbage
609 if (!Assert(callCountNon == -1))
610 return NTFR(TFR.FAIL);
611
612 // - Static
613 if (!Assert(callCountS == 1))
614 return NTFR(TFR.FAIL);
615
616 // - Global
617 if (!Assert(callCountG == 1))
618 return NTFR(TFR.FAIL);
619
620 // - Global proto
621 if (!Assert(callCountGP == 0))
622 return NTFR(TFR.FAIL);
623
624 // - Static proto
625 if (!Assert(callCountSP == 0))
626 return NTFR(TFR.FAIL);
627
628 // - proto
629 if (!Assert(callCountP == 0))
630 return NTFR(TFR.FAIL);
631
632 // - proto native
633 if (!Assert(callCountPN == 0))
634 return NTFR(TFR.FAIL);
635
636 // - static proto native
637 if (!Assert(callCountSPN == 0))
638 return NTFR(TFR.FAIL);
639
640 return NTFR(TFR.SUCCESS);
641 }
642
643 //---------------------------------------------------------------------------
644 // Helpers
645 //---------------------------------------------------------------------------
646 // Snore
647 float Sleep(float timeS)
648 {
649 float startTime = GetGame().GetTickTime();
650 while (GetGame().GetTickTime() - startTime < timeS)
651 {
652 // Zzz
653 }
654
655 return GetGame().GetTickTime() - startTime;
656 }
657
658 //---------------------------------------------------------------------------
659 // Example stress method
661 {
662 float startTime = GetGame().GetTickTime();
663
664 for (int i = 0; i < 1000; ++i)
665 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
666
667 return GetGame().GetTickTime() - startTime;
668 }
669
670 //---------------------------------------------------------------------------
671 // Example stress method 2
673 {
674 float startTime = GetGame().GetTickTime();
675
676 for (int i = 0; i < 1000; ++i)
677 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
678
679 return GetGame().GetTickTime() - startTime;
680 }
681
682 //---------------------------------------------------------------------------
683 // To make sure it is only ever called in that test
685 {
686 int dummy = 3;
687 }
688
689 //---------------------------------------------------------------------------
690 // To make sure it is only ever called in that test
692 {
693 int dummy = 3;
694 }
695}
696
697class EPTHelperClass
698{
699 float Sleep2(float timeS)
700 {
701 float startTime = GetGame().GetTickTime();
702 while (GetGame().GetTickTime() - startTime < timeS)
703 {
704 // Zzz
705 }
707 return GetGame().GetTickTime() - startTime;
708 }
709 float SleepAgain(float timeS)
710 {
711 float startTime = GetGame().GetTickTime();
712 while (GetGame().GetTickTime() - startTime < timeS)
713 {
714 // Zzz
715 }
716
717 return GetGame().GetTickTime() - startTime;
718 }
719
721 {
722 float startTime = GetGame().GetTickTime();
723
724 Sleep2(3);
726
727 return GetGame().GetTickTime() - startTime;
728 }
729}
DayZGame GetDayZGame()
Definition DayZGame.c:3530
float SleepAgain(float timeS)
float DoEverything()
EnProfilerTests TestFramework Sleep2(float timeS)
string Type
TFResult NTFR(TFR result)
bool Assert(bool condition)
void TFResult(TFR result)
TFR
void AddInitTest(string test)
TFResult BTFR(bool result)
Definition Debug.c:14
static void TFLog(string message=LOG_DEFAULT, TestFramework caller=null, string function="")
Definition Debug.c:243
Set of methods for accessing script profiling data.
Definition EnProfiler.c:73
TFResult TestFuncCountData()
TFResult TestClearFlags()
TFResult TestModule()
void TestFuncCountDataHelper()
bool m_bWasProfilerEnabled
Remember this, so we can restore the previous state before the test!
TFResult TestAddFlags()
TFResult TestSetFlags()
TFResult TestTogglingImmediate()
TFResult TestToggling()
TFResult TestClassTimeData()
static void TestFuncCountDataHelperStatic()
TFResult TestClassCountData()
float Sleep(float timeS)
TFResult TestFuncTimeData()
static int GetEnumSize(typename e)
Return amount of values in enum.
Definition EnConvert.c:620
static int GetEnumValue(typename e, int idx)
Return the nth value in the enum.
Definition EnConvert.c:631
static int StringToEnum(typename e, string enumName)
Return enum value from string name.
Definition EnConvert.c:609
The error handler itself, for managing and distributing errors to modules Manages the ErrorHandlerMod...
static proto native ErrorModuleHandler GetInstance()
Gets the EMH Instance.
Definition EnMath.c:7
proto native CGame GetGame()
ErrorExSeverity
Definition EnDebug.c:62
enum ShapeType ErrorEx
static proto float AbsFloat(float f)
Returns absolute value.
static proto int GetTimeResolution()
Get the currently set time resolution.
static proto EnProfilerModule GetModule()
Get the currently profiled module.
static proto void GetTimePerFunc(notnull out array< ref EnProfilerTimeFuncPair > outArr, int count=int.MAX)
Obtain [SD] for Time Per Function.
static proto int GetAllocationsOfClass(typename clss, bool immediate=false)
Obtain [SD] or [PD] regarding the allocations of a specific class.
static proto float GetTimeOfClass(typename clss, bool immediate=false)
Obtain [SD] or [PD] regarding the time a specific class consumed.
static proto int GetCountOfFunc(string funct, typename clss, bool immediate=false)
Obtain [SD] or [PD] regarding the amount of times a specific function was called.
static proto int GetFlags()
Get the currently used flags across the API.
static proto int ClearFlags(bool sessionReset=true)
Remove all flags from the currently used set of EnProfilerFlags across the API.
static proto float GetTimeOfFunc(string funct, typename clss, bool immediate=false)
Obtain [SD] or [PD] regarding the time consumed by a specific function.
static proto void SetTimeResolution(int resolution)
Set the resolution of the fetched Time data.
static proto void SortData()
The internal sorting that happens at the end of the frame (so it is NOT necessary to call this manual...
static proto void SetModule(EnProfilerModule module, bool sessionReset=true)
Set the module to be profiled.
static proto void ResetSession(bool fullReset=false)
Perform [SR], clearing SessionFrame, ProfiledSessionFrames, [SD] and [PD] (except for [CI])
static bool IsEnabledP()
Return if script profiling is enabled through EnProfiler.
Definition EnProfiler.c:138
static bool IsEnabledC()
Return if script profiling is actually turned on inside of the script context.
Definition EnProfiler.c:152
static proto int GetInstancesOfClass(typename clss, bool immediate=false)
Obtain [SD] or [PD] regarding the [CI] of a specific class.
EnProfilerFlags
Flags that influences the behaviour of the EnProfiler API, applied through ...Flags functions.
Definition EnProfiler.c:9
static proto void GetTimePerClass(notnull out array< ref EnProfilerTimeClassPair > outArr, int count=int.MAX)
Obtain [SD] for Time Per Class.
static proto int SetFlags(int flags, bool sessionReset=true)
Override the currently used set of EnProfilerFlags across the API.
static proto void Enable(bool enable, bool immediate=false, bool sessionReset=true)
Enable the gathering of script profiling data.
EnProfilerModule
Current base scripted modules.
Definition EnProfiler.c:22
static proto bool NameToModule(string moduleName, out EnProfilerModule module)
Convert string to EnProfilerModule.
static bool RequestImmediateData()
Helper method to ascertain the profiler will record [PD] right after this call.
Definition EnProfiler.c:735
static proto int RemoveFlags(int flags, bool sessionReset=true)
Remove flags from the currently used set of EnProfilerFlags across the API.
static proto int GetCountOfFuncG(string funct, bool immediate=false)
Obtain [SD] or [PD] regarding the amount of times a specific function was called.
static proto int AddFlags(int flags, bool sessionReset=true)
Add flags to the currently used set of EnProfilerFlags across the API.