DayZ 1.24
Loading...
Searching...
No Matches
EnProfilerTests.c File Reference

Go to the source code of this file.

Classes

class  EnProfilerTests
 

Functions

EnProfilerTests TestFramework Sleep2 (float timeS)
 
void EnProfilerTests ()
 
void ~EnProfilerTests ()
 
TFResult TestToggling ()
 
TFResult TestTogglingImmediate ()
 
TFResult TestSetFlags ()
 
TFResult TestClearFlags ()
 
TFResult TestAddFlags ()
 
TFResult TestModule ()
 
TFResult TestClassTimeData ()
 
TFResult TestClassCountData ()
 
TFResult TestFuncTimeData ()
 
TFResult TestFuncCountData ()
 
float Sleep (float timeS)
 
float StringFormat ()
 
float StringConcat ()
 
void TestFuncCountDataHelper ()
 
static void TestFuncCountDataHelperStatic ()
 
float SleepAgain (float timeS)
 
float DoEverything ()
 

Variables

bool m_bWasProfilerEnabled
 Remember this, so we can restore the previous state before the test!
 

Function Documentation

◆ DoEverything()

float DoEverything ( )

Definition at line 720 of file EnProfilerTests.c.

721 {
722 float startTime = GetGame().GetTickTime();
723
724 Sleep2(3);
725 SleepAgain(3);
726
727 return GetGame().GetTickTime() - startTime;
728 }
float SleepAgain(float timeS)
EnProfilerTests TestFramework Sleep2(float timeS)
proto native CGame GetGame()

References GetGame(), and Sleep2().

◆ EnProfilerTests()

void Sleep2::EnProfilerTests ( )

Definition at line 706 of file EnProfilerTests.c.

710 {
711 float startTime = GetGame().GetTickTime();
712 while (GetGame().GetTickTime() - startTime < timeS)
713 {
714 // Zzz
715 }
716
717 return GetGame().GetTickTime() - startTime;
718 }
719
720 float DoEverything()
float DoEverything()

◆ Sleep()

float Sleep2::Sleep ( float timeS)

Definition at line 1344 of file EnProfilerTests.c.

1344 : TestFramework
1345{
1348
1349 //---------------------------------------------------------------------------
1350 // Ctor - Decides the tests to run
1351 //---------------------------------------------------------------------------
1352 void EnProfilerTests()
1353 {
1355
1356 AddInitTest("TestToggling");
1357 AddInitTest("TestTogglingImmediate");
1358 AddInitTest("TestSetFlags");
1359 AddInitTest("TestClearFlags");
1360 AddInitTest("TestAddFlags");
1361 AddInitTest("TestModule");
1362 AddInitTest("TestClassTimeData");
1363 AddInitTest("TestClassCountData");
1364 AddInitTest("TestFuncTimeData");
1365 AddInitTest("TestFuncCountData");
1366 }
1367
1368 //---------------------------------------------------------------------------
1369 // Dtor
1370 //---------------------------------------------------------------------------
1371 void ~EnProfilerTests()
1372 {
1374 }
1375
1376 //---------------------------------------------------------------------------
1377 // Tests
1378 //---------------------------------------------------------------------------
1379 // Test toggling state
1381 {
1385 {
1388 }
1389
1390 return NTFR(TFR.FAIL);
1391 }
1392
1393 //---------------------------------------------------------------------------
1394 // Test toggling immediate state
1396 {
1400 {
1403 }
1404
1405 return NTFR(TFR.FAIL);
1406 }
1407
1408 //---------------------------------------------------------------------------
1409 // Test SetFlags/GetFlags
1411 {
1413
1414 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1415 {
1417 EnProfiler.SetFlags(flags);
1418
1419 if (!Assert(EnProfiler.GetFlags() == flags))
1420 {
1422 return NTFR(TFR.FAIL);
1423 }
1424
1425 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1426 {
1428 EnProfiler.SetFlags(flags);
1429
1430 if (!Assert(EnProfiler.GetFlags() == flags))
1431 {
1433 return NTFR(TFR.FAIL);
1434 }
1435 }
1436 }
1437
1438 // Let's test some bogus
1439 EnProfiler.SetFlags(-333);
1442 if (!Assert(bogusFlags == 0))
1443 {
1445 return NTFR(TFR.FAIL);
1446 }
1447
1450 if (!Assert(bogusFlags == 0))
1451 {
1453 return NTFR(TFR.FAIL);
1454 }
1455
1456 // Reset
1458 return NTFR(TFR.SUCCESS);
1459 }
1460
1461 //---------------------------------------------------------------------------
1462 // Test removing of flags
1464 {
1466
1468
1470 {
1472 return NTFR(TFR.FAIL);
1473 }
1474
1477
1478 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1479 {
1481 return NTFR(TFR.FAIL);
1482 }
1483
1486
1487 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1488 {
1490 return NTFR(TFR.FAIL);
1491 }
1492
1493 // Reset
1495 return NTFR(TFR.SUCCESS);
1496 }
1497
1498 //---------------------------------------------------------------------------
1499 // Test adding of flags
1501 {
1503
1505
1506 // Return should match resulting flags
1508 {
1510 return NTFR(TFR.FAIL);
1511 }
1512
1513 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1514 {
1516 return NTFR(TFR.FAIL);
1517 }
1518
1519 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1520 {
1522 return NTFR(TFR.FAIL);
1523 }
1524
1525 // Reset
1527 return NTFR(TFR.SUCCESS);
1528 }
1529
1530 //---------------------------------------------------------------------------
1531 // Test module
1533 {
1534 // File lives in Game, use it while testing
1536
1537 // This was added at the same time as this API, so check if it works as well
1538 string nameOfCurrentModule = Type().GetModule();
1539 if (!Assert(nameOfCurrentModule != ""))
1540 return NTFR(TFR.FAIL);
1541
1542 // We know we are in Game, so use it as a test
1545 return NTFR(TFR.FAIL);
1546
1548 return NTFR(TFR.FAIL);
1549
1550 // Test if setting and getting works
1553
1555 {
1557 return NTFR(TFR.FAIL);
1558 }
1559
1560 // Data to restore
1563
1564 // Make sure we are only profiling Game and that the data is clean
1565 // Only valid for the Get...Per... methods, as they need to be sorted
1567
1568 // GetTickTime() returns in seconds, so gather the results in seconds too
1571
1572 // Time to sleeb
1573 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1574 float timeSlept = Sleep(0.3);
1575 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1576 float diff = postTime - previousTime - timeSlept;
1577
1578 // Restore
1580
1581 // We called the function, so it must have some time
1582 if (!Assert(postTime > 0))
1583 {
1585
1586 if (!wasEnabled)
1587 EnProfiler.Enable(false, true);
1588
1590
1591 return NTFR(TFR.FAIL);
1592 }
1593
1594 if (!Assert(diff < 0.00001))
1595 {
1597
1598 if (!wasEnabled)
1599 EnProfiler.Enable(false, true);
1600
1602
1603 return NTFR(TFR.FAIL);
1604 }
1605
1606 // Clean the session
1608
1609 // Something from a different module should not get sorted, so just fire something from a different module
1610 for (int i = 0; i < 1000; ++i)
1611 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1612
1613 // Sort and gather the data and validate if it is correct
1617
1618 Debug.TFLog("Game fncs:", this, "TestModule");
1619
1620 int funcCount = timePerFunc.Count();
1621 for (int j = 0; j < funcCount; ++j)
1622 {
1624 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1625 // We are currently profiling Game, so this Core function shouldn't be present
1626 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1627 {
1629
1630 if (!wasEnabled)
1631 EnProfiler.Enable(false, true);
1632
1634
1635 return NTFR(TFR.FAIL);
1636 }
1637 }
1638
1641
1642 int classCount = timePerClass.Count();
1643 for (int k = 0; k < classCount; ++k)
1644 {
1645 typename type = timePerClass[k].param2;
1647 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1648 {
1650
1651 if (!wasEnabled)
1652 EnProfiler.Enable(false, true);
1653
1655
1656 return NTFR(TFR.FAIL);
1657 }
1658
1659 // Only classes from Game should be present
1660 if (!Assert(classModule == eptModule))
1661 {
1663
1664 if (!wasEnabled)
1665 EnProfiler.Enable(false, true);
1666
1668
1669 return NTFR(TFR.FAIL);
1670 }
1671 }
1672
1673 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1676 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1678
1679 bool found = false;
1680
1681 Debug.TFLog("Core fncs:", this, "TestModule");
1682
1683 funcCount = timePerFunc.Count();
1684 for (int l = 0; l < funcCount; ++l)
1685 {
1687 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1688 // We are currently profiling Core, so this Core function should be present
1689 if (tfpc.param2 == "EnumTools::StringToEnum")
1690 {
1691 found = true;
1692 break;
1693 }
1694 }
1695
1696 Assert(found);
1697
1698 // Test some bogus
1700 EnProfiler.SetModule(-333);
1701 bool success = Assert(EnProfiler.GetModule() == mod);
1702 EnProfiler.SetModule(6003);
1704
1707
1708 if (!wasEnabled)
1709 EnProfiler.Enable(false, true);
1710
1711 return BTFR(success && found);
1712 }
1713
1714 //---------------------------------------------------------------------------
1715 // Test to see if class time data is correct
1717 {
1718 // We should restore this when done
1721
1722 // GetTickTime() returns in seconds, so gather the results in seconds too
1724
1725 // Create the classes
1727
1728 // Time to stress
1729 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1730 float timeStressed = clss.DoEverything();
1731 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1732 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1735
1736 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1737
1738 // Restore
1740 if (!wasEnabled)
1741 EnProfiler.Enable(false, true);
1742
1743 // We called the function, so it must have some time
1744 if (!Assert(postTime > 0))
1745 return NTFR(TFR.FAIL);
1746
1748 return NTFR(TFR.FAIL);
1749
1750 if (!Assert(diff < 0.001))
1751 return NTFR(TFR.FAIL);
1752
1753 return NTFR(TFR.SUCCESS);
1754 }
1755
1756 //---------------------------------------------------------------------------
1757 // Test to see if class count data is correct
1759 {
1760 const int allocAmount = 9;
1761 const int releaseAmount = 6;
1763
1764 // We should restore this when done
1766
1767 // Time to test
1770
1772 for (int i = 0; i < allocAmount; ++i)
1773 instanceArr.Insert(new EPTHelperClass());
1774
1775 for (int j = 0; j < releaseAmount; ++j)
1776 delete instanceArr[j];
1777
1778 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1779 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1780
1783
1784 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1785
1786 // Restore
1787 if (!wasEnabled)
1788 EnProfiler.Enable(false, true);
1789
1790 // Time to check
1791 if (!Assert(alloced == allocAmount))
1792 return NTFR(TFR.FAIL);
1793
1795 return NTFR(TFR.FAIL);
1796
1797 return NTFR(TFR.SUCCESS);
1798 }
1799
1800 //---------------------------------------------------------------------------
1801 // Test to see if func time data is correct
1803 {
1804 // We should restore this when done
1807
1808 // GetTickTime() returns in seconds, so gather the results in seconds too
1810
1811 // Time to stress
1812 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1813 float timeStressed = StringFormat();
1814 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1817
1818 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1819 float timeStressed2 = StringConcat();
1820 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1823
1824 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1825
1826 // Restore
1828 if (!wasEnabled)
1829 EnProfiler.Enable(false, true);
1830
1831 // We called the function, so it must have some time
1832 if (!Assert(postTime > 0))
1833 return NTFR(TFR.FAIL);
1834
1835 if (!Assert(diff < 0.001))
1836 return NTFR(TFR.FAIL);
1837
1838 if (!Assert(postTime2 > 0))
1839 return NTFR(TFR.FAIL);
1840
1841 if (!Assert(diff2 < 0.001))
1842 return NTFR(TFR.FAIL);
1843
1844 // I know that string.Format is faster than additive concatenation
1846 return NTFR(TFR.FAIL);
1847
1848 return NTFR(TFR.SUCCESS);
1849 }
1850
1851 //---------------------------------------------------------------------------
1852 // Test to see if func count data is correct
1854 {
1855 // We should restore this when done
1857
1858 // Time to count
1859
1860 // - CallFunction
1861 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1862 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1863 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1864
1866
1867 // - CallFunctionParams
1868 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1869 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1870 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1871
1873
1874 // - Regular call
1875 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1877 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1878
1880
1881 // - Call
1882 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1883 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1884 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1885
1887
1888 // - Garbage
1889 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1890
1891 // - Static
1892 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1894 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1895
1897
1898 // - Global
1899 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1900 GetDayZGame();
1901 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1902
1904
1905 // - Global proto
1906 // Not tracked, so don't need to compare before and after, should always be 0
1907 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1908 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1909
1910 // - Static proto
1911 // Not tracked, so don't need to compare before and after, should always be 0
1912 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1913
1914 // - proto
1915 // Not tracked, so don't need to compare before and after, should always be 0
1916 GetGame().GetHostName();
1917 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1918
1919 // - proto native
1920 // Not tracked, so don't need to compare before and after, should always be 0
1921 GetGame().IsServer();
1922 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1923
1924 // - static proto native
1925 // Not tracked, so don't need to compare before and after, should always be 0
1927 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1928
1929 // Restore
1930 if (!wasEnabled)
1931 EnProfiler.Enable(false, true);
1932
1933 // Do the checks
1934
1935 // - CallFunction
1936 if (!Assert(callCountCF == 1))
1937 return NTFR(TFR.FAIL);
1938
1939 // - CallFunctionParams
1940 if (!Assert(callCountCFP == 1))
1941 return NTFR(TFR.FAIL);
1942
1943 // - Regular call
1944 if (!Assert(callCountRG == 1))
1945 return NTFR(TFR.FAIL);
1946
1947 // - Call
1948 if (!Assert(callCountC == 1))
1949 return NTFR(TFR.FAIL);
1950
1951 // - Garbage
1952 if (!Assert(callCountNon == -1))
1953 return NTFR(TFR.FAIL);
1954
1955 // - Static
1956 if (!Assert(callCountS == 1))
1957 return NTFR(TFR.FAIL);
1958
1959 // - Global
1960 if (!Assert(callCountG == 1))
1961 return NTFR(TFR.FAIL);
1962
1963 // - Global proto
1964 if (!Assert(callCountGP == 0))
1965 return NTFR(TFR.FAIL);
1966
1967 // - Static proto
1968 if (!Assert(callCountSP == 0))
1969 return NTFR(TFR.FAIL);
1970
1971 // - proto
1972 if (!Assert(callCountP == 0))
1973 return NTFR(TFR.FAIL);
1974
1975 // - proto native
1976 if (!Assert(callCountPN == 0))
1977 return NTFR(TFR.FAIL);
1978
1979 // - static proto native
1980 if (!Assert(callCountSPN == 0))
1981 return NTFR(TFR.FAIL);
1982
1983 return NTFR(TFR.SUCCESS);
1984 }
1985
1986 //---------------------------------------------------------------------------
1987 // Helpers
1988 //---------------------------------------------------------------------------
1989 // Snore
1990 float Sleep(float timeS)
1991 {
1992 float startTime = GetGame().GetTickTime();
1993 while (GetGame().GetTickTime() - startTime < timeS)
1994 {
1995 // Zzz
1996 }
1997
1998 return GetGame().GetTickTime() - startTime;
1999 }
2000
2001 //---------------------------------------------------------------------------
2002 // Example stress method
2003 float StringFormat()
2004 {
2005 float startTime = GetGame().GetTickTime();
2006
2007 for (int i = 0; i < 1000; ++i)
2008 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
2009
2010 return GetGame().GetTickTime() - startTime;
2011 }
2012
2013 //---------------------------------------------------------------------------
2014 // Example stress method 2
2015 float StringConcat()
2016 {
2017 float startTime = GetGame().GetTickTime();
2018
2019 for (int i = 0; i < 1000; ++i)
2020 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
2021
2022 return GetGame().GetTickTime() - startTime;
2023 }
2024
2025 //---------------------------------------------------------------------------
2026 // To make sure it is only ever called in that test
2028 {
2029 int dummy = 3;
2030 }
2031
2032 //---------------------------------------------------------------------------
2033 // To make sure it is only ever called in that test
2034 static void TestFuncCountDataHelperStatic()
2035 {
2036 int dummy = 3;
2037 }
2038}
2039
2040class EPTHelperClass
2041{
2042 float Sleep2(float timeS)
2043 {
2044 float startTime = GetGame().GetTickTime();
2045 while (GetGame().GetTickTime() - startTime < timeS)
2046 {
2047 // Zzz
2048 }
2049
2050 return GetGame().GetTickTime() - startTime;
2051 }
2052 float SleepAgain(float timeS)
2053 {
2054 float startTime = GetGame().GetTickTime();
2055 while (GetGame().GetTickTime() - startTime < timeS)
2056 {
2057 // Zzz
2058 }
2059
2060 return GetGame().GetTickTime() - startTime;
2061 }
2062
2063 float DoEverything()
2064 {
2065 float startTime = GetGame().GetTickTime();
2066
2067 Sleep2(3);
2068 SleepAgain(3);
2069
2070 return GetGame().GetTickTime() - startTime;
2071 }
2072}
DayZGame GetDayZGame()
Definition DayZGame.c:3530
float StringConcat()
float Sleep(float timeS)
void TestFuncCountDataHelper()
void EnProfilerTests()
TFResult TestModule()
TFResult TestClearFlags()
void ~EnProfilerTests()
TFResult TestTogglingImmediate()
TFResult TestFuncCountData()
TFResult TestClassTimeData()
static void TestFuncCountDataHelperStatic()
float StringFormat()
TFResult TestClassCountData()
TFResult TestAddFlags()
bool m_bWasProfilerEnabled
Remember this, so we can restore the previous state before the test!
TFResult TestFuncTimeData()
TFResult TestSetFlags()
TFResult TestToggling()
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
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
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.

◆ Sleep2()

Definition at line 1 of file EnProfilerTests.c.

700 {
701 float startTime = GetGame().GetTickTime();
702 while (GetGame().GetTickTime() - startTime < timeS)
703 {
704 // Zzz
705 }
706
707 return GetGame().GetTickTime() - startTime;
708 }

Referenced by DoEverything().

◆ SleepAgain()

float SleepAgain ( float timeS)

Definition at line 709 of file EnProfilerTests.c.

710 {
711 float startTime = GetGame().GetTickTime();
712 while (GetGame().GetTickTime() - startTime < timeS)
713 {
714 // Zzz
715 }
716
717 return GetGame().GetTickTime() - startTime;
718 }

References GetGame().

◆ StringConcat()

float Sleep2::StringConcat ( )

Definition at line 1369 of file EnProfilerTests.c.

1369 : TestFramework
1370{
1373
1374 //---------------------------------------------------------------------------
1375 // Ctor - Decides the tests to run
1376 //---------------------------------------------------------------------------
1377 void EnProfilerTests()
1378 {
1380
1381 AddInitTest("TestToggling");
1382 AddInitTest("TestTogglingImmediate");
1383 AddInitTest("TestSetFlags");
1384 AddInitTest("TestClearFlags");
1385 AddInitTest("TestAddFlags");
1386 AddInitTest("TestModule");
1387 AddInitTest("TestClassTimeData");
1388 AddInitTest("TestClassCountData");
1389 AddInitTest("TestFuncTimeData");
1390 AddInitTest("TestFuncCountData");
1391 }
1392
1393 //---------------------------------------------------------------------------
1394 // Dtor
1395 //---------------------------------------------------------------------------
1396 void ~EnProfilerTests()
1397 {
1399 }
1400
1401 //---------------------------------------------------------------------------
1402 // Tests
1403 //---------------------------------------------------------------------------
1404 // Test toggling state
1406 {
1410 {
1413 }
1414
1415 return NTFR(TFR.FAIL);
1416 }
1417
1418 //---------------------------------------------------------------------------
1419 // Test toggling immediate state
1421 {
1425 {
1428 }
1429
1430 return NTFR(TFR.FAIL);
1431 }
1432
1433 //---------------------------------------------------------------------------
1434 // Test SetFlags/GetFlags
1436 {
1438
1439 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1440 {
1442 EnProfiler.SetFlags(flags);
1443
1444 if (!Assert(EnProfiler.GetFlags() == flags))
1445 {
1447 return NTFR(TFR.FAIL);
1448 }
1449
1450 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1451 {
1453 EnProfiler.SetFlags(flags);
1454
1455 if (!Assert(EnProfiler.GetFlags() == flags))
1456 {
1458 return NTFR(TFR.FAIL);
1459 }
1460 }
1461 }
1462
1463 // Let's test some bogus
1464 EnProfiler.SetFlags(-333);
1467 if (!Assert(bogusFlags == 0))
1468 {
1470 return NTFR(TFR.FAIL);
1471 }
1472
1475 if (!Assert(bogusFlags == 0))
1476 {
1478 return NTFR(TFR.FAIL);
1479 }
1480
1481 // Reset
1483 return NTFR(TFR.SUCCESS);
1484 }
1485
1486 //---------------------------------------------------------------------------
1487 // Test removing of flags
1489 {
1491
1493
1495 {
1497 return NTFR(TFR.FAIL);
1498 }
1499
1502
1503 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1504 {
1506 return NTFR(TFR.FAIL);
1507 }
1508
1511
1512 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1513 {
1515 return NTFR(TFR.FAIL);
1516 }
1517
1518 // Reset
1520 return NTFR(TFR.SUCCESS);
1521 }
1522
1523 //---------------------------------------------------------------------------
1524 // Test adding of flags
1526 {
1528
1530
1531 // Return should match resulting flags
1533 {
1535 return NTFR(TFR.FAIL);
1536 }
1537
1538 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1539 {
1541 return NTFR(TFR.FAIL);
1542 }
1543
1544 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1545 {
1547 return NTFR(TFR.FAIL);
1548 }
1549
1550 // Reset
1552 return NTFR(TFR.SUCCESS);
1553 }
1554
1555 //---------------------------------------------------------------------------
1556 // Test module
1558 {
1559 // File lives in Game, use it while testing
1561
1562 // This was added at the same time as this API, so check if it works as well
1563 string nameOfCurrentModule = Type().GetModule();
1564 if (!Assert(nameOfCurrentModule != ""))
1565 return NTFR(TFR.FAIL);
1566
1567 // We know we are in Game, so use it as a test
1570 return NTFR(TFR.FAIL);
1571
1573 return NTFR(TFR.FAIL);
1574
1575 // Test if setting and getting works
1578
1580 {
1582 return NTFR(TFR.FAIL);
1583 }
1584
1585 // Data to restore
1588
1589 // Make sure we are only profiling Game and that the data is clean
1590 // Only valid for the Get...Per... methods, as they need to be sorted
1592
1593 // GetTickTime() returns in seconds, so gather the results in seconds too
1596
1597 // Time to sleeb
1598 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1599 float timeSlept = Sleep(0.3);
1600 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1601 float diff = postTime - previousTime - timeSlept;
1602
1603 // Restore
1605
1606 // We called the function, so it must have some time
1607 if (!Assert(postTime > 0))
1608 {
1610
1611 if (!wasEnabled)
1612 EnProfiler.Enable(false, true);
1613
1615
1616 return NTFR(TFR.FAIL);
1617 }
1618
1619 if (!Assert(diff < 0.00001))
1620 {
1622
1623 if (!wasEnabled)
1624 EnProfiler.Enable(false, true);
1625
1627
1628 return NTFR(TFR.FAIL);
1629 }
1630
1631 // Clean the session
1633
1634 // Something from a different module should not get sorted, so just fire something from a different module
1635 for (int i = 0; i < 1000; ++i)
1636 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1637
1638 // Sort and gather the data and validate if it is correct
1642
1643 Debug.TFLog("Game fncs:", this, "TestModule");
1644
1645 int funcCount = timePerFunc.Count();
1646 for (int j = 0; j < funcCount; ++j)
1647 {
1649 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1650 // We are currently profiling Game, so this Core function shouldn't be present
1651 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1652 {
1654
1655 if (!wasEnabled)
1656 EnProfiler.Enable(false, true);
1657
1659
1660 return NTFR(TFR.FAIL);
1661 }
1662 }
1663
1666
1667 int classCount = timePerClass.Count();
1668 for (int k = 0; k < classCount; ++k)
1669 {
1670 typename type = timePerClass[k].param2;
1672 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1673 {
1675
1676 if (!wasEnabled)
1677 EnProfiler.Enable(false, true);
1678
1680
1681 return NTFR(TFR.FAIL);
1682 }
1683
1684 // Only classes from Game should be present
1685 if (!Assert(classModule == eptModule))
1686 {
1688
1689 if (!wasEnabled)
1690 EnProfiler.Enable(false, true);
1691
1693
1694 return NTFR(TFR.FAIL);
1695 }
1696 }
1697
1698 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1701 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1703
1704 bool found = false;
1705
1706 Debug.TFLog("Core fncs:", this, "TestModule");
1707
1708 funcCount = timePerFunc.Count();
1709 for (int l = 0; l < funcCount; ++l)
1710 {
1712 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1713 // We are currently profiling Core, so this Core function should be present
1714 if (tfpc.param2 == "EnumTools::StringToEnum")
1715 {
1716 found = true;
1717 break;
1718 }
1719 }
1720
1721 Assert(found);
1722
1723 // Test some bogus
1725 EnProfiler.SetModule(-333);
1726 bool success = Assert(EnProfiler.GetModule() == mod);
1727 EnProfiler.SetModule(6003);
1729
1732
1733 if (!wasEnabled)
1734 EnProfiler.Enable(false, true);
1735
1736 return BTFR(success && found);
1737 }
1738
1739 //---------------------------------------------------------------------------
1740 // Test to see if class time data is correct
1742 {
1743 // We should restore this when done
1746
1747 // GetTickTime() returns in seconds, so gather the results in seconds too
1749
1750 // Create the classes
1752
1753 // Time to stress
1754 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1755 float timeStressed = clss.DoEverything();
1756 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1757 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1760
1761 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1762
1763 // Restore
1765 if (!wasEnabled)
1766 EnProfiler.Enable(false, true);
1767
1768 // We called the function, so it must have some time
1769 if (!Assert(postTime > 0))
1770 return NTFR(TFR.FAIL);
1771
1773 return NTFR(TFR.FAIL);
1774
1775 if (!Assert(diff < 0.001))
1776 return NTFR(TFR.FAIL);
1777
1778 return NTFR(TFR.SUCCESS);
1779 }
1780
1781 //---------------------------------------------------------------------------
1782 // Test to see if class count data is correct
1784 {
1785 const int allocAmount = 9;
1786 const int releaseAmount = 6;
1788
1789 // We should restore this when done
1791
1792 // Time to test
1795
1797 for (int i = 0; i < allocAmount; ++i)
1798 instanceArr.Insert(new EPTHelperClass());
1799
1800 for (int j = 0; j < releaseAmount; ++j)
1801 delete instanceArr[j];
1802
1803 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1804 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1805
1808
1809 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1810
1811 // Restore
1812 if (!wasEnabled)
1813 EnProfiler.Enable(false, true);
1814
1815 // Time to check
1816 if (!Assert(alloced == allocAmount))
1817 return NTFR(TFR.FAIL);
1818
1820 return NTFR(TFR.FAIL);
1821
1822 return NTFR(TFR.SUCCESS);
1823 }
1824
1825 //---------------------------------------------------------------------------
1826 // Test to see if func time data is correct
1828 {
1829 // We should restore this when done
1832
1833 // GetTickTime() returns in seconds, so gather the results in seconds too
1835
1836 // Time to stress
1837 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1838 float timeStressed = StringFormat();
1839 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1842
1843 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1844 float timeStressed2 = StringConcat();
1845 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1848
1849 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1850
1851 // Restore
1853 if (!wasEnabled)
1854 EnProfiler.Enable(false, true);
1855
1856 // We called the function, so it must have some time
1857 if (!Assert(postTime > 0))
1858 return NTFR(TFR.FAIL);
1859
1860 if (!Assert(diff < 0.001))
1861 return NTFR(TFR.FAIL);
1862
1863 if (!Assert(postTime2 > 0))
1864 return NTFR(TFR.FAIL);
1865
1866 if (!Assert(diff2 < 0.001))
1867 return NTFR(TFR.FAIL);
1868
1869 // I know that string.Format is faster than additive concatenation
1871 return NTFR(TFR.FAIL);
1872
1873 return NTFR(TFR.SUCCESS);
1874 }
1875
1876 //---------------------------------------------------------------------------
1877 // Test to see if func count data is correct
1879 {
1880 // We should restore this when done
1882
1883 // Time to count
1884
1885 // - CallFunction
1886 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1887 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1888 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1889
1891
1892 // - CallFunctionParams
1893 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1894 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1895 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1896
1898
1899 // - Regular call
1900 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1902 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1903
1905
1906 // - Call
1907 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1908 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1909 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1910
1912
1913 // - Garbage
1914 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1915
1916 // - Static
1917 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1919 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1920
1922
1923 // - Global
1924 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1925 GetDayZGame();
1926 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1927
1929
1930 // - Global proto
1931 // Not tracked, so don't need to compare before and after, should always be 0
1932 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1933 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1934
1935 // - Static proto
1936 // Not tracked, so don't need to compare before and after, should always be 0
1937 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1938
1939 // - proto
1940 // Not tracked, so don't need to compare before and after, should always be 0
1941 GetGame().GetHostName();
1942 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1943
1944 // - proto native
1945 // Not tracked, so don't need to compare before and after, should always be 0
1946 GetGame().IsServer();
1947 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1948
1949 // - static proto native
1950 // Not tracked, so don't need to compare before and after, should always be 0
1952 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1953
1954 // Restore
1955 if (!wasEnabled)
1956 EnProfiler.Enable(false, true);
1957
1958 // Do the checks
1959
1960 // - CallFunction
1961 if (!Assert(callCountCF == 1))
1962 return NTFR(TFR.FAIL);
1963
1964 // - CallFunctionParams
1965 if (!Assert(callCountCFP == 1))
1966 return NTFR(TFR.FAIL);
1967
1968 // - Regular call
1969 if (!Assert(callCountRG == 1))
1970 return NTFR(TFR.FAIL);
1971
1972 // - Call
1973 if (!Assert(callCountC == 1))
1974 return NTFR(TFR.FAIL);
1975
1976 // - Garbage
1977 if (!Assert(callCountNon == -1))
1978 return NTFR(TFR.FAIL);
1979
1980 // - Static
1981 if (!Assert(callCountS == 1))
1982 return NTFR(TFR.FAIL);
1983
1984 // - Global
1985 if (!Assert(callCountG == 1))
1986 return NTFR(TFR.FAIL);
1987
1988 // - Global proto
1989 if (!Assert(callCountGP == 0))
1990 return NTFR(TFR.FAIL);
1991
1992 // - Static proto
1993 if (!Assert(callCountSP == 0))
1994 return NTFR(TFR.FAIL);
1995
1996 // - proto
1997 if (!Assert(callCountP == 0))
1998 return NTFR(TFR.FAIL);
1999
2000 // - proto native
2001 if (!Assert(callCountPN == 0))
2002 return NTFR(TFR.FAIL);
2003
2004 // - static proto native
2005 if (!Assert(callCountSPN == 0))
2006 return NTFR(TFR.FAIL);
2007
2008 return NTFR(TFR.SUCCESS);
2009 }
2010
2011 //---------------------------------------------------------------------------
2012 // Helpers
2013 //---------------------------------------------------------------------------
2014 // Snore
2015 float Sleep(float timeS)
2016 {
2017 float startTime = GetGame().GetTickTime();
2018 while (GetGame().GetTickTime() - startTime < timeS)
2019 {
2020 // Zzz
2021 }
2022
2023 return GetGame().GetTickTime() - startTime;
2024 }
2025
2026 //---------------------------------------------------------------------------
2027 // Example stress method
2028 float StringFormat()
2029 {
2030 float startTime = GetGame().GetTickTime();
2031
2032 for (int i = 0; i < 1000; ++i)
2033 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
2034
2035 return GetGame().GetTickTime() - startTime;
2036 }
2037
2038 //---------------------------------------------------------------------------
2039 // Example stress method 2
2040 float StringConcat()
2041 {
2042 float startTime = GetGame().GetTickTime();
2043
2044 for (int i = 0; i < 1000; ++i)
2045 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
2046
2047 return GetGame().GetTickTime() - startTime;
2048 }
2049
2050 //---------------------------------------------------------------------------
2051 // To make sure it is only ever called in that test
2053 {
2054 int dummy = 3;
2055 }
2056
2057 //---------------------------------------------------------------------------
2058 // To make sure it is only ever called in that test
2059 static void TestFuncCountDataHelperStatic()
2060 {
2061 int dummy = 3;
2062 }
2063}
2064
2065class EPTHelperClass
2066{
2067 float Sleep2(float timeS)
2068 {
2069 float startTime = GetGame().GetTickTime();
2070 while (GetGame().GetTickTime() - startTime < timeS)
2071 {
2072 // Zzz
2073 }
2074
2075 return GetGame().GetTickTime() - startTime;
2076 }
2077 float SleepAgain(float timeS)
2078 {
2079 float startTime = GetGame().GetTickTime();
2080 while (GetGame().GetTickTime() - startTime < timeS)
2081 {
2082 // Zzz
2083 }
2084
2085 return GetGame().GetTickTime() - startTime;
2086 }
2087
2088 float DoEverything()
2089 {
2090 float startTime = GetGame().GetTickTime();
2091
2092 Sleep2(3);
2093 SleepAgain(3);
2094
2095 return GetGame().GetTickTime() - startTime;
2096 }
2097}

◆ StringFormat()

float Sleep2::StringFormat ( )

Definition at line 1357 of file EnProfilerTests.c.

1357 : TestFramework
1358{
1361
1362 //---------------------------------------------------------------------------
1363 // Ctor - Decides the tests to run
1364 //---------------------------------------------------------------------------
1365 void EnProfilerTests()
1366 {
1368
1369 AddInitTest("TestToggling");
1370 AddInitTest("TestTogglingImmediate");
1371 AddInitTest("TestSetFlags");
1372 AddInitTest("TestClearFlags");
1373 AddInitTest("TestAddFlags");
1374 AddInitTest("TestModule");
1375 AddInitTest("TestClassTimeData");
1376 AddInitTest("TestClassCountData");
1377 AddInitTest("TestFuncTimeData");
1378 AddInitTest("TestFuncCountData");
1379 }
1380
1381 //---------------------------------------------------------------------------
1382 // Dtor
1383 //---------------------------------------------------------------------------
1384 void ~EnProfilerTests()
1385 {
1387 }
1388
1389 //---------------------------------------------------------------------------
1390 // Tests
1391 //---------------------------------------------------------------------------
1392 // Test toggling state
1394 {
1398 {
1401 }
1402
1403 return NTFR(TFR.FAIL);
1404 }
1405
1406 //---------------------------------------------------------------------------
1407 // Test toggling immediate state
1409 {
1413 {
1416 }
1417
1418 return NTFR(TFR.FAIL);
1419 }
1420
1421 //---------------------------------------------------------------------------
1422 // Test SetFlags/GetFlags
1424 {
1426
1427 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1428 {
1430 EnProfiler.SetFlags(flags);
1431
1432 if (!Assert(EnProfiler.GetFlags() == flags))
1433 {
1435 return NTFR(TFR.FAIL);
1436 }
1437
1438 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1439 {
1441 EnProfiler.SetFlags(flags);
1442
1443 if (!Assert(EnProfiler.GetFlags() == flags))
1444 {
1446 return NTFR(TFR.FAIL);
1447 }
1448 }
1449 }
1450
1451 // Let's test some bogus
1452 EnProfiler.SetFlags(-333);
1455 if (!Assert(bogusFlags == 0))
1456 {
1458 return NTFR(TFR.FAIL);
1459 }
1460
1463 if (!Assert(bogusFlags == 0))
1464 {
1466 return NTFR(TFR.FAIL);
1467 }
1468
1469 // Reset
1471 return NTFR(TFR.SUCCESS);
1472 }
1473
1474 //---------------------------------------------------------------------------
1475 // Test removing of flags
1477 {
1479
1481
1483 {
1485 return NTFR(TFR.FAIL);
1486 }
1487
1490
1491 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1492 {
1494 return NTFR(TFR.FAIL);
1495 }
1496
1499
1500 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1501 {
1503 return NTFR(TFR.FAIL);
1504 }
1505
1506 // Reset
1508 return NTFR(TFR.SUCCESS);
1509 }
1510
1511 //---------------------------------------------------------------------------
1512 // Test adding of flags
1514 {
1516
1518
1519 // Return should match resulting flags
1521 {
1523 return NTFR(TFR.FAIL);
1524 }
1525
1526 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1527 {
1529 return NTFR(TFR.FAIL);
1530 }
1531
1532 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1533 {
1535 return NTFR(TFR.FAIL);
1536 }
1537
1538 // Reset
1540 return NTFR(TFR.SUCCESS);
1541 }
1542
1543 //---------------------------------------------------------------------------
1544 // Test module
1546 {
1547 // File lives in Game, use it while testing
1549
1550 // This was added at the same time as this API, so check if it works as well
1551 string nameOfCurrentModule = Type().GetModule();
1552 if (!Assert(nameOfCurrentModule != ""))
1553 return NTFR(TFR.FAIL);
1554
1555 // We know we are in Game, so use it as a test
1558 return NTFR(TFR.FAIL);
1559
1561 return NTFR(TFR.FAIL);
1562
1563 // Test if setting and getting works
1566
1568 {
1570 return NTFR(TFR.FAIL);
1571 }
1572
1573 // Data to restore
1576
1577 // Make sure we are only profiling Game and that the data is clean
1578 // Only valid for the Get...Per... methods, as they need to be sorted
1580
1581 // GetTickTime() returns in seconds, so gather the results in seconds too
1584
1585 // Time to sleeb
1586 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1587 float timeSlept = Sleep(0.3);
1588 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1589 float diff = postTime - previousTime - timeSlept;
1590
1591 // Restore
1593
1594 // We called the function, so it must have some time
1595 if (!Assert(postTime > 0))
1596 {
1598
1599 if (!wasEnabled)
1600 EnProfiler.Enable(false, true);
1601
1603
1604 return NTFR(TFR.FAIL);
1605 }
1606
1607 if (!Assert(diff < 0.00001))
1608 {
1610
1611 if (!wasEnabled)
1612 EnProfiler.Enable(false, true);
1613
1615
1616 return NTFR(TFR.FAIL);
1617 }
1618
1619 // Clean the session
1621
1622 // Something from a different module should not get sorted, so just fire something from a different module
1623 for (int i = 0; i < 1000; ++i)
1624 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1625
1626 // Sort and gather the data and validate if it is correct
1630
1631 Debug.TFLog("Game fncs:", this, "TestModule");
1632
1633 int funcCount = timePerFunc.Count();
1634 for (int j = 0; j < funcCount; ++j)
1635 {
1637 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1638 // We are currently profiling Game, so this Core function shouldn't be present
1639 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1640 {
1642
1643 if (!wasEnabled)
1644 EnProfiler.Enable(false, true);
1645
1647
1648 return NTFR(TFR.FAIL);
1649 }
1650 }
1651
1654
1655 int classCount = timePerClass.Count();
1656 for (int k = 0; k < classCount; ++k)
1657 {
1658 typename type = timePerClass[k].param2;
1660 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1661 {
1663
1664 if (!wasEnabled)
1665 EnProfiler.Enable(false, true);
1666
1668
1669 return NTFR(TFR.FAIL);
1670 }
1671
1672 // Only classes from Game should be present
1673 if (!Assert(classModule == eptModule))
1674 {
1676
1677 if (!wasEnabled)
1678 EnProfiler.Enable(false, true);
1679
1681
1682 return NTFR(TFR.FAIL);
1683 }
1684 }
1685
1686 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1689 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1691
1692 bool found = false;
1693
1694 Debug.TFLog("Core fncs:", this, "TestModule");
1695
1696 funcCount = timePerFunc.Count();
1697 for (int l = 0; l < funcCount; ++l)
1698 {
1700 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1701 // We are currently profiling Core, so this Core function should be present
1702 if (tfpc.param2 == "EnumTools::StringToEnum")
1703 {
1704 found = true;
1705 break;
1706 }
1707 }
1708
1709 Assert(found);
1710
1711 // Test some bogus
1713 EnProfiler.SetModule(-333);
1714 bool success = Assert(EnProfiler.GetModule() == mod);
1715 EnProfiler.SetModule(6003);
1717
1720
1721 if (!wasEnabled)
1722 EnProfiler.Enable(false, true);
1723
1724 return BTFR(success && found);
1725 }
1726
1727 //---------------------------------------------------------------------------
1728 // Test to see if class time data is correct
1730 {
1731 // We should restore this when done
1734
1735 // GetTickTime() returns in seconds, so gather the results in seconds too
1737
1738 // Create the classes
1740
1741 // Time to stress
1742 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1743 float timeStressed = clss.DoEverything();
1744 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1745 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1748
1749 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1750
1751 // Restore
1753 if (!wasEnabled)
1754 EnProfiler.Enable(false, true);
1755
1756 // We called the function, so it must have some time
1757 if (!Assert(postTime > 0))
1758 return NTFR(TFR.FAIL);
1759
1761 return NTFR(TFR.FAIL);
1762
1763 if (!Assert(diff < 0.001))
1764 return NTFR(TFR.FAIL);
1765
1766 return NTFR(TFR.SUCCESS);
1767 }
1768
1769 //---------------------------------------------------------------------------
1770 // Test to see if class count data is correct
1772 {
1773 const int allocAmount = 9;
1774 const int releaseAmount = 6;
1776
1777 // We should restore this when done
1779
1780 // Time to test
1783
1785 for (int i = 0; i < allocAmount; ++i)
1786 instanceArr.Insert(new EPTHelperClass());
1787
1788 for (int j = 0; j < releaseAmount; ++j)
1789 delete instanceArr[j];
1790
1791 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1792 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1793
1796
1797 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1798
1799 // Restore
1800 if (!wasEnabled)
1801 EnProfiler.Enable(false, true);
1802
1803 // Time to check
1804 if (!Assert(alloced == allocAmount))
1805 return NTFR(TFR.FAIL);
1806
1808 return NTFR(TFR.FAIL);
1809
1810 return NTFR(TFR.SUCCESS);
1811 }
1812
1813 //---------------------------------------------------------------------------
1814 // Test to see if func time data is correct
1816 {
1817 // We should restore this when done
1820
1821 // GetTickTime() returns in seconds, so gather the results in seconds too
1823
1824 // Time to stress
1825 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1826 float timeStressed = StringFormat();
1827 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1830
1831 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1832 float timeStressed2 = StringConcat();
1833 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1836
1837 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1838
1839 // Restore
1841 if (!wasEnabled)
1842 EnProfiler.Enable(false, true);
1843
1844 // We called the function, so it must have some time
1845 if (!Assert(postTime > 0))
1846 return NTFR(TFR.FAIL);
1847
1848 if (!Assert(diff < 0.001))
1849 return NTFR(TFR.FAIL);
1850
1851 if (!Assert(postTime2 > 0))
1852 return NTFR(TFR.FAIL);
1853
1854 if (!Assert(diff2 < 0.001))
1855 return NTFR(TFR.FAIL);
1856
1857 // I know that string.Format is faster than additive concatenation
1859 return NTFR(TFR.FAIL);
1860
1861 return NTFR(TFR.SUCCESS);
1862 }
1863
1864 //---------------------------------------------------------------------------
1865 // Test to see if func count data is correct
1867 {
1868 // We should restore this when done
1870
1871 // Time to count
1872
1873 // - CallFunction
1874 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1875 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1876 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1877
1879
1880 // - CallFunctionParams
1881 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1882 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1883 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1884
1886
1887 // - Regular call
1888 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1890 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1891
1893
1894 // - Call
1895 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1896 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1897 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1898
1900
1901 // - Garbage
1902 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1903
1904 // - Static
1905 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1907 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1908
1910
1911 // - Global
1912 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1913 GetDayZGame();
1914 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1915
1917
1918 // - Global proto
1919 // Not tracked, so don't need to compare before and after, should always be 0
1920 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1921 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1922
1923 // - Static proto
1924 // Not tracked, so don't need to compare before and after, should always be 0
1925 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1926
1927 // - proto
1928 // Not tracked, so don't need to compare before and after, should always be 0
1929 GetGame().GetHostName();
1930 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1931
1932 // - proto native
1933 // Not tracked, so don't need to compare before and after, should always be 0
1934 GetGame().IsServer();
1935 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1936
1937 // - static proto native
1938 // Not tracked, so don't need to compare before and after, should always be 0
1940 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1941
1942 // Restore
1943 if (!wasEnabled)
1944 EnProfiler.Enable(false, true);
1945
1946 // Do the checks
1947
1948 // - CallFunction
1949 if (!Assert(callCountCF == 1))
1950 return NTFR(TFR.FAIL);
1951
1952 // - CallFunctionParams
1953 if (!Assert(callCountCFP == 1))
1954 return NTFR(TFR.FAIL);
1955
1956 // - Regular call
1957 if (!Assert(callCountRG == 1))
1958 return NTFR(TFR.FAIL);
1959
1960 // - Call
1961 if (!Assert(callCountC == 1))
1962 return NTFR(TFR.FAIL);
1963
1964 // - Garbage
1965 if (!Assert(callCountNon == -1))
1966 return NTFR(TFR.FAIL);
1967
1968 // - Static
1969 if (!Assert(callCountS == 1))
1970 return NTFR(TFR.FAIL);
1971
1972 // - Global
1973 if (!Assert(callCountG == 1))
1974 return NTFR(TFR.FAIL);
1975
1976 // - Global proto
1977 if (!Assert(callCountGP == 0))
1978 return NTFR(TFR.FAIL);
1979
1980 // - Static proto
1981 if (!Assert(callCountSP == 0))
1982 return NTFR(TFR.FAIL);
1983
1984 // - proto
1985 if (!Assert(callCountP == 0))
1986 return NTFR(TFR.FAIL);
1987
1988 // - proto native
1989 if (!Assert(callCountPN == 0))
1990 return NTFR(TFR.FAIL);
1991
1992 // - static proto native
1993 if (!Assert(callCountSPN == 0))
1994 return NTFR(TFR.FAIL);
1995
1996 return NTFR(TFR.SUCCESS);
1997 }
1998
1999 //---------------------------------------------------------------------------
2000 // Helpers
2001 //---------------------------------------------------------------------------
2002 // Snore
2003 float Sleep(float timeS)
2004 {
2005 float startTime = GetGame().GetTickTime();
2006 while (GetGame().GetTickTime() - startTime < timeS)
2007 {
2008 // Zzz
2009 }
2010
2011 return GetGame().GetTickTime() - startTime;
2012 }
2013
2014 //---------------------------------------------------------------------------
2015 // Example stress method
2016 float StringFormat()
2017 {
2018 float startTime = GetGame().GetTickTime();
2019
2020 for (int i = 0; i < 1000; ++i)
2021 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
2022
2023 return GetGame().GetTickTime() - startTime;
2024 }
2025
2026 //---------------------------------------------------------------------------
2027 // Example stress method 2
2028 float StringConcat()
2029 {
2030 float startTime = GetGame().GetTickTime();
2031
2032 for (int i = 0; i < 1000; ++i)
2033 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
2034
2035 return GetGame().GetTickTime() - startTime;
2036 }
2037
2038 //---------------------------------------------------------------------------
2039 // To make sure it is only ever called in that test
2041 {
2042 int dummy = 3;
2043 }
2044
2045 //---------------------------------------------------------------------------
2046 // To make sure it is only ever called in that test
2047 static void TestFuncCountDataHelperStatic()
2048 {
2049 int dummy = 3;
2050 }
2051}
2052
2053class EPTHelperClass
2054{
2055 float Sleep2(float timeS)
2056 {
2057 float startTime = GetGame().GetTickTime();
2058 while (GetGame().GetTickTime() - startTime < timeS)
2059 {
2060 // Zzz
2061 }
2062
2063 return GetGame().GetTickTime() - startTime;
2064 }
2065 float SleepAgain(float timeS)
2066 {
2067 float startTime = GetGame().GetTickTime();
2068 while (GetGame().GetTickTime() - startTime < timeS)
2069 {
2070 // Zzz
2071 }
2072
2073 return GetGame().GetTickTime() - startTime;
2074 }
2075
2076 float DoEverything()
2077 {
2078 float startTime = GetGame().GetTickTime();
2079
2080 Sleep2(3);
2081 SleepAgain(3);
2082
2083 return GetGame().GetTickTime() - startTime;
2084 }
2085}

◆ TestAddFlags()

TFResult Sleep2::TestAddFlags ( )

Definition at line 854 of file EnProfilerTests.c.

854 : TestFramework
855{
858
859 //---------------------------------------------------------------------------
860 // Ctor - Decides the tests to run
861 //---------------------------------------------------------------------------
862 void EnProfilerTests()
863 {
865
866 AddInitTest("TestToggling");
867 AddInitTest("TestTogglingImmediate");
868 AddInitTest("TestSetFlags");
869 AddInitTest("TestClearFlags");
870 AddInitTest("TestAddFlags");
871 AddInitTest("TestModule");
872 AddInitTest("TestClassTimeData");
873 AddInitTest("TestClassCountData");
874 AddInitTest("TestFuncTimeData");
875 AddInitTest("TestFuncCountData");
876 }
877
878 //---------------------------------------------------------------------------
879 // Dtor
880 //---------------------------------------------------------------------------
881 void ~EnProfilerTests()
882 {
884 }
885
886 //---------------------------------------------------------------------------
887 // Tests
888 //---------------------------------------------------------------------------
889 // Test toggling state
891 {
895 {
898 }
899
900 return NTFR(TFR.FAIL);
901 }
902
903 //---------------------------------------------------------------------------
904 // Test toggling immediate state
906 {
910 {
913 }
914
915 return NTFR(TFR.FAIL);
916 }
917
918 //---------------------------------------------------------------------------
919 // Test SetFlags/GetFlags
921 {
923
924 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
925 {
927 EnProfiler.SetFlags(flags);
928
929 if (!Assert(EnProfiler.GetFlags() == flags))
930 {
932 return NTFR(TFR.FAIL);
933 }
934
935 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
936 {
938 EnProfiler.SetFlags(flags);
939
940 if (!Assert(EnProfiler.GetFlags() == flags))
941 {
943 return NTFR(TFR.FAIL);
944 }
945 }
946 }
947
948 // Let's test some bogus
949 EnProfiler.SetFlags(-333);
952 if (!Assert(bogusFlags == 0))
953 {
955 return NTFR(TFR.FAIL);
956 }
957
960 if (!Assert(bogusFlags == 0))
961 {
963 return NTFR(TFR.FAIL);
964 }
965
966 // Reset
968 return NTFR(TFR.SUCCESS);
969 }
970
971 //---------------------------------------------------------------------------
972 // Test removing of flags
974 {
976
978
980 {
982 return NTFR(TFR.FAIL);
983 }
984
987
989 {
991 return NTFR(TFR.FAIL);
992 }
993
996
998 {
1000 return NTFR(TFR.FAIL);
1001 }
1002
1003 // Reset
1005 return NTFR(TFR.SUCCESS);
1006 }
1007
1008 //---------------------------------------------------------------------------
1009 // Test adding of flags
1011 {
1013
1015
1016 // Return should match resulting flags
1018 {
1020 return NTFR(TFR.FAIL);
1021 }
1022
1023 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1024 {
1026 return NTFR(TFR.FAIL);
1027 }
1028
1029 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1030 {
1032 return NTFR(TFR.FAIL);
1033 }
1034
1035 // Reset
1037 return NTFR(TFR.SUCCESS);
1038 }
1039
1040 //---------------------------------------------------------------------------
1041 // Test module
1043 {
1044 // File lives in Game, use it while testing
1046
1047 // This was added at the same time as this API, so check if it works as well
1048 string nameOfCurrentModule = Type().GetModule();
1049 if (!Assert(nameOfCurrentModule != ""))
1050 return NTFR(TFR.FAIL);
1051
1052 // We know we are in Game, so use it as a test
1055 return NTFR(TFR.FAIL);
1056
1058 return NTFR(TFR.FAIL);
1059
1060 // Test if setting and getting works
1063
1065 {
1067 return NTFR(TFR.FAIL);
1068 }
1069
1070 // Data to restore
1073
1074 // Make sure we are only profiling Game and that the data is clean
1075 // Only valid for the Get...Per... methods, as they need to be sorted
1077
1078 // GetTickTime() returns in seconds, so gather the results in seconds too
1081
1082 // Time to sleeb
1083 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1084 float timeSlept = Sleep(0.3);
1085 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1086 float diff = postTime - previousTime - timeSlept;
1087
1088 // Restore
1090
1091 // We called the function, so it must have some time
1092 if (!Assert(postTime > 0))
1093 {
1095
1096 if (!wasEnabled)
1097 EnProfiler.Enable(false, true);
1098
1100
1101 return NTFR(TFR.FAIL);
1102 }
1103
1104 if (!Assert(diff < 0.00001))
1105 {
1107
1108 if (!wasEnabled)
1109 EnProfiler.Enable(false, true);
1110
1112
1113 return NTFR(TFR.FAIL);
1114 }
1115
1116 // Clean the session
1118
1119 // Something from a different module should not get sorted, so just fire something from a different module
1120 for (int i = 0; i < 1000; ++i)
1121 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1122
1123 // Sort and gather the data and validate if it is correct
1127
1128 Debug.TFLog("Game fncs:", this, "TestModule");
1129
1130 int funcCount = timePerFunc.Count();
1131 for (int j = 0; j < funcCount; ++j)
1132 {
1134 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1135 // We are currently profiling Game, so this Core function shouldn't be present
1136 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1137 {
1139
1140 if (!wasEnabled)
1141 EnProfiler.Enable(false, true);
1142
1144
1145 return NTFR(TFR.FAIL);
1146 }
1147 }
1148
1151
1152 int classCount = timePerClass.Count();
1153 for (int k = 0; k < classCount; ++k)
1154 {
1155 typename type = timePerClass[k].param2;
1157 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1158 {
1160
1161 if (!wasEnabled)
1162 EnProfiler.Enable(false, true);
1163
1165
1166 return NTFR(TFR.FAIL);
1167 }
1168
1169 // Only classes from Game should be present
1170 if (!Assert(classModule == eptModule))
1171 {
1173
1174 if (!wasEnabled)
1175 EnProfiler.Enable(false, true);
1176
1178
1179 return NTFR(TFR.FAIL);
1180 }
1181 }
1182
1183 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1186 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1188
1189 bool found = false;
1190
1191 Debug.TFLog("Core fncs:", this, "TestModule");
1192
1193 funcCount = timePerFunc.Count();
1194 for (int l = 0; l < funcCount; ++l)
1195 {
1197 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1198 // We are currently profiling Core, so this Core function should be present
1199 if (tfpc.param2 == "EnumTools::StringToEnum")
1200 {
1201 found = true;
1202 break;
1203 }
1204 }
1205
1206 Assert(found);
1207
1208 // Test some bogus
1210 EnProfiler.SetModule(-333);
1211 bool success = Assert(EnProfiler.GetModule() == mod);
1212 EnProfiler.SetModule(6003);
1214
1217
1218 if (!wasEnabled)
1219 EnProfiler.Enable(false, true);
1220
1221 return BTFR(success && found);
1222 }
1223
1224 //---------------------------------------------------------------------------
1225 // Test to see if class time data is correct
1227 {
1228 // We should restore this when done
1231
1232 // GetTickTime() returns in seconds, so gather the results in seconds too
1234
1235 // Create the classes
1237
1238 // Time to stress
1239 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1240 float timeStressed = clss.DoEverything();
1241 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1242 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1245
1246 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1247
1248 // Restore
1250 if (!wasEnabled)
1251 EnProfiler.Enable(false, true);
1252
1253 // We called the function, so it must have some time
1254 if (!Assert(postTime > 0))
1255 return NTFR(TFR.FAIL);
1256
1258 return NTFR(TFR.FAIL);
1259
1260 if (!Assert(diff < 0.001))
1261 return NTFR(TFR.FAIL);
1262
1263 return NTFR(TFR.SUCCESS);
1264 }
1265
1266 //---------------------------------------------------------------------------
1267 // Test to see if class count data is correct
1269 {
1270 const int allocAmount = 9;
1271 const int releaseAmount = 6;
1273
1274 // We should restore this when done
1276
1277 // Time to test
1280
1282 for (int i = 0; i < allocAmount; ++i)
1283 instanceArr.Insert(new EPTHelperClass());
1284
1285 for (int j = 0; j < releaseAmount; ++j)
1286 delete instanceArr[j];
1287
1288 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1289 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1290
1293
1294 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1295
1296 // Restore
1297 if (!wasEnabled)
1298 EnProfiler.Enable(false, true);
1299
1300 // Time to check
1301 if (!Assert(alloced == allocAmount))
1302 return NTFR(TFR.FAIL);
1303
1305 return NTFR(TFR.FAIL);
1306
1307 return NTFR(TFR.SUCCESS);
1308 }
1309
1310 //---------------------------------------------------------------------------
1311 // Test to see if func time data is correct
1313 {
1314 // We should restore this when done
1317
1318 // GetTickTime() returns in seconds, so gather the results in seconds too
1320
1321 // Time to stress
1322 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1323 float timeStressed = StringFormat();
1324 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1327
1328 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1329 float timeStressed2 = StringConcat();
1330 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1333
1334 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1335
1336 // Restore
1338 if (!wasEnabled)
1339 EnProfiler.Enable(false, true);
1340
1341 // We called the function, so it must have some time
1342 if (!Assert(postTime > 0))
1343 return NTFR(TFR.FAIL);
1344
1345 if (!Assert(diff < 0.001))
1346 return NTFR(TFR.FAIL);
1347
1348 if (!Assert(postTime2 > 0))
1349 return NTFR(TFR.FAIL);
1350
1351 if (!Assert(diff2 < 0.001))
1352 return NTFR(TFR.FAIL);
1353
1354 // I know that string.Format is faster than additive concatenation
1356 return NTFR(TFR.FAIL);
1357
1358 return NTFR(TFR.SUCCESS);
1359 }
1360
1361 //---------------------------------------------------------------------------
1362 // Test to see if func count data is correct
1364 {
1365 // We should restore this when done
1367
1368 // Time to count
1369
1370 // - CallFunction
1371 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1372 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1373 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1374
1376
1377 // - CallFunctionParams
1378 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1379 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1380 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1381
1383
1384 // - Regular call
1385 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1387 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1388
1390
1391 // - Call
1392 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1393 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1394 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1395
1397
1398 // - Garbage
1399 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1400
1401 // - Static
1402 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1404 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1405
1407
1408 // - Global
1409 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1410 GetDayZGame();
1411 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1412
1414
1415 // - Global proto
1416 // Not tracked, so don't need to compare before and after, should always be 0
1417 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1418 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1419
1420 // - Static proto
1421 // Not tracked, so don't need to compare before and after, should always be 0
1422 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1423
1424 // - proto
1425 // Not tracked, so don't need to compare before and after, should always be 0
1426 GetGame().GetHostName();
1427 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1428
1429 // - proto native
1430 // Not tracked, so don't need to compare before and after, should always be 0
1431 GetGame().IsServer();
1432 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1433
1434 // - static proto native
1435 // Not tracked, so don't need to compare before and after, should always be 0
1437 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1438
1439 // Restore
1440 if (!wasEnabled)
1441 EnProfiler.Enable(false, true);
1442
1443 // Do the checks
1444
1445 // - CallFunction
1446 if (!Assert(callCountCF == 1))
1447 return NTFR(TFR.FAIL);
1448
1449 // - CallFunctionParams
1450 if (!Assert(callCountCFP == 1))
1451 return NTFR(TFR.FAIL);
1452
1453 // - Regular call
1454 if (!Assert(callCountRG == 1))
1455 return NTFR(TFR.FAIL);
1456
1457 // - Call
1458 if (!Assert(callCountC == 1))
1459 return NTFR(TFR.FAIL);
1460
1461 // - Garbage
1462 if (!Assert(callCountNon == -1))
1463 return NTFR(TFR.FAIL);
1464
1465 // - Static
1466 if (!Assert(callCountS == 1))
1467 return NTFR(TFR.FAIL);
1468
1469 // - Global
1470 if (!Assert(callCountG == 1))
1471 return NTFR(TFR.FAIL);
1472
1473 // - Global proto
1474 if (!Assert(callCountGP == 0))
1475 return NTFR(TFR.FAIL);
1476
1477 // - Static proto
1478 if (!Assert(callCountSP == 0))
1479 return NTFR(TFR.FAIL);
1480
1481 // - proto
1482 if (!Assert(callCountP == 0))
1483 return NTFR(TFR.FAIL);
1484
1485 // - proto native
1486 if (!Assert(callCountPN == 0))
1487 return NTFR(TFR.FAIL);
1488
1489 // - static proto native
1490 if (!Assert(callCountSPN == 0))
1491 return NTFR(TFR.FAIL);
1492
1493 return NTFR(TFR.SUCCESS);
1494 }
1495
1496 //---------------------------------------------------------------------------
1497 // Helpers
1498 //---------------------------------------------------------------------------
1499 // Snore
1500 float Sleep(float timeS)
1501 {
1502 float startTime = GetGame().GetTickTime();
1503 while (GetGame().GetTickTime() - startTime < timeS)
1504 {
1505 // Zzz
1506 }
1507
1508 return GetGame().GetTickTime() - startTime;
1509 }
1510
1511 //---------------------------------------------------------------------------
1512 // Example stress method
1513 float StringFormat()
1514 {
1515 float startTime = GetGame().GetTickTime();
1516
1517 for (int i = 0; i < 1000; ++i)
1518 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1519
1520 return GetGame().GetTickTime() - startTime;
1521 }
1522
1523 //---------------------------------------------------------------------------
1524 // Example stress method 2
1525 float StringConcat()
1526 {
1527 float startTime = GetGame().GetTickTime();
1528
1529 for (int i = 0; i < 1000; ++i)
1530 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1531
1532 return GetGame().GetTickTime() - startTime;
1533 }
1534
1535 //---------------------------------------------------------------------------
1536 // To make sure it is only ever called in that test
1538 {
1539 int dummy = 3;
1540 }
1541
1542 //---------------------------------------------------------------------------
1543 // To make sure it is only ever called in that test
1544 static void TestFuncCountDataHelperStatic()
1545 {
1546 int dummy = 3;
1547 }
1548}
1549
1550class EPTHelperClass
1551{
1552 float Sleep2(float timeS)
1553 {
1554 float startTime = GetGame().GetTickTime();
1555 while (GetGame().GetTickTime() - startTime < timeS)
1556 {
1557 // Zzz
1558 }
1559
1560 return GetGame().GetTickTime() - startTime;
1561 }
1562 float SleepAgain(float timeS)
1563 {
1564 float startTime = GetGame().GetTickTime();
1565 while (GetGame().GetTickTime() - startTime < timeS)
1566 {
1567 // Zzz
1568 }
1569
1570 return GetGame().GetTickTime() - startTime;
1571 }
1572
1573 float DoEverything()
1574 {
1575 float startTime = GetGame().GetTickTime();
1576
1577 Sleep2(3);
1578 SleepAgain(3);
1579
1580 return GetGame().GetTickTime() - startTime;
1581 }
1582}

◆ TestClassCountData()

TFResult Sleep2::TestClassCountData ( )

Definition at line 1112 of file EnProfilerTests.c.

1112 : TestFramework
1113{
1116
1117 //---------------------------------------------------------------------------
1118 // Ctor - Decides the tests to run
1119 //---------------------------------------------------------------------------
1120 void EnProfilerTests()
1121 {
1123
1124 AddInitTest("TestToggling");
1125 AddInitTest("TestTogglingImmediate");
1126 AddInitTest("TestSetFlags");
1127 AddInitTest("TestClearFlags");
1128 AddInitTest("TestAddFlags");
1129 AddInitTest("TestModule");
1130 AddInitTest("TestClassTimeData");
1131 AddInitTest("TestClassCountData");
1132 AddInitTest("TestFuncTimeData");
1133 AddInitTest("TestFuncCountData");
1134 }
1135
1136 //---------------------------------------------------------------------------
1137 // Dtor
1138 //---------------------------------------------------------------------------
1139 void ~EnProfilerTests()
1140 {
1142 }
1143
1144 //---------------------------------------------------------------------------
1145 // Tests
1146 //---------------------------------------------------------------------------
1147 // Test toggling state
1149 {
1153 {
1156 }
1157
1158 return NTFR(TFR.FAIL);
1159 }
1160
1161 //---------------------------------------------------------------------------
1162 // Test toggling immediate state
1164 {
1168 {
1171 }
1172
1173 return NTFR(TFR.FAIL);
1174 }
1175
1176 //---------------------------------------------------------------------------
1177 // Test SetFlags/GetFlags
1179 {
1181
1182 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1183 {
1185 EnProfiler.SetFlags(flags);
1186
1187 if (!Assert(EnProfiler.GetFlags() == flags))
1188 {
1190 return NTFR(TFR.FAIL);
1191 }
1192
1193 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1194 {
1196 EnProfiler.SetFlags(flags);
1197
1198 if (!Assert(EnProfiler.GetFlags() == flags))
1199 {
1201 return NTFR(TFR.FAIL);
1202 }
1203 }
1204 }
1205
1206 // Let's test some bogus
1207 EnProfiler.SetFlags(-333);
1210 if (!Assert(bogusFlags == 0))
1211 {
1213 return NTFR(TFR.FAIL);
1214 }
1215
1218 if (!Assert(bogusFlags == 0))
1219 {
1221 return NTFR(TFR.FAIL);
1222 }
1223
1224 // Reset
1226 return NTFR(TFR.SUCCESS);
1227 }
1228
1229 //---------------------------------------------------------------------------
1230 // Test removing of flags
1232 {
1234
1236
1238 {
1240 return NTFR(TFR.FAIL);
1241 }
1242
1245
1246 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1247 {
1249 return NTFR(TFR.FAIL);
1250 }
1251
1254
1255 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1256 {
1258 return NTFR(TFR.FAIL);
1259 }
1260
1261 // Reset
1263 return NTFR(TFR.SUCCESS);
1264 }
1265
1266 //---------------------------------------------------------------------------
1267 // Test adding of flags
1269 {
1271
1273
1274 // Return should match resulting flags
1276 {
1278 return NTFR(TFR.FAIL);
1279 }
1280
1281 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1282 {
1284 return NTFR(TFR.FAIL);
1285 }
1286
1287 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1288 {
1290 return NTFR(TFR.FAIL);
1291 }
1292
1293 // Reset
1295 return NTFR(TFR.SUCCESS);
1296 }
1297
1298 //---------------------------------------------------------------------------
1299 // Test module
1301 {
1302 // File lives in Game, use it while testing
1304
1305 // This was added at the same time as this API, so check if it works as well
1306 string nameOfCurrentModule = Type().GetModule();
1307 if (!Assert(nameOfCurrentModule != ""))
1308 return NTFR(TFR.FAIL);
1309
1310 // We know we are in Game, so use it as a test
1313 return NTFR(TFR.FAIL);
1314
1316 return NTFR(TFR.FAIL);
1317
1318 // Test if setting and getting works
1321
1323 {
1325 return NTFR(TFR.FAIL);
1326 }
1327
1328 // Data to restore
1331
1332 // Make sure we are only profiling Game and that the data is clean
1333 // Only valid for the Get...Per... methods, as they need to be sorted
1335
1336 // GetTickTime() returns in seconds, so gather the results in seconds too
1339
1340 // Time to sleeb
1341 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1342 float timeSlept = Sleep(0.3);
1343 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1344 float diff = postTime - previousTime - timeSlept;
1345
1346 // Restore
1348
1349 // We called the function, so it must have some time
1350 if (!Assert(postTime > 0))
1351 {
1353
1354 if (!wasEnabled)
1355 EnProfiler.Enable(false, true);
1356
1358
1359 return NTFR(TFR.FAIL);
1360 }
1361
1362 if (!Assert(diff < 0.00001))
1363 {
1365
1366 if (!wasEnabled)
1367 EnProfiler.Enable(false, true);
1368
1370
1371 return NTFR(TFR.FAIL);
1372 }
1373
1374 // Clean the session
1376
1377 // Something from a different module should not get sorted, so just fire something from a different module
1378 for (int i = 0; i < 1000; ++i)
1379 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1380
1381 // Sort and gather the data and validate if it is correct
1385
1386 Debug.TFLog("Game fncs:", this, "TestModule");
1387
1388 int funcCount = timePerFunc.Count();
1389 for (int j = 0; j < funcCount; ++j)
1390 {
1392 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1393 // We are currently profiling Game, so this Core function shouldn't be present
1394 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1395 {
1397
1398 if (!wasEnabled)
1399 EnProfiler.Enable(false, true);
1400
1402
1403 return NTFR(TFR.FAIL);
1404 }
1405 }
1406
1409
1410 int classCount = timePerClass.Count();
1411 for (int k = 0; k < classCount; ++k)
1412 {
1413 typename type = timePerClass[k].param2;
1415 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1416 {
1418
1419 if (!wasEnabled)
1420 EnProfiler.Enable(false, true);
1421
1423
1424 return NTFR(TFR.FAIL);
1425 }
1426
1427 // Only classes from Game should be present
1428 if (!Assert(classModule == eptModule))
1429 {
1431
1432 if (!wasEnabled)
1433 EnProfiler.Enable(false, true);
1434
1436
1437 return NTFR(TFR.FAIL);
1438 }
1439 }
1440
1441 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1444 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1446
1447 bool found = false;
1448
1449 Debug.TFLog("Core fncs:", this, "TestModule");
1450
1451 funcCount = timePerFunc.Count();
1452 for (int l = 0; l < funcCount; ++l)
1453 {
1455 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1456 // We are currently profiling Core, so this Core function should be present
1457 if (tfpc.param2 == "EnumTools::StringToEnum")
1458 {
1459 found = true;
1460 break;
1461 }
1462 }
1463
1464 Assert(found);
1465
1466 // Test some bogus
1468 EnProfiler.SetModule(-333);
1469 bool success = Assert(EnProfiler.GetModule() == mod);
1470 EnProfiler.SetModule(6003);
1472
1475
1476 if (!wasEnabled)
1477 EnProfiler.Enable(false, true);
1478
1479 return BTFR(success && found);
1480 }
1481
1482 //---------------------------------------------------------------------------
1483 // Test to see if class time data is correct
1485 {
1486 // We should restore this when done
1489
1490 // GetTickTime() returns in seconds, so gather the results in seconds too
1492
1493 // Create the classes
1495
1496 // Time to stress
1497 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1498 float timeStressed = clss.DoEverything();
1499 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1500 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1503
1504 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1505
1506 // Restore
1508 if (!wasEnabled)
1509 EnProfiler.Enable(false, true);
1510
1511 // We called the function, so it must have some time
1512 if (!Assert(postTime > 0))
1513 return NTFR(TFR.FAIL);
1514
1516 return NTFR(TFR.FAIL);
1517
1518 if (!Assert(diff < 0.001))
1519 return NTFR(TFR.FAIL);
1520
1521 return NTFR(TFR.SUCCESS);
1522 }
1523
1524 //---------------------------------------------------------------------------
1525 // Test to see if class count data is correct
1527 {
1528 const int allocAmount = 9;
1529 const int releaseAmount = 6;
1531
1532 // We should restore this when done
1534
1535 // Time to test
1538
1540 for (int i = 0; i < allocAmount; ++i)
1541 instanceArr.Insert(new EPTHelperClass());
1542
1543 for (int j = 0; j < releaseAmount; ++j)
1544 delete instanceArr[j];
1545
1546 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1547 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1548
1551
1552 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1553
1554 // Restore
1555 if (!wasEnabled)
1556 EnProfiler.Enable(false, true);
1557
1558 // Time to check
1559 if (!Assert(alloced == allocAmount))
1560 return NTFR(TFR.FAIL);
1561
1563 return NTFR(TFR.FAIL);
1564
1565 return NTFR(TFR.SUCCESS);
1566 }
1567
1568 //---------------------------------------------------------------------------
1569 // Test to see if func time data is correct
1571 {
1572 // We should restore this when done
1575
1576 // GetTickTime() returns in seconds, so gather the results in seconds too
1578
1579 // Time to stress
1580 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1581 float timeStressed = StringFormat();
1582 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1585
1586 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1587 float timeStressed2 = StringConcat();
1588 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1591
1592 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1593
1594 // Restore
1596 if (!wasEnabled)
1597 EnProfiler.Enable(false, true);
1598
1599 // We called the function, so it must have some time
1600 if (!Assert(postTime > 0))
1601 return NTFR(TFR.FAIL);
1602
1603 if (!Assert(diff < 0.001))
1604 return NTFR(TFR.FAIL);
1605
1606 if (!Assert(postTime2 > 0))
1607 return NTFR(TFR.FAIL);
1608
1609 if (!Assert(diff2 < 0.001))
1610 return NTFR(TFR.FAIL);
1611
1612 // I know that string.Format is faster than additive concatenation
1614 return NTFR(TFR.FAIL);
1615
1616 return NTFR(TFR.SUCCESS);
1617 }
1618
1619 //---------------------------------------------------------------------------
1620 // Test to see if func count data is correct
1622 {
1623 // We should restore this when done
1625
1626 // Time to count
1627
1628 // - CallFunction
1629 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1630 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1631 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1632
1634
1635 // - CallFunctionParams
1636 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1637 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1638 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1639
1641
1642 // - Regular call
1643 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1645 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1646
1648
1649 // - Call
1650 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1651 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1652 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1653
1655
1656 // - Garbage
1657 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1658
1659 // - Static
1660 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1662 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1663
1665
1666 // - Global
1667 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1668 GetDayZGame();
1669 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1670
1672
1673 // - Global proto
1674 // Not tracked, so don't need to compare before and after, should always be 0
1675 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1676 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1677
1678 // - Static proto
1679 // Not tracked, so don't need to compare before and after, should always be 0
1680 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1681
1682 // - proto
1683 // Not tracked, so don't need to compare before and after, should always be 0
1684 GetGame().GetHostName();
1685 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1686
1687 // - proto native
1688 // Not tracked, so don't need to compare before and after, should always be 0
1689 GetGame().IsServer();
1690 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1691
1692 // - static proto native
1693 // Not tracked, so don't need to compare before and after, should always be 0
1695 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1696
1697 // Restore
1698 if (!wasEnabled)
1699 EnProfiler.Enable(false, true);
1700
1701 // Do the checks
1702
1703 // - CallFunction
1704 if (!Assert(callCountCF == 1))
1705 return NTFR(TFR.FAIL);
1706
1707 // - CallFunctionParams
1708 if (!Assert(callCountCFP == 1))
1709 return NTFR(TFR.FAIL);
1710
1711 // - Regular call
1712 if (!Assert(callCountRG == 1))
1713 return NTFR(TFR.FAIL);
1714
1715 // - Call
1716 if (!Assert(callCountC == 1))
1717 return NTFR(TFR.FAIL);
1718
1719 // - Garbage
1720 if (!Assert(callCountNon == -1))
1721 return NTFR(TFR.FAIL);
1722
1723 // - Static
1724 if (!Assert(callCountS == 1))
1725 return NTFR(TFR.FAIL);
1726
1727 // - Global
1728 if (!Assert(callCountG == 1))
1729 return NTFR(TFR.FAIL);
1730
1731 // - Global proto
1732 if (!Assert(callCountGP == 0))
1733 return NTFR(TFR.FAIL);
1734
1735 // - Static proto
1736 if (!Assert(callCountSP == 0))
1737 return NTFR(TFR.FAIL);
1738
1739 // - proto
1740 if (!Assert(callCountP == 0))
1741 return NTFR(TFR.FAIL);
1742
1743 // - proto native
1744 if (!Assert(callCountPN == 0))
1745 return NTFR(TFR.FAIL);
1746
1747 // - static proto native
1748 if (!Assert(callCountSPN == 0))
1749 return NTFR(TFR.FAIL);
1750
1751 return NTFR(TFR.SUCCESS);
1752 }
1753
1754 //---------------------------------------------------------------------------
1755 // Helpers
1756 //---------------------------------------------------------------------------
1757 // Snore
1758 float Sleep(float timeS)
1759 {
1760 float startTime = GetGame().GetTickTime();
1761 while (GetGame().GetTickTime() - startTime < timeS)
1762 {
1763 // Zzz
1764 }
1765
1766 return GetGame().GetTickTime() - startTime;
1767 }
1768
1769 //---------------------------------------------------------------------------
1770 // Example stress method
1771 float StringFormat()
1772 {
1773 float startTime = GetGame().GetTickTime();
1774
1775 for (int i = 0; i < 1000; ++i)
1776 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1777
1778 return GetGame().GetTickTime() - startTime;
1779 }
1780
1781 //---------------------------------------------------------------------------
1782 // Example stress method 2
1783 float StringConcat()
1784 {
1785 float startTime = GetGame().GetTickTime();
1786
1787 for (int i = 0; i < 1000; ++i)
1788 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1789
1790 return GetGame().GetTickTime() - startTime;
1791 }
1792
1793 //---------------------------------------------------------------------------
1794 // To make sure it is only ever called in that test
1796 {
1797 int dummy = 3;
1798 }
1799
1800 //---------------------------------------------------------------------------
1801 // To make sure it is only ever called in that test
1802 static void TestFuncCountDataHelperStatic()
1803 {
1804 int dummy = 3;
1805 }
1806}
1807
1808class EPTHelperClass
1809{
1810 float Sleep2(float timeS)
1811 {
1812 float startTime = GetGame().GetTickTime();
1813 while (GetGame().GetTickTime() - startTime < timeS)
1814 {
1815 // Zzz
1816 }
1817
1818 return GetGame().GetTickTime() - startTime;
1819 }
1820 float SleepAgain(float timeS)
1821 {
1822 float startTime = GetGame().GetTickTime();
1823 while (GetGame().GetTickTime() - startTime < timeS)
1824 {
1825 // Zzz
1826 }
1827
1828 return GetGame().GetTickTime() - startTime;
1829 }
1830
1831 float DoEverything()
1832 {
1833 float startTime = GetGame().GetTickTime();
1834
1835 Sleep2(3);
1836 SleepAgain(3);
1837
1838 return GetGame().GetTickTime() - startTime;
1839 }
1840}

◆ TestClassTimeData()

TFResult Sleep2::TestClassTimeData ( )

Definition at line 1070 of file EnProfilerTests.c.

1070 : TestFramework
1071{
1074
1075 //---------------------------------------------------------------------------
1076 // Ctor - Decides the tests to run
1077 //---------------------------------------------------------------------------
1078 void EnProfilerTests()
1079 {
1081
1082 AddInitTest("TestToggling");
1083 AddInitTest("TestTogglingImmediate");
1084 AddInitTest("TestSetFlags");
1085 AddInitTest("TestClearFlags");
1086 AddInitTest("TestAddFlags");
1087 AddInitTest("TestModule");
1088 AddInitTest("TestClassTimeData");
1089 AddInitTest("TestClassCountData");
1090 AddInitTest("TestFuncTimeData");
1091 AddInitTest("TestFuncCountData");
1092 }
1093
1094 //---------------------------------------------------------------------------
1095 // Dtor
1096 //---------------------------------------------------------------------------
1097 void ~EnProfilerTests()
1098 {
1100 }
1101
1102 //---------------------------------------------------------------------------
1103 // Tests
1104 //---------------------------------------------------------------------------
1105 // Test toggling state
1107 {
1111 {
1114 }
1115
1116 return NTFR(TFR.FAIL);
1117 }
1118
1119 //---------------------------------------------------------------------------
1120 // Test toggling immediate state
1122 {
1126 {
1129 }
1130
1131 return NTFR(TFR.FAIL);
1132 }
1133
1134 //---------------------------------------------------------------------------
1135 // Test SetFlags/GetFlags
1137 {
1139
1140 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1141 {
1143 EnProfiler.SetFlags(flags);
1144
1145 if (!Assert(EnProfiler.GetFlags() == flags))
1146 {
1148 return NTFR(TFR.FAIL);
1149 }
1150
1151 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1152 {
1154 EnProfiler.SetFlags(flags);
1155
1156 if (!Assert(EnProfiler.GetFlags() == flags))
1157 {
1159 return NTFR(TFR.FAIL);
1160 }
1161 }
1162 }
1163
1164 // Let's test some bogus
1165 EnProfiler.SetFlags(-333);
1168 if (!Assert(bogusFlags == 0))
1169 {
1171 return NTFR(TFR.FAIL);
1172 }
1173
1176 if (!Assert(bogusFlags == 0))
1177 {
1179 return NTFR(TFR.FAIL);
1180 }
1181
1182 // Reset
1184 return NTFR(TFR.SUCCESS);
1185 }
1186
1187 //---------------------------------------------------------------------------
1188 // Test removing of flags
1190 {
1192
1194
1196 {
1198 return NTFR(TFR.FAIL);
1199 }
1200
1203
1204 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1205 {
1207 return NTFR(TFR.FAIL);
1208 }
1209
1212
1213 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1214 {
1216 return NTFR(TFR.FAIL);
1217 }
1218
1219 // Reset
1221 return NTFR(TFR.SUCCESS);
1222 }
1223
1224 //---------------------------------------------------------------------------
1225 // Test adding of flags
1227 {
1229
1231
1232 // Return should match resulting flags
1234 {
1236 return NTFR(TFR.FAIL);
1237 }
1238
1239 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1240 {
1242 return NTFR(TFR.FAIL);
1243 }
1244
1245 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1246 {
1248 return NTFR(TFR.FAIL);
1249 }
1250
1251 // Reset
1253 return NTFR(TFR.SUCCESS);
1254 }
1255
1256 //---------------------------------------------------------------------------
1257 // Test module
1259 {
1260 // File lives in Game, use it while testing
1262
1263 // This was added at the same time as this API, so check if it works as well
1264 string nameOfCurrentModule = Type().GetModule();
1265 if (!Assert(nameOfCurrentModule != ""))
1266 return NTFR(TFR.FAIL);
1267
1268 // We know we are in Game, so use it as a test
1271 return NTFR(TFR.FAIL);
1272
1274 return NTFR(TFR.FAIL);
1275
1276 // Test if setting and getting works
1279
1281 {
1283 return NTFR(TFR.FAIL);
1284 }
1285
1286 // Data to restore
1289
1290 // Make sure we are only profiling Game and that the data is clean
1291 // Only valid for the Get...Per... methods, as they need to be sorted
1293
1294 // GetTickTime() returns in seconds, so gather the results in seconds too
1297
1298 // Time to sleeb
1299 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1300 float timeSlept = Sleep(0.3);
1301 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1302 float diff = postTime - previousTime - timeSlept;
1303
1304 // Restore
1306
1307 // We called the function, so it must have some time
1308 if (!Assert(postTime > 0))
1309 {
1311
1312 if (!wasEnabled)
1313 EnProfiler.Enable(false, true);
1314
1316
1317 return NTFR(TFR.FAIL);
1318 }
1319
1320 if (!Assert(diff < 0.00001))
1321 {
1323
1324 if (!wasEnabled)
1325 EnProfiler.Enable(false, true);
1326
1328
1329 return NTFR(TFR.FAIL);
1330 }
1331
1332 // Clean the session
1334
1335 // Something from a different module should not get sorted, so just fire something from a different module
1336 for (int i = 0; i < 1000; ++i)
1337 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1338
1339 // Sort and gather the data and validate if it is correct
1343
1344 Debug.TFLog("Game fncs:", this, "TestModule");
1345
1346 int funcCount = timePerFunc.Count();
1347 for (int j = 0; j < funcCount; ++j)
1348 {
1350 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1351 // We are currently profiling Game, so this Core function shouldn't be present
1352 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1353 {
1355
1356 if (!wasEnabled)
1357 EnProfiler.Enable(false, true);
1358
1360
1361 return NTFR(TFR.FAIL);
1362 }
1363 }
1364
1367
1368 int classCount = timePerClass.Count();
1369 for (int k = 0; k < classCount; ++k)
1370 {
1371 typename type = timePerClass[k].param2;
1373 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1374 {
1376
1377 if (!wasEnabled)
1378 EnProfiler.Enable(false, true);
1379
1381
1382 return NTFR(TFR.FAIL);
1383 }
1384
1385 // Only classes from Game should be present
1386 if (!Assert(classModule == eptModule))
1387 {
1389
1390 if (!wasEnabled)
1391 EnProfiler.Enable(false, true);
1392
1394
1395 return NTFR(TFR.FAIL);
1396 }
1397 }
1398
1399 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1402 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1404
1405 bool found = false;
1406
1407 Debug.TFLog("Core fncs:", this, "TestModule");
1408
1409 funcCount = timePerFunc.Count();
1410 for (int l = 0; l < funcCount; ++l)
1411 {
1413 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1414 // We are currently profiling Core, so this Core function should be present
1415 if (tfpc.param2 == "EnumTools::StringToEnum")
1416 {
1417 found = true;
1418 break;
1419 }
1420 }
1421
1422 Assert(found);
1423
1424 // Test some bogus
1426 EnProfiler.SetModule(-333);
1427 bool success = Assert(EnProfiler.GetModule() == mod);
1428 EnProfiler.SetModule(6003);
1430
1433
1434 if (!wasEnabled)
1435 EnProfiler.Enable(false, true);
1436
1437 return BTFR(success && found);
1438 }
1439
1440 //---------------------------------------------------------------------------
1441 // Test to see if class time data is correct
1443 {
1444 // We should restore this when done
1447
1448 // GetTickTime() returns in seconds, so gather the results in seconds too
1450
1451 // Create the classes
1453
1454 // Time to stress
1455 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1456 float timeStressed = clss.DoEverything();
1457 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1458 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1461
1462 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1463
1464 // Restore
1466 if (!wasEnabled)
1467 EnProfiler.Enable(false, true);
1468
1469 // We called the function, so it must have some time
1470 if (!Assert(postTime > 0))
1471 return NTFR(TFR.FAIL);
1472
1474 return NTFR(TFR.FAIL);
1475
1476 if (!Assert(diff < 0.001))
1477 return NTFR(TFR.FAIL);
1478
1479 return NTFR(TFR.SUCCESS);
1480 }
1481
1482 //---------------------------------------------------------------------------
1483 // Test to see if class count data is correct
1485 {
1486 const int allocAmount = 9;
1487 const int releaseAmount = 6;
1489
1490 // We should restore this when done
1492
1493 // Time to test
1496
1498 for (int i = 0; i < allocAmount; ++i)
1499 instanceArr.Insert(new EPTHelperClass());
1500
1501 for (int j = 0; j < releaseAmount; ++j)
1502 delete instanceArr[j];
1503
1504 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1505 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1506
1509
1510 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1511
1512 // Restore
1513 if (!wasEnabled)
1514 EnProfiler.Enable(false, true);
1515
1516 // Time to check
1517 if (!Assert(alloced == allocAmount))
1518 return NTFR(TFR.FAIL);
1519
1521 return NTFR(TFR.FAIL);
1522
1523 return NTFR(TFR.SUCCESS);
1524 }
1525
1526 //---------------------------------------------------------------------------
1527 // Test to see if func time data is correct
1529 {
1530 // We should restore this when done
1533
1534 // GetTickTime() returns in seconds, so gather the results in seconds too
1536
1537 // Time to stress
1538 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1539 float timeStressed = StringFormat();
1540 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1543
1544 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1545 float timeStressed2 = StringConcat();
1546 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1549
1550 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1551
1552 // Restore
1554 if (!wasEnabled)
1555 EnProfiler.Enable(false, true);
1556
1557 // We called the function, so it must have some time
1558 if (!Assert(postTime > 0))
1559 return NTFR(TFR.FAIL);
1560
1561 if (!Assert(diff < 0.001))
1562 return NTFR(TFR.FAIL);
1563
1564 if (!Assert(postTime2 > 0))
1565 return NTFR(TFR.FAIL);
1566
1567 if (!Assert(diff2 < 0.001))
1568 return NTFR(TFR.FAIL);
1569
1570 // I know that string.Format is faster than additive concatenation
1572 return NTFR(TFR.FAIL);
1573
1574 return NTFR(TFR.SUCCESS);
1575 }
1576
1577 //---------------------------------------------------------------------------
1578 // Test to see if func count data is correct
1580 {
1581 // We should restore this when done
1583
1584 // Time to count
1585
1586 // - CallFunction
1587 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1588 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1589 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1590
1592
1593 // - CallFunctionParams
1594 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1595 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1596 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1597
1599
1600 // - Regular call
1601 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1603 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1604
1606
1607 // - Call
1608 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1609 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1610 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1611
1613
1614 // - Garbage
1615 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1616
1617 // - Static
1618 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1620 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1621
1623
1624 // - Global
1625 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1626 GetDayZGame();
1627 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1628
1630
1631 // - Global proto
1632 // Not tracked, so don't need to compare before and after, should always be 0
1633 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1634 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1635
1636 // - Static proto
1637 // Not tracked, so don't need to compare before and after, should always be 0
1638 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1639
1640 // - proto
1641 // Not tracked, so don't need to compare before and after, should always be 0
1642 GetGame().GetHostName();
1643 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1644
1645 // - proto native
1646 // Not tracked, so don't need to compare before and after, should always be 0
1647 GetGame().IsServer();
1648 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1649
1650 // - static proto native
1651 // Not tracked, so don't need to compare before and after, should always be 0
1653 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1654
1655 // Restore
1656 if (!wasEnabled)
1657 EnProfiler.Enable(false, true);
1658
1659 // Do the checks
1660
1661 // - CallFunction
1662 if (!Assert(callCountCF == 1))
1663 return NTFR(TFR.FAIL);
1664
1665 // - CallFunctionParams
1666 if (!Assert(callCountCFP == 1))
1667 return NTFR(TFR.FAIL);
1668
1669 // - Regular call
1670 if (!Assert(callCountRG == 1))
1671 return NTFR(TFR.FAIL);
1672
1673 // - Call
1674 if (!Assert(callCountC == 1))
1675 return NTFR(TFR.FAIL);
1676
1677 // - Garbage
1678 if (!Assert(callCountNon == -1))
1679 return NTFR(TFR.FAIL);
1680
1681 // - Static
1682 if (!Assert(callCountS == 1))
1683 return NTFR(TFR.FAIL);
1684
1685 // - Global
1686 if (!Assert(callCountG == 1))
1687 return NTFR(TFR.FAIL);
1688
1689 // - Global proto
1690 if (!Assert(callCountGP == 0))
1691 return NTFR(TFR.FAIL);
1692
1693 // - Static proto
1694 if (!Assert(callCountSP == 0))
1695 return NTFR(TFR.FAIL);
1696
1697 // - proto
1698 if (!Assert(callCountP == 0))
1699 return NTFR(TFR.FAIL);
1700
1701 // - proto native
1702 if (!Assert(callCountPN == 0))
1703 return NTFR(TFR.FAIL);
1704
1705 // - static proto native
1706 if (!Assert(callCountSPN == 0))
1707 return NTFR(TFR.FAIL);
1708
1709 return NTFR(TFR.SUCCESS);
1710 }
1711
1712 //---------------------------------------------------------------------------
1713 // Helpers
1714 //---------------------------------------------------------------------------
1715 // Snore
1716 float Sleep(float timeS)
1717 {
1718 float startTime = GetGame().GetTickTime();
1719 while (GetGame().GetTickTime() - startTime < timeS)
1720 {
1721 // Zzz
1722 }
1723
1724 return GetGame().GetTickTime() - startTime;
1725 }
1726
1727 //---------------------------------------------------------------------------
1728 // Example stress method
1729 float StringFormat()
1730 {
1731 float startTime = GetGame().GetTickTime();
1732
1733 for (int i = 0; i < 1000; ++i)
1734 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1735
1736 return GetGame().GetTickTime() - startTime;
1737 }
1738
1739 //---------------------------------------------------------------------------
1740 // Example stress method 2
1741 float StringConcat()
1742 {
1743 float startTime = GetGame().GetTickTime();
1744
1745 for (int i = 0; i < 1000; ++i)
1746 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1747
1748 return GetGame().GetTickTime() - startTime;
1749 }
1750
1751 //---------------------------------------------------------------------------
1752 // To make sure it is only ever called in that test
1754 {
1755 int dummy = 3;
1756 }
1757
1758 //---------------------------------------------------------------------------
1759 // To make sure it is only ever called in that test
1760 static void TestFuncCountDataHelperStatic()
1761 {
1762 int dummy = 3;
1763 }
1764}
1765
1766class EPTHelperClass
1767{
1768 float Sleep2(float timeS)
1769 {
1770 float startTime = GetGame().GetTickTime();
1771 while (GetGame().GetTickTime() - startTime < timeS)
1772 {
1773 // Zzz
1774 }
1775
1776 return GetGame().GetTickTime() - startTime;
1777 }
1778 float SleepAgain(float timeS)
1779 {
1780 float startTime = GetGame().GetTickTime();
1781 while (GetGame().GetTickTime() - startTime < timeS)
1782 {
1783 // Zzz
1784 }
1785
1786 return GetGame().GetTickTime() - startTime;
1787 }
1788
1789 float DoEverything()
1790 {
1791 float startTime = GetGame().GetTickTime();
1792
1793 Sleep2(3);
1794 SleepAgain(3);
1795
1796 return GetGame().GetTickTime() - startTime;
1797 }
1798}

◆ TestClearFlags()

TFResult Sleep2::TestClearFlags ( )

Definition at line 817 of file EnProfilerTests.c.

817 : TestFramework
818{
821
822 //---------------------------------------------------------------------------
823 // Ctor - Decides the tests to run
824 //---------------------------------------------------------------------------
825 void EnProfilerTests()
826 {
828
829 AddInitTest("TestToggling");
830 AddInitTest("TestTogglingImmediate");
831 AddInitTest("TestSetFlags");
832 AddInitTest("TestClearFlags");
833 AddInitTest("TestAddFlags");
834 AddInitTest("TestModule");
835 AddInitTest("TestClassTimeData");
836 AddInitTest("TestClassCountData");
837 AddInitTest("TestFuncTimeData");
838 AddInitTest("TestFuncCountData");
839 }
840
841 //---------------------------------------------------------------------------
842 // Dtor
843 //---------------------------------------------------------------------------
844 void ~EnProfilerTests()
845 {
847 }
848
849 //---------------------------------------------------------------------------
850 // Tests
851 //---------------------------------------------------------------------------
852 // Test toggling state
854 {
858 {
861 }
862
863 return NTFR(TFR.FAIL);
864 }
865
866 //---------------------------------------------------------------------------
867 // Test toggling immediate state
869 {
873 {
876 }
877
878 return NTFR(TFR.FAIL);
879 }
880
881 //---------------------------------------------------------------------------
882 // Test SetFlags/GetFlags
884 {
886
887 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
888 {
890 EnProfiler.SetFlags(flags);
891
892 if (!Assert(EnProfiler.GetFlags() == flags))
893 {
895 return NTFR(TFR.FAIL);
896 }
897
898 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
899 {
901 EnProfiler.SetFlags(flags);
902
903 if (!Assert(EnProfiler.GetFlags() == flags))
904 {
906 return NTFR(TFR.FAIL);
907 }
908 }
909 }
910
911 // Let's test some bogus
912 EnProfiler.SetFlags(-333);
915 if (!Assert(bogusFlags == 0))
916 {
918 return NTFR(TFR.FAIL);
919 }
920
923 if (!Assert(bogusFlags == 0))
924 {
926 return NTFR(TFR.FAIL);
927 }
928
929 // Reset
931 return NTFR(TFR.SUCCESS);
932 }
933
934 //---------------------------------------------------------------------------
935 // Test removing of flags
937 {
939
941
943 {
945 return NTFR(TFR.FAIL);
946 }
947
950
952 {
954 return NTFR(TFR.FAIL);
955 }
956
959
961 {
963 return NTFR(TFR.FAIL);
964 }
965
966 // Reset
968 return NTFR(TFR.SUCCESS);
969 }
970
971 //---------------------------------------------------------------------------
972 // Test adding of flags
974 {
976
978
979 // Return should match resulting flags
981 {
983 return NTFR(TFR.FAIL);
984 }
985
986 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
987 {
989 return NTFR(TFR.FAIL);
990 }
991
992 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
993 {
995 return NTFR(TFR.FAIL);
996 }
997
998 // Reset
1000 return NTFR(TFR.SUCCESS);
1001 }
1002
1003 //---------------------------------------------------------------------------
1004 // Test module
1006 {
1007 // File lives in Game, use it while testing
1009
1010 // This was added at the same time as this API, so check if it works as well
1011 string nameOfCurrentModule = Type().GetModule();
1012 if (!Assert(nameOfCurrentModule != ""))
1013 return NTFR(TFR.FAIL);
1014
1015 // We know we are in Game, so use it as a test
1018 return NTFR(TFR.FAIL);
1019
1021 return NTFR(TFR.FAIL);
1022
1023 // Test if setting and getting works
1026
1028 {
1030 return NTFR(TFR.FAIL);
1031 }
1032
1033 // Data to restore
1036
1037 // Make sure we are only profiling Game and that the data is clean
1038 // Only valid for the Get...Per... methods, as they need to be sorted
1040
1041 // GetTickTime() returns in seconds, so gather the results in seconds too
1044
1045 // Time to sleeb
1046 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1047 float timeSlept = Sleep(0.3);
1048 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1049 float diff = postTime - previousTime - timeSlept;
1050
1051 // Restore
1053
1054 // We called the function, so it must have some time
1055 if (!Assert(postTime > 0))
1056 {
1058
1059 if (!wasEnabled)
1060 EnProfiler.Enable(false, true);
1061
1063
1064 return NTFR(TFR.FAIL);
1065 }
1066
1067 if (!Assert(diff < 0.00001))
1068 {
1070
1071 if (!wasEnabled)
1072 EnProfiler.Enable(false, true);
1073
1075
1076 return NTFR(TFR.FAIL);
1077 }
1078
1079 // Clean the session
1081
1082 // Something from a different module should not get sorted, so just fire something from a different module
1083 for (int i = 0; i < 1000; ++i)
1084 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1085
1086 // Sort and gather the data and validate if it is correct
1090
1091 Debug.TFLog("Game fncs:", this, "TestModule");
1092
1093 int funcCount = timePerFunc.Count();
1094 for (int j = 0; j < funcCount; ++j)
1095 {
1097 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1098 // We are currently profiling Game, so this Core function shouldn't be present
1099 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1100 {
1102
1103 if (!wasEnabled)
1104 EnProfiler.Enable(false, true);
1105
1107
1108 return NTFR(TFR.FAIL);
1109 }
1110 }
1111
1114
1115 int classCount = timePerClass.Count();
1116 for (int k = 0; k < classCount; ++k)
1117 {
1118 typename type = timePerClass[k].param2;
1120 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1121 {
1123
1124 if (!wasEnabled)
1125 EnProfiler.Enable(false, true);
1126
1128
1129 return NTFR(TFR.FAIL);
1130 }
1131
1132 // Only classes from Game should be present
1133 if (!Assert(classModule == eptModule))
1134 {
1136
1137 if (!wasEnabled)
1138 EnProfiler.Enable(false, true);
1139
1141
1142 return NTFR(TFR.FAIL);
1143 }
1144 }
1145
1146 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1149 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1151
1152 bool found = false;
1153
1154 Debug.TFLog("Core fncs:", this, "TestModule");
1155
1156 funcCount = timePerFunc.Count();
1157 for (int l = 0; l < funcCount; ++l)
1158 {
1160 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1161 // We are currently profiling Core, so this Core function should be present
1162 if (tfpc.param2 == "EnumTools::StringToEnum")
1163 {
1164 found = true;
1165 break;
1166 }
1167 }
1168
1169 Assert(found);
1170
1171 // Test some bogus
1173 EnProfiler.SetModule(-333);
1174 bool success = Assert(EnProfiler.GetModule() == mod);
1175 EnProfiler.SetModule(6003);
1177
1180
1181 if (!wasEnabled)
1182 EnProfiler.Enable(false, true);
1183
1184 return BTFR(success && found);
1185 }
1186
1187 //---------------------------------------------------------------------------
1188 // Test to see if class time data is correct
1190 {
1191 // We should restore this when done
1194
1195 // GetTickTime() returns in seconds, so gather the results in seconds too
1197
1198 // Create the classes
1200
1201 // Time to stress
1202 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1203 float timeStressed = clss.DoEverything();
1204 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1205 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1208
1209 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1210
1211 // Restore
1213 if (!wasEnabled)
1214 EnProfiler.Enable(false, true);
1215
1216 // We called the function, so it must have some time
1217 if (!Assert(postTime > 0))
1218 return NTFR(TFR.FAIL);
1219
1221 return NTFR(TFR.FAIL);
1222
1223 if (!Assert(diff < 0.001))
1224 return NTFR(TFR.FAIL);
1225
1226 return NTFR(TFR.SUCCESS);
1227 }
1228
1229 //---------------------------------------------------------------------------
1230 // Test to see if class count data is correct
1232 {
1233 const int allocAmount = 9;
1234 const int releaseAmount = 6;
1236
1237 // We should restore this when done
1239
1240 // Time to test
1243
1245 for (int i = 0; i < allocAmount; ++i)
1246 instanceArr.Insert(new EPTHelperClass());
1247
1248 for (int j = 0; j < releaseAmount; ++j)
1249 delete instanceArr[j];
1250
1251 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1252 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1253
1256
1257 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1258
1259 // Restore
1260 if (!wasEnabled)
1261 EnProfiler.Enable(false, true);
1262
1263 // Time to check
1264 if (!Assert(alloced == allocAmount))
1265 return NTFR(TFR.FAIL);
1266
1268 return NTFR(TFR.FAIL);
1269
1270 return NTFR(TFR.SUCCESS);
1271 }
1272
1273 //---------------------------------------------------------------------------
1274 // Test to see if func time data is correct
1276 {
1277 // We should restore this when done
1280
1281 // GetTickTime() returns in seconds, so gather the results in seconds too
1283
1284 // Time to stress
1285 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1286 float timeStressed = StringFormat();
1287 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1290
1291 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1292 float timeStressed2 = StringConcat();
1293 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1296
1297 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1298
1299 // Restore
1301 if (!wasEnabled)
1302 EnProfiler.Enable(false, true);
1303
1304 // We called the function, so it must have some time
1305 if (!Assert(postTime > 0))
1306 return NTFR(TFR.FAIL);
1307
1308 if (!Assert(diff < 0.001))
1309 return NTFR(TFR.FAIL);
1310
1311 if (!Assert(postTime2 > 0))
1312 return NTFR(TFR.FAIL);
1313
1314 if (!Assert(diff2 < 0.001))
1315 return NTFR(TFR.FAIL);
1316
1317 // I know that string.Format is faster than additive concatenation
1319 return NTFR(TFR.FAIL);
1320
1321 return NTFR(TFR.SUCCESS);
1322 }
1323
1324 //---------------------------------------------------------------------------
1325 // Test to see if func count data is correct
1327 {
1328 // We should restore this when done
1330
1331 // Time to count
1332
1333 // - CallFunction
1334 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1335 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1336 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1337
1339
1340 // - CallFunctionParams
1341 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1342 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1343 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1344
1346
1347 // - Regular call
1348 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1350 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1351
1353
1354 // - Call
1355 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1356 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1357 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1358
1360
1361 // - Garbage
1362 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1363
1364 // - Static
1365 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1367 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1368
1370
1371 // - Global
1372 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1373 GetDayZGame();
1374 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1375
1377
1378 // - Global proto
1379 // Not tracked, so don't need to compare before and after, should always be 0
1380 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1381 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1382
1383 // - Static proto
1384 // Not tracked, so don't need to compare before and after, should always be 0
1385 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1386
1387 // - proto
1388 // Not tracked, so don't need to compare before and after, should always be 0
1389 GetGame().GetHostName();
1390 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1391
1392 // - proto native
1393 // Not tracked, so don't need to compare before and after, should always be 0
1394 GetGame().IsServer();
1395 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1396
1397 // - static proto native
1398 // Not tracked, so don't need to compare before and after, should always be 0
1400 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1401
1402 // Restore
1403 if (!wasEnabled)
1404 EnProfiler.Enable(false, true);
1405
1406 // Do the checks
1407
1408 // - CallFunction
1409 if (!Assert(callCountCF == 1))
1410 return NTFR(TFR.FAIL);
1411
1412 // - CallFunctionParams
1413 if (!Assert(callCountCFP == 1))
1414 return NTFR(TFR.FAIL);
1415
1416 // - Regular call
1417 if (!Assert(callCountRG == 1))
1418 return NTFR(TFR.FAIL);
1419
1420 // - Call
1421 if (!Assert(callCountC == 1))
1422 return NTFR(TFR.FAIL);
1423
1424 // - Garbage
1425 if (!Assert(callCountNon == -1))
1426 return NTFR(TFR.FAIL);
1427
1428 // - Static
1429 if (!Assert(callCountS == 1))
1430 return NTFR(TFR.FAIL);
1431
1432 // - Global
1433 if (!Assert(callCountG == 1))
1434 return NTFR(TFR.FAIL);
1435
1436 // - Global proto
1437 if (!Assert(callCountGP == 0))
1438 return NTFR(TFR.FAIL);
1439
1440 // - Static proto
1441 if (!Assert(callCountSP == 0))
1442 return NTFR(TFR.FAIL);
1443
1444 // - proto
1445 if (!Assert(callCountP == 0))
1446 return NTFR(TFR.FAIL);
1447
1448 // - proto native
1449 if (!Assert(callCountPN == 0))
1450 return NTFR(TFR.FAIL);
1451
1452 // - static proto native
1453 if (!Assert(callCountSPN == 0))
1454 return NTFR(TFR.FAIL);
1455
1456 return NTFR(TFR.SUCCESS);
1457 }
1458
1459 //---------------------------------------------------------------------------
1460 // Helpers
1461 //---------------------------------------------------------------------------
1462 // Snore
1463 float Sleep(float timeS)
1464 {
1465 float startTime = GetGame().GetTickTime();
1466 while (GetGame().GetTickTime() - startTime < timeS)
1467 {
1468 // Zzz
1469 }
1470
1471 return GetGame().GetTickTime() - startTime;
1472 }
1473
1474 //---------------------------------------------------------------------------
1475 // Example stress method
1476 float StringFormat()
1477 {
1478 float startTime = GetGame().GetTickTime();
1479
1480 for (int i = 0; i < 1000; ++i)
1481 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1482
1483 return GetGame().GetTickTime() - startTime;
1484 }
1485
1486 //---------------------------------------------------------------------------
1487 // Example stress method 2
1488 float StringConcat()
1489 {
1490 float startTime = GetGame().GetTickTime();
1491
1492 for (int i = 0; i < 1000; ++i)
1493 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1494
1495 return GetGame().GetTickTime() - startTime;
1496 }
1497
1498 //---------------------------------------------------------------------------
1499 // To make sure it is only ever called in that test
1501 {
1502 int dummy = 3;
1503 }
1504
1505 //---------------------------------------------------------------------------
1506 // To make sure it is only ever called in that test
1507 static void TestFuncCountDataHelperStatic()
1508 {
1509 int dummy = 3;
1510 }
1511}
1512
1513class EPTHelperClass
1514{
1515 float Sleep2(float timeS)
1516 {
1517 float startTime = GetGame().GetTickTime();
1518 while (GetGame().GetTickTime() - startTime < timeS)
1519 {
1520 // Zzz
1521 }
1522
1523 return GetGame().GetTickTime() - startTime;
1524 }
1525 float SleepAgain(float timeS)
1526 {
1527 float startTime = GetGame().GetTickTime();
1528 while (GetGame().GetTickTime() - startTime < timeS)
1529 {
1530 // Zzz
1531 }
1532
1533 return GetGame().GetTickTime() - startTime;
1534 }
1535
1536 float DoEverything()
1537 {
1538 float startTime = GetGame().GetTickTime();
1539
1540 Sleep2(3);
1541 SleepAgain(3);
1542
1543 return GetGame().GetTickTime() - startTime;
1544 }
1545}

◆ TestFuncCountData()

TFResult Sleep2::TestFuncCountData ( )

Definition at line 1207 of file EnProfilerTests.c.

1207 : TestFramework
1208{
1211
1212 //---------------------------------------------------------------------------
1213 // Ctor - Decides the tests to run
1214 //---------------------------------------------------------------------------
1215 void EnProfilerTests()
1216 {
1218
1219 AddInitTest("TestToggling");
1220 AddInitTest("TestTogglingImmediate");
1221 AddInitTest("TestSetFlags");
1222 AddInitTest("TestClearFlags");
1223 AddInitTest("TestAddFlags");
1224 AddInitTest("TestModule");
1225 AddInitTest("TestClassTimeData");
1226 AddInitTest("TestClassCountData");
1227 AddInitTest("TestFuncTimeData");
1228 AddInitTest("TestFuncCountData");
1229 }
1230
1231 //---------------------------------------------------------------------------
1232 // Dtor
1233 //---------------------------------------------------------------------------
1234 void ~EnProfilerTests()
1235 {
1237 }
1238
1239 //---------------------------------------------------------------------------
1240 // Tests
1241 //---------------------------------------------------------------------------
1242 // Test toggling state
1244 {
1248 {
1251 }
1252
1253 return NTFR(TFR.FAIL);
1254 }
1255
1256 //---------------------------------------------------------------------------
1257 // Test toggling immediate state
1259 {
1263 {
1266 }
1267
1268 return NTFR(TFR.FAIL);
1269 }
1270
1271 //---------------------------------------------------------------------------
1272 // Test SetFlags/GetFlags
1274 {
1276
1277 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1278 {
1280 EnProfiler.SetFlags(flags);
1281
1282 if (!Assert(EnProfiler.GetFlags() == flags))
1283 {
1285 return NTFR(TFR.FAIL);
1286 }
1287
1288 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1289 {
1291 EnProfiler.SetFlags(flags);
1292
1293 if (!Assert(EnProfiler.GetFlags() == flags))
1294 {
1296 return NTFR(TFR.FAIL);
1297 }
1298 }
1299 }
1300
1301 // Let's test some bogus
1302 EnProfiler.SetFlags(-333);
1305 if (!Assert(bogusFlags == 0))
1306 {
1308 return NTFR(TFR.FAIL);
1309 }
1310
1313 if (!Assert(bogusFlags == 0))
1314 {
1316 return NTFR(TFR.FAIL);
1317 }
1318
1319 // Reset
1321 return NTFR(TFR.SUCCESS);
1322 }
1323
1324 //---------------------------------------------------------------------------
1325 // Test removing of flags
1327 {
1329
1331
1333 {
1335 return NTFR(TFR.FAIL);
1336 }
1337
1340
1341 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1342 {
1344 return NTFR(TFR.FAIL);
1345 }
1346
1349
1350 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1351 {
1353 return NTFR(TFR.FAIL);
1354 }
1355
1356 // Reset
1358 return NTFR(TFR.SUCCESS);
1359 }
1360
1361 //---------------------------------------------------------------------------
1362 // Test adding of flags
1364 {
1366
1368
1369 // Return should match resulting flags
1371 {
1373 return NTFR(TFR.FAIL);
1374 }
1375
1376 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1377 {
1379 return NTFR(TFR.FAIL);
1380 }
1381
1382 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1383 {
1385 return NTFR(TFR.FAIL);
1386 }
1387
1388 // Reset
1390 return NTFR(TFR.SUCCESS);
1391 }
1392
1393 //---------------------------------------------------------------------------
1394 // Test module
1396 {
1397 // File lives in Game, use it while testing
1399
1400 // This was added at the same time as this API, so check if it works as well
1401 string nameOfCurrentModule = Type().GetModule();
1402 if (!Assert(nameOfCurrentModule != ""))
1403 return NTFR(TFR.FAIL);
1404
1405 // We know we are in Game, so use it as a test
1408 return NTFR(TFR.FAIL);
1409
1411 return NTFR(TFR.FAIL);
1412
1413 // Test if setting and getting works
1416
1418 {
1420 return NTFR(TFR.FAIL);
1421 }
1422
1423 // Data to restore
1426
1427 // Make sure we are only profiling Game and that the data is clean
1428 // Only valid for the Get...Per... methods, as they need to be sorted
1430
1431 // GetTickTime() returns in seconds, so gather the results in seconds too
1434
1435 // Time to sleeb
1436 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1437 float timeSlept = Sleep(0.3);
1438 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1439 float diff = postTime - previousTime - timeSlept;
1440
1441 // Restore
1443
1444 // We called the function, so it must have some time
1445 if (!Assert(postTime > 0))
1446 {
1448
1449 if (!wasEnabled)
1450 EnProfiler.Enable(false, true);
1451
1453
1454 return NTFR(TFR.FAIL);
1455 }
1456
1457 if (!Assert(diff < 0.00001))
1458 {
1460
1461 if (!wasEnabled)
1462 EnProfiler.Enable(false, true);
1463
1465
1466 return NTFR(TFR.FAIL);
1467 }
1468
1469 // Clean the session
1471
1472 // Something from a different module should not get sorted, so just fire something from a different module
1473 for (int i = 0; i < 1000; ++i)
1474 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1475
1476 // Sort and gather the data and validate if it is correct
1480
1481 Debug.TFLog("Game fncs:", this, "TestModule");
1482
1483 int funcCount = timePerFunc.Count();
1484 for (int j = 0; j < funcCount; ++j)
1485 {
1487 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1488 // We are currently profiling Game, so this Core function shouldn't be present
1489 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1490 {
1492
1493 if (!wasEnabled)
1494 EnProfiler.Enable(false, true);
1495
1497
1498 return NTFR(TFR.FAIL);
1499 }
1500 }
1501
1504
1505 int classCount = timePerClass.Count();
1506 for (int k = 0; k < classCount; ++k)
1507 {
1508 typename type = timePerClass[k].param2;
1510 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1511 {
1513
1514 if (!wasEnabled)
1515 EnProfiler.Enable(false, true);
1516
1518
1519 return NTFR(TFR.FAIL);
1520 }
1521
1522 // Only classes from Game should be present
1523 if (!Assert(classModule == eptModule))
1524 {
1526
1527 if (!wasEnabled)
1528 EnProfiler.Enable(false, true);
1529
1531
1532 return NTFR(TFR.FAIL);
1533 }
1534 }
1535
1536 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1539 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1541
1542 bool found = false;
1543
1544 Debug.TFLog("Core fncs:", this, "TestModule");
1545
1546 funcCount = timePerFunc.Count();
1547 for (int l = 0; l < funcCount; ++l)
1548 {
1550 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1551 // We are currently profiling Core, so this Core function should be present
1552 if (tfpc.param2 == "EnumTools::StringToEnum")
1553 {
1554 found = true;
1555 break;
1556 }
1557 }
1558
1559 Assert(found);
1560
1561 // Test some bogus
1563 EnProfiler.SetModule(-333);
1564 bool success = Assert(EnProfiler.GetModule() == mod);
1565 EnProfiler.SetModule(6003);
1567
1570
1571 if (!wasEnabled)
1572 EnProfiler.Enable(false, true);
1573
1574 return BTFR(success && found);
1575 }
1576
1577 //---------------------------------------------------------------------------
1578 // Test to see if class time data is correct
1580 {
1581 // We should restore this when done
1584
1585 // GetTickTime() returns in seconds, so gather the results in seconds too
1587
1588 // Create the classes
1590
1591 // Time to stress
1592 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1593 float timeStressed = clss.DoEverything();
1594 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1595 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1598
1599 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1600
1601 // Restore
1603 if (!wasEnabled)
1604 EnProfiler.Enable(false, true);
1605
1606 // We called the function, so it must have some time
1607 if (!Assert(postTime > 0))
1608 return NTFR(TFR.FAIL);
1609
1611 return NTFR(TFR.FAIL);
1612
1613 if (!Assert(diff < 0.001))
1614 return NTFR(TFR.FAIL);
1615
1616 return NTFR(TFR.SUCCESS);
1617 }
1618
1619 //---------------------------------------------------------------------------
1620 // Test to see if class count data is correct
1622 {
1623 const int allocAmount = 9;
1624 const int releaseAmount = 6;
1626
1627 // We should restore this when done
1629
1630 // Time to test
1633
1635 for (int i = 0; i < allocAmount; ++i)
1636 instanceArr.Insert(new EPTHelperClass());
1637
1638 for (int j = 0; j < releaseAmount; ++j)
1639 delete instanceArr[j];
1640
1641 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1642 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1643
1646
1647 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1648
1649 // Restore
1650 if (!wasEnabled)
1651 EnProfiler.Enable(false, true);
1652
1653 // Time to check
1654 if (!Assert(alloced == allocAmount))
1655 return NTFR(TFR.FAIL);
1656
1658 return NTFR(TFR.FAIL);
1659
1660 return NTFR(TFR.SUCCESS);
1661 }
1662
1663 //---------------------------------------------------------------------------
1664 // Test to see if func time data is correct
1666 {
1667 // We should restore this when done
1670
1671 // GetTickTime() returns in seconds, so gather the results in seconds too
1673
1674 // Time to stress
1675 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1676 float timeStressed = StringFormat();
1677 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1680
1681 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1682 float timeStressed2 = StringConcat();
1683 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1686
1687 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1688
1689 // Restore
1691 if (!wasEnabled)
1692 EnProfiler.Enable(false, true);
1693
1694 // We called the function, so it must have some time
1695 if (!Assert(postTime > 0))
1696 return NTFR(TFR.FAIL);
1697
1698 if (!Assert(diff < 0.001))
1699 return NTFR(TFR.FAIL);
1700
1701 if (!Assert(postTime2 > 0))
1702 return NTFR(TFR.FAIL);
1703
1704 if (!Assert(diff2 < 0.001))
1705 return NTFR(TFR.FAIL);
1706
1707 // I know that string.Format is faster than additive concatenation
1709 return NTFR(TFR.FAIL);
1710
1711 return NTFR(TFR.SUCCESS);
1712 }
1713
1714 //---------------------------------------------------------------------------
1715 // Test to see if func count data is correct
1717 {
1718 // We should restore this when done
1720
1721 // Time to count
1722
1723 // - CallFunction
1724 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1725 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1726 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1727
1729
1730 // - CallFunctionParams
1731 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1732 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1733 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1734
1736
1737 // - Regular call
1738 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1740 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1741
1743
1744 // - Call
1745 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1746 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1747 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1748
1750
1751 // - Garbage
1752 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1753
1754 // - Static
1755 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1757 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1758
1760
1761 // - Global
1762 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1763 GetDayZGame();
1764 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1765
1767
1768 // - Global proto
1769 // Not tracked, so don't need to compare before and after, should always be 0
1770 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1771 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1772
1773 // - Static proto
1774 // Not tracked, so don't need to compare before and after, should always be 0
1775 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1776
1777 // - proto
1778 // Not tracked, so don't need to compare before and after, should always be 0
1779 GetGame().GetHostName();
1780 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1781
1782 // - proto native
1783 // Not tracked, so don't need to compare before and after, should always be 0
1784 GetGame().IsServer();
1785 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1786
1787 // - static proto native
1788 // Not tracked, so don't need to compare before and after, should always be 0
1790 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1791
1792 // Restore
1793 if (!wasEnabled)
1794 EnProfiler.Enable(false, true);
1795
1796 // Do the checks
1797
1798 // - CallFunction
1799 if (!Assert(callCountCF == 1))
1800 return NTFR(TFR.FAIL);
1801
1802 // - CallFunctionParams
1803 if (!Assert(callCountCFP == 1))
1804 return NTFR(TFR.FAIL);
1805
1806 // - Regular call
1807 if (!Assert(callCountRG == 1))
1808 return NTFR(TFR.FAIL);
1809
1810 // - Call
1811 if (!Assert(callCountC == 1))
1812 return NTFR(TFR.FAIL);
1813
1814 // - Garbage
1815 if (!Assert(callCountNon == -1))
1816 return NTFR(TFR.FAIL);
1817
1818 // - Static
1819 if (!Assert(callCountS == 1))
1820 return NTFR(TFR.FAIL);
1821
1822 // - Global
1823 if (!Assert(callCountG == 1))
1824 return NTFR(TFR.FAIL);
1825
1826 // - Global proto
1827 if (!Assert(callCountGP == 0))
1828 return NTFR(TFR.FAIL);
1829
1830 // - Static proto
1831 if (!Assert(callCountSP == 0))
1832 return NTFR(TFR.FAIL);
1833
1834 // - proto
1835 if (!Assert(callCountP == 0))
1836 return NTFR(TFR.FAIL);
1837
1838 // - proto native
1839 if (!Assert(callCountPN == 0))
1840 return NTFR(TFR.FAIL);
1841
1842 // - static proto native
1843 if (!Assert(callCountSPN == 0))
1844 return NTFR(TFR.FAIL);
1845
1846 return NTFR(TFR.SUCCESS);
1847 }
1848
1849 //---------------------------------------------------------------------------
1850 // Helpers
1851 //---------------------------------------------------------------------------
1852 // Snore
1853 float Sleep(float timeS)
1854 {
1855 float startTime = GetGame().GetTickTime();
1856 while (GetGame().GetTickTime() - startTime < timeS)
1857 {
1858 // Zzz
1859 }
1860
1861 return GetGame().GetTickTime() - startTime;
1862 }
1863
1864 //---------------------------------------------------------------------------
1865 // Example stress method
1866 float StringFormat()
1867 {
1868 float startTime = GetGame().GetTickTime();
1869
1870 for (int i = 0; i < 1000; ++i)
1871 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1872
1873 return GetGame().GetTickTime() - startTime;
1874 }
1875
1876 //---------------------------------------------------------------------------
1877 // Example stress method 2
1878 float StringConcat()
1879 {
1880 float startTime = GetGame().GetTickTime();
1881
1882 for (int i = 0; i < 1000; ++i)
1883 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1884
1885 return GetGame().GetTickTime() - startTime;
1886 }
1887
1888 //---------------------------------------------------------------------------
1889 // To make sure it is only ever called in that test
1891 {
1892 int dummy = 3;
1893 }
1894
1895 //---------------------------------------------------------------------------
1896 // To make sure it is only ever called in that test
1897 static void TestFuncCountDataHelperStatic()
1898 {
1899 int dummy = 3;
1900 }
1901}
1902
1903class EPTHelperClass
1904{
1905 float Sleep2(float timeS)
1906 {
1907 float startTime = GetGame().GetTickTime();
1908 while (GetGame().GetTickTime() - startTime < timeS)
1909 {
1910 // Zzz
1911 }
1912
1913 return GetGame().GetTickTime() - startTime;
1914 }
1915 float SleepAgain(float timeS)
1916 {
1917 float startTime = GetGame().GetTickTime();
1918 while (GetGame().GetTickTime() - startTime < timeS)
1919 {
1920 // Zzz
1921 }
1922
1923 return GetGame().GetTickTime() - startTime;
1924 }
1925
1926 float DoEverything()
1927 {
1928 float startTime = GetGame().GetTickTime();
1929
1930 Sleep2(3);
1931 SleepAgain(3);
1932
1933 return GetGame().GetTickTime() - startTime;
1934 }
1935}

◆ TestFuncCountDataHelper()

void Sleep2::TestFuncCountDataHelper ( )

Definition at line 1381 of file EnProfilerTests.c.

1381 : TestFramework
1382{
1385
1386 //---------------------------------------------------------------------------
1387 // Ctor - Decides the tests to run
1388 //---------------------------------------------------------------------------
1389 void EnProfilerTests()
1390 {
1392
1393 AddInitTest("TestToggling");
1394 AddInitTest("TestTogglingImmediate");
1395 AddInitTest("TestSetFlags");
1396 AddInitTest("TestClearFlags");
1397 AddInitTest("TestAddFlags");
1398 AddInitTest("TestModule");
1399 AddInitTest("TestClassTimeData");
1400 AddInitTest("TestClassCountData");
1401 AddInitTest("TestFuncTimeData");
1402 AddInitTest("TestFuncCountData");
1403 }
1404
1405 //---------------------------------------------------------------------------
1406 // Dtor
1407 //---------------------------------------------------------------------------
1408 void ~EnProfilerTests()
1409 {
1411 }
1412
1413 //---------------------------------------------------------------------------
1414 // Tests
1415 //---------------------------------------------------------------------------
1416 // Test toggling state
1418 {
1422 {
1425 }
1426
1427 return NTFR(TFR.FAIL);
1428 }
1429
1430 //---------------------------------------------------------------------------
1431 // Test toggling immediate state
1433 {
1437 {
1440 }
1441
1442 return NTFR(TFR.FAIL);
1443 }
1444
1445 //---------------------------------------------------------------------------
1446 // Test SetFlags/GetFlags
1448 {
1450
1451 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1452 {
1454 EnProfiler.SetFlags(flags);
1455
1456 if (!Assert(EnProfiler.GetFlags() == flags))
1457 {
1459 return NTFR(TFR.FAIL);
1460 }
1461
1462 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1463 {
1465 EnProfiler.SetFlags(flags);
1466
1467 if (!Assert(EnProfiler.GetFlags() == flags))
1468 {
1470 return NTFR(TFR.FAIL);
1471 }
1472 }
1473 }
1474
1475 // Let's test some bogus
1476 EnProfiler.SetFlags(-333);
1479 if (!Assert(bogusFlags == 0))
1480 {
1482 return NTFR(TFR.FAIL);
1483 }
1484
1487 if (!Assert(bogusFlags == 0))
1488 {
1490 return NTFR(TFR.FAIL);
1491 }
1492
1493 // Reset
1495 return NTFR(TFR.SUCCESS);
1496 }
1497
1498 //---------------------------------------------------------------------------
1499 // Test removing of flags
1501 {
1503
1505
1507 {
1509 return NTFR(TFR.FAIL);
1510 }
1511
1514
1515 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1516 {
1518 return NTFR(TFR.FAIL);
1519 }
1520
1523
1524 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1525 {
1527 return NTFR(TFR.FAIL);
1528 }
1529
1530 // Reset
1532 return NTFR(TFR.SUCCESS);
1533 }
1534
1535 //---------------------------------------------------------------------------
1536 // Test adding of flags
1538 {
1540
1542
1543 // Return should match resulting flags
1545 {
1547 return NTFR(TFR.FAIL);
1548 }
1549
1550 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1551 {
1553 return NTFR(TFR.FAIL);
1554 }
1555
1556 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1557 {
1559 return NTFR(TFR.FAIL);
1560 }
1561
1562 // Reset
1564 return NTFR(TFR.SUCCESS);
1565 }
1566
1567 //---------------------------------------------------------------------------
1568 // Test module
1570 {
1571 // File lives in Game, use it while testing
1573
1574 // This was added at the same time as this API, so check if it works as well
1575 string nameOfCurrentModule = Type().GetModule();
1576 if (!Assert(nameOfCurrentModule != ""))
1577 return NTFR(TFR.FAIL);
1578
1579 // We know we are in Game, so use it as a test
1582 return NTFR(TFR.FAIL);
1583
1585 return NTFR(TFR.FAIL);
1586
1587 // Test if setting and getting works
1590
1592 {
1594 return NTFR(TFR.FAIL);
1595 }
1596
1597 // Data to restore
1600
1601 // Make sure we are only profiling Game and that the data is clean
1602 // Only valid for the Get...Per... methods, as they need to be sorted
1604
1605 // GetTickTime() returns in seconds, so gather the results in seconds too
1608
1609 // Time to sleeb
1610 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1611 float timeSlept = Sleep(0.3);
1612 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1613 float diff = postTime - previousTime - timeSlept;
1614
1615 // Restore
1617
1618 // We called the function, so it must have some time
1619 if (!Assert(postTime > 0))
1620 {
1622
1623 if (!wasEnabled)
1624 EnProfiler.Enable(false, true);
1625
1627
1628 return NTFR(TFR.FAIL);
1629 }
1630
1631 if (!Assert(diff < 0.00001))
1632 {
1634
1635 if (!wasEnabled)
1636 EnProfiler.Enable(false, true);
1637
1639
1640 return NTFR(TFR.FAIL);
1641 }
1642
1643 // Clean the session
1645
1646 // Something from a different module should not get sorted, so just fire something from a different module
1647 for (int i = 0; i < 1000; ++i)
1648 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1649
1650 // Sort and gather the data and validate if it is correct
1654
1655 Debug.TFLog("Game fncs:", this, "TestModule");
1656
1657 int funcCount = timePerFunc.Count();
1658 for (int j = 0; j < funcCount; ++j)
1659 {
1661 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1662 // We are currently profiling Game, so this Core function shouldn't be present
1663 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1664 {
1666
1667 if (!wasEnabled)
1668 EnProfiler.Enable(false, true);
1669
1671
1672 return NTFR(TFR.FAIL);
1673 }
1674 }
1675
1678
1679 int classCount = timePerClass.Count();
1680 for (int k = 0; k < classCount; ++k)
1681 {
1682 typename type = timePerClass[k].param2;
1684 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1685 {
1687
1688 if (!wasEnabled)
1689 EnProfiler.Enable(false, true);
1690
1692
1693 return NTFR(TFR.FAIL);
1694 }
1695
1696 // Only classes from Game should be present
1697 if (!Assert(classModule == eptModule))
1698 {
1700
1701 if (!wasEnabled)
1702 EnProfiler.Enable(false, true);
1703
1705
1706 return NTFR(TFR.FAIL);
1707 }
1708 }
1709
1710 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1713 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1715
1716 bool found = false;
1717
1718 Debug.TFLog("Core fncs:", this, "TestModule");
1719
1720 funcCount = timePerFunc.Count();
1721 for (int l = 0; l < funcCount; ++l)
1722 {
1724 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1725 // We are currently profiling Core, so this Core function should be present
1726 if (tfpc.param2 == "EnumTools::StringToEnum")
1727 {
1728 found = true;
1729 break;
1730 }
1731 }
1732
1733 Assert(found);
1734
1735 // Test some bogus
1737 EnProfiler.SetModule(-333);
1738 bool success = Assert(EnProfiler.GetModule() == mod);
1739 EnProfiler.SetModule(6003);
1741
1744
1745 if (!wasEnabled)
1746 EnProfiler.Enable(false, true);
1747
1748 return BTFR(success && found);
1749 }
1750
1751 //---------------------------------------------------------------------------
1752 // Test to see if class time data is correct
1754 {
1755 // We should restore this when done
1758
1759 // GetTickTime() returns in seconds, so gather the results in seconds too
1761
1762 // Create the classes
1764
1765 // Time to stress
1766 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1767 float timeStressed = clss.DoEverything();
1768 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1769 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1772
1773 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1774
1775 // Restore
1777 if (!wasEnabled)
1778 EnProfiler.Enable(false, true);
1779
1780 // We called the function, so it must have some time
1781 if (!Assert(postTime > 0))
1782 return NTFR(TFR.FAIL);
1783
1785 return NTFR(TFR.FAIL);
1786
1787 if (!Assert(diff < 0.001))
1788 return NTFR(TFR.FAIL);
1789
1790 return NTFR(TFR.SUCCESS);
1791 }
1792
1793 //---------------------------------------------------------------------------
1794 // Test to see if class count data is correct
1796 {
1797 const int allocAmount = 9;
1798 const int releaseAmount = 6;
1800
1801 // We should restore this when done
1803
1804 // Time to test
1807
1809 for (int i = 0; i < allocAmount; ++i)
1810 instanceArr.Insert(new EPTHelperClass());
1811
1812 for (int j = 0; j < releaseAmount; ++j)
1813 delete instanceArr[j];
1814
1815 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1816 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1817
1820
1821 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1822
1823 // Restore
1824 if (!wasEnabled)
1825 EnProfiler.Enable(false, true);
1826
1827 // Time to check
1828 if (!Assert(alloced == allocAmount))
1829 return NTFR(TFR.FAIL);
1830
1832 return NTFR(TFR.FAIL);
1833
1834 return NTFR(TFR.SUCCESS);
1835 }
1836
1837 //---------------------------------------------------------------------------
1838 // Test to see if func time data is correct
1840 {
1841 // We should restore this when done
1844
1845 // GetTickTime() returns in seconds, so gather the results in seconds too
1847
1848 // Time to stress
1849 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1850 float timeStressed = StringFormat();
1851 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1854
1855 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1856 float timeStressed2 = StringConcat();
1857 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1860
1861 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1862
1863 // Restore
1865 if (!wasEnabled)
1866 EnProfiler.Enable(false, true);
1867
1868 // We called the function, so it must have some time
1869 if (!Assert(postTime > 0))
1870 return NTFR(TFR.FAIL);
1871
1872 if (!Assert(diff < 0.001))
1873 return NTFR(TFR.FAIL);
1874
1875 if (!Assert(postTime2 > 0))
1876 return NTFR(TFR.FAIL);
1877
1878 if (!Assert(diff2 < 0.001))
1879 return NTFR(TFR.FAIL);
1880
1881 // I know that string.Format is faster than additive concatenation
1883 return NTFR(TFR.FAIL);
1884
1885 return NTFR(TFR.SUCCESS);
1886 }
1887
1888 //---------------------------------------------------------------------------
1889 // Test to see if func count data is correct
1891 {
1892 // We should restore this when done
1894
1895 // Time to count
1896
1897 // - CallFunction
1898 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1899 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1900 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1901
1903
1904 // - CallFunctionParams
1905 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1906 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1907 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1908
1910
1911 // - Regular call
1912 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1914 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1915
1917
1918 // - Call
1919 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1920 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1921 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1922
1924
1925 // - Garbage
1926 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1927
1928 // - Static
1929 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1931 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1932
1934
1935 // - Global
1936 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1937 GetDayZGame();
1938 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1939
1941
1942 // - Global proto
1943 // Not tracked, so don't need to compare before and after, should always be 0
1944 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1945 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1946
1947 // - Static proto
1948 // Not tracked, so don't need to compare before and after, should always be 0
1949 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1950
1951 // - proto
1952 // Not tracked, so don't need to compare before and after, should always be 0
1953 GetGame().GetHostName();
1954 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1955
1956 // - proto native
1957 // Not tracked, so don't need to compare before and after, should always be 0
1958 GetGame().IsServer();
1959 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1960
1961 // - static proto native
1962 // Not tracked, so don't need to compare before and after, should always be 0
1964 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1965
1966 // Restore
1967 if (!wasEnabled)
1968 EnProfiler.Enable(false, true);
1969
1970 // Do the checks
1971
1972 // - CallFunction
1973 if (!Assert(callCountCF == 1))
1974 return NTFR(TFR.FAIL);
1975
1976 // - CallFunctionParams
1977 if (!Assert(callCountCFP == 1))
1978 return NTFR(TFR.FAIL);
1979
1980 // - Regular call
1981 if (!Assert(callCountRG == 1))
1982 return NTFR(TFR.FAIL);
1983
1984 // - Call
1985 if (!Assert(callCountC == 1))
1986 return NTFR(TFR.FAIL);
1987
1988 // - Garbage
1989 if (!Assert(callCountNon == -1))
1990 return NTFR(TFR.FAIL);
1991
1992 // - Static
1993 if (!Assert(callCountS == 1))
1994 return NTFR(TFR.FAIL);
1995
1996 // - Global
1997 if (!Assert(callCountG == 1))
1998 return NTFR(TFR.FAIL);
1999
2000 // - Global proto
2001 if (!Assert(callCountGP == 0))
2002 return NTFR(TFR.FAIL);
2003
2004 // - Static proto
2005 if (!Assert(callCountSP == 0))
2006 return NTFR(TFR.FAIL);
2007
2008 // - proto
2009 if (!Assert(callCountP == 0))
2010 return NTFR(TFR.FAIL);
2011
2012 // - proto native
2013 if (!Assert(callCountPN == 0))
2014 return NTFR(TFR.FAIL);
2015
2016 // - static proto native
2017 if (!Assert(callCountSPN == 0))
2018 return NTFR(TFR.FAIL);
2019
2020 return NTFR(TFR.SUCCESS);
2021 }
2022
2023 //---------------------------------------------------------------------------
2024 // Helpers
2025 //---------------------------------------------------------------------------
2026 // Snore
2027 float Sleep(float timeS)
2028 {
2029 float startTime = GetGame().GetTickTime();
2030 while (GetGame().GetTickTime() - startTime < timeS)
2031 {
2032 // Zzz
2033 }
2034
2035 return GetGame().GetTickTime() - startTime;
2036 }
2037
2038 //---------------------------------------------------------------------------
2039 // Example stress method
2040 float StringFormat()
2041 {
2042 float startTime = GetGame().GetTickTime();
2043
2044 for (int i = 0; i < 1000; ++i)
2045 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
2046
2047 return GetGame().GetTickTime() - startTime;
2048 }
2049
2050 //---------------------------------------------------------------------------
2051 // Example stress method 2
2052 float StringConcat()
2053 {
2054 float startTime = GetGame().GetTickTime();
2055
2056 for (int i = 0; i < 1000; ++i)
2057 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
2058
2059 return GetGame().GetTickTime() - startTime;
2060 }
2061
2062 //---------------------------------------------------------------------------
2063 // To make sure it is only ever called in that test
2065 {
2066 int dummy = 3;
2067 }
2068
2069 //---------------------------------------------------------------------------
2070 // To make sure it is only ever called in that test
2071 static void TestFuncCountDataHelperStatic()
2072 {
2073 int dummy = 3;
2074 }
2075}
2076
2077class EPTHelperClass
2078{
2079 float Sleep2(float timeS)
2080 {
2081 float startTime = GetGame().GetTickTime();
2082 while (GetGame().GetTickTime() - startTime < timeS)
2083 {
2084 // Zzz
2085 }
2086
2087 return GetGame().GetTickTime() - startTime;
2088 }
2089 float SleepAgain(float timeS)
2090 {
2091 float startTime = GetGame().GetTickTime();
2092 while (GetGame().GetTickTime() - startTime < timeS)
2093 {
2094 // Zzz
2095 }
2096
2097 return GetGame().GetTickTime() - startTime;
2098 }
2099
2100 float DoEverything()
2101 {
2102 float startTime = GetGame().GetTickTime();
2103
2104 Sleep2(3);
2105 SleepAgain(3);
2106
2107 return GetGame().GetTickTime() - startTime;
2108 }
2109}

◆ TestFuncCountDataHelperStatic()

static void Sleep2::TestFuncCountDataHelperStatic ( )
static

Definition at line 1388 of file EnProfilerTests.c.

1388 : TestFramework
1389{
1392
1393 //---------------------------------------------------------------------------
1394 // Ctor - Decides the tests to run
1395 //---------------------------------------------------------------------------
1396 void EnProfilerTests()
1397 {
1399
1400 AddInitTest("TestToggling");
1401 AddInitTest("TestTogglingImmediate");
1402 AddInitTest("TestSetFlags");
1403 AddInitTest("TestClearFlags");
1404 AddInitTest("TestAddFlags");
1405 AddInitTest("TestModule");
1406 AddInitTest("TestClassTimeData");
1407 AddInitTest("TestClassCountData");
1408 AddInitTest("TestFuncTimeData");
1409 AddInitTest("TestFuncCountData");
1410 }
1411
1412 //---------------------------------------------------------------------------
1413 // Dtor
1414 //---------------------------------------------------------------------------
1415 void ~EnProfilerTests()
1416 {
1418 }
1419
1420 //---------------------------------------------------------------------------
1421 // Tests
1422 //---------------------------------------------------------------------------
1423 // Test toggling state
1425 {
1429 {
1432 }
1433
1434 return NTFR(TFR.FAIL);
1435 }
1436
1437 //---------------------------------------------------------------------------
1438 // Test toggling immediate state
1440 {
1444 {
1447 }
1448
1449 return NTFR(TFR.FAIL);
1450 }
1451
1452 //---------------------------------------------------------------------------
1453 // Test SetFlags/GetFlags
1455 {
1457
1458 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1459 {
1461 EnProfiler.SetFlags(flags);
1462
1463 if (!Assert(EnProfiler.GetFlags() == flags))
1464 {
1466 return NTFR(TFR.FAIL);
1467 }
1468
1469 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1470 {
1472 EnProfiler.SetFlags(flags);
1473
1474 if (!Assert(EnProfiler.GetFlags() == flags))
1475 {
1477 return NTFR(TFR.FAIL);
1478 }
1479 }
1480 }
1481
1482 // Let's test some bogus
1483 EnProfiler.SetFlags(-333);
1486 if (!Assert(bogusFlags == 0))
1487 {
1489 return NTFR(TFR.FAIL);
1490 }
1491
1494 if (!Assert(bogusFlags == 0))
1495 {
1497 return NTFR(TFR.FAIL);
1498 }
1499
1500 // Reset
1502 return NTFR(TFR.SUCCESS);
1503 }
1504
1505 //---------------------------------------------------------------------------
1506 // Test removing of flags
1508 {
1510
1512
1514 {
1516 return NTFR(TFR.FAIL);
1517 }
1518
1521
1522 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1523 {
1525 return NTFR(TFR.FAIL);
1526 }
1527
1530
1531 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1532 {
1534 return NTFR(TFR.FAIL);
1535 }
1536
1537 // Reset
1539 return NTFR(TFR.SUCCESS);
1540 }
1541
1542 //---------------------------------------------------------------------------
1543 // Test adding of flags
1545 {
1547
1549
1550 // Return should match resulting flags
1552 {
1554 return NTFR(TFR.FAIL);
1555 }
1556
1557 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1558 {
1560 return NTFR(TFR.FAIL);
1561 }
1562
1563 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1564 {
1566 return NTFR(TFR.FAIL);
1567 }
1568
1569 // Reset
1571 return NTFR(TFR.SUCCESS);
1572 }
1573
1574 //---------------------------------------------------------------------------
1575 // Test module
1577 {
1578 // File lives in Game, use it while testing
1580
1581 // This was added at the same time as this API, so check if it works as well
1582 string nameOfCurrentModule = Type().GetModule();
1583 if (!Assert(nameOfCurrentModule != ""))
1584 return NTFR(TFR.FAIL);
1585
1586 // We know we are in Game, so use it as a test
1589 return NTFR(TFR.FAIL);
1590
1592 return NTFR(TFR.FAIL);
1593
1594 // Test if setting and getting works
1597
1599 {
1601 return NTFR(TFR.FAIL);
1602 }
1603
1604 // Data to restore
1607
1608 // Make sure we are only profiling Game and that the data is clean
1609 // Only valid for the Get...Per... methods, as they need to be sorted
1611
1612 // GetTickTime() returns in seconds, so gather the results in seconds too
1615
1616 // Time to sleeb
1617 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1618 float timeSlept = Sleep(0.3);
1619 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1620 float diff = postTime - previousTime - timeSlept;
1621
1622 // Restore
1624
1625 // We called the function, so it must have some time
1626 if (!Assert(postTime > 0))
1627 {
1629
1630 if (!wasEnabled)
1631 EnProfiler.Enable(false, true);
1632
1634
1635 return NTFR(TFR.FAIL);
1636 }
1637
1638 if (!Assert(diff < 0.00001))
1639 {
1641
1642 if (!wasEnabled)
1643 EnProfiler.Enable(false, true);
1644
1646
1647 return NTFR(TFR.FAIL);
1648 }
1649
1650 // Clean the session
1652
1653 // Something from a different module should not get sorted, so just fire something from a different module
1654 for (int i = 0; i < 1000; ++i)
1655 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1656
1657 // Sort and gather the data and validate if it is correct
1661
1662 Debug.TFLog("Game fncs:", this, "TestModule");
1663
1664 int funcCount = timePerFunc.Count();
1665 for (int j = 0; j < funcCount; ++j)
1666 {
1668 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1669 // We are currently profiling Game, so this Core function shouldn't be present
1670 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1671 {
1673
1674 if (!wasEnabled)
1675 EnProfiler.Enable(false, true);
1676
1678
1679 return NTFR(TFR.FAIL);
1680 }
1681 }
1682
1685
1686 int classCount = timePerClass.Count();
1687 for (int k = 0; k < classCount; ++k)
1688 {
1689 typename type = timePerClass[k].param2;
1691 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1692 {
1694
1695 if (!wasEnabled)
1696 EnProfiler.Enable(false, true);
1697
1699
1700 return NTFR(TFR.FAIL);
1701 }
1702
1703 // Only classes from Game should be present
1704 if (!Assert(classModule == eptModule))
1705 {
1707
1708 if (!wasEnabled)
1709 EnProfiler.Enable(false, true);
1710
1712
1713 return NTFR(TFR.FAIL);
1714 }
1715 }
1716
1717 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1720 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1722
1723 bool found = false;
1724
1725 Debug.TFLog("Core fncs:", this, "TestModule");
1726
1727 funcCount = timePerFunc.Count();
1728 for (int l = 0; l < funcCount; ++l)
1729 {
1731 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1732 // We are currently profiling Core, so this Core function should be present
1733 if (tfpc.param2 == "EnumTools::StringToEnum")
1734 {
1735 found = true;
1736 break;
1737 }
1738 }
1739
1740 Assert(found);
1741
1742 // Test some bogus
1744 EnProfiler.SetModule(-333);
1745 bool success = Assert(EnProfiler.GetModule() == mod);
1746 EnProfiler.SetModule(6003);
1748
1751
1752 if (!wasEnabled)
1753 EnProfiler.Enable(false, true);
1754
1755 return BTFR(success && found);
1756 }
1757
1758 //---------------------------------------------------------------------------
1759 // Test to see if class time data is correct
1761 {
1762 // We should restore this when done
1765
1766 // GetTickTime() returns in seconds, so gather the results in seconds too
1768
1769 // Create the classes
1771
1772 // Time to stress
1773 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1774 float timeStressed = clss.DoEverything();
1775 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1776 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1779
1780 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1781
1782 // Restore
1784 if (!wasEnabled)
1785 EnProfiler.Enable(false, true);
1786
1787 // We called the function, so it must have some time
1788 if (!Assert(postTime > 0))
1789 return NTFR(TFR.FAIL);
1790
1792 return NTFR(TFR.FAIL);
1793
1794 if (!Assert(diff < 0.001))
1795 return NTFR(TFR.FAIL);
1796
1797 return NTFR(TFR.SUCCESS);
1798 }
1799
1800 //---------------------------------------------------------------------------
1801 // Test to see if class count data is correct
1803 {
1804 const int allocAmount = 9;
1805 const int releaseAmount = 6;
1807
1808 // We should restore this when done
1810
1811 // Time to test
1814
1816 for (int i = 0; i < allocAmount; ++i)
1817 instanceArr.Insert(new EPTHelperClass());
1818
1819 for (int j = 0; j < releaseAmount; ++j)
1820 delete instanceArr[j];
1821
1822 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1823 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1824
1827
1828 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1829
1830 // Restore
1831 if (!wasEnabled)
1832 EnProfiler.Enable(false, true);
1833
1834 // Time to check
1835 if (!Assert(alloced == allocAmount))
1836 return NTFR(TFR.FAIL);
1837
1839 return NTFR(TFR.FAIL);
1840
1841 return NTFR(TFR.SUCCESS);
1842 }
1843
1844 //---------------------------------------------------------------------------
1845 // Test to see if func time data is correct
1847 {
1848 // We should restore this when done
1851
1852 // GetTickTime() returns in seconds, so gather the results in seconds too
1854
1855 // Time to stress
1856 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1857 float timeStressed = StringFormat();
1858 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1861
1862 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1863 float timeStressed2 = StringConcat();
1864 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1867
1868 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1869
1870 // Restore
1872 if (!wasEnabled)
1873 EnProfiler.Enable(false, true);
1874
1875 // We called the function, so it must have some time
1876 if (!Assert(postTime > 0))
1877 return NTFR(TFR.FAIL);
1878
1879 if (!Assert(diff < 0.001))
1880 return NTFR(TFR.FAIL);
1881
1882 if (!Assert(postTime2 > 0))
1883 return NTFR(TFR.FAIL);
1884
1885 if (!Assert(diff2 < 0.001))
1886 return NTFR(TFR.FAIL);
1887
1888 // I know that string.Format is faster than additive concatenation
1890 return NTFR(TFR.FAIL);
1891
1892 return NTFR(TFR.SUCCESS);
1893 }
1894
1895 //---------------------------------------------------------------------------
1896 // Test to see if func count data is correct
1898 {
1899 // We should restore this when done
1901
1902 // Time to count
1903
1904 // - CallFunction
1905 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1906 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1907 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1908
1910
1911 // - CallFunctionParams
1912 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1913 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1914 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1915
1917
1918 // - Regular call
1919 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1921 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1922
1924
1925 // - Call
1926 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1927 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1928 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1929
1931
1932 // - Garbage
1933 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1934
1935 // - Static
1936 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1938 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1939
1941
1942 // - Global
1943 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1944 GetDayZGame();
1945 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1946
1948
1949 // - Global proto
1950 // Not tracked, so don't need to compare before and after, should always be 0
1951 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1952 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1953
1954 // - Static proto
1955 // Not tracked, so don't need to compare before and after, should always be 0
1956 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1957
1958 // - proto
1959 // Not tracked, so don't need to compare before and after, should always be 0
1960 GetGame().GetHostName();
1961 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1962
1963 // - proto native
1964 // Not tracked, so don't need to compare before and after, should always be 0
1965 GetGame().IsServer();
1966 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1967
1968 // - static proto native
1969 // Not tracked, so don't need to compare before and after, should always be 0
1971 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1972
1973 // Restore
1974 if (!wasEnabled)
1975 EnProfiler.Enable(false, true);
1976
1977 // Do the checks
1978
1979 // - CallFunction
1980 if (!Assert(callCountCF == 1))
1981 return NTFR(TFR.FAIL);
1982
1983 // - CallFunctionParams
1984 if (!Assert(callCountCFP == 1))
1985 return NTFR(TFR.FAIL);
1986
1987 // - Regular call
1988 if (!Assert(callCountRG == 1))
1989 return NTFR(TFR.FAIL);
1990
1991 // - Call
1992 if (!Assert(callCountC == 1))
1993 return NTFR(TFR.FAIL);
1994
1995 // - Garbage
1996 if (!Assert(callCountNon == -1))
1997 return NTFR(TFR.FAIL);
1998
1999 // - Static
2000 if (!Assert(callCountS == 1))
2001 return NTFR(TFR.FAIL);
2002
2003 // - Global
2004 if (!Assert(callCountG == 1))
2005 return NTFR(TFR.FAIL);
2006
2007 // - Global proto
2008 if (!Assert(callCountGP == 0))
2009 return NTFR(TFR.FAIL);
2010
2011 // - Static proto
2012 if (!Assert(callCountSP == 0))
2013 return NTFR(TFR.FAIL);
2014
2015 // - proto
2016 if (!Assert(callCountP == 0))
2017 return NTFR(TFR.FAIL);
2018
2019 // - proto native
2020 if (!Assert(callCountPN == 0))
2021 return NTFR(TFR.FAIL);
2022
2023 // - static proto native
2024 if (!Assert(callCountSPN == 0))
2025 return NTFR(TFR.FAIL);
2026
2027 return NTFR(TFR.SUCCESS);
2028 }
2029
2030 //---------------------------------------------------------------------------
2031 // Helpers
2032 //---------------------------------------------------------------------------
2033 // Snore
2034 float Sleep(float timeS)
2035 {
2036 float startTime = GetGame().GetTickTime();
2037 while (GetGame().GetTickTime() - startTime < timeS)
2038 {
2039 // Zzz
2040 }
2041
2042 return GetGame().GetTickTime() - startTime;
2043 }
2044
2045 //---------------------------------------------------------------------------
2046 // Example stress method
2047 float StringFormat()
2048 {
2049 float startTime = GetGame().GetTickTime();
2050
2051 for (int i = 0; i < 1000; ++i)
2052 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
2053
2054 return GetGame().GetTickTime() - startTime;
2055 }
2056
2057 //---------------------------------------------------------------------------
2058 // Example stress method 2
2059 float StringConcat()
2060 {
2061 float startTime = GetGame().GetTickTime();
2062
2063 for (int i = 0; i < 1000; ++i)
2064 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
2065
2066 return GetGame().GetTickTime() - startTime;
2067 }
2068
2069 //---------------------------------------------------------------------------
2070 // To make sure it is only ever called in that test
2072 {
2073 int dummy = 3;
2074 }
2075
2076 //---------------------------------------------------------------------------
2077 // To make sure it is only ever called in that test
2078 static void TestFuncCountDataHelperStatic()
2079 {
2080 int dummy = 3;
2081 }
2082}
2083
2084class EPTHelperClass
2085{
2086 float Sleep2(float timeS)
2087 {
2088 float startTime = GetGame().GetTickTime();
2089 while (GetGame().GetTickTime() - startTime < timeS)
2090 {
2091 // Zzz
2092 }
2093
2094 return GetGame().GetTickTime() - startTime;
2095 }
2096 float SleepAgain(float timeS)
2097 {
2098 float startTime = GetGame().GetTickTime();
2099 while (GetGame().GetTickTime() - startTime < timeS)
2100 {
2101 // Zzz
2102 }
2103
2104 return GetGame().GetTickTime() - startTime;
2105 }
2106
2107 float DoEverything()
2108 {
2109 float startTime = GetGame().GetTickTime();
2110
2111 Sleep2(3);
2112 SleepAgain(3);
2113
2114 return GetGame().GetTickTime() - startTime;
2115 }
2116}

◆ TestFuncTimeData()

TFResult Sleep2::TestFuncTimeData ( )

Definition at line 1156 of file EnProfilerTests.c.

1156 : TestFramework
1157{
1160
1161 //---------------------------------------------------------------------------
1162 // Ctor - Decides the tests to run
1163 //---------------------------------------------------------------------------
1164 void EnProfilerTests()
1165 {
1167
1168 AddInitTest("TestToggling");
1169 AddInitTest("TestTogglingImmediate");
1170 AddInitTest("TestSetFlags");
1171 AddInitTest("TestClearFlags");
1172 AddInitTest("TestAddFlags");
1173 AddInitTest("TestModule");
1174 AddInitTest("TestClassTimeData");
1175 AddInitTest("TestClassCountData");
1176 AddInitTest("TestFuncTimeData");
1177 AddInitTest("TestFuncCountData");
1178 }
1179
1180 //---------------------------------------------------------------------------
1181 // Dtor
1182 //---------------------------------------------------------------------------
1183 void ~EnProfilerTests()
1184 {
1186 }
1187
1188 //---------------------------------------------------------------------------
1189 // Tests
1190 //---------------------------------------------------------------------------
1191 // Test toggling state
1193 {
1197 {
1200 }
1201
1202 return NTFR(TFR.FAIL);
1203 }
1204
1205 //---------------------------------------------------------------------------
1206 // Test toggling immediate state
1208 {
1212 {
1215 }
1216
1217 return NTFR(TFR.FAIL);
1218 }
1219
1220 //---------------------------------------------------------------------------
1221 // Test SetFlags/GetFlags
1223 {
1225
1226 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1227 {
1229 EnProfiler.SetFlags(flags);
1230
1231 if (!Assert(EnProfiler.GetFlags() == flags))
1232 {
1234 return NTFR(TFR.FAIL);
1235 }
1236
1237 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1238 {
1240 EnProfiler.SetFlags(flags);
1241
1242 if (!Assert(EnProfiler.GetFlags() == flags))
1243 {
1245 return NTFR(TFR.FAIL);
1246 }
1247 }
1248 }
1249
1250 // Let's test some bogus
1251 EnProfiler.SetFlags(-333);
1254 if (!Assert(bogusFlags == 0))
1255 {
1257 return NTFR(TFR.FAIL);
1258 }
1259
1262 if (!Assert(bogusFlags == 0))
1263 {
1265 return NTFR(TFR.FAIL);
1266 }
1267
1268 // Reset
1270 return NTFR(TFR.SUCCESS);
1271 }
1272
1273 //---------------------------------------------------------------------------
1274 // Test removing of flags
1276 {
1278
1280
1282 {
1284 return NTFR(TFR.FAIL);
1285 }
1286
1289
1290 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1291 {
1293 return NTFR(TFR.FAIL);
1294 }
1295
1298
1299 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1300 {
1302 return NTFR(TFR.FAIL);
1303 }
1304
1305 // Reset
1307 return NTFR(TFR.SUCCESS);
1308 }
1309
1310 //---------------------------------------------------------------------------
1311 // Test adding of flags
1313 {
1315
1317
1318 // Return should match resulting flags
1320 {
1322 return NTFR(TFR.FAIL);
1323 }
1324
1325 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1326 {
1328 return NTFR(TFR.FAIL);
1329 }
1330
1331 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1332 {
1334 return NTFR(TFR.FAIL);
1335 }
1336
1337 // Reset
1339 return NTFR(TFR.SUCCESS);
1340 }
1341
1342 //---------------------------------------------------------------------------
1343 // Test module
1345 {
1346 // File lives in Game, use it while testing
1348
1349 // This was added at the same time as this API, so check if it works as well
1350 string nameOfCurrentModule = Type().GetModule();
1351 if (!Assert(nameOfCurrentModule != ""))
1352 return NTFR(TFR.FAIL);
1353
1354 // We know we are in Game, so use it as a test
1357 return NTFR(TFR.FAIL);
1358
1360 return NTFR(TFR.FAIL);
1361
1362 // Test if setting and getting works
1365
1367 {
1369 return NTFR(TFR.FAIL);
1370 }
1371
1372 // Data to restore
1375
1376 // Make sure we are only profiling Game and that the data is clean
1377 // Only valid for the Get...Per... methods, as they need to be sorted
1379
1380 // GetTickTime() returns in seconds, so gather the results in seconds too
1383
1384 // Time to sleeb
1385 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1386 float timeSlept = Sleep(0.3);
1387 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1388 float diff = postTime - previousTime - timeSlept;
1389
1390 // Restore
1392
1393 // We called the function, so it must have some time
1394 if (!Assert(postTime > 0))
1395 {
1397
1398 if (!wasEnabled)
1399 EnProfiler.Enable(false, true);
1400
1402
1403 return NTFR(TFR.FAIL);
1404 }
1405
1406 if (!Assert(diff < 0.00001))
1407 {
1409
1410 if (!wasEnabled)
1411 EnProfiler.Enable(false, true);
1412
1414
1415 return NTFR(TFR.FAIL);
1416 }
1417
1418 // Clean the session
1420
1421 // Something from a different module should not get sorted, so just fire something from a different module
1422 for (int i = 0; i < 1000; ++i)
1423 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1424
1425 // Sort and gather the data and validate if it is correct
1429
1430 Debug.TFLog("Game fncs:", this, "TestModule");
1431
1432 int funcCount = timePerFunc.Count();
1433 for (int j = 0; j < funcCount; ++j)
1434 {
1436 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1437 // We are currently profiling Game, so this Core function shouldn't be present
1438 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1439 {
1441
1442 if (!wasEnabled)
1443 EnProfiler.Enable(false, true);
1444
1446
1447 return NTFR(TFR.FAIL);
1448 }
1449 }
1450
1453
1454 int classCount = timePerClass.Count();
1455 for (int k = 0; k < classCount; ++k)
1456 {
1457 typename type = timePerClass[k].param2;
1459 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1460 {
1462
1463 if (!wasEnabled)
1464 EnProfiler.Enable(false, true);
1465
1467
1468 return NTFR(TFR.FAIL);
1469 }
1470
1471 // Only classes from Game should be present
1472 if (!Assert(classModule == eptModule))
1473 {
1475
1476 if (!wasEnabled)
1477 EnProfiler.Enable(false, true);
1478
1480
1481 return NTFR(TFR.FAIL);
1482 }
1483 }
1484
1485 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1488 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1490
1491 bool found = false;
1492
1493 Debug.TFLog("Core fncs:", this, "TestModule");
1494
1495 funcCount = timePerFunc.Count();
1496 for (int l = 0; l < funcCount; ++l)
1497 {
1499 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1500 // We are currently profiling Core, so this Core function should be present
1501 if (tfpc.param2 == "EnumTools::StringToEnum")
1502 {
1503 found = true;
1504 break;
1505 }
1506 }
1507
1508 Assert(found);
1509
1510 // Test some bogus
1512 EnProfiler.SetModule(-333);
1513 bool success = Assert(EnProfiler.GetModule() == mod);
1514 EnProfiler.SetModule(6003);
1516
1519
1520 if (!wasEnabled)
1521 EnProfiler.Enable(false, true);
1522
1523 return BTFR(success && found);
1524 }
1525
1526 //---------------------------------------------------------------------------
1527 // Test to see if class time data is correct
1529 {
1530 // We should restore this when done
1533
1534 // GetTickTime() returns in seconds, so gather the results in seconds too
1536
1537 // Create the classes
1539
1540 // Time to stress
1541 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1542 float timeStressed = clss.DoEverything();
1543 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1544 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1547
1548 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1549
1550 // Restore
1552 if (!wasEnabled)
1553 EnProfiler.Enable(false, true);
1554
1555 // We called the function, so it must have some time
1556 if (!Assert(postTime > 0))
1557 return NTFR(TFR.FAIL);
1558
1560 return NTFR(TFR.FAIL);
1561
1562 if (!Assert(diff < 0.001))
1563 return NTFR(TFR.FAIL);
1564
1565 return NTFR(TFR.SUCCESS);
1566 }
1567
1568 //---------------------------------------------------------------------------
1569 // Test to see if class count data is correct
1571 {
1572 const int allocAmount = 9;
1573 const int releaseAmount = 6;
1575
1576 // We should restore this when done
1578
1579 // Time to test
1582
1584 for (int i = 0; i < allocAmount; ++i)
1585 instanceArr.Insert(new EPTHelperClass());
1586
1587 for (int j = 0; j < releaseAmount; ++j)
1588 delete instanceArr[j];
1589
1590 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1591 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1592
1595
1596 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1597
1598 // Restore
1599 if (!wasEnabled)
1600 EnProfiler.Enable(false, true);
1601
1602 // Time to check
1603 if (!Assert(alloced == allocAmount))
1604 return NTFR(TFR.FAIL);
1605
1607 return NTFR(TFR.FAIL);
1608
1609 return NTFR(TFR.SUCCESS);
1610 }
1611
1612 //---------------------------------------------------------------------------
1613 // Test to see if func time data is correct
1615 {
1616 // We should restore this when done
1619
1620 // GetTickTime() returns in seconds, so gather the results in seconds too
1622
1623 // Time to stress
1624 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1625 float timeStressed = StringFormat();
1626 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1629
1630 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1631 float timeStressed2 = StringConcat();
1632 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1635
1636 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1637
1638 // Restore
1640 if (!wasEnabled)
1641 EnProfiler.Enable(false, true);
1642
1643 // We called the function, so it must have some time
1644 if (!Assert(postTime > 0))
1645 return NTFR(TFR.FAIL);
1646
1647 if (!Assert(diff < 0.001))
1648 return NTFR(TFR.FAIL);
1649
1650 if (!Assert(postTime2 > 0))
1651 return NTFR(TFR.FAIL);
1652
1653 if (!Assert(diff2 < 0.001))
1654 return NTFR(TFR.FAIL);
1655
1656 // I know that string.Format is faster than additive concatenation
1658 return NTFR(TFR.FAIL);
1659
1660 return NTFR(TFR.SUCCESS);
1661 }
1662
1663 //---------------------------------------------------------------------------
1664 // Test to see if func count data is correct
1666 {
1667 // We should restore this when done
1669
1670 // Time to count
1671
1672 // - CallFunction
1673 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1674 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1675 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1676
1678
1679 // - CallFunctionParams
1680 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1681 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1682 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1683
1685
1686 // - Regular call
1687 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1689 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1690
1692
1693 // - Call
1694 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1695 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1696 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1697
1699
1700 // - Garbage
1701 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1702
1703 // - Static
1704 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1706 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1707
1709
1710 // - Global
1711 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1712 GetDayZGame();
1713 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1714
1716
1717 // - Global proto
1718 // Not tracked, so don't need to compare before and after, should always be 0
1719 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1720 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1721
1722 // - Static proto
1723 // Not tracked, so don't need to compare before and after, should always be 0
1724 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1725
1726 // - proto
1727 // Not tracked, so don't need to compare before and after, should always be 0
1728 GetGame().GetHostName();
1729 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1730
1731 // - proto native
1732 // Not tracked, so don't need to compare before and after, should always be 0
1733 GetGame().IsServer();
1734 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1735
1736 // - static proto native
1737 // Not tracked, so don't need to compare before and after, should always be 0
1739 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1740
1741 // Restore
1742 if (!wasEnabled)
1743 EnProfiler.Enable(false, true);
1744
1745 // Do the checks
1746
1747 // - CallFunction
1748 if (!Assert(callCountCF == 1))
1749 return NTFR(TFR.FAIL);
1750
1751 // - CallFunctionParams
1752 if (!Assert(callCountCFP == 1))
1753 return NTFR(TFR.FAIL);
1754
1755 // - Regular call
1756 if (!Assert(callCountRG == 1))
1757 return NTFR(TFR.FAIL);
1758
1759 // - Call
1760 if (!Assert(callCountC == 1))
1761 return NTFR(TFR.FAIL);
1762
1763 // - Garbage
1764 if (!Assert(callCountNon == -1))
1765 return NTFR(TFR.FAIL);
1766
1767 // - Static
1768 if (!Assert(callCountS == 1))
1769 return NTFR(TFR.FAIL);
1770
1771 // - Global
1772 if (!Assert(callCountG == 1))
1773 return NTFR(TFR.FAIL);
1774
1775 // - Global proto
1776 if (!Assert(callCountGP == 0))
1777 return NTFR(TFR.FAIL);
1778
1779 // - Static proto
1780 if (!Assert(callCountSP == 0))
1781 return NTFR(TFR.FAIL);
1782
1783 // - proto
1784 if (!Assert(callCountP == 0))
1785 return NTFR(TFR.FAIL);
1786
1787 // - proto native
1788 if (!Assert(callCountPN == 0))
1789 return NTFR(TFR.FAIL);
1790
1791 // - static proto native
1792 if (!Assert(callCountSPN == 0))
1793 return NTFR(TFR.FAIL);
1794
1795 return NTFR(TFR.SUCCESS);
1796 }
1797
1798 //---------------------------------------------------------------------------
1799 // Helpers
1800 //---------------------------------------------------------------------------
1801 // Snore
1802 float Sleep(float timeS)
1803 {
1804 float startTime = GetGame().GetTickTime();
1805 while (GetGame().GetTickTime() - startTime < timeS)
1806 {
1807 // Zzz
1808 }
1809
1810 return GetGame().GetTickTime() - startTime;
1811 }
1812
1813 //---------------------------------------------------------------------------
1814 // Example stress method
1815 float StringFormat()
1816 {
1817 float startTime = GetGame().GetTickTime();
1818
1819 for (int i = 0; i < 1000; ++i)
1820 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1821
1822 return GetGame().GetTickTime() - startTime;
1823 }
1824
1825 //---------------------------------------------------------------------------
1826 // Example stress method 2
1827 float StringConcat()
1828 {
1829 float startTime = GetGame().GetTickTime();
1830
1831 for (int i = 0; i < 1000; ++i)
1832 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1833
1834 return GetGame().GetTickTime() - startTime;
1835 }
1836
1837 //---------------------------------------------------------------------------
1838 // To make sure it is only ever called in that test
1840 {
1841 int dummy = 3;
1842 }
1843
1844 //---------------------------------------------------------------------------
1845 // To make sure it is only ever called in that test
1846 static void TestFuncCountDataHelperStatic()
1847 {
1848 int dummy = 3;
1849 }
1850}
1851
1852class EPTHelperClass
1853{
1854 float Sleep2(float timeS)
1855 {
1856 float startTime = GetGame().GetTickTime();
1857 while (GetGame().GetTickTime() - startTime < timeS)
1858 {
1859 // Zzz
1860 }
1861
1862 return GetGame().GetTickTime() - startTime;
1863 }
1864 float SleepAgain(float timeS)
1865 {
1866 float startTime = GetGame().GetTickTime();
1867 while (GetGame().GetTickTime() - startTime < timeS)
1868 {
1869 // Zzz
1870 }
1871
1872 return GetGame().GetTickTime() - startTime;
1873 }
1874
1875 float DoEverything()
1876 {
1877 float startTime = GetGame().GetTickTime();
1878
1879 Sleep2(3);
1880 SleepAgain(3);
1881
1882 return GetGame().GetTickTime() - startTime;
1883 }
1884}

◆ TestModule()

TFResult Sleep2::TestModule ( )

Definition at line 886 of file EnProfilerTests.c.

886 : TestFramework
887{
890
891 //---------------------------------------------------------------------------
892 // Ctor - Decides the tests to run
893 //---------------------------------------------------------------------------
894 void EnProfilerTests()
895 {
897
898 AddInitTest("TestToggling");
899 AddInitTest("TestTogglingImmediate");
900 AddInitTest("TestSetFlags");
901 AddInitTest("TestClearFlags");
902 AddInitTest("TestAddFlags");
903 AddInitTest("TestModule");
904 AddInitTest("TestClassTimeData");
905 AddInitTest("TestClassCountData");
906 AddInitTest("TestFuncTimeData");
907 AddInitTest("TestFuncCountData");
908 }
909
910 //---------------------------------------------------------------------------
911 // Dtor
912 //---------------------------------------------------------------------------
913 void ~EnProfilerTests()
914 {
916 }
917
918 //---------------------------------------------------------------------------
919 // Tests
920 //---------------------------------------------------------------------------
921 // Test toggling state
923 {
927 {
930 }
931
932 return NTFR(TFR.FAIL);
933 }
934
935 //---------------------------------------------------------------------------
936 // Test toggling immediate state
938 {
942 {
945 }
946
947 return NTFR(TFR.FAIL);
948 }
949
950 //---------------------------------------------------------------------------
951 // Test SetFlags/GetFlags
953 {
955
956 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
957 {
959 EnProfiler.SetFlags(flags);
960
961 if (!Assert(EnProfiler.GetFlags() == flags))
962 {
964 return NTFR(TFR.FAIL);
965 }
966
967 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
968 {
970 EnProfiler.SetFlags(flags);
971
972 if (!Assert(EnProfiler.GetFlags() == flags))
973 {
975 return NTFR(TFR.FAIL);
976 }
977 }
978 }
979
980 // Let's test some bogus
981 EnProfiler.SetFlags(-333);
984 if (!Assert(bogusFlags == 0))
985 {
987 return NTFR(TFR.FAIL);
988 }
989
992 if (!Assert(bogusFlags == 0))
993 {
995 return NTFR(TFR.FAIL);
996 }
997
998 // Reset
1000 return NTFR(TFR.SUCCESS);
1001 }
1002
1003 //---------------------------------------------------------------------------
1004 // Test removing of flags
1006 {
1008
1010
1012 {
1014 return NTFR(TFR.FAIL);
1015 }
1016
1019
1020 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1021 {
1023 return NTFR(TFR.FAIL);
1024 }
1025
1028
1029 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1030 {
1032 return NTFR(TFR.FAIL);
1033 }
1034
1035 // Reset
1037 return NTFR(TFR.SUCCESS);
1038 }
1039
1040 //---------------------------------------------------------------------------
1041 // Test adding of flags
1043 {
1045
1047
1048 // Return should match resulting flags
1050 {
1052 return NTFR(TFR.FAIL);
1053 }
1054
1055 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1056 {
1058 return NTFR(TFR.FAIL);
1059 }
1060
1061 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1062 {
1064 return NTFR(TFR.FAIL);
1065 }
1066
1067 // Reset
1069 return NTFR(TFR.SUCCESS);
1070 }
1071
1072 //---------------------------------------------------------------------------
1073 // Test module
1075 {
1076 // File lives in Game, use it while testing
1078
1079 // This was added at the same time as this API, so check if it works as well
1080 string nameOfCurrentModule = Type().GetModule();
1081 if (!Assert(nameOfCurrentModule != ""))
1082 return NTFR(TFR.FAIL);
1083
1084 // We know we are in Game, so use it as a test
1087 return NTFR(TFR.FAIL);
1088
1090 return NTFR(TFR.FAIL);
1091
1092 // Test if setting and getting works
1095
1097 {
1099 return NTFR(TFR.FAIL);
1100 }
1101
1102 // Data to restore
1105
1106 // Make sure we are only profiling Game and that the data is clean
1107 // Only valid for the Get...Per... methods, as they need to be sorted
1109
1110 // GetTickTime() returns in seconds, so gather the results in seconds too
1113
1114 // Time to sleeb
1115 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1116 float timeSlept = Sleep(0.3);
1117 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1118 float diff = postTime - previousTime - timeSlept;
1119
1120 // Restore
1122
1123 // We called the function, so it must have some time
1124 if (!Assert(postTime > 0))
1125 {
1127
1128 if (!wasEnabled)
1129 EnProfiler.Enable(false, true);
1130
1132
1133 return NTFR(TFR.FAIL);
1134 }
1135
1136 if (!Assert(diff < 0.00001))
1137 {
1139
1140 if (!wasEnabled)
1141 EnProfiler.Enable(false, true);
1142
1144
1145 return NTFR(TFR.FAIL);
1146 }
1147
1148 // Clean the session
1150
1151 // Something from a different module should not get sorted, so just fire something from a different module
1152 for (int i = 0; i < 1000; ++i)
1153 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1154
1155 // Sort and gather the data and validate if it is correct
1159
1160 Debug.TFLog("Game fncs:", this, "TestModule");
1161
1162 int funcCount = timePerFunc.Count();
1163 for (int j = 0; j < funcCount; ++j)
1164 {
1166 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1167 // We are currently profiling Game, so this Core function shouldn't be present
1168 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1169 {
1171
1172 if (!wasEnabled)
1173 EnProfiler.Enable(false, true);
1174
1176
1177 return NTFR(TFR.FAIL);
1178 }
1179 }
1180
1183
1184 int classCount = timePerClass.Count();
1185 for (int k = 0; k < classCount; ++k)
1186 {
1187 typename type = timePerClass[k].param2;
1189 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1190 {
1192
1193 if (!wasEnabled)
1194 EnProfiler.Enable(false, true);
1195
1197
1198 return NTFR(TFR.FAIL);
1199 }
1200
1201 // Only classes from Game should be present
1202 if (!Assert(classModule == eptModule))
1203 {
1205
1206 if (!wasEnabled)
1207 EnProfiler.Enable(false, true);
1208
1210
1211 return NTFR(TFR.FAIL);
1212 }
1213 }
1214
1215 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1218 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1220
1221 bool found = false;
1222
1223 Debug.TFLog("Core fncs:", this, "TestModule");
1224
1225 funcCount = timePerFunc.Count();
1226 for (int l = 0; l < funcCount; ++l)
1227 {
1229 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1230 // We are currently profiling Core, so this Core function should be present
1231 if (tfpc.param2 == "EnumTools::StringToEnum")
1232 {
1233 found = true;
1234 break;
1235 }
1236 }
1237
1238 Assert(found);
1239
1240 // Test some bogus
1242 EnProfiler.SetModule(-333);
1243 bool success = Assert(EnProfiler.GetModule() == mod);
1244 EnProfiler.SetModule(6003);
1246
1249
1250 if (!wasEnabled)
1251 EnProfiler.Enable(false, true);
1252
1253 return BTFR(success && found);
1254 }
1255
1256 //---------------------------------------------------------------------------
1257 // Test to see if class time data is correct
1259 {
1260 // We should restore this when done
1263
1264 // GetTickTime() returns in seconds, so gather the results in seconds too
1266
1267 // Create the classes
1269
1270 // Time to stress
1271 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1272 float timeStressed = clss.DoEverything();
1273 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1274 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1277
1278 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1279
1280 // Restore
1282 if (!wasEnabled)
1283 EnProfiler.Enable(false, true);
1284
1285 // We called the function, so it must have some time
1286 if (!Assert(postTime > 0))
1287 return NTFR(TFR.FAIL);
1288
1290 return NTFR(TFR.FAIL);
1291
1292 if (!Assert(diff < 0.001))
1293 return NTFR(TFR.FAIL);
1294
1295 return NTFR(TFR.SUCCESS);
1296 }
1297
1298 //---------------------------------------------------------------------------
1299 // Test to see if class count data is correct
1301 {
1302 const int allocAmount = 9;
1303 const int releaseAmount = 6;
1305
1306 // We should restore this when done
1308
1309 // Time to test
1312
1314 for (int i = 0; i < allocAmount; ++i)
1315 instanceArr.Insert(new EPTHelperClass());
1316
1317 for (int j = 0; j < releaseAmount; ++j)
1318 delete instanceArr[j];
1319
1320 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1321 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1322
1325
1326 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1327
1328 // Restore
1329 if (!wasEnabled)
1330 EnProfiler.Enable(false, true);
1331
1332 // Time to check
1333 if (!Assert(alloced == allocAmount))
1334 return NTFR(TFR.FAIL);
1335
1337 return NTFR(TFR.FAIL);
1338
1339 return NTFR(TFR.SUCCESS);
1340 }
1341
1342 //---------------------------------------------------------------------------
1343 // Test to see if func time data is correct
1345 {
1346 // We should restore this when done
1349
1350 // GetTickTime() returns in seconds, so gather the results in seconds too
1352
1353 // Time to stress
1354 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1355 float timeStressed = StringFormat();
1356 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1359
1360 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1361 float timeStressed2 = StringConcat();
1362 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1365
1366 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1367
1368 // Restore
1370 if (!wasEnabled)
1371 EnProfiler.Enable(false, true);
1372
1373 // We called the function, so it must have some time
1374 if (!Assert(postTime > 0))
1375 return NTFR(TFR.FAIL);
1376
1377 if (!Assert(diff < 0.001))
1378 return NTFR(TFR.FAIL);
1379
1380 if (!Assert(postTime2 > 0))
1381 return NTFR(TFR.FAIL);
1382
1383 if (!Assert(diff2 < 0.001))
1384 return NTFR(TFR.FAIL);
1385
1386 // I know that string.Format is faster than additive concatenation
1388 return NTFR(TFR.FAIL);
1389
1390 return NTFR(TFR.SUCCESS);
1391 }
1392
1393 //---------------------------------------------------------------------------
1394 // Test to see if func count data is correct
1396 {
1397 // We should restore this when done
1399
1400 // Time to count
1401
1402 // - CallFunction
1403 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1404 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1405 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1406
1408
1409 // - CallFunctionParams
1410 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1411 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1412 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1413
1415
1416 // - Regular call
1417 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1419 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1420
1422
1423 // - Call
1424 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1425 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1426 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1427
1429
1430 // - Garbage
1431 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1432
1433 // - Static
1434 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1436 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1437
1439
1440 // - Global
1441 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1442 GetDayZGame();
1443 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1444
1446
1447 // - Global proto
1448 // Not tracked, so don't need to compare before and after, should always be 0
1449 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1450 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1451
1452 // - Static proto
1453 // Not tracked, so don't need to compare before and after, should always be 0
1454 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1455
1456 // - proto
1457 // Not tracked, so don't need to compare before and after, should always be 0
1458 GetGame().GetHostName();
1459 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1460
1461 // - proto native
1462 // Not tracked, so don't need to compare before and after, should always be 0
1463 GetGame().IsServer();
1464 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1465
1466 // - static proto native
1467 // Not tracked, so don't need to compare before and after, should always be 0
1469 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1470
1471 // Restore
1472 if (!wasEnabled)
1473 EnProfiler.Enable(false, true);
1474
1475 // Do the checks
1476
1477 // - CallFunction
1478 if (!Assert(callCountCF == 1))
1479 return NTFR(TFR.FAIL);
1480
1481 // - CallFunctionParams
1482 if (!Assert(callCountCFP == 1))
1483 return NTFR(TFR.FAIL);
1484
1485 // - Regular call
1486 if (!Assert(callCountRG == 1))
1487 return NTFR(TFR.FAIL);
1488
1489 // - Call
1490 if (!Assert(callCountC == 1))
1491 return NTFR(TFR.FAIL);
1492
1493 // - Garbage
1494 if (!Assert(callCountNon == -1))
1495 return NTFR(TFR.FAIL);
1496
1497 // - Static
1498 if (!Assert(callCountS == 1))
1499 return NTFR(TFR.FAIL);
1500
1501 // - Global
1502 if (!Assert(callCountG == 1))
1503 return NTFR(TFR.FAIL);
1504
1505 // - Global proto
1506 if (!Assert(callCountGP == 0))
1507 return NTFR(TFR.FAIL);
1508
1509 // - Static proto
1510 if (!Assert(callCountSP == 0))
1511 return NTFR(TFR.FAIL);
1512
1513 // - proto
1514 if (!Assert(callCountP == 0))
1515 return NTFR(TFR.FAIL);
1516
1517 // - proto native
1518 if (!Assert(callCountPN == 0))
1519 return NTFR(TFR.FAIL);
1520
1521 // - static proto native
1522 if (!Assert(callCountSPN == 0))
1523 return NTFR(TFR.FAIL);
1524
1525 return NTFR(TFR.SUCCESS);
1526 }
1527
1528 //---------------------------------------------------------------------------
1529 // Helpers
1530 //---------------------------------------------------------------------------
1531 // Snore
1532 float Sleep(float timeS)
1533 {
1534 float startTime = GetGame().GetTickTime();
1535 while (GetGame().GetTickTime() - startTime < timeS)
1536 {
1537 // Zzz
1538 }
1539
1540 return GetGame().GetTickTime() - startTime;
1541 }
1542
1543 //---------------------------------------------------------------------------
1544 // Example stress method
1545 float StringFormat()
1546 {
1547 float startTime = GetGame().GetTickTime();
1548
1549 for (int i = 0; i < 1000; ++i)
1550 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1551
1552 return GetGame().GetTickTime() - startTime;
1553 }
1554
1555 //---------------------------------------------------------------------------
1556 // Example stress method 2
1557 float StringConcat()
1558 {
1559 float startTime = GetGame().GetTickTime();
1560
1561 for (int i = 0; i < 1000; ++i)
1562 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1563
1564 return GetGame().GetTickTime() - startTime;
1565 }
1566
1567 //---------------------------------------------------------------------------
1568 // To make sure it is only ever called in that test
1570 {
1571 int dummy = 3;
1572 }
1573
1574 //---------------------------------------------------------------------------
1575 // To make sure it is only ever called in that test
1576 static void TestFuncCountDataHelperStatic()
1577 {
1578 int dummy = 3;
1579 }
1580}
1581
1582class EPTHelperClass
1583{
1584 float Sleep2(float timeS)
1585 {
1586 float startTime = GetGame().GetTickTime();
1587 while (GetGame().GetTickTime() - startTime < timeS)
1588 {
1589 // Zzz
1590 }
1591
1592 return GetGame().GetTickTime() - startTime;
1593 }
1594 float SleepAgain(float timeS)
1595 {
1596 float startTime = GetGame().GetTickTime();
1597 while (GetGame().GetTickTime() - startTime < timeS)
1598 {
1599 // Zzz
1600 }
1601
1602 return GetGame().GetTickTime() - startTime;
1603 }
1604
1605 float DoEverything()
1606 {
1607 float startTime = GetGame().GetTickTime();
1608
1609 Sleep2(3);
1610 SleepAgain(3);
1611
1612 return GetGame().GetTickTime() - startTime;
1613 }
1614}

◆ TestSetFlags()

TFResult Sleep2::TestSetFlags ( )

Definition at line 764 of file EnProfilerTests.c.

764 : TestFramework
765{
768
769 //---------------------------------------------------------------------------
770 // Ctor - Decides the tests to run
771 //---------------------------------------------------------------------------
772 void EnProfilerTests()
773 {
775
776 AddInitTest("TestToggling");
777 AddInitTest("TestTogglingImmediate");
778 AddInitTest("TestSetFlags");
779 AddInitTest("TestClearFlags");
780 AddInitTest("TestAddFlags");
781 AddInitTest("TestModule");
782 AddInitTest("TestClassTimeData");
783 AddInitTest("TestClassCountData");
784 AddInitTest("TestFuncTimeData");
785 AddInitTest("TestFuncCountData");
786 }
787
788 //---------------------------------------------------------------------------
789 // Dtor
790 //---------------------------------------------------------------------------
791 void ~EnProfilerTests()
792 {
794 }
795
796 //---------------------------------------------------------------------------
797 // Tests
798 //---------------------------------------------------------------------------
799 // Test toggling state
801 {
805 {
808 }
809
810 return NTFR(TFR.FAIL);
811 }
812
813 //---------------------------------------------------------------------------
814 // Test toggling immediate state
816 {
820 {
823 }
824
825 return NTFR(TFR.FAIL);
826 }
827
828 //---------------------------------------------------------------------------
829 // Test SetFlags/GetFlags
831 {
833
834 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
835 {
837 EnProfiler.SetFlags(flags);
838
839 if (!Assert(EnProfiler.GetFlags() == flags))
840 {
842 return NTFR(TFR.FAIL);
843 }
844
845 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
846 {
848 EnProfiler.SetFlags(flags);
849
850 if (!Assert(EnProfiler.GetFlags() == flags))
851 {
853 return NTFR(TFR.FAIL);
854 }
855 }
856 }
857
858 // Let's test some bogus
859 EnProfiler.SetFlags(-333);
862 if (!Assert(bogusFlags == 0))
863 {
865 return NTFR(TFR.FAIL);
866 }
867
870 if (!Assert(bogusFlags == 0))
871 {
873 return NTFR(TFR.FAIL);
874 }
875
876 // Reset
878 return NTFR(TFR.SUCCESS);
879 }
880
881 //---------------------------------------------------------------------------
882 // Test removing of flags
884 {
886
888
890 {
892 return NTFR(TFR.FAIL);
893 }
894
897
899 {
901 return NTFR(TFR.FAIL);
902 }
903
906
908 {
910 return NTFR(TFR.FAIL);
911 }
912
913 // Reset
915 return NTFR(TFR.SUCCESS);
916 }
917
918 //---------------------------------------------------------------------------
919 // Test adding of flags
921 {
923
925
926 // Return should match resulting flags
928 {
930 return NTFR(TFR.FAIL);
931 }
932
933 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
934 {
936 return NTFR(TFR.FAIL);
937 }
938
939 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
940 {
942 return NTFR(TFR.FAIL);
943 }
944
945 // Reset
947 return NTFR(TFR.SUCCESS);
948 }
949
950 //---------------------------------------------------------------------------
951 // Test module
953 {
954 // File lives in Game, use it while testing
956
957 // This was added at the same time as this API, so check if it works as well
958 string nameOfCurrentModule = Type().GetModule();
959 if (!Assert(nameOfCurrentModule != ""))
960 return NTFR(TFR.FAIL);
961
962 // We know we are in Game, so use it as a test
965 return NTFR(TFR.FAIL);
966
968 return NTFR(TFR.FAIL);
969
970 // Test if setting and getting works
973
975 {
977 return NTFR(TFR.FAIL);
978 }
979
980 // Data to restore
983
984 // Make sure we are only profiling Game and that the data is clean
985 // Only valid for the Get...Per... methods, as they need to be sorted
987
988 // GetTickTime() returns in seconds, so gather the results in seconds too
991
992 // Time to sleeb
993 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
994 float timeSlept = Sleep(0.3);
995 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
997
998 // Restore
1000
1001 // We called the function, so it must have some time
1002 if (!Assert(postTime > 0))
1003 {
1005
1006 if (!wasEnabled)
1007 EnProfiler.Enable(false, true);
1008
1010
1011 return NTFR(TFR.FAIL);
1012 }
1013
1014 if (!Assert(diff < 0.00001))
1015 {
1017
1018 if (!wasEnabled)
1019 EnProfiler.Enable(false, true);
1020
1022
1023 return NTFR(TFR.FAIL);
1024 }
1025
1026 // Clean the session
1028
1029 // Something from a different module should not get sorted, so just fire something from a different module
1030 for (int i = 0; i < 1000; ++i)
1031 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1032
1033 // Sort and gather the data and validate if it is correct
1037
1038 Debug.TFLog("Game fncs:", this, "TestModule");
1039
1040 int funcCount = timePerFunc.Count();
1041 for (int j = 0; j < funcCount; ++j)
1042 {
1044 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1045 // We are currently profiling Game, so this Core function shouldn't be present
1046 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1047 {
1049
1050 if (!wasEnabled)
1051 EnProfiler.Enable(false, true);
1052
1054
1055 return NTFR(TFR.FAIL);
1056 }
1057 }
1058
1061
1062 int classCount = timePerClass.Count();
1063 for (int k = 0; k < classCount; ++k)
1064 {
1065 typename type = timePerClass[k].param2;
1067 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1068 {
1070
1071 if (!wasEnabled)
1072 EnProfiler.Enable(false, true);
1073
1075
1076 return NTFR(TFR.FAIL);
1077 }
1078
1079 // Only classes from Game should be present
1080 if (!Assert(classModule == eptModule))
1081 {
1083
1084 if (!wasEnabled)
1085 EnProfiler.Enable(false, true);
1086
1088
1089 return NTFR(TFR.FAIL);
1090 }
1091 }
1092
1093 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1096 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1098
1099 bool found = false;
1100
1101 Debug.TFLog("Core fncs:", this, "TestModule");
1102
1103 funcCount = timePerFunc.Count();
1104 for (int l = 0; l < funcCount; ++l)
1105 {
1107 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1108 // We are currently profiling Core, so this Core function should be present
1109 if (tfpc.param2 == "EnumTools::StringToEnum")
1110 {
1111 found = true;
1112 break;
1113 }
1114 }
1115
1116 Assert(found);
1117
1118 // Test some bogus
1120 EnProfiler.SetModule(-333);
1121 bool success = Assert(EnProfiler.GetModule() == mod);
1122 EnProfiler.SetModule(6003);
1124
1127
1128 if (!wasEnabled)
1129 EnProfiler.Enable(false, true);
1130
1131 return BTFR(success && found);
1132 }
1133
1134 //---------------------------------------------------------------------------
1135 // Test to see if class time data is correct
1137 {
1138 // We should restore this when done
1141
1142 // GetTickTime() returns in seconds, so gather the results in seconds too
1144
1145 // Create the classes
1147
1148 // Time to stress
1149 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1150 float timeStressed = clss.DoEverything();
1151 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1152 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1155
1156 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1157
1158 // Restore
1160 if (!wasEnabled)
1161 EnProfiler.Enable(false, true);
1162
1163 // We called the function, so it must have some time
1164 if (!Assert(postTime > 0))
1165 return NTFR(TFR.FAIL);
1166
1168 return NTFR(TFR.FAIL);
1169
1170 if (!Assert(diff < 0.001))
1171 return NTFR(TFR.FAIL);
1172
1173 return NTFR(TFR.SUCCESS);
1174 }
1175
1176 //---------------------------------------------------------------------------
1177 // Test to see if class count data is correct
1179 {
1180 const int allocAmount = 9;
1181 const int releaseAmount = 6;
1183
1184 // We should restore this when done
1186
1187 // Time to test
1190
1192 for (int i = 0; i < allocAmount; ++i)
1193 instanceArr.Insert(new EPTHelperClass());
1194
1195 for (int j = 0; j < releaseAmount; ++j)
1196 delete instanceArr[j];
1197
1198 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1199 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1200
1203
1204 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1205
1206 // Restore
1207 if (!wasEnabled)
1208 EnProfiler.Enable(false, true);
1209
1210 // Time to check
1211 if (!Assert(alloced == allocAmount))
1212 return NTFR(TFR.FAIL);
1213
1215 return NTFR(TFR.FAIL);
1216
1217 return NTFR(TFR.SUCCESS);
1218 }
1219
1220 //---------------------------------------------------------------------------
1221 // Test to see if func time data is correct
1223 {
1224 // We should restore this when done
1227
1228 // GetTickTime() returns in seconds, so gather the results in seconds too
1230
1231 // Time to stress
1232 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1233 float timeStressed = StringFormat();
1234 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1237
1238 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1239 float timeStressed2 = StringConcat();
1240 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1243
1244 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1245
1246 // Restore
1248 if (!wasEnabled)
1249 EnProfiler.Enable(false, true);
1250
1251 // We called the function, so it must have some time
1252 if (!Assert(postTime > 0))
1253 return NTFR(TFR.FAIL);
1254
1255 if (!Assert(diff < 0.001))
1256 return NTFR(TFR.FAIL);
1257
1258 if (!Assert(postTime2 > 0))
1259 return NTFR(TFR.FAIL);
1260
1261 if (!Assert(diff2 < 0.001))
1262 return NTFR(TFR.FAIL);
1263
1264 // I know that string.Format is faster than additive concatenation
1266 return NTFR(TFR.FAIL);
1267
1268 return NTFR(TFR.SUCCESS);
1269 }
1270
1271 //---------------------------------------------------------------------------
1272 // Test to see if func count data is correct
1274 {
1275 // We should restore this when done
1277
1278 // Time to count
1279
1280 // - CallFunction
1281 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1282 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1283 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1284
1286
1287 // - CallFunctionParams
1288 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1289 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1290 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1291
1293
1294 // - Regular call
1295 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1297 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1298
1300
1301 // - Call
1302 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1303 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1304 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1305
1307
1308 // - Garbage
1309 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1310
1311 // - Static
1312 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1314 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1315
1317
1318 // - Global
1319 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1320 GetDayZGame();
1321 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1322
1324
1325 // - Global proto
1326 // Not tracked, so don't need to compare before and after, should always be 0
1327 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1328 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1329
1330 // - Static proto
1331 // Not tracked, so don't need to compare before and after, should always be 0
1332 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1333
1334 // - proto
1335 // Not tracked, so don't need to compare before and after, should always be 0
1336 GetGame().GetHostName();
1337 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1338
1339 // - proto native
1340 // Not tracked, so don't need to compare before and after, should always be 0
1341 GetGame().IsServer();
1342 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1343
1344 // - static proto native
1345 // Not tracked, so don't need to compare before and after, should always be 0
1347 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1348
1349 // Restore
1350 if (!wasEnabled)
1351 EnProfiler.Enable(false, true);
1352
1353 // Do the checks
1354
1355 // - CallFunction
1356 if (!Assert(callCountCF == 1))
1357 return NTFR(TFR.FAIL);
1358
1359 // - CallFunctionParams
1360 if (!Assert(callCountCFP == 1))
1361 return NTFR(TFR.FAIL);
1362
1363 // - Regular call
1364 if (!Assert(callCountRG == 1))
1365 return NTFR(TFR.FAIL);
1366
1367 // - Call
1368 if (!Assert(callCountC == 1))
1369 return NTFR(TFR.FAIL);
1370
1371 // - Garbage
1372 if (!Assert(callCountNon == -1))
1373 return NTFR(TFR.FAIL);
1374
1375 // - Static
1376 if (!Assert(callCountS == 1))
1377 return NTFR(TFR.FAIL);
1378
1379 // - Global
1380 if (!Assert(callCountG == 1))
1381 return NTFR(TFR.FAIL);
1382
1383 // - Global proto
1384 if (!Assert(callCountGP == 0))
1385 return NTFR(TFR.FAIL);
1386
1387 // - Static proto
1388 if (!Assert(callCountSP == 0))
1389 return NTFR(TFR.FAIL);
1390
1391 // - proto
1392 if (!Assert(callCountP == 0))
1393 return NTFR(TFR.FAIL);
1394
1395 // - proto native
1396 if (!Assert(callCountPN == 0))
1397 return NTFR(TFR.FAIL);
1398
1399 // - static proto native
1400 if (!Assert(callCountSPN == 0))
1401 return NTFR(TFR.FAIL);
1402
1403 return NTFR(TFR.SUCCESS);
1404 }
1405
1406 //---------------------------------------------------------------------------
1407 // Helpers
1408 //---------------------------------------------------------------------------
1409 // Snore
1410 float Sleep(float timeS)
1411 {
1412 float startTime = GetGame().GetTickTime();
1413 while (GetGame().GetTickTime() - startTime < timeS)
1414 {
1415 // Zzz
1416 }
1417
1418 return GetGame().GetTickTime() - startTime;
1419 }
1420
1421 //---------------------------------------------------------------------------
1422 // Example stress method
1423 float StringFormat()
1424 {
1425 float startTime = GetGame().GetTickTime();
1426
1427 for (int i = 0; i < 1000; ++i)
1428 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1429
1430 return GetGame().GetTickTime() - startTime;
1431 }
1432
1433 //---------------------------------------------------------------------------
1434 // Example stress method 2
1435 float StringConcat()
1436 {
1437 float startTime = GetGame().GetTickTime();
1438
1439 for (int i = 0; i < 1000; ++i)
1440 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1441
1442 return GetGame().GetTickTime() - startTime;
1443 }
1444
1445 //---------------------------------------------------------------------------
1446 // To make sure it is only ever called in that test
1448 {
1449 int dummy = 3;
1450 }
1451
1452 //---------------------------------------------------------------------------
1453 // To make sure it is only ever called in that test
1454 static void TestFuncCountDataHelperStatic()
1455 {
1456 int dummy = 3;
1457 }
1458}
1459
1460class EPTHelperClass
1461{
1462 float Sleep2(float timeS)
1463 {
1464 float startTime = GetGame().GetTickTime();
1465 while (GetGame().GetTickTime() - startTime < timeS)
1466 {
1467 // Zzz
1468 }
1469
1470 return GetGame().GetTickTime() - startTime;
1471 }
1472 float SleepAgain(float timeS)
1473 {
1474 float startTime = GetGame().GetTickTime();
1475 while (GetGame().GetTickTime() - startTime < timeS)
1476 {
1477 // Zzz
1478 }
1479
1480 return GetGame().GetTickTime() - startTime;
1481 }
1482
1483 float DoEverything()
1484 {
1485 float startTime = GetGame().GetTickTime();
1486
1487 Sleep2(3);
1488 SleepAgain(3);
1489
1490 return GetGame().GetTickTime() - startTime;
1491 }
1492}

◆ TestToggling()

TFResult Sleep2::TestToggling ( )

Definition at line 734 of file EnProfilerTests.c.

734 : TestFramework
735{
738
739 //---------------------------------------------------------------------------
740 // Ctor - Decides the tests to run
741 //---------------------------------------------------------------------------
742 void EnProfilerTests()
743 {
745
746 AddInitTest("TestToggling");
747 AddInitTest("TestTogglingImmediate");
748 AddInitTest("TestSetFlags");
749 AddInitTest("TestClearFlags");
750 AddInitTest("TestAddFlags");
751 AddInitTest("TestModule");
752 AddInitTest("TestClassTimeData");
753 AddInitTest("TestClassCountData");
754 AddInitTest("TestFuncTimeData");
755 AddInitTest("TestFuncCountData");
756 }
757
758 //---------------------------------------------------------------------------
759 // Dtor
760 //---------------------------------------------------------------------------
761 void ~EnProfilerTests()
762 {
764 }
765
766 //---------------------------------------------------------------------------
767 // Tests
768 //---------------------------------------------------------------------------
769 // Test toggling state
771 {
775 {
778 }
779
780 return NTFR(TFR.FAIL);
781 }
782
783 //---------------------------------------------------------------------------
784 // Test toggling immediate state
786 {
790 {
793 }
794
795 return NTFR(TFR.FAIL);
796 }
797
798 //---------------------------------------------------------------------------
799 // Test SetFlags/GetFlags
801 {
803
804 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
805 {
807 EnProfiler.SetFlags(flags);
808
809 if (!Assert(EnProfiler.GetFlags() == flags))
810 {
812 return NTFR(TFR.FAIL);
813 }
814
815 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
816 {
818 EnProfiler.SetFlags(flags);
819
820 if (!Assert(EnProfiler.GetFlags() == flags))
821 {
823 return NTFR(TFR.FAIL);
824 }
825 }
826 }
827
828 // Let's test some bogus
829 EnProfiler.SetFlags(-333);
832 if (!Assert(bogusFlags == 0))
833 {
835 return NTFR(TFR.FAIL);
836 }
837
840 if (!Assert(bogusFlags == 0))
841 {
843 return NTFR(TFR.FAIL);
844 }
845
846 // Reset
848 return NTFR(TFR.SUCCESS);
849 }
850
851 //---------------------------------------------------------------------------
852 // Test removing of flags
854 {
856
858
860 {
862 return NTFR(TFR.FAIL);
863 }
864
867
869 {
871 return NTFR(TFR.FAIL);
872 }
873
876
878 {
880 return NTFR(TFR.FAIL);
881 }
882
883 // Reset
885 return NTFR(TFR.SUCCESS);
886 }
887
888 //---------------------------------------------------------------------------
889 // Test adding of flags
891 {
893
895
896 // Return should match resulting flags
898 {
900 return NTFR(TFR.FAIL);
901 }
902
903 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
904 {
906 return NTFR(TFR.FAIL);
907 }
908
909 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
910 {
912 return NTFR(TFR.FAIL);
913 }
914
915 // Reset
917 return NTFR(TFR.SUCCESS);
918 }
919
920 //---------------------------------------------------------------------------
921 // Test module
923 {
924 // File lives in Game, use it while testing
926
927 // This was added at the same time as this API, so check if it works as well
928 string nameOfCurrentModule = Type().GetModule();
929 if (!Assert(nameOfCurrentModule != ""))
930 return NTFR(TFR.FAIL);
931
932 // We know we are in Game, so use it as a test
935 return NTFR(TFR.FAIL);
936
938 return NTFR(TFR.FAIL);
939
940 // Test if setting and getting works
943
945 {
947 return NTFR(TFR.FAIL);
948 }
949
950 // Data to restore
953
954 // Make sure we are only profiling Game and that the data is clean
955 // Only valid for the Get...Per... methods, as they need to be sorted
957
958 // GetTickTime() returns in seconds, so gather the results in seconds too
961
962 // Time to sleeb
963 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
964 float timeSlept = Sleep(0.3);
965 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
967
968 // Restore
970
971 // We called the function, so it must have some time
972 if (!Assert(postTime > 0))
973 {
975
976 if (!wasEnabled)
977 EnProfiler.Enable(false, true);
978
980
981 return NTFR(TFR.FAIL);
982 }
983
984 if (!Assert(diff < 0.00001))
985 {
987
988 if (!wasEnabled)
989 EnProfiler.Enable(false, true);
990
992
993 return NTFR(TFR.FAIL);
994 }
995
996 // Clean the session
998
999 // Something from a different module should not get sorted, so just fire something from a different module
1000 for (int i = 0; i < 1000; ++i)
1001 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1002
1003 // Sort and gather the data and validate if it is correct
1007
1008 Debug.TFLog("Game fncs:", this, "TestModule");
1009
1010 int funcCount = timePerFunc.Count();
1011 for (int j = 0; j < funcCount; ++j)
1012 {
1014 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1015 // We are currently profiling Game, so this Core function shouldn't be present
1016 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1017 {
1019
1020 if (!wasEnabled)
1021 EnProfiler.Enable(false, true);
1022
1024
1025 return NTFR(TFR.FAIL);
1026 }
1027 }
1028
1031
1032 int classCount = timePerClass.Count();
1033 for (int k = 0; k < classCount; ++k)
1034 {
1035 typename type = timePerClass[k].param2;
1037 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1038 {
1040
1041 if (!wasEnabled)
1042 EnProfiler.Enable(false, true);
1043
1045
1046 return NTFR(TFR.FAIL);
1047 }
1048
1049 // Only classes from Game should be present
1050 if (!Assert(classModule == eptModule))
1051 {
1053
1054 if (!wasEnabled)
1055 EnProfiler.Enable(false, true);
1056
1058
1059 return NTFR(TFR.FAIL);
1060 }
1061 }
1062
1063 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1066 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1068
1069 bool found = false;
1070
1071 Debug.TFLog("Core fncs:", this, "TestModule");
1072
1073 funcCount = timePerFunc.Count();
1074 for (int l = 0; l < funcCount; ++l)
1075 {
1077 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1078 // We are currently profiling Core, so this Core function should be present
1079 if (tfpc.param2 == "EnumTools::StringToEnum")
1080 {
1081 found = true;
1082 break;
1083 }
1084 }
1085
1086 Assert(found);
1087
1088 // Test some bogus
1090 EnProfiler.SetModule(-333);
1091 bool success = Assert(EnProfiler.GetModule() == mod);
1092 EnProfiler.SetModule(6003);
1094
1097
1098 if (!wasEnabled)
1099 EnProfiler.Enable(false, true);
1100
1101 return BTFR(success && found);
1102 }
1103
1104 //---------------------------------------------------------------------------
1105 // Test to see if class time data is correct
1107 {
1108 // We should restore this when done
1111
1112 // GetTickTime() returns in seconds, so gather the results in seconds too
1114
1115 // Create the classes
1117
1118 // Time to stress
1119 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1120 float timeStressed = clss.DoEverything();
1121 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1122 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1125
1126 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1127
1128 // Restore
1130 if (!wasEnabled)
1131 EnProfiler.Enable(false, true);
1132
1133 // We called the function, so it must have some time
1134 if (!Assert(postTime > 0))
1135 return NTFR(TFR.FAIL);
1136
1138 return NTFR(TFR.FAIL);
1139
1140 if (!Assert(diff < 0.001))
1141 return NTFR(TFR.FAIL);
1142
1143 return NTFR(TFR.SUCCESS);
1144 }
1145
1146 //---------------------------------------------------------------------------
1147 // Test to see if class count data is correct
1149 {
1150 const int allocAmount = 9;
1151 const int releaseAmount = 6;
1153
1154 // We should restore this when done
1156
1157 // Time to test
1160
1162 for (int i = 0; i < allocAmount; ++i)
1163 instanceArr.Insert(new EPTHelperClass());
1164
1165 for (int j = 0; j < releaseAmount; ++j)
1166 delete instanceArr[j];
1167
1168 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1169 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1170
1173
1174 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1175
1176 // Restore
1177 if (!wasEnabled)
1178 EnProfiler.Enable(false, true);
1179
1180 // Time to check
1181 if (!Assert(alloced == allocAmount))
1182 return NTFR(TFR.FAIL);
1183
1185 return NTFR(TFR.FAIL);
1186
1187 return NTFR(TFR.SUCCESS);
1188 }
1189
1190 //---------------------------------------------------------------------------
1191 // Test to see if func time data is correct
1193 {
1194 // We should restore this when done
1197
1198 // GetTickTime() returns in seconds, so gather the results in seconds too
1200
1201 // Time to stress
1202 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1203 float timeStressed = StringFormat();
1204 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1207
1208 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1209 float timeStressed2 = StringConcat();
1210 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1213
1214 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1215
1216 // Restore
1218 if (!wasEnabled)
1219 EnProfiler.Enable(false, true);
1220
1221 // We called the function, so it must have some time
1222 if (!Assert(postTime > 0))
1223 return NTFR(TFR.FAIL);
1224
1225 if (!Assert(diff < 0.001))
1226 return NTFR(TFR.FAIL);
1227
1228 if (!Assert(postTime2 > 0))
1229 return NTFR(TFR.FAIL);
1230
1231 if (!Assert(diff2 < 0.001))
1232 return NTFR(TFR.FAIL);
1233
1234 // I know that string.Format is faster than additive concatenation
1236 return NTFR(TFR.FAIL);
1237
1238 return NTFR(TFR.SUCCESS);
1239 }
1240
1241 //---------------------------------------------------------------------------
1242 // Test to see if func count data is correct
1244 {
1245 // We should restore this when done
1247
1248 // Time to count
1249
1250 // - CallFunction
1251 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1252 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1253 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1254
1256
1257 // - CallFunctionParams
1258 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1259 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1260 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1261
1263
1264 // - Regular call
1265 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1267 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1268
1270
1271 // - Call
1272 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1273 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1274 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1275
1277
1278 // - Garbage
1279 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1280
1281 // - Static
1282 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1284 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1285
1287
1288 // - Global
1289 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1290 GetDayZGame();
1291 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1292
1294
1295 // - Global proto
1296 // Not tracked, so don't need to compare before and after, should always be 0
1297 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1298 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1299
1300 // - Static proto
1301 // Not tracked, so don't need to compare before and after, should always be 0
1302 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1303
1304 // - proto
1305 // Not tracked, so don't need to compare before and after, should always be 0
1306 GetGame().GetHostName();
1307 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1308
1309 // - proto native
1310 // Not tracked, so don't need to compare before and after, should always be 0
1311 GetGame().IsServer();
1312 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1313
1314 // - static proto native
1315 // Not tracked, so don't need to compare before and after, should always be 0
1317 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1318
1319 // Restore
1320 if (!wasEnabled)
1321 EnProfiler.Enable(false, true);
1322
1323 // Do the checks
1324
1325 // - CallFunction
1326 if (!Assert(callCountCF == 1))
1327 return NTFR(TFR.FAIL);
1328
1329 // - CallFunctionParams
1330 if (!Assert(callCountCFP == 1))
1331 return NTFR(TFR.FAIL);
1332
1333 // - Regular call
1334 if (!Assert(callCountRG == 1))
1335 return NTFR(TFR.FAIL);
1336
1337 // - Call
1338 if (!Assert(callCountC == 1))
1339 return NTFR(TFR.FAIL);
1340
1341 // - Garbage
1342 if (!Assert(callCountNon == -1))
1343 return NTFR(TFR.FAIL);
1344
1345 // - Static
1346 if (!Assert(callCountS == 1))
1347 return NTFR(TFR.FAIL);
1348
1349 // - Global
1350 if (!Assert(callCountG == 1))
1351 return NTFR(TFR.FAIL);
1352
1353 // - Global proto
1354 if (!Assert(callCountGP == 0))
1355 return NTFR(TFR.FAIL);
1356
1357 // - Static proto
1358 if (!Assert(callCountSP == 0))
1359 return NTFR(TFR.FAIL);
1360
1361 // - proto
1362 if (!Assert(callCountP == 0))
1363 return NTFR(TFR.FAIL);
1364
1365 // - proto native
1366 if (!Assert(callCountPN == 0))
1367 return NTFR(TFR.FAIL);
1368
1369 // - static proto native
1370 if (!Assert(callCountSPN == 0))
1371 return NTFR(TFR.FAIL);
1372
1373 return NTFR(TFR.SUCCESS);
1374 }
1375
1376 //---------------------------------------------------------------------------
1377 // Helpers
1378 //---------------------------------------------------------------------------
1379 // Snore
1380 float Sleep(float timeS)
1381 {
1382 float startTime = GetGame().GetTickTime();
1383 while (GetGame().GetTickTime() - startTime < timeS)
1384 {
1385 // Zzz
1386 }
1387
1388 return GetGame().GetTickTime() - startTime;
1389 }
1390
1391 //---------------------------------------------------------------------------
1392 // Example stress method
1393 float StringFormat()
1394 {
1395 float startTime = GetGame().GetTickTime();
1396
1397 for (int i = 0; i < 1000; ++i)
1398 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1399
1400 return GetGame().GetTickTime() - startTime;
1401 }
1402
1403 //---------------------------------------------------------------------------
1404 // Example stress method 2
1405 float StringConcat()
1406 {
1407 float startTime = GetGame().GetTickTime();
1408
1409 for (int i = 0; i < 1000; ++i)
1410 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1411
1412 return GetGame().GetTickTime() - startTime;
1413 }
1414
1415 //---------------------------------------------------------------------------
1416 // To make sure it is only ever called in that test
1418 {
1419 int dummy = 3;
1420 }
1421
1422 //---------------------------------------------------------------------------
1423 // To make sure it is only ever called in that test
1424 static void TestFuncCountDataHelperStatic()
1425 {
1426 int dummy = 3;
1427 }
1428}
1429
1430class EPTHelperClass
1431{
1432 float Sleep2(float timeS)
1433 {
1434 float startTime = GetGame().GetTickTime();
1435 while (GetGame().GetTickTime() - startTime < timeS)
1436 {
1437 // Zzz
1438 }
1439
1440 return GetGame().GetTickTime() - startTime;
1441 }
1442 float SleepAgain(float timeS)
1443 {
1444 float startTime = GetGame().GetTickTime();
1445 while (GetGame().GetTickTime() - startTime < timeS)
1446 {
1447 // Zzz
1448 }
1449
1450 return GetGame().GetTickTime() - startTime;
1451 }
1452
1453 float DoEverything()
1454 {
1455 float startTime = GetGame().GetTickTime();
1456
1457 Sleep2(3);
1458 SleepAgain(3);
1459
1460 return GetGame().GetTickTime() - startTime;
1461 }
1462}

◆ TestTogglingImmediate()

TFResult Sleep2::TestTogglingImmediate ( )

Definition at line 749 of file EnProfilerTests.c.

749 : TestFramework
750{
753
754 //---------------------------------------------------------------------------
755 // Ctor - Decides the tests to run
756 //---------------------------------------------------------------------------
757 void EnProfilerTests()
758 {
760
761 AddInitTest("TestToggling");
762 AddInitTest("TestTogglingImmediate");
763 AddInitTest("TestSetFlags");
764 AddInitTest("TestClearFlags");
765 AddInitTest("TestAddFlags");
766 AddInitTest("TestModule");
767 AddInitTest("TestClassTimeData");
768 AddInitTest("TestClassCountData");
769 AddInitTest("TestFuncTimeData");
770 AddInitTest("TestFuncCountData");
771 }
772
773 //---------------------------------------------------------------------------
774 // Dtor
775 //---------------------------------------------------------------------------
776 void ~EnProfilerTests()
777 {
779 }
780
781 //---------------------------------------------------------------------------
782 // Tests
783 //---------------------------------------------------------------------------
784 // Test toggling state
786 {
790 {
793 }
794
795 return NTFR(TFR.FAIL);
796 }
797
798 //---------------------------------------------------------------------------
799 // Test toggling immediate state
801 {
805 {
808 }
809
810 return NTFR(TFR.FAIL);
811 }
812
813 //---------------------------------------------------------------------------
814 // Test SetFlags/GetFlags
816 {
818
819 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
820 {
822 EnProfiler.SetFlags(flags);
823
824 if (!Assert(EnProfiler.GetFlags() == flags))
825 {
827 return NTFR(TFR.FAIL);
828 }
829
830 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
831 {
833 EnProfiler.SetFlags(flags);
834
835 if (!Assert(EnProfiler.GetFlags() == flags))
836 {
838 return NTFR(TFR.FAIL);
839 }
840 }
841 }
842
843 // Let's test some bogus
844 EnProfiler.SetFlags(-333);
847 if (!Assert(bogusFlags == 0))
848 {
850 return NTFR(TFR.FAIL);
851 }
852
855 if (!Assert(bogusFlags == 0))
856 {
858 return NTFR(TFR.FAIL);
859 }
860
861 // Reset
863 return NTFR(TFR.SUCCESS);
864 }
865
866 //---------------------------------------------------------------------------
867 // Test removing of flags
869 {
871
873
875 {
877 return NTFR(TFR.FAIL);
878 }
879
882
884 {
886 return NTFR(TFR.FAIL);
887 }
888
891
893 {
895 return NTFR(TFR.FAIL);
896 }
897
898 // Reset
900 return NTFR(TFR.SUCCESS);
901 }
902
903 //---------------------------------------------------------------------------
904 // Test adding of flags
906 {
908
910
911 // Return should match resulting flags
913 {
915 return NTFR(TFR.FAIL);
916 }
917
918 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
919 {
921 return NTFR(TFR.FAIL);
922 }
923
924 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
925 {
927 return NTFR(TFR.FAIL);
928 }
929
930 // Reset
932 return NTFR(TFR.SUCCESS);
933 }
934
935 //---------------------------------------------------------------------------
936 // Test module
938 {
939 // File lives in Game, use it while testing
941
942 // This was added at the same time as this API, so check if it works as well
943 string nameOfCurrentModule = Type().GetModule();
944 if (!Assert(nameOfCurrentModule != ""))
945 return NTFR(TFR.FAIL);
946
947 // We know we are in Game, so use it as a test
950 return NTFR(TFR.FAIL);
951
953 return NTFR(TFR.FAIL);
954
955 // Test if setting and getting works
958
960 {
962 return NTFR(TFR.FAIL);
963 }
964
965 // Data to restore
968
969 // Make sure we are only profiling Game and that the data is clean
970 // Only valid for the Get...Per... methods, as they need to be sorted
972
973 // GetTickTime() returns in seconds, so gather the results in seconds too
976
977 // Time to sleeb
978 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
979 float timeSlept = Sleep(0.3);
980 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
982
983 // Restore
985
986 // We called the function, so it must have some time
987 if (!Assert(postTime > 0))
988 {
990
991 if (!wasEnabled)
992 EnProfiler.Enable(false, true);
993
995
996 return NTFR(TFR.FAIL);
997 }
998
999 if (!Assert(diff < 0.00001))
1000 {
1002
1003 if (!wasEnabled)
1004 EnProfiler.Enable(false, true);
1005
1007
1008 return NTFR(TFR.FAIL);
1009 }
1010
1011 // Clean the session
1013
1014 // Something from a different module should not get sorted, so just fire something from a different module
1015 for (int i = 0; i < 1000; ++i)
1016 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1017
1018 // Sort and gather the data and validate if it is correct
1022
1023 Debug.TFLog("Game fncs:", this, "TestModule");
1024
1025 int funcCount = timePerFunc.Count();
1026 for (int j = 0; j < funcCount; ++j)
1027 {
1029 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1030 // We are currently profiling Game, so this Core function shouldn't be present
1031 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1032 {
1034
1035 if (!wasEnabled)
1036 EnProfiler.Enable(false, true);
1037
1039
1040 return NTFR(TFR.FAIL);
1041 }
1042 }
1043
1046
1047 int classCount = timePerClass.Count();
1048 for (int k = 0; k < classCount; ++k)
1049 {
1050 typename type = timePerClass[k].param2;
1052 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1053 {
1055
1056 if (!wasEnabled)
1057 EnProfiler.Enable(false, true);
1058
1060
1061 return NTFR(TFR.FAIL);
1062 }
1063
1064 // Only classes from Game should be present
1065 if (!Assert(classModule == eptModule))
1066 {
1068
1069 if (!wasEnabled)
1070 EnProfiler.Enable(false, true);
1071
1073
1074 return NTFR(TFR.FAIL);
1075 }
1076 }
1077
1078 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1081 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1083
1084 bool found = false;
1085
1086 Debug.TFLog("Core fncs:", this, "TestModule");
1087
1088 funcCount = timePerFunc.Count();
1089 for (int l = 0; l < funcCount; ++l)
1090 {
1092 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1093 // We are currently profiling Core, so this Core function should be present
1094 if (tfpc.param2 == "EnumTools::StringToEnum")
1095 {
1096 found = true;
1097 break;
1098 }
1099 }
1100
1101 Assert(found);
1102
1103 // Test some bogus
1105 EnProfiler.SetModule(-333);
1106 bool success = Assert(EnProfiler.GetModule() == mod);
1107 EnProfiler.SetModule(6003);
1109
1112
1113 if (!wasEnabled)
1114 EnProfiler.Enable(false, true);
1115
1116 return BTFR(success && found);
1117 }
1118
1119 //---------------------------------------------------------------------------
1120 // Test to see if class time data is correct
1122 {
1123 // We should restore this when done
1126
1127 // GetTickTime() returns in seconds, so gather the results in seconds too
1129
1130 // Create the classes
1132
1133 // Time to stress
1134 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1135 float timeStressed = clss.DoEverything();
1136 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1137 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1140
1141 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1142
1143 // Restore
1145 if (!wasEnabled)
1146 EnProfiler.Enable(false, true);
1147
1148 // We called the function, so it must have some time
1149 if (!Assert(postTime > 0))
1150 return NTFR(TFR.FAIL);
1151
1153 return NTFR(TFR.FAIL);
1154
1155 if (!Assert(diff < 0.001))
1156 return NTFR(TFR.FAIL);
1157
1158 return NTFR(TFR.SUCCESS);
1159 }
1160
1161 //---------------------------------------------------------------------------
1162 // Test to see if class count data is correct
1164 {
1165 const int allocAmount = 9;
1166 const int releaseAmount = 6;
1168
1169 // We should restore this when done
1171
1172 // Time to test
1175
1177 for (int i = 0; i < allocAmount; ++i)
1178 instanceArr.Insert(new EPTHelperClass());
1179
1180 for (int j = 0; j < releaseAmount; ++j)
1181 delete instanceArr[j];
1182
1183 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1184 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1185
1188
1189 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1190
1191 // Restore
1192 if (!wasEnabled)
1193 EnProfiler.Enable(false, true);
1194
1195 // Time to check
1196 if (!Assert(alloced == allocAmount))
1197 return NTFR(TFR.FAIL);
1198
1200 return NTFR(TFR.FAIL);
1201
1202 return NTFR(TFR.SUCCESS);
1203 }
1204
1205 //---------------------------------------------------------------------------
1206 // Test to see if func time data is correct
1208 {
1209 // We should restore this when done
1212
1213 // GetTickTime() returns in seconds, so gather the results in seconds too
1215
1216 // Time to stress
1217 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1218 float timeStressed = StringFormat();
1219 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1222
1223 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1224 float timeStressed2 = StringConcat();
1225 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1228
1229 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1230
1231 // Restore
1233 if (!wasEnabled)
1234 EnProfiler.Enable(false, true);
1235
1236 // We called the function, so it must have some time
1237 if (!Assert(postTime > 0))
1238 return NTFR(TFR.FAIL);
1239
1240 if (!Assert(diff < 0.001))
1241 return NTFR(TFR.FAIL);
1242
1243 if (!Assert(postTime2 > 0))
1244 return NTFR(TFR.FAIL);
1245
1246 if (!Assert(diff2 < 0.001))
1247 return NTFR(TFR.FAIL);
1248
1249 // I know that string.Format is faster than additive concatenation
1251 return NTFR(TFR.FAIL);
1252
1253 return NTFR(TFR.SUCCESS);
1254 }
1255
1256 //---------------------------------------------------------------------------
1257 // Test to see if func count data is correct
1259 {
1260 // We should restore this when done
1262
1263 // Time to count
1264
1265 // - CallFunction
1266 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1267 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1268 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1269
1271
1272 // - CallFunctionParams
1273 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1274 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1275 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1276
1278
1279 // - Regular call
1280 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1282 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1283
1285
1286 // - Call
1287 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1288 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1289 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1290
1292
1293 // - Garbage
1294 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1295
1296 // - Static
1297 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1299 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1300
1302
1303 // - Global
1304 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1305 GetDayZGame();
1306 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1307
1309
1310 // - Global proto
1311 // Not tracked, so don't need to compare before and after, should always be 0
1312 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1313 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1314
1315 // - Static proto
1316 // Not tracked, so don't need to compare before and after, should always be 0
1317 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1318
1319 // - proto
1320 // Not tracked, so don't need to compare before and after, should always be 0
1321 GetGame().GetHostName();
1322 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1323
1324 // - proto native
1325 // Not tracked, so don't need to compare before and after, should always be 0
1326 GetGame().IsServer();
1327 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1328
1329 // - static proto native
1330 // Not tracked, so don't need to compare before and after, should always be 0
1332 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1333
1334 // Restore
1335 if (!wasEnabled)
1336 EnProfiler.Enable(false, true);
1337
1338 // Do the checks
1339
1340 // - CallFunction
1341 if (!Assert(callCountCF == 1))
1342 return NTFR(TFR.FAIL);
1343
1344 // - CallFunctionParams
1345 if (!Assert(callCountCFP == 1))
1346 return NTFR(TFR.FAIL);
1347
1348 // - Regular call
1349 if (!Assert(callCountRG == 1))
1350 return NTFR(TFR.FAIL);
1351
1352 // - Call
1353 if (!Assert(callCountC == 1))
1354 return NTFR(TFR.FAIL);
1355
1356 // - Garbage
1357 if (!Assert(callCountNon == -1))
1358 return NTFR(TFR.FAIL);
1359
1360 // - Static
1361 if (!Assert(callCountS == 1))
1362 return NTFR(TFR.FAIL);
1363
1364 // - Global
1365 if (!Assert(callCountG == 1))
1366 return NTFR(TFR.FAIL);
1367
1368 // - Global proto
1369 if (!Assert(callCountGP == 0))
1370 return NTFR(TFR.FAIL);
1371
1372 // - Static proto
1373 if (!Assert(callCountSP == 0))
1374 return NTFR(TFR.FAIL);
1375
1376 // - proto
1377 if (!Assert(callCountP == 0))
1378 return NTFR(TFR.FAIL);
1379
1380 // - proto native
1381 if (!Assert(callCountPN == 0))
1382 return NTFR(TFR.FAIL);
1383
1384 // - static proto native
1385 if (!Assert(callCountSPN == 0))
1386 return NTFR(TFR.FAIL);
1387
1388 return NTFR(TFR.SUCCESS);
1389 }
1390
1391 //---------------------------------------------------------------------------
1392 // Helpers
1393 //---------------------------------------------------------------------------
1394 // Snore
1395 float Sleep(float timeS)
1396 {
1397 float startTime = GetGame().GetTickTime();
1398 while (GetGame().GetTickTime() - startTime < timeS)
1399 {
1400 // Zzz
1401 }
1402
1403 return GetGame().GetTickTime() - startTime;
1404 }
1405
1406 //---------------------------------------------------------------------------
1407 // Example stress method
1408 float StringFormat()
1409 {
1410 float startTime = GetGame().GetTickTime();
1411
1412 for (int i = 0; i < 1000; ++i)
1413 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1414
1415 return GetGame().GetTickTime() - startTime;
1416 }
1417
1418 //---------------------------------------------------------------------------
1419 // Example stress method 2
1420 float StringConcat()
1421 {
1422 float startTime = GetGame().GetTickTime();
1423
1424 for (int i = 0; i < 1000; ++i)
1425 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1426
1427 return GetGame().GetTickTime() - startTime;
1428 }
1429
1430 //---------------------------------------------------------------------------
1431 // To make sure it is only ever called in that test
1433 {
1434 int dummy = 3;
1435 }
1436
1437 //---------------------------------------------------------------------------
1438 // To make sure it is only ever called in that test
1439 static void TestFuncCountDataHelperStatic()
1440 {
1441 int dummy = 3;
1442 }
1443}
1444
1445class EPTHelperClass
1446{
1447 float Sleep2(float timeS)
1448 {
1449 float startTime = GetGame().GetTickTime();
1450 while (GetGame().GetTickTime() - startTime < timeS)
1451 {
1452 // Zzz
1453 }
1454
1455 return GetGame().GetTickTime() - startTime;
1456 }
1457 float SleepAgain(float timeS)
1458 {
1459 float startTime = GetGame().GetTickTime();
1460 while (GetGame().GetTickTime() - startTime < timeS)
1461 {
1462 // Zzz
1463 }
1464
1465 return GetGame().GetTickTime() - startTime;
1466 }
1467
1468 float DoEverything()
1469 {
1470 float startTime = GetGame().GetTickTime();
1471
1472 Sleep2(3);
1473 SleepAgain(3);
1474
1475 return GetGame().GetTickTime() - startTime;
1476 }
1477}

◆ ~EnProfilerTests()

void Sleep2::~EnProfilerTests ( )

Definition at line 725 of file EnProfilerTests.c.

Variable Documentation

◆ m_bWasProfilerEnabled

bool m_bWasProfilerEnabled

Remember this, so we can restore the previous state before the test!

Definition at line 701 of file EnProfilerTests.c.