DayZ 1.26
DayZ Explorer by KGB
Загрузка...
Поиск...
Не найдено
Файл EnProfilerTests.c

См. исходные тексты.

Структуры данных

class  EnProfilerTests
 

Функции

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 ()
 

Переменные

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

Функции

◆ DoEverything()

float DoEverything ( )
785 {
786 float startTime = GetGame().GetTickTime();
787
788 Sleep2(3);
789 SleepAgain(3);
790
791 return GetGame().GetTickTime() - startTime;
792 }
float SleepAgain(float timeS)
Definition EnProfilerTests.c:773
EnProfilerTests TestFramework Sleep2(float timeS)
Definition EnProfilerTests.c:763
Definition EntityAI.c:95
proto native CGame GetGame()

Перекрестные ссылки GetGame() и Sleep2().

◆ EnProfilerTests()

void EnProfilerTests ( )
774 {
775 float startTime = GetGame().GetTickTime();
776 while (GetGame().GetTickTime() - startTime < timeS)
777 {
778 // Zzz
779 }
780
781 return GetGame().GetTickTime() - startTime;
782 }
783
784 float DoEverything()
float DoEverything()
Definition EnProfilerTests.c:784

◆ Sleep()

float Sleep ( float timeS)
1468 : TestFramework
1469{
1472
1473 //---------------------------------------------------------------------------
1474 // Ctor - Decides the tests to run
1475 //---------------------------------------------------------------------------
1476 void EnProfilerTests()
1477 {
1479
1480 AddInitTest("TestToggling");
1481 AddInitTest("TestTogglingImmediate");
1482 AddInitTest("TestSetFlags");
1483 AddInitTest("TestClearFlags");
1484 AddInitTest("TestAddFlags");
1485 AddInitTest("TestModule");
1486 AddInitTest("TestClassTimeData");
1487 AddInitTest("TestClassCountData");
1488 AddInitTest("TestFuncTimeData");
1489 AddInitTest("TestFuncCountData");
1490 }
1491
1492 //---------------------------------------------------------------------------
1493 // Dtor
1494 //---------------------------------------------------------------------------
1495 void ~EnProfilerTests()
1496 {
1498 }
1499
1500 //---------------------------------------------------------------------------
1501 // Tests
1502 //---------------------------------------------------------------------------
1503 // Test toggling state
1505 {
1509 {
1512 }
1513
1514 return NTFR(TFR.FAIL);
1515 }
1516
1517 //---------------------------------------------------------------------------
1518 // Test toggling immediate state
1520 {
1524 {
1527 }
1528
1529 return NTFR(TFR.FAIL);
1530 }
1531
1532 //---------------------------------------------------------------------------
1533 // Test SetFlags/GetFlags
1535 {
1537
1538 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1539 {
1541 EnProfiler.SetFlags(flags);
1542
1543 if (!Assert(EnProfiler.GetFlags() == flags))
1544 {
1546 return NTFR(TFR.FAIL);
1547 }
1548
1549 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1550 {
1552 EnProfiler.SetFlags(flags);
1553
1554 if (!Assert(EnProfiler.GetFlags() == flags))
1555 {
1557 return NTFR(TFR.FAIL);
1558 }
1559 }
1560 }
1561
1562 // Let's test some bogus
1563 EnProfiler.SetFlags(-333);
1566 if (!Assert(bogusFlags == 0))
1567 {
1569 return NTFR(TFR.FAIL);
1570 }
1571
1574 if (!Assert(bogusFlags == 0))
1575 {
1577 return NTFR(TFR.FAIL);
1578 }
1579
1580 // Reset
1582 return NTFR(TFR.SUCCESS);
1583 }
1584
1585 //---------------------------------------------------------------------------
1586 // Test removing of flags
1588 {
1590
1592
1594 {
1596 return NTFR(TFR.FAIL);
1597 }
1598
1601
1602 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1603 {
1605 return NTFR(TFR.FAIL);
1606 }
1607
1610
1611 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1612 {
1614 return NTFR(TFR.FAIL);
1615 }
1616
1617 // Reset
1619 return NTFR(TFR.SUCCESS);
1620 }
1621
1622 //---------------------------------------------------------------------------
1623 // Test adding of flags
1625 {
1627
1629
1630 // Return should match resulting flags
1632 {
1634 return NTFR(TFR.FAIL);
1635 }
1636
1637 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1638 {
1640 return NTFR(TFR.FAIL);
1641 }
1642
1643 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1644 {
1646 return NTFR(TFR.FAIL);
1647 }
1648
1649 // Reset
1651 return NTFR(TFR.SUCCESS);
1652 }
1653
1654 //---------------------------------------------------------------------------
1655 // Test module
1657 {
1658 // File lives in Game, use it while testing
1660
1661 // This was added at the same time as this API, so check if it works as well
1662 string nameOfCurrentModule = Type().GetModule();
1663 if (!Assert(nameOfCurrentModule != ""))
1664 {
1665 return NTFR(TFR.FAIL);
1666 }
1667
1668 // We know we are in Game, so use it as a test
1671 {
1672 return NTFR(TFR.FAIL);
1673 }
1674
1676 {
1677 return NTFR(TFR.FAIL);
1678 }
1679
1680 // Test if setting and getting works
1683
1685 {
1687 return NTFR(TFR.FAIL);
1688 }
1689
1690 // Data to restore
1693
1694 // Make sure we are only profiling Game and that the data is clean
1695 // Only valid for the Get...Per... methods, as they need to be sorted
1697
1698 // GetTickTime() returns in seconds, so gather the results in seconds too
1701
1702 // Time to sleeb
1703 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1704 float timeSlept = Sleep(0.3);
1705 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1706 float diff = postTime - previousTime - timeSlept;
1707
1708 // Restore
1710
1711 // We called the function, so it must have some time
1712 if (!Assert(postTime > 0))
1713 {
1715
1716 if (!wasEnabled)
1717 EnProfiler.Enable(false, true);
1718
1720
1721 return NTFR(TFR.FAIL);
1722 }
1723
1724 if (!Assert(diff < 0.00001))
1725 {
1727
1728 if (!wasEnabled)
1729 EnProfiler.Enable(false, true);
1730
1732
1733 return NTFR(TFR.FAIL);
1734 }
1735
1736 // Clean the session
1738
1739 // Something from a different module should not get sorted, so just fire something from a different module
1740 for (int i = 0; i < 1000; ++i)
1741 {
1742 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1743 }
1744
1745 // Sort and gather the data and validate if it is correct
1749
1750 Debug.TFLog("Game fncs:", this, "TestModule");
1751
1752 int funcCount = timePerFunc.Count();
1753 for (int j = 0; j < funcCount; ++j)
1754 {
1756 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1757 // We are currently profiling Game, so this Core function shouldn't be present
1758 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1759 {
1761
1762 if (!wasEnabled)
1763 EnProfiler.Enable(false, true);
1764
1766
1767 return NTFR(TFR.FAIL);
1768 }
1769 }
1770
1773
1774 int classCount = timePerClass.Count();
1775 for (int k = 0; k < classCount; ++k)
1776 {
1777 typename type = timePerClass[k].param2;
1779 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1780 {
1782
1783 if (!wasEnabled)
1784 EnProfiler.Enable(false, true);
1785
1787
1788 return NTFR(TFR.FAIL);
1789 }
1790
1791 // Only classes from Game should be present
1792 if (!Assert(classModule == eptModule))
1793 {
1795
1796 if (!wasEnabled)
1797 EnProfiler.Enable(false, true);
1798
1800
1801 return NTFR(TFR.FAIL);
1802 }
1803 }
1804
1805 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1808 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1810
1811 bool found = false;
1812
1813 Debug.TFLog("Core fncs:", this, "TestModule");
1814
1815 funcCount = timePerFunc.Count();
1816 for (int l = 0; l < funcCount; ++l)
1817 {
1819 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1820 // We are currently profiling Core, so this Core function should be present
1821 if (tfpc.param2 == "EnumTools::StringToEnum")
1822 {
1823 found = true;
1824 break;
1825 }
1826 }
1827
1828 Assert(found);
1829
1830 // Test some bogus
1832 EnProfiler.SetModule(-333);
1833 bool success = Assert(EnProfiler.GetModule() == mod);
1834 EnProfiler.SetModule(6003);
1836
1839
1840 if (!wasEnabled)
1841 EnProfiler.Enable(false, true);
1842
1843 return BTFR(success && found);
1844 }
1845
1846 //---------------------------------------------------------------------------
1847 // Test to see if class time data is correct
1849 {
1850 // We should restore this when done
1853
1854 // GetTickTime() returns in seconds, so gather the results in seconds too
1856
1857 // Create the classes
1859
1860 // Time to stress
1861 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1862 float timeStressed = clss.DoEverything();
1863 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1864 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1867
1868 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
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 {
1878 return NTFR(TFR.FAIL);
1879 }
1880
1882 {
1883 return NTFR(TFR.FAIL);
1884 }
1885
1886 if (!Assert(diff < 0.001))
1887 {
1888 return NTFR(TFR.FAIL);
1889 }
1890
1891 return NTFR(TFR.SUCCESS);
1892 }
1893
1894 //---------------------------------------------------------------------------
1895 // Test to see if class count data is correct
1897 {
1898 const int allocAmount = 9;
1899 const int releaseAmount = 6;
1901
1902 // We should restore this when done
1904
1905 // Time to test
1908
1910 for (int i = 0; i < allocAmount; ++i)
1911 {
1912 instanceArr.Insert(new EPTHelperClass());
1913 }
1914
1915 for (int j = 0; j < releaseAmount; ++j)
1916 {
1917 delete instanceArr[j];
1918 }
1919
1920 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1921 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1922
1925
1926 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1927
1928 // Restore
1929 if (!wasEnabled)
1930 EnProfiler.Enable(false, true);
1931
1932 // Time to check
1933 if (!Assert(alloced == allocAmount))
1934 {
1935 return NTFR(TFR.FAIL);
1936 }
1937
1939 {
1940 return NTFR(TFR.FAIL);
1941 }
1942
1943 return NTFR(TFR.SUCCESS);
1944 }
1945
1946 //---------------------------------------------------------------------------
1947 // Test to see if func time data is correct
1949 {
1950 // We should restore this when done
1953
1954 // GetTickTime() returns in seconds, so gather the results in seconds too
1956
1957 // Time to stress
1958 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1959 float timeStressed = StringFormat();
1960 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1963
1964 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1965 float timeStressed2 = StringConcat();
1966 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1969
1970 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1971
1972 // Restore
1974 if (!wasEnabled)
1975 {
1976 EnProfiler.Enable(false, true);
1977 }
1978
1979 // We called the function, so it must have some time
1980 if (!Assert(postTime > 0))
1981 {
1982 return NTFR(TFR.FAIL);
1983 }
1984
1985 if (!Assert(diff < 0.001))
1986 {
1987 return NTFR(TFR.FAIL);
1988 }
1989
1990 if (!Assert(postTime2 > 0))
1991 {
1992 return NTFR(TFR.FAIL);
1993 }
1994
1995 if (!Assert(diff2 < 0.001))
1996 {
1997 return NTFR(TFR.FAIL);
1998 }
1999
2000 // I know that string.Format is faster than additive concatenation
2002 {
2003 return NTFR(TFR.FAIL);
2004 }
2005
2006 return NTFR(TFR.SUCCESS);
2007 }
2008
2009 //---------------------------------------------------------------------------
2010 // Test to see if func count data is correct
2012 {
2013 // We should restore this when done
2015
2016 // Time to count
2017
2018 // - CallFunction
2019 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2020 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
2021 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2022
2024
2025 // - CallFunctionParams
2026 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2027 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
2028 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2029
2031
2032 // - Regular call
2033 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2035 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2036
2038
2039 // - Call
2040 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2041 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
2042 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2043
2045
2046 // - Garbage
2047 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
2048
2049 // - Static
2050 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
2052 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
2053
2055
2056 // - Global
2057 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
2058 GetDayZGame();
2059 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
2060
2062
2063 // - Global proto
2064 // Not tracked, so don't need to compare before and after, should always be 0
2065 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
2066 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
2067
2068 // - Static proto
2069 // Not tracked, so don't need to compare before and after, should always be 0
2070 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
2071
2072 // - proto
2073 // Not tracked, so don't need to compare before and after, should always be 0
2074 GetGame().GetHostName();
2075 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
2076
2077 // - proto native
2078 // Not tracked, so don't need to compare before and after, should always be 0
2079 GetGame().IsServer();
2080 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
2081
2082 // - static proto native
2083 // Not tracked, so don't need to compare before and after, should always be 0
2085 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
2086
2087 // Restore
2088 if (!wasEnabled)
2089 {
2090 EnProfiler.Enable(false, true);
2091 }
2092
2093 // Do the checks
2094
2095 // - CallFunction
2096 if (!Assert(callCountCF == 1))
2097 {
2098 return NTFR(TFR.FAIL);
2099 }
2100
2101 // - CallFunctionParams
2102 if (!Assert(callCountCFP == 1))
2103 {
2104 return NTFR(TFR.FAIL);
2105 }
2106
2107 // - Regular call
2108 if (!Assert(callCountRG == 1))
2109 {
2110 return NTFR(TFR.FAIL);
2111 }
2112
2113 // - Call
2114 if (!Assert(callCountC == 1))
2115 {
2116 return NTFR(TFR.FAIL);
2117 }
2118
2119 // - Garbage
2120 if (!Assert(callCountNon == -1))
2121 {
2122 return NTFR(TFR.FAIL);
2123 }
2124
2125 // - Static
2126 if (!Assert(callCountS == 1))
2127 {
2128 return NTFR(TFR.FAIL);
2129 }
2130
2131 // - Global
2132 if (!Assert(callCountG == 1))
2133 {
2134 return NTFR(TFR.FAIL);
2135 }
2136
2137 // - Global proto
2138 if (!Assert(callCountGP == 0))
2139 {
2140 return NTFR(TFR.FAIL);
2141 }
2142
2143 // - Static proto
2144 if (!Assert(callCountSP == 0))
2145 {
2146 return NTFR(TFR.FAIL);
2147 }
2148
2149 // - proto
2150 if (!Assert(callCountP == 0))
2151 {
2152 return NTFR(TFR.FAIL);
2153 }
2154
2155 // - proto native
2156 if (!Assert(callCountPN == 0))
2157 {
2158 return NTFR(TFR.FAIL);
2159 }
2160
2161 // - static proto native
2162 if (!Assert(callCountSPN == 0))
2163 {
2164 return NTFR(TFR.FAIL);
2165 }
2166
2167 return NTFR(TFR.SUCCESS);
2168 }
2169
2170 //---------------------------------------------------------------------------
2171 // Helpers
2172 //---------------------------------------------------------------------------
2173 // Snore
2174 float Sleep(float timeS)
2175 {
2176 float startTime = GetGame().GetTickTime();
2177 while (GetGame().GetTickTime() - startTime < timeS)
2178 {
2179 // Zzz
2180 }
2181
2182 return GetGame().GetTickTime() - startTime;
2183 }
2184
2185 //---------------------------------------------------------------------------
2186 // Example stress method
2187 float StringFormat()
2188 {
2189 float startTime = GetGame().GetTickTime();
2190
2191 for (int i = 0; i < 1000; ++i)
2192 {
2193 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
2194 }
2195
2196 return GetGame().GetTickTime() - startTime;
2197 }
2198
2199 //---------------------------------------------------------------------------
2200 // Example stress method 2
2201 float StringConcat()
2202 {
2203 float startTime = GetGame().GetTickTime();
2204
2205 for (int i = 0; i < 1000; ++i)
2206 {
2207 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
2208 }
2209
2210 return GetGame().GetTickTime() - startTime;
2211 }
2212
2213 //---------------------------------------------------------------------------
2214 // To make sure it is only ever called in that test
2216 {
2217 int dummy = 3;
2218 }
2219
2220 //---------------------------------------------------------------------------
2221 // To make sure it is only ever called in that test
2222 static void TestFuncCountDataHelperStatic()
2223 {
2224 int dummy = 3;
2225 }
2226}
2227
2228class EPTHelperClass
2229{
2230 float Sleep2(float timeS)
2231 {
2232 float startTime = GetGame().GetTickTime();
2233 while (GetGame().GetTickTime() - startTime < timeS)
2234 {
2235 // Zzz
2236 }
2237
2238 return GetGame().GetTickTime() - startTime;
2239 }
2240 float SleepAgain(float timeS)
2241 {
2242 float startTime = GetGame().GetTickTime();
2243 while (GetGame().GetTickTime() - startTime < timeS)
2244 {
2245 // Zzz
2246 }
2247
2248 return GetGame().GetTickTime() - startTime;
2249 }
2250
2251 float DoEverything()
2252 {
2253 float startTime = GetGame().GetTickTime();
2254
2255 Sleep2(3);
2256 SleepAgain(3);
2257
2258 return GetGame().GetTickTime() - startTime;
2259 }
2260}
DayZGame GetDayZGame()
Definition DayZGame.c:3817
void ~EnProfilerTests()
Definition EnProfilerTests.c:789
TFResult TestModule()
Definition EnProfilerTests.c:950
TFResult TestFuncTimeData()
Definition EnProfilerTests.c:1242
TFResult TestClassTimeData()
Definition EnProfilerTests.c:1142
TFResult TestFuncCountData()
Definition EnProfilerTests.c:1305
void TestFuncCountDataHelper()
Definition EnProfilerTests.c:1509
void EnProfilerTests()
Definition EnProfilerTests.c:770
TFResult TestSetFlags()
Definition EnProfilerTests.c:828
float Sleep(float timeS)
Definition EnProfilerTests.c:1468
TFResult TestTogglingImmediate()
Definition EnProfilerTests.c:813
float StringFormat()
Definition EnProfilerTests.c:1481
TFResult TestToggling()
Definition EnProfilerTests.c:798
TFResult TestAddFlags()
Definition EnProfilerTests.c:918
TFResult TestClearFlags()
Definition EnProfilerTests.c:881
TFResult TestClassCountData()
Definition EnProfilerTests.c:1190
float StringConcat()
Definition EnProfilerTests.c:1495
static void TestFuncCountDataHelperStatic()
Definition EnProfilerTests.c:1516
bool m_bWasProfilerEnabled
Remember this, so we can restore the previous state before the test!
Definition EnProfilerTests.c:765
string Type
Definition JsonDataContaminatedArea.c:11
TFResult NTFR(TFR result)
Definition TestFramework.c:273
bool Assert(bool condition)
Definition TestFramework.c:262
void TFResult(TFR result)
Definition TestFramework.c:12
TFR
Definition TestFramework.c:2
void AddInitTest(string test)
Definition TestFramework.c:249
TFResult BTFR(bool result)
Definition TestFramework.c:278
Definition Debug.c:14
static void TFLog(string message=LOG_DEFAULT, TestFramework caller=null, string function="")
Definition Debug.c:191
Set of methods for accessing script profiling data.
Definition EnProfiler.c:73
Definition EnConvert.c:590
static int GetEnumValue(typename e, int idx)
Return the nth value in the enum.
Definition EnConvert.c:634
static int GetEnumSize(typename e)
Return amount of values in enum.
Definition EnConvert.c:623
static int StringToEnum(typename e, string enumName)
Return enum value from string name.
Definition EnConvert.c:612
The error handler itself, for managing and distributing errors to modules Manages the ErrorHandlerMod...
Definition ErrorModuleHandler.c:29
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 bool IsEnabledP()
Return if script profiling is enabled through EnProfiler.
Definition EnProfiler.c:138
static proto int GetTimeResolution()
Get the currently set time resolution.
static proto int GetAllocationsOfClass(typename clss, bool immediate=false)
Obtain [SD] or [PD] regarding the allocations of a specific class.
static proto EnProfilerModule GetModule()
Get the currently profiled module.
static proto int SetFlags(int flags, bool sessionReset=true)
Override the currently used set of EnProfilerFlags across the API.
static proto void ResetSession(bool fullReset=false)
Perform [SR], clearing SessionFrame, ProfiledSessionFrames, [SD] and [PD] (except for [CI])
static proto void GetTimePerFunc(notnull out array< ref EnProfilerTimeFuncPair > outArr, int count=int.MAX)
Obtain [SD] for Time Per Function.
static proto int AddFlags(int flags, bool sessionReset=true)
Add flags to the currently used set of EnProfilerFlags across the API.
static proto void SetTimeResolution(int resolution)
Set the resolution of the fetched Time data.
static proto float GetTimeOfClass(typename clss, bool immediate=false)
Obtain [SD] or [PD] regarding the time a specific class consumed.
static proto void SetModule(EnProfilerModule module, bool sessionReset=true)
Set the module to be profiled.
static bool IsEnabledC()
Return if script profiling is actually turned on inside of the script context.
Definition EnProfiler.c:152
static proto float GetTimeOfFunc(string funct, typename clss, bool immediate=false)
Obtain [SD] or [PD] regarding the time consumed by a specific function.
static bool RequestImmediateData()
Helper method to ascertain the profiler will record [PD] right after this call.
Definition EnProfiler.c:745
static proto void GetTimePerClass(notnull out array< ref EnProfilerTimeClassPair > outArr, int count=int.MAX)
Obtain [SD] for Time Per Class.
static proto int GetInstancesOfClass(typename clss, bool immediate=false)
Obtain [SD] or [PD] regarding the [CI] of a specific class.
static proto void Enable(bool enable, bool immediate=false, bool sessionReset=true)
Enable the gathering of script profiling data.
static proto bool NameToModule(string moduleName, out EnProfilerModule module)
Convert string to EnProfilerModule.
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 int GetFlags()
Get the currently used flags across the API.
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.
EnProfilerFlags
Flags that influences the behaviour of the EnProfiler API, applied through ...Flags functions.
Definition EnProfiler.c:9
EnProfilerModule
Current base scripted modules.
Definition EnProfiler.c:22
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 ClearFlags(bool sessionReset=true)
Remove all flags from the currently used set of EnProfilerFlags across the API.
static proto int RemoveFlags(int flags, bool sessionReset=true)
Remove flags from the currently used set of EnProfilerFlags across the API.

◆ Sleep2()

764 {
765 float startTime = GetGame().GetTickTime();
766 while (GetGame().GetTickTime() - startTime < timeS)
767 {
768 // Zzz
769 }
770
771 return GetGame().GetTickTime() - startTime;
772 }

Используется в DoEverything().

◆ SleepAgain()

float SleepAgain ( float timeS)
774 {
775 float startTime = GetGame().GetTickTime();
776 while (GetGame().GetTickTime() - startTime < timeS)
777 {
778 // Zzz
779 }
780
781 return GetGame().GetTickTime() - startTime;
782 }

Перекрестные ссылки GetGame().

◆ StringConcat()

float StringConcat ( )
1495 : TestFramework
1496{
1499
1500 //---------------------------------------------------------------------------
1501 // Ctor - Decides the tests to run
1502 //---------------------------------------------------------------------------
1503 void EnProfilerTests()
1504 {
1506
1507 AddInitTest("TestToggling");
1508 AddInitTest("TestTogglingImmediate");
1509 AddInitTest("TestSetFlags");
1510 AddInitTest("TestClearFlags");
1511 AddInitTest("TestAddFlags");
1512 AddInitTest("TestModule");
1513 AddInitTest("TestClassTimeData");
1514 AddInitTest("TestClassCountData");
1515 AddInitTest("TestFuncTimeData");
1516 AddInitTest("TestFuncCountData");
1517 }
1518
1519 //---------------------------------------------------------------------------
1520 // Dtor
1521 //---------------------------------------------------------------------------
1522 void ~EnProfilerTests()
1523 {
1525 }
1526
1527 //---------------------------------------------------------------------------
1528 // Tests
1529 //---------------------------------------------------------------------------
1530 // Test toggling state
1532 {
1536 {
1539 }
1540
1541 return NTFR(TFR.FAIL);
1542 }
1543
1544 //---------------------------------------------------------------------------
1545 // Test toggling immediate state
1547 {
1551 {
1554 }
1555
1556 return NTFR(TFR.FAIL);
1557 }
1558
1559 //---------------------------------------------------------------------------
1560 // Test SetFlags/GetFlags
1562 {
1564
1565 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1566 {
1568 EnProfiler.SetFlags(flags);
1569
1570 if (!Assert(EnProfiler.GetFlags() == flags))
1571 {
1573 return NTFR(TFR.FAIL);
1574 }
1575
1576 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1577 {
1579 EnProfiler.SetFlags(flags);
1580
1581 if (!Assert(EnProfiler.GetFlags() == flags))
1582 {
1584 return NTFR(TFR.FAIL);
1585 }
1586 }
1587 }
1588
1589 // Let's test some bogus
1590 EnProfiler.SetFlags(-333);
1593 if (!Assert(bogusFlags == 0))
1594 {
1596 return NTFR(TFR.FAIL);
1597 }
1598
1601 if (!Assert(bogusFlags == 0))
1602 {
1604 return NTFR(TFR.FAIL);
1605 }
1606
1607 // Reset
1609 return NTFR(TFR.SUCCESS);
1610 }
1611
1612 //---------------------------------------------------------------------------
1613 // Test removing of flags
1615 {
1617
1619
1621 {
1623 return NTFR(TFR.FAIL);
1624 }
1625
1628
1629 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1630 {
1632 return NTFR(TFR.FAIL);
1633 }
1634
1637
1638 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1639 {
1641 return NTFR(TFR.FAIL);
1642 }
1643
1644 // Reset
1646 return NTFR(TFR.SUCCESS);
1647 }
1648
1649 //---------------------------------------------------------------------------
1650 // Test adding of flags
1652 {
1654
1656
1657 // Return should match resulting flags
1659 {
1661 return NTFR(TFR.FAIL);
1662 }
1663
1664 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1665 {
1667 return NTFR(TFR.FAIL);
1668 }
1669
1670 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1671 {
1673 return NTFR(TFR.FAIL);
1674 }
1675
1676 // Reset
1678 return NTFR(TFR.SUCCESS);
1679 }
1680
1681 //---------------------------------------------------------------------------
1682 // Test module
1684 {
1685 // File lives in Game, use it while testing
1687
1688 // This was added at the same time as this API, so check if it works as well
1689 string nameOfCurrentModule = Type().GetModule();
1690 if (!Assert(nameOfCurrentModule != ""))
1691 {
1692 return NTFR(TFR.FAIL);
1693 }
1694
1695 // We know we are in Game, so use it as a test
1698 {
1699 return NTFR(TFR.FAIL);
1700 }
1701
1703 {
1704 return NTFR(TFR.FAIL);
1705 }
1706
1707 // Test if setting and getting works
1710
1712 {
1714 return NTFR(TFR.FAIL);
1715 }
1716
1717 // Data to restore
1720
1721 // Make sure we are only profiling Game and that the data is clean
1722 // Only valid for the Get...Per... methods, as they need to be sorted
1724
1725 // GetTickTime() returns in seconds, so gather the results in seconds too
1728
1729 // Time to sleeb
1730 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1731 float timeSlept = Sleep(0.3);
1732 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1733 float diff = postTime - previousTime - timeSlept;
1734
1735 // Restore
1737
1738 // We called the function, so it must have some time
1739 if (!Assert(postTime > 0))
1740 {
1742
1743 if (!wasEnabled)
1744 EnProfiler.Enable(false, true);
1745
1747
1748 return NTFR(TFR.FAIL);
1749 }
1750
1751 if (!Assert(diff < 0.00001))
1752 {
1754
1755 if (!wasEnabled)
1756 EnProfiler.Enable(false, true);
1757
1759
1760 return NTFR(TFR.FAIL);
1761 }
1762
1763 // Clean the session
1765
1766 // Something from a different module should not get sorted, so just fire something from a different module
1767 for (int i = 0; i < 1000; ++i)
1768 {
1769 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1770 }
1771
1772 // Sort and gather the data and validate if it is correct
1776
1777 Debug.TFLog("Game fncs:", this, "TestModule");
1778
1779 int funcCount = timePerFunc.Count();
1780 for (int j = 0; j < funcCount; ++j)
1781 {
1783 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1784 // We are currently profiling Game, so this Core function shouldn't be present
1785 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1786 {
1788
1789 if (!wasEnabled)
1790 EnProfiler.Enable(false, true);
1791
1793
1794 return NTFR(TFR.FAIL);
1795 }
1796 }
1797
1800
1801 int classCount = timePerClass.Count();
1802 for (int k = 0; k < classCount; ++k)
1803 {
1804 typename type = timePerClass[k].param2;
1806 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1807 {
1809
1810 if (!wasEnabled)
1811 EnProfiler.Enable(false, true);
1812
1814
1815 return NTFR(TFR.FAIL);
1816 }
1817
1818 // Only classes from Game should be present
1819 if (!Assert(classModule == eptModule))
1820 {
1822
1823 if (!wasEnabled)
1824 EnProfiler.Enable(false, true);
1825
1827
1828 return NTFR(TFR.FAIL);
1829 }
1830 }
1831
1832 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1835 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1837
1838 bool found = false;
1839
1840 Debug.TFLog("Core fncs:", this, "TestModule");
1841
1842 funcCount = timePerFunc.Count();
1843 for (int l = 0; l < funcCount; ++l)
1844 {
1846 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1847 // We are currently profiling Core, so this Core function should be present
1848 if (tfpc.param2 == "EnumTools::StringToEnum")
1849 {
1850 found = true;
1851 break;
1852 }
1853 }
1854
1855 Assert(found);
1856
1857 // Test some bogus
1859 EnProfiler.SetModule(-333);
1860 bool success = Assert(EnProfiler.GetModule() == mod);
1861 EnProfiler.SetModule(6003);
1863
1866
1867 if (!wasEnabled)
1868 EnProfiler.Enable(false, true);
1869
1870 return BTFR(success && found);
1871 }
1872
1873 //---------------------------------------------------------------------------
1874 // Test to see if class time data is correct
1876 {
1877 // We should restore this when done
1880
1881 // GetTickTime() returns in seconds, so gather the results in seconds too
1883
1884 // Create the classes
1886
1887 // Time to stress
1888 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1889 float timeStressed = clss.DoEverything();
1890 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1891 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1894
1895 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1896
1897 // Restore
1899 if (!wasEnabled)
1900 EnProfiler.Enable(false, true);
1901
1902 // We called the function, so it must have some time
1903 if (!Assert(postTime > 0))
1904 {
1905 return NTFR(TFR.FAIL);
1906 }
1907
1909 {
1910 return NTFR(TFR.FAIL);
1911 }
1912
1913 if (!Assert(diff < 0.001))
1914 {
1915 return NTFR(TFR.FAIL);
1916 }
1917
1918 return NTFR(TFR.SUCCESS);
1919 }
1920
1921 //---------------------------------------------------------------------------
1922 // Test to see if class count data is correct
1924 {
1925 const int allocAmount = 9;
1926 const int releaseAmount = 6;
1928
1929 // We should restore this when done
1931
1932 // Time to test
1935
1937 for (int i = 0; i < allocAmount; ++i)
1938 {
1939 instanceArr.Insert(new EPTHelperClass());
1940 }
1941
1942 for (int j = 0; j < releaseAmount; ++j)
1943 {
1944 delete instanceArr[j];
1945 }
1946
1947 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1948 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1949
1952
1953 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1954
1955 // Restore
1956 if (!wasEnabled)
1957 EnProfiler.Enable(false, true);
1958
1959 // Time to check
1960 if (!Assert(alloced == allocAmount))
1961 {
1962 return NTFR(TFR.FAIL);
1963 }
1964
1966 {
1967 return NTFR(TFR.FAIL);
1968 }
1969
1970 return NTFR(TFR.SUCCESS);
1971 }
1972
1973 //---------------------------------------------------------------------------
1974 // Test to see if func time data is correct
1976 {
1977 // We should restore this when done
1980
1981 // GetTickTime() returns in seconds, so gather the results in seconds too
1983
1984 // Time to stress
1985 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1986 float timeStressed = StringFormat();
1987 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1990
1991 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1992 float timeStressed2 = StringConcat();
1993 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1996
1997 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1998
1999 // Restore
2001 if (!wasEnabled)
2002 {
2003 EnProfiler.Enable(false, true);
2004 }
2005
2006 // We called the function, so it must have some time
2007 if (!Assert(postTime > 0))
2008 {
2009 return NTFR(TFR.FAIL);
2010 }
2011
2012 if (!Assert(diff < 0.001))
2013 {
2014 return NTFR(TFR.FAIL);
2015 }
2016
2017 if (!Assert(postTime2 > 0))
2018 {
2019 return NTFR(TFR.FAIL);
2020 }
2021
2022 if (!Assert(diff2 < 0.001))
2023 {
2024 return NTFR(TFR.FAIL);
2025 }
2026
2027 // I know that string.Format is faster than additive concatenation
2029 {
2030 return NTFR(TFR.FAIL);
2031 }
2032
2033 return NTFR(TFR.SUCCESS);
2034 }
2035
2036 //---------------------------------------------------------------------------
2037 // Test to see if func count data is correct
2039 {
2040 // We should restore this when done
2042
2043 // Time to count
2044
2045 // - CallFunction
2046 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2047 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
2048 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2049
2051
2052 // - CallFunctionParams
2053 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2054 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
2055 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2056
2058
2059 // - Regular call
2060 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2062 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2063
2065
2066 // - Call
2067 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2068 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
2069 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2070
2072
2073 // - Garbage
2074 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
2075
2076 // - Static
2077 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
2079 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
2080
2082
2083 // - Global
2084 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
2085 GetDayZGame();
2086 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
2087
2089
2090 // - Global proto
2091 // Not tracked, so don't need to compare before and after, should always be 0
2092 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
2093 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
2094
2095 // - Static proto
2096 // Not tracked, so don't need to compare before and after, should always be 0
2097 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
2098
2099 // - proto
2100 // Not tracked, so don't need to compare before and after, should always be 0
2101 GetGame().GetHostName();
2102 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
2103
2104 // - proto native
2105 // Not tracked, so don't need to compare before and after, should always be 0
2106 GetGame().IsServer();
2107 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
2108
2109 // - static proto native
2110 // Not tracked, so don't need to compare before and after, should always be 0
2112 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
2113
2114 // Restore
2115 if (!wasEnabled)
2116 {
2117 EnProfiler.Enable(false, true);
2118 }
2119
2120 // Do the checks
2121
2122 // - CallFunction
2123 if (!Assert(callCountCF == 1))
2124 {
2125 return NTFR(TFR.FAIL);
2126 }
2127
2128 // - CallFunctionParams
2129 if (!Assert(callCountCFP == 1))
2130 {
2131 return NTFR(TFR.FAIL);
2132 }
2133
2134 // - Regular call
2135 if (!Assert(callCountRG == 1))
2136 {
2137 return NTFR(TFR.FAIL);
2138 }
2139
2140 // - Call
2141 if (!Assert(callCountC == 1))
2142 {
2143 return NTFR(TFR.FAIL);
2144 }
2145
2146 // - Garbage
2147 if (!Assert(callCountNon == -1))
2148 {
2149 return NTFR(TFR.FAIL);
2150 }
2151
2152 // - Static
2153 if (!Assert(callCountS == 1))
2154 {
2155 return NTFR(TFR.FAIL);
2156 }
2157
2158 // - Global
2159 if (!Assert(callCountG == 1))
2160 {
2161 return NTFR(TFR.FAIL);
2162 }
2163
2164 // - Global proto
2165 if (!Assert(callCountGP == 0))
2166 {
2167 return NTFR(TFR.FAIL);
2168 }
2169
2170 // - Static proto
2171 if (!Assert(callCountSP == 0))
2172 {
2173 return NTFR(TFR.FAIL);
2174 }
2175
2176 // - proto
2177 if (!Assert(callCountP == 0))
2178 {
2179 return NTFR(TFR.FAIL);
2180 }
2181
2182 // - proto native
2183 if (!Assert(callCountPN == 0))
2184 {
2185 return NTFR(TFR.FAIL);
2186 }
2187
2188 // - static proto native
2189 if (!Assert(callCountSPN == 0))
2190 {
2191 return NTFR(TFR.FAIL);
2192 }
2193
2194 return NTFR(TFR.SUCCESS);
2195 }
2196
2197 //---------------------------------------------------------------------------
2198 // Helpers
2199 //---------------------------------------------------------------------------
2200 // Snore
2201 float Sleep(float timeS)
2202 {
2203 float startTime = GetGame().GetTickTime();
2204 while (GetGame().GetTickTime() - startTime < timeS)
2205 {
2206 // Zzz
2207 }
2208
2209 return GetGame().GetTickTime() - startTime;
2210 }
2211
2212 //---------------------------------------------------------------------------
2213 // Example stress method
2214 float StringFormat()
2215 {
2216 float startTime = GetGame().GetTickTime();
2217
2218 for (int i = 0; i < 1000; ++i)
2219 {
2220 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
2221 }
2222
2223 return GetGame().GetTickTime() - startTime;
2224 }
2225
2226 //---------------------------------------------------------------------------
2227 // Example stress method 2
2228 float StringConcat()
2229 {
2230 float startTime = GetGame().GetTickTime();
2231
2232 for (int i = 0; i < 1000; ++i)
2233 {
2234 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
2235 }
2236
2237 return GetGame().GetTickTime() - startTime;
2238 }
2239
2240 //---------------------------------------------------------------------------
2241 // To make sure it is only ever called in that test
2243 {
2244 int dummy = 3;
2245 }
2246
2247 //---------------------------------------------------------------------------
2248 // To make sure it is only ever called in that test
2249 static void TestFuncCountDataHelperStatic()
2250 {
2251 int dummy = 3;
2252 }
2253}
2254
2255class EPTHelperClass
2256{
2257 float Sleep2(float timeS)
2258 {
2259 float startTime = GetGame().GetTickTime();
2260 while (GetGame().GetTickTime() - startTime < timeS)
2261 {
2262 // Zzz
2263 }
2264
2265 return GetGame().GetTickTime() - startTime;
2266 }
2267 float SleepAgain(float timeS)
2268 {
2269 float startTime = GetGame().GetTickTime();
2270 while (GetGame().GetTickTime() - startTime < timeS)
2271 {
2272 // Zzz
2273 }
2274
2275 return GetGame().GetTickTime() - startTime;
2276 }
2277
2278 float DoEverything()
2279 {
2280 float startTime = GetGame().GetTickTime();
2281
2282 Sleep2(3);
2283 SleepAgain(3);
2284
2285 return GetGame().GetTickTime() - startTime;
2286 }
2287}

◆ StringFormat()

float StringFormat ( )
1481 : TestFramework
1482{
1485
1486 //---------------------------------------------------------------------------
1487 // Ctor - Decides the tests to run
1488 //---------------------------------------------------------------------------
1489 void EnProfilerTests()
1490 {
1492
1493 AddInitTest("TestToggling");
1494 AddInitTest("TestTogglingImmediate");
1495 AddInitTest("TestSetFlags");
1496 AddInitTest("TestClearFlags");
1497 AddInitTest("TestAddFlags");
1498 AddInitTest("TestModule");
1499 AddInitTest("TestClassTimeData");
1500 AddInitTest("TestClassCountData");
1501 AddInitTest("TestFuncTimeData");
1502 AddInitTest("TestFuncCountData");
1503 }
1504
1505 //---------------------------------------------------------------------------
1506 // Dtor
1507 //---------------------------------------------------------------------------
1508 void ~EnProfilerTests()
1509 {
1511 }
1512
1513 //---------------------------------------------------------------------------
1514 // Tests
1515 //---------------------------------------------------------------------------
1516 // Test toggling state
1518 {
1522 {
1525 }
1526
1527 return NTFR(TFR.FAIL);
1528 }
1529
1530 //---------------------------------------------------------------------------
1531 // Test toggling immediate state
1533 {
1537 {
1540 }
1541
1542 return NTFR(TFR.FAIL);
1543 }
1544
1545 //---------------------------------------------------------------------------
1546 // Test SetFlags/GetFlags
1548 {
1550
1551 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1552 {
1554 EnProfiler.SetFlags(flags);
1555
1556 if (!Assert(EnProfiler.GetFlags() == flags))
1557 {
1559 return NTFR(TFR.FAIL);
1560 }
1561
1562 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1563 {
1565 EnProfiler.SetFlags(flags);
1566
1567 if (!Assert(EnProfiler.GetFlags() == flags))
1568 {
1570 return NTFR(TFR.FAIL);
1571 }
1572 }
1573 }
1574
1575 // Let's test some bogus
1576 EnProfiler.SetFlags(-333);
1579 if (!Assert(bogusFlags == 0))
1580 {
1582 return NTFR(TFR.FAIL);
1583 }
1584
1587 if (!Assert(bogusFlags == 0))
1588 {
1590 return NTFR(TFR.FAIL);
1591 }
1592
1593 // Reset
1595 return NTFR(TFR.SUCCESS);
1596 }
1597
1598 //---------------------------------------------------------------------------
1599 // Test removing of flags
1601 {
1603
1605
1607 {
1609 return NTFR(TFR.FAIL);
1610 }
1611
1614
1615 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1616 {
1618 return NTFR(TFR.FAIL);
1619 }
1620
1623
1624 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1625 {
1627 return NTFR(TFR.FAIL);
1628 }
1629
1630 // Reset
1632 return NTFR(TFR.SUCCESS);
1633 }
1634
1635 //---------------------------------------------------------------------------
1636 // Test adding of flags
1638 {
1640
1642
1643 // Return should match resulting flags
1645 {
1647 return NTFR(TFR.FAIL);
1648 }
1649
1650 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1651 {
1653 return NTFR(TFR.FAIL);
1654 }
1655
1656 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1657 {
1659 return NTFR(TFR.FAIL);
1660 }
1661
1662 // Reset
1664 return NTFR(TFR.SUCCESS);
1665 }
1666
1667 //---------------------------------------------------------------------------
1668 // Test module
1670 {
1671 // File lives in Game, use it while testing
1673
1674 // This was added at the same time as this API, so check if it works as well
1675 string nameOfCurrentModule = Type().GetModule();
1676 if (!Assert(nameOfCurrentModule != ""))
1677 {
1678 return NTFR(TFR.FAIL);
1679 }
1680
1681 // We know we are in Game, so use it as a test
1684 {
1685 return NTFR(TFR.FAIL);
1686 }
1687
1689 {
1690 return NTFR(TFR.FAIL);
1691 }
1692
1693 // Test if setting and getting works
1696
1698 {
1700 return NTFR(TFR.FAIL);
1701 }
1702
1703 // Data to restore
1706
1707 // Make sure we are only profiling Game and that the data is clean
1708 // Only valid for the Get...Per... methods, as they need to be sorted
1710
1711 // GetTickTime() returns in seconds, so gather the results in seconds too
1714
1715 // Time to sleeb
1716 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1717 float timeSlept = Sleep(0.3);
1718 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1719 float diff = postTime - previousTime - timeSlept;
1720
1721 // Restore
1723
1724 // We called the function, so it must have some time
1725 if (!Assert(postTime > 0))
1726 {
1728
1729 if (!wasEnabled)
1730 EnProfiler.Enable(false, true);
1731
1733
1734 return NTFR(TFR.FAIL);
1735 }
1736
1737 if (!Assert(diff < 0.00001))
1738 {
1740
1741 if (!wasEnabled)
1742 EnProfiler.Enable(false, true);
1743
1745
1746 return NTFR(TFR.FAIL);
1747 }
1748
1749 // Clean the session
1751
1752 // Something from a different module should not get sorted, so just fire something from a different module
1753 for (int i = 0; i < 1000; ++i)
1754 {
1755 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1756 }
1757
1758 // Sort and gather the data and validate if it is correct
1762
1763 Debug.TFLog("Game fncs:", this, "TestModule");
1764
1765 int funcCount = timePerFunc.Count();
1766 for (int j = 0; j < funcCount; ++j)
1767 {
1769 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1770 // We are currently profiling Game, so this Core function shouldn't be present
1771 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1772 {
1774
1775 if (!wasEnabled)
1776 EnProfiler.Enable(false, true);
1777
1779
1780 return NTFR(TFR.FAIL);
1781 }
1782 }
1783
1786
1787 int classCount = timePerClass.Count();
1788 for (int k = 0; k < classCount; ++k)
1789 {
1790 typename type = timePerClass[k].param2;
1792 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1793 {
1795
1796 if (!wasEnabled)
1797 EnProfiler.Enable(false, true);
1798
1800
1801 return NTFR(TFR.FAIL);
1802 }
1803
1804 // Only classes from Game should be present
1805 if (!Assert(classModule == eptModule))
1806 {
1808
1809 if (!wasEnabled)
1810 EnProfiler.Enable(false, true);
1811
1813
1814 return NTFR(TFR.FAIL);
1815 }
1816 }
1817
1818 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1821 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1823
1824 bool found = false;
1825
1826 Debug.TFLog("Core fncs:", this, "TestModule");
1827
1828 funcCount = timePerFunc.Count();
1829 for (int l = 0; l < funcCount; ++l)
1830 {
1832 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1833 // We are currently profiling Core, so this Core function should be present
1834 if (tfpc.param2 == "EnumTools::StringToEnum")
1835 {
1836 found = true;
1837 break;
1838 }
1839 }
1840
1841 Assert(found);
1842
1843 // Test some bogus
1845 EnProfiler.SetModule(-333);
1846 bool success = Assert(EnProfiler.GetModule() == mod);
1847 EnProfiler.SetModule(6003);
1849
1852
1853 if (!wasEnabled)
1854 EnProfiler.Enable(false, true);
1855
1856 return BTFR(success && found);
1857 }
1858
1859 //---------------------------------------------------------------------------
1860 // Test to see if class time data is correct
1862 {
1863 // We should restore this when done
1866
1867 // GetTickTime() returns in seconds, so gather the results in seconds too
1869
1870 // Create the classes
1872
1873 // Time to stress
1874 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1875 float timeStressed = clss.DoEverything();
1876 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1877 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1880
1881 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1882
1883 // Restore
1885 if (!wasEnabled)
1886 EnProfiler.Enable(false, true);
1887
1888 // We called the function, so it must have some time
1889 if (!Assert(postTime > 0))
1890 {
1891 return NTFR(TFR.FAIL);
1892 }
1893
1895 {
1896 return NTFR(TFR.FAIL);
1897 }
1898
1899 if (!Assert(diff < 0.001))
1900 {
1901 return NTFR(TFR.FAIL);
1902 }
1903
1904 return NTFR(TFR.SUCCESS);
1905 }
1906
1907 //---------------------------------------------------------------------------
1908 // Test to see if class count data is correct
1910 {
1911 const int allocAmount = 9;
1912 const int releaseAmount = 6;
1914
1915 // We should restore this when done
1917
1918 // Time to test
1921
1923 for (int i = 0; i < allocAmount; ++i)
1924 {
1925 instanceArr.Insert(new EPTHelperClass());
1926 }
1927
1928 for (int j = 0; j < releaseAmount; ++j)
1929 {
1930 delete instanceArr[j];
1931 }
1932
1933 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1934 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1935
1938
1939 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1940
1941 // Restore
1942 if (!wasEnabled)
1943 EnProfiler.Enable(false, true);
1944
1945 // Time to check
1946 if (!Assert(alloced == allocAmount))
1947 {
1948 return NTFR(TFR.FAIL);
1949 }
1950
1952 {
1953 return NTFR(TFR.FAIL);
1954 }
1955
1956 return NTFR(TFR.SUCCESS);
1957 }
1958
1959 //---------------------------------------------------------------------------
1960 // Test to see if func time data is correct
1962 {
1963 // We should restore this when done
1966
1967 // GetTickTime() returns in seconds, so gather the results in seconds too
1969
1970 // Time to stress
1971 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1972 float timeStressed = StringFormat();
1973 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1976
1977 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1978 float timeStressed2 = StringConcat();
1979 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1982
1983 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1984
1985 // Restore
1987 if (!wasEnabled)
1988 {
1989 EnProfiler.Enable(false, true);
1990 }
1991
1992 // We called the function, so it must have some time
1993 if (!Assert(postTime > 0))
1994 {
1995 return NTFR(TFR.FAIL);
1996 }
1997
1998 if (!Assert(diff < 0.001))
1999 {
2000 return NTFR(TFR.FAIL);
2001 }
2002
2003 if (!Assert(postTime2 > 0))
2004 {
2005 return NTFR(TFR.FAIL);
2006 }
2007
2008 if (!Assert(diff2 < 0.001))
2009 {
2010 return NTFR(TFR.FAIL);
2011 }
2012
2013 // I know that string.Format is faster than additive concatenation
2015 {
2016 return NTFR(TFR.FAIL);
2017 }
2018
2019 return NTFR(TFR.SUCCESS);
2020 }
2021
2022 //---------------------------------------------------------------------------
2023 // Test to see if func count data is correct
2025 {
2026 // We should restore this when done
2028
2029 // Time to count
2030
2031 // - CallFunction
2032 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2033 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
2034 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2035
2037
2038 // - CallFunctionParams
2039 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2040 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
2041 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2042
2044
2045 // - Regular call
2046 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2048 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2049
2051
2052 // - Call
2053 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2054 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
2055 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2056
2058
2059 // - Garbage
2060 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
2061
2062 // - Static
2063 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
2065 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
2066
2068
2069 // - Global
2070 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
2071 GetDayZGame();
2072 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
2073
2075
2076 // - Global proto
2077 // Not tracked, so don't need to compare before and after, should always be 0
2078 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
2079 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
2080
2081 // - Static proto
2082 // Not tracked, so don't need to compare before and after, should always be 0
2083 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
2084
2085 // - proto
2086 // Not tracked, so don't need to compare before and after, should always be 0
2087 GetGame().GetHostName();
2088 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
2089
2090 // - proto native
2091 // Not tracked, so don't need to compare before and after, should always be 0
2092 GetGame().IsServer();
2093 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
2094
2095 // - static proto native
2096 // Not tracked, so don't need to compare before and after, should always be 0
2098 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
2099
2100 // Restore
2101 if (!wasEnabled)
2102 {
2103 EnProfiler.Enable(false, true);
2104 }
2105
2106 // Do the checks
2107
2108 // - CallFunction
2109 if (!Assert(callCountCF == 1))
2110 {
2111 return NTFR(TFR.FAIL);
2112 }
2113
2114 // - CallFunctionParams
2115 if (!Assert(callCountCFP == 1))
2116 {
2117 return NTFR(TFR.FAIL);
2118 }
2119
2120 // - Regular call
2121 if (!Assert(callCountRG == 1))
2122 {
2123 return NTFR(TFR.FAIL);
2124 }
2125
2126 // - Call
2127 if (!Assert(callCountC == 1))
2128 {
2129 return NTFR(TFR.FAIL);
2130 }
2131
2132 // - Garbage
2133 if (!Assert(callCountNon == -1))
2134 {
2135 return NTFR(TFR.FAIL);
2136 }
2137
2138 // - Static
2139 if (!Assert(callCountS == 1))
2140 {
2141 return NTFR(TFR.FAIL);
2142 }
2143
2144 // - Global
2145 if (!Assert(callCountG == 1))
2146 {
2147 return NTFR(TFR.FAIL);
2148 }
2149
2150 // - Global proto
2151 if (!Assert(callCountGP == 0))
2152 {
2153 return NTFR(TFR.FAIL);
2154 }
2155
2156 // - Static proto
2157 if (!Assert(callCountSP == 0))
2158 {
2159 return NTFR(TFR.FAIL);
2160 }
2161
2162 // - proto
2163 if (!Assert(callCountP == 0))
2164 {
2165 return NTFR(TFR.FAIL);
2166 }
2167
2168 // - proto native
2169 if (!Assert(callCountPN == 0))
2170 {
2171 return NTFR(TFR.FAIL);
2172 }
2173
2174 // - static proto native
2175 if (!Assert(callCountSPN == 0))
2176 {
2177 return NTFR(TFR.FAIL);
2178 }
2179
2180 return NTFR(TFR.SUCCESS);
2181 }
2182
2183 //---------------------------------------------------------------------------
2184 // Helpers
2185 //---------------------------------------------------------------------------
2186 // Snore
2187 float Sleep(float timeS)
2188 {
2189 float startTime = GetGame().GetTickTime();
2190 while (GetGame().GetTickTime() - startTime < timeS)
2191 {
2192 // Zzz
2193 }
2194
2195 return GetGame().GetTickTime() - startTime;
2196 }
2197
2198 //---------------------------------------------------------------------------
2199 // Example stress method
2200 float StringFormat()
2201 {
2202 float startTime = GetGame().GetTickTime();
2203
2204 for (int i = 0; i < 1000; ++i)
2205 {
2206 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
2207 }
2208
2209 return GetGame().GetTickTime() - startTime;
2210 }
2211
2212 //---------------------------------------------------------------------------
2213 // Example stress method 2
2214 float StringConcat()
2215 {
2216 float startTime = GetGame().GetTickTime();
2217
2218 for (int i = 0; i < 1000; ++i)
2219 {
2220 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
2221 }
2222
2223 return GetGame().GetTickTime() - startTime;
2224 }
2225
2226 //---------------------------------------------------------------------------
2227 // To make sure it is only ever called in that test
2229 {
2230 int dummy = 3;
2231 }
2232
2233 //---------------------------------------------------------------------------
2234 // To make sure it is only ever called in that test
2235 static void TestFuncCountDataHelperStatic()
2236 {
2237 int dummy = 3;
2238 }
2239}
2240
2241class EPTHelperClass
2242{
2243 float Sleep2(float timeS)
2244 {
2245 float startTime = GetGame().GetTickTime();
2246 while (GetGame().GetTickTime() - startTime < timeS)
2247 {
2248 // Zzz
2249 }
2250
2251 return GetGame().GetTickTime() - startTime;
2252 }
2253 float SleepAgain(float timeS)
2254 {
2255 float startTime = GetGame().GetTickTime();
2256 while (GetGame().GetTickTime() - startTime < timeS)
2257 {
2258 // Zzz
2259 }
2260
2261 return GetGame().GetTickTime() - startTime;
2262 }
2263
2264 float DoEverything()
2265 {
2266 float startTime = GetGame().GetTickTime();
2267
2268 Sleep2(3);
2269 SleepAgain(3);
2270
2271 return GetGame().GetTickTime() - startTime;
2272 }
2273}

◆ TestAddFlags()

TFResult TestAddFlags ( )
918 : TestFramework
919{
922
923 //---------------------------------------------------------------------------
924 // Ctor - Decides the tests to run
925 //---------------------------------------------------------------------------
926 void EnProfilerTests()
927 {
929
930 AddInitTest("TestToggling");
931 AddInitTest("TestTogglingImmediate");
932 AddInitTest("TestSetFlags");
933 AddInitTest("TestClearFlags");
934 AddInitTest("TestAddFlags");
935 AddInitTest("TestModule");
936 AddInitTest("TestClassTimeData");
937 AddInitTest("TestClassCountData");
938 AddInitTest("TestFuncTimeData");
939 AddInitTest("TestFuncCountData");
940 }
941
942 //---------------------------------------------------------------------------
943 // Dtor
944 //---------------------------------------------------------------------------
945 void ~EnProfilerTests()
946 {
948 }
949
950 //---------------------------------------------------------------------------
951 // Tests
952 //---------------------------------------------------------------------------
953 // Test toggling state
955 {
959 {
962 }
963
964 return NTFR(TFR.FAIL);
965 }
966
967 //---------------------------------------------------------------------------
968 // Test toggling immediate state
970 {
974 {
977 }
978
979 return NTFR(TFR.FAIL);
980 }
981
982 //---------------------------------------------------------------------------
983 // Test SetFlags/GetFlags
985 {
987
988 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
989 {
991 EnProfiler.SetFlags(flags);
992
993 if (!Assert(EnProfiler.GetFlags() == flags))
994 {
996 return NTFR(TFR.FAIL);
997 }
998
999 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1000 {
1002 EnProfiler.SetFlags(flags);
1003
1004 if (!Assert(EnProfiler.GetFlags() == flags))
1005 {
1007 return NTFR(TFR.FAIL);
1008 }
1009 }
1010 }
1011
1012 // Let's test some bogus
1013 EnProfiler.SetFlags(-333);
1016 if (!Assert(bogusFlags == 0))
1017 {
1019 return NTFR(TFR.FAIL);
1020 }
1021
1024 if (!Assert(bogusFlags == 0))
1025 {
1027 return NTFR(TFR.FAIL);
1028 }
1029
1030 // Reset
1032 return NTFR(TFR.SUCCESS);
1033 }
1034
1035 //---------------------------------------------------------------------------
1036 // Test removing of flags
1038 {
1040
1042
1044 {
1046 return NTFR(TFR.FAIL);
1047 }
1048
1051
1052 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1053 {
1055 return NTFR(TFR.FAIL);
1056 }
1057
1060
1061 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1062 {
1064 return NTFR(TFR.FAIL);
1065 }
1066
1067 // Reset
1069 return NTFR(TFR.SUCCESS);
1070 }
1071
1072 //---------------------------------------------------------------------------
1073 // Test adding of flags
1075 {
1077
1079
1080 // Return should match resulting flags
1082 {
1084 return NTFR(TFR.FAIL);
1085 }
1086
1087 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1088 {
1090 return NTFR(TFR.FAIL);
1091 }
1092
1093 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1094 {
1096 return NTFR(TFR.FAIL);
1097 }
1098
1099 // Reset
1101 return NTFR(TFR.SUCCESS);
1102 }
1103
1104 //---------------------------------------------------------------------------
1105 // Test module
1107 {
1108 // File lives in Game, use it while testing
1110
1111 // This was added at the same time as this API, so check if it works as well
1112 string nameOfCurrentModule = Type().GetModule();
1113 if (!Assert(nameOfCurrentModule != ""))
1114 {
1115 return NTFR(TFR.FAIL);
1116 }
1117
1118 // We know we are in Game, so use it as a test
1121 {
1122 return NTFR(TFR.FAIL);
1123 }
1124
1126 {
1127 return NTFR(TFR.FAIL);
1128 }
1129
1130 // Test if setting and getting works
1133
1135 {
1137 return NTFR(TFR.FAIL);
1138 }
1139
1140 // Data to restore
1143
1144 // Make sure we are only profiling Game and that the data is clean
1145 // Only valid for the Get...Per... methods, as they need to be sorted
1147
1148 // GetTickTime() returns in seconds, so gather the results in seconds too
1151
1152 // Time to sleeb
1153 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1154 float timeSlept = Sleep(0.3);
1155 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1156 float diff = postTime - previousTime - timeSlept;
1157
1158 // Restore
1160
1161 // We called the function, so it must have some time
1162 if (!Assert(postTime > 0))
1163 {
1165
1166 if (!wasEnabled)
1167 EnProfiler.Enable(false, true);
1168
1170
1171 return NTFR(TFR.FAIL);
1172 }
1173
1174 if (!Assert(diff < 0.00001))
1175 {
1177
1178 if (!wasEnabled)
1179 EnProfiler.Enable(false, true);
1180
1182
1183 return NTFR(TFR.FAIL);
1184 }
1185
1186 // Clean the session
1188
1189 // Something from a different module should not get sorted, so just fire something from a different module
1190 for (int i = 0; i < 1000; ++i)
1191 {
1192 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1193 }
1194
1195 // Sort and gather the data and validate if it is correct
1199
1200 Debug.TFLog("Game fncs:", this, "TestModule");
1201
1202 int funcCount = timePerFunc.Count();
1203 for (int j = 0; j < funcCount; ++j)
1204 {
1206 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1207 // We are currently profiling Game, so this Core function shouldn't be present
1208 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1209 {
1211
1212 if (!wasEnabled)
1213 EnProfiler.Enable(false, true);
1214
1216
1217 return NTFR(TFR.FAIL);
1218 }
1219 }
1220
1223
1224 int classCount = timePerClass.Count();
1225 for (int k = 0; k < classCount; ++k)
1226 {
1227 typename type = timePerClass[k].param2;
1229 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1230 {
1232
1233 if (!wasEnabled)
1234 EnProfiler.Enable(false, true);
1235
1237
1238 return NTFR(TFR.FAIL);
1239 }
1240
1241 // Only classes from Game should be present
1242 if (!Assert(classModule == eptModule))
1243 {
1245
1246 if (!wasEnabled)
1247 EnProfiler.Enable(false, true);
1248
1250
1251 return NTFR(TFR.FAIL);
1252 }
1253 }
1254
1255 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1258 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1260
1261 bool found = false;
1262
1263 Debug.TFLog("Core fncs:", this, "TestModule");
1264
1265 funcCount = timePerFunc.Count();
1266 for (int l = 0; l < funcCount; ++l)
1267 {
1269 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1270 // We are currently profiling Core, so this Core function should be present
1271 if (tfpc.param2 == "EnumTools::StringToEnum")
1272 {
1273 found = true;
1274 break;
1275 }
1276 }
1277
1278 Assert(found);
1279
1280 // Test some bogus
1282 EnProfiler.SetModule(-333);
1283 bool success = Assert(EnProfiler.GetModule() == mod);
1284 EnProfiler.SetModule(6003);
1286
1289
1290 if (!wasEnabled)
1291 EnProfiler.Enable(false, true);
1292
1293 return BTFR(success && found);
1294 }
1295
1296 //---------------------------------------------------------------------------
1297 // Test to see if class time data is correct
1299 {
1300 // We should restore this when done
1303
1304 // GetTickTime() returns in seconds, so gather the results in seconds too
1306
1307 // Create the classes
1309
1310 // Time to stress
1311 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1312 float timeStressed = clss.DoEverything();
1313 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1314 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1317
1318 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1319
1320 // Restore
1322 if (!wasEnabled)
1323 EnProfiler.Enable(false, true);
1324
1325 // We called the function, so it must have some time
1326 if (!Assert(postTime > 0))
1327 {
1328 return NTFR(TFR.FAIL);
1329 }
1330
1332 {
1333 return NTFR(TFR.FAIL);
1334 }
1335
1336 if (!Assert(diff < 0.001))
1337 {
1338 return NTFR(TFR.FAIL);
1339 }
1340
1341 return NTFR(TFR.SUCCESS);
1342 }
1343
1344 //---------------------------------------------------------------------------
1345 // Test to see if class count data is correct
1347 {
1348 const int allocAmount = 9;
1349 const int releaseAmount = 6;
1351
1352 // We should restore this when done
1354
1355 // Time to test
1358
1360 for (int i = 0; i < allocAmount; ++i)
1361 {
1362 instanceArr.Insert(new EPTHelperClass());
1363 }
1364
1365 for (int j = 0; j < releaseAmount; ++j)
1366 {
1367 delete instanceArr[j];
1368 }
1369
1370 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1371 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1372
1375
1376 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1377
1378 // Restore
1379 if (!wasEnabled)
1380 EnProfiler.Enable(false, true);
1381
1382 // Time to check
1383 if (!Assert(alloced == allocAmount))
1384 {
1385 return NTFR(TFR.FAIL);
1386 }
1387
1389 {
1390 return NTFR(TFR.FAIL);
1391 }
1392
1393 return NTFR(TFR.SUCCESS);
1394 }
1395
1396 //---------------------------------------------------------------------------
1397 // Test to see if func time data is correct
1399 {
1400 // We should restore this when done
1403
1404 // GetTickTime() returns in seconds, so gather the results in seconds too
1406
1407 // Time to stress
1408 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1409 float timeStressed = StringFormat();
1410 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1413
1414 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1415 float timeStressed2 = StringConcat();
1416 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1419
1420 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1421
1422 // Restore
1424 if (!wasEnabled)
1425 {
1426 EnProfiler.Enable(false, true);
1427 }
1428
1429 // We called the function, so it must have some time
1430 if (!Assert(postTime > 0))
1431 {
1432 return NTFR(TFR.FAIL);
1433 }
1434
1435 if (!Assert(diff < 0.001))
1436 {
1437 return NTFR(TFR.FAIL);
1438 }
1439
1440 if (!Assert(postTime2 > 0))
1441 {
1442 return NTFR(TFR.FAIL);
1443 }
1444
1445 if (!Assert(diff2 < 0.001))
1446 {
1447 return NTFR(TFR.FAIL);
1448 }
1449
1450 // I know that string.Format is faster than additive concatenation
1452 {
1453 return NTFR(TFR.FAIL);
1454 }
1455
1456 return NTFR(TFR.SUCCESS);
1457 }
1458
1459 //---------------------------------------------------------------------------
1460 // Test to see if func count data is correct
1462 {
1463 // We should restore this when done
1465
1466 // Time to count
1467
1468 // - CallFunction
1469 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1470 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1471 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1472
1474
1475 // - CallFunctionParams
1476 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1477 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1478 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1479
1481
1482 // - Regular call
1483 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1485 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1486
1488
1489 // - Call
1490 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1491 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1492 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1493
1495
1496 // - Garbage
1497 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1498
1499 // - Static
1500 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1502 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1503
1505
1506 // - Global
1507 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1508 GetDayZGame();
1509 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1510
1512
1513 // - Global proto
1514 // Not tracked, so don't need to compare before and after, should always be 0
1515 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1516 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1517
1518 // - Static proto
1519 // Not tracked, so don't need to compare before and after, should always be 0
1520 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1521
1522 // - proto
1523 // Not tracked, so don't need to compare before and after, should always be 0
1524 GetGame().GetHostName();
1525 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1526
1527 // - proto native
1528 // Not tracked, so don't need to compare before and after, should always be 0
1529 GetGame().IsServer();
1530 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1531
1532 // - static proto native
1533 // Not tracked, so don't need to compare before and after, should always be 0
1535 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1536
1537 // Restore
1538 if (!wasEnabled)
1539 {
1540 EnProfiler.Enable(false, true);
1541 }
1542
1543 // Do the checks
1544
1545 // - CallFunction
1546 if (!Assert(callCountCF == 1))
1547 {
1548 return NTFR(TFR.FAIL);
1549 }
1550
1551 // - CallFunctionParams
1552 if (!Assert(callCountCFP == 1))
1553 {
1554 return NTFR(TFR.FAIL);
1555 }
1556
1557 // - Regular call
1558 if (!Assert(callCountRG == 1))
1559 {
1560 return NTFR(TFR.FAIL);
1561 }
1562
1563 // - Call
1564 if (!Assert(callCountC == 1))
1565 {
1566 return NTFR(TFR.FAIL);
1567 }
1568
1569 // - Garbage
1570 if (!Assert(callCountNon == -1))
1571 {
1572 return NTFR(TFR.FAIL);
1573 }
1574
1575 // - Static
1576 if (!Assert(callCountS == 1))
1577 {
1578 return NTFR(TFR.FAIL);
1579 }
1580
1581 // - Global
1582 if (!Assert(callCountG == 1))
1583 {
1584 return NTFR(TFR.FAIL);
1585 }
1586
1587 // - Global proto
1588 if (!Assert(callCountGP == 0))
1589 {
1590 return NTFR(TFR.FAIL);
1591 }
1592
1593 // - Static proto
1594 if (!Assert(callCountSP == 0))
1595 {
1596 return NTFR(TFR.FAIL);
1597 }
1598
1599 // - proto
1600 if (!Assert(callCountP == 0))
1601 {
1602 return NTFR(TFR.FAIL);
1603 }
1604
1605 // - proto native
1606 if (!Assert(callCountPN == 0))
1607 {
1608 return NTFR(TFR.FAIL);
1609 }
1610
1611 // - static proto native
1612 if (!Assert(callCountSPN == 0))
1613 {
1614 return NTFR(TFR.FAIL);
1615 }
1616
1617 return NTFR(TFR.SUCCESS);
1618 }
1619
1620 //---------------------------------------------------------------------------
1621 // Helpers
1622 //---------------------------------------------------------------------------
1623 // Snore
1624 float Sleep(float timeS)
1625 {
1626 float startTime = GetGame().GetTickTime();
1627 while (GetGame().GetTickTime() - startTime < timeS)
1628 {
1629 // Zzz
1630 }
1631
1632 return GetGame().GetTickTime() - startTime;
1633 }
1634
1635 //---------------------------------------------------------------------------
1636 // Example stress method
1637 float StringFormat()
1638 {
1639 float startTime = GetGame().GetTickTime();
1640
1641 for (int i = 0; i < 1000; ++i)
1642 {
1643 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1644 }
1645
1646 return GetGame().GetTickTime() - startTime;
1647 }
1648
1649 //---------------------------------------------------------------------------
1650 // Example stress method 2
1651 float StringConcat()
1652 {
1653 float startTime = GetGame().GetTickTime();
1654
1655 for (int i = 0; i < 1000; ++i)
1656 {
1657 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1658 }
1659
1660 return GetGame().GetTickTime() - startTime;
1661 }
1662
1663 //---------------------------------------------------------------------------
1664 // To make sure it is only ever called in that test
1666 {
1667 int dummy = 3;
1668 }
1669
1670 //---------------------------------------------------------------------------
1671 // To make sure it is only ever called in that test
1672 static void TestFuncCountDataHelperStatic()
1673 {
1674 int dummy = 3;
1675 }
1676}
1677
1678class EPTHelperClass
1679{
1680 float Sleep2(float timeS)
1681 {
1682 float startTime = GetGame().GetTickTime();
1683 while (GetGame().GetTickTime() - startTime < timeS)
1684 {
1685 // Zzz
1686 }
1687
1688 return GetGame().GetTickTime() - startTime;
1689 }
1690 float SleepAgain(float timeS)
1691 {
1692 float startTime = GetGame().GetTickTime();
1693 while (GetGame().GetTickTime() - startTime < timeS)
1694 {
1695 // Zzz
1696 }
1697
1698 return GetGame().GetTickTime() - startTime;
1699 }
1700
1701 float DoEverything()
1702 {
1703 float startTime = GetGame().GetTickTime();
1704
1705 Sleep2(3);
1706 SleepAgain(3);
1707
1708 return GetGame().GetTickTime() - startTime;
1709 }
1710}

◆ TestClassCountData()

TFResult TestClassCountData ( )
1190 : TestFramework
1191{
1194
1195 //---------------------------------------------------------------------------
1196 // Ctor - Decides the tests to run
1197 //---------------------------------------------------------------------------
1198 void EnProfilerTests()
1199 {
1201
1202 AddInitTest("TestToggling");
1203 AddInitTest("TestTogglingImmediate");
1204 AddInitTest("TestSetFlags");
1205 AddInitTest("TestClearFlags");
1206 AddInitTest("TestAddFlags");
1207 AddInitTest("TestModule");
1208 AddInitTest("TestClassTimeData");
1209 AddInitTest("TestClassCountData");
1210 AddInitTest("TestFuncTimeData");
1211 AddInitTest("TestFuncCountData");
1212 }
1213
1214 //---------------------------------------------------------------------------
1215 // Dtor
1216 //---------------------------------------------------------------------------
1217 void ~EnProfilerTests()
1218 {
1220 }
1221
1222 //---------------------------------------------------------------------------
1223 // Tests
1224 //---------------------------------------------------------------------------
1225 // Test toggling state
1227 {
1231 {
1234 }
1235
1236 return NTFR(TFR.FAIL);
1237 }
1238
1239 //---------------------------------------------------------------------------
1240 // Test toggling immediate state
1242 {
1246 {
1249 }
1250
1251 return NTFR(TFR.FAIL);
1252 }
1253
1254 //---------------------------------------------------------------------------
1255 // Test SetFlags/GetFlags
1257 {
1259
1260 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1261 {
1263 EnProfiler.SetFlags(flags);
1264
1265 if (!Assert(EnProfiler.GetFlags() == flags))
1266 {
1268 return NTFR(TFR.FAIL);
1269 }
1270
1271 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1272 {
1274 EnProfiler.SetFlags(flags);
1275
1276 if (!Assert(EnProfiler.GetFlags() == flags))
1277 {
1279 return NTFR(TFR.FAIL);
1280 }
1281 }
1282 }
1283
1284 // Let's test some bogus
1285 EnProfiler.SetFlags(-333);
1288 if (!Assert(bogusFlags == 0))
1289 {
1291 return NTFR(TFR.FAIL);
1292 }
1293
1296 if (!Assert(bogusFlags == 0))
1297 {
1299 return NTFR(TFR.FAIL);
1300 }
1301
1302 // Reset
1304 return NTFR(TFR.SUCCESS);
1305 }
1306
1307 //---------------------------------------------------------------------------
1308 // Test removing of flags
1310 {
1312
1314
1316 {
1318 return NTFR(TFR.FAIL);
1319 }
1320
1323
1324 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1325 {
1327 return NTFR(TFR.FAIL);
1328 }
1329
1332
1333 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1334 {
1336 return NTFR(TFR.FAIL);
1337 }
1338
1339 // Reset
1341 return NTFR(TFR.SUCCESS);
1342 }
1343
1344 //---------------------------------------------------------------------------
1345 // Test adding of flags
1347 {
1349
1351
1352 // Return should match resulting flags
1354 {
1356 return NTFR(TFR.FAIL);
1357 }
1358
1359 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1360 {
1362 return NTFR(TFR.FAIL);
1363 }
1364
1365 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1366 {
1368 return NTFR(TFR.FAIL);
1369 }
1370
1371 // Reset
1373 return NTFR(TFR.SUCCESS);
1374 }
1375
1376 //---------------------------------------------------------------------------
1377 // Test module
1379 {
1380 // File lives in Game, use it while testing
1382
1383 // This was added at the same time as this API, so check if it works as well
1384 string nameOfCurrentModule = Type().GetModule();
1385 if (!Assert(nameOfCurrentModule != ""))
1386 {
1387 return NTFR(TFR.FAIL);
1388 }
1389
1390 // We know we are in Game, so use it as a test
1393 {
1394 return NTFR(TFR.FAIL);
1395 }
1396
1398 {
1399 return NTFR(TFR.FAIL);
1400 }
1401
1402 // Test if setting and getting works
1405
1407 {
1409 return NTFR(TFR.FAIL);
1410 }
1411
1412 // Data to restore
1415
1416 // Make sure we are only profiling Game and that the data is clean
1417 // Only valid for the Get...Per... methods, as they need to be sorted
1419
1420 // GetTickTime() returns in seconds, so gather the results in seconds too
1423
1424 // Time to sleeb
1425 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1426 float timeSlept = Sleep(0.3);
1427 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1428 float diff = postTime - previousTime - timeSlept;
1429
1430 // Restore
1432
1433 // We called the function, so it must have some time
1434 if (!Assert(postTime > 0))
1435 {
1437
1438 if (!wasEnabled)
1439 EnProfiler.Enable(false, true);
1440
1442
1443 return NTFR(TFR.FAIL);
1444 }
1445
1446 if (!Assert(diff < 0.00001))
1447 {
1449
1450 if (!wasEnabled)
1451 EnProfiler.Enable(false, true);
1452
1454
1455 return NTFR(TFR.FAIL);
1456 }
1457
1458 // Clean the session
1460
1461 // Something from a different module should not get sorted, so just fire something from a different module
1462 for (int i = 0; i < 1000; ++i)
1463 {
1464 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1465 }
1466
1467 // Sort and gather the data and validate if it is correct
1471
1472 Debug.TFLog("Game fncs:", this, "TestModule");
1473
1474 int funcCount = timePerFunc.Count();
1475 for (int j = 0; j < funcCount; ++j)
1476 {
1478 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1479 // We are currently profiling Game, so this Core function shouldn't be present
1480 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1481 {
1483
1484 if (!wasEnabled)
1485 EnProfiler.Enable(false, true);
1486
1488
1489 return NTFR(TFR.FAIL);
1490 }
1491 }
1492
1495
1496 int classCount = timePerClass.Count();
1497 for (int k = 0; k < classCount; ++k)
1498 {
1499 typename type = timePerClass[k].param2;
1501 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1502 {
1504
1505 if (!wasEnabled)
1506 EnProfiler.Enable(false, true);
1507
1509
1510 return NTFR(TFR.FAIL);
1511 }
1512
1513 // Only classes from Game should be present
1514 if (!Assert(classModule == eptModule))
1515 {
1517
1518 if (!wasEnabled)
1519 EnProfiler.Enable(false, true);
1520
1522
1523 return NTFR(TFR.FAIL);
1524 }
1525 }
1526
1527 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1530 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1532
1533 bool found = false;
1534
1535 Debug.TFLog("Core fncs:", this, "TestModule");
1536
1537 funcCount = timePerFunc.Count();
1538 for (int l = 0; l < funcCount; ++l)
1539 {
1541 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1542 // We are currently profiling Core, so this Core function should be present
1543 if (tfpc.param2 == "EnumTools::StringToEnum")
1544 {
1545 found = true;
1546 break;
1547 }
1548 }
1549
1550 Assert(found);
1551
1552 // Test some bogus
1554 EnProfiler.SetModule(-333);
1555 bool success = Assert(EnProfiler.GetModule() == mod);
1556 EnProfiler.SetModule(6003);
1558
1561
1562 if (!wasEnabled)
1563 EnProfiler.Enable(false, true);
1564
1565 return BTFR(success && found);
1566 }
1567
1568 //---------------------------------------------------------------------------
1569 // Test to see if class 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 // Create the classes
1581
1582 // Time to stress
1583 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1584 float timeStressed = clss.DoEverything();
1585 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1586 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1589
1590 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1591
1592 // Restore
1594 if (!wasEnabled)
1595 EnProfiler.Enable(false, true);
1596
1597 // We called the function, so it must have some time
1598 if (!Assert(postTime > 0))
1599 {
1600 return NTFR(TFR.FAIL);
1601 }
1602
1604 {
1605 return NTFR(TFR.FAIL);
1606 }
1607
1608 if (!Assert(diff < 0.001))
1609 {
1610 return NTFR(TFR.FAIL);
1611 }
1612
1613 return NTFR(TFR.SUCCESS);
1614 }
1615
1616 //---------------------------------------------------------------------------
1617 // Test to see if class count data is correct
1619 {
1620 const int allocAmount = 9;
1621 const int releaseAmount = 6;
1623
1624 // We should restore this when done
1626
1627 // Time to test
1630
1632 for (int i = 0; i < allocAmount; ++i)
1633 {
1634 instanceArr.Insert(new EPTHelperClass());
1635 }
1636
1637 for (int j = 0; j < releaseAmount; ++j)
1638 {
1639 delete instanceArr[j];
1640 }
1641
1642 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1643 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1644
1647
1648 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1649
1650 // Restore
1651 if (!wasEnabled)
1652 EnProfiler.Enable(false, true);
1653
1654 // Time to check
1655 if (!Assert(alloced == allocAmount))
1656 {
1657 return NTFR(TFR.FAIL);
1658 }
1659
1661 {
1662 return NTFR(TFR.FAIL);
1663 }
1664
1665 return NTFR(TFR.SUCCESS);
1666 }
1667
1668 //---------------------------------------------------------------------------
1669 // Test to see if func time data is correct
1671 {
1672 // We should restore this when done
1675
1676 // GetTickTime() returns in seconds, so gather the results in seconds too
1678
1679 // Time to stress
1680 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1681 float timeStressed = StringFormat();
1682 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1685
1686 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1687 float timeStressed2 = StringConcat();
1688 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1691
1692 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1693
1694 // Restore
1696 if (!wasEnabled)
1697 {
1698 EnProfiler.Enable(false, true);
1699 }
1700
1701 // We called the function, so it must have some time
1702 if (!Assert(postTime > 0))
1703 {
1704 return NTFR(TFR.FAIL);
1705 }
1706
1707 if (!Assert(diff < 0.001))
1708 {
1709 return NTFR(TFR.FAIL);
1710 }
1711
1712 if (!Assert(postTime2 > 0))
1713 {
1714 return NTFR(TFR.FAIL);
1715 }
1716
1717 if (!Assert(diff2 < 0.001))
1718 {
1719 return NTFR(TFR.FAIL);
1720 }
1721
1722 // I know that string.Format is faster than additive concatenation
1724 {
1725 return NTFR(TFR.FAIL);
1726 }
1727
1728 return NTFR(TFR.SUCCESS);
1729 }
1730
1731 //---------------------------------------------------------------------------
1732 // Test to see if func count data is correct
1734 {
1735 // We should restore this when done
1737
1738 // Time to count
1739
1740 // - CallFunction
1741 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1742 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1743 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1744
1746
1747 // - CallFunctionParams
1748 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1749 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1750 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1751
1753
1754 // - Regular call
1755 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1757 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1758
1760
1761 // - Call
1762 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1763 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1764 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1765
1767
1768 // - Garbage
1769 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1770
1771 // - Static
1772 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1774 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1775
1777
1778 // - Global
1779 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1780 GetDayZGame();
1781 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1782
1784
1785 // - Global proto
1786 // Not tracked, so don't need to compare before and after, should always be 0
1787 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1788 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1789
1790 // - Static proto
1791 // Not tracked, so don't need to compare before and after, should always be 0
1792 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1793
1794 // - proto
1795 // Not tracked, so don't need to compare before and after, should always be 0
1796 GetGame().GetHostName();
1797 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1798
1799 // - proto native
1800 // Not tracked, so don't need to compare before and after, should always be 0
1801 GetGame().IsServer();
1802 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1803
1804 // - static proto native
1805 // Not tracked, so don't need to compare before and after, should always be 0
1807 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1808
1809 // Restore
1810 if (!wasEnabled)
1811 {
1812 EnProfiler.Enable(false, true);
1813 }
1814
1815 // Do the checks
1816
1817 // - CallFunction
1818 if (!Assert(callCountCF == 1))
1819 {
1820 return NTFR(TFR.FAIL);
1821 }
1822
1823 // - CallFunctionParams
1824 if (!Assert(callCountCFP == 1))
1825 {
1826 return NTFR(TFR.FAIL);
1827 }
1828
1829 // - Regular call
1830 if (!Assert(callCountRG == 1))
1831 {
1832 return NTFR(TFR.FAIL);
1833 }
1834
1835 // - Call
1836 if (!Assert(callCountC == 1))
1837 {
1838 return NTFR(TFR.FAIL);
1839 }
1840
1841 // - Garbage
1842 if (!Assert(callCountNon == -1))
1843 {
1844 return NTFR(TFR.FAIL);
1845 }
1846
1847 // - Static
1848 if (!Assert(callCountS == 1))
1849 {
1850 return NTFR(TFR.FAIL);
1851 }
1852
1853 // - Global
1854 if (!Assert(callCountG == 1))
1855 {
1856 return NTFR(TFR.FAIL);
1857 }
1858
1859 // - Global proto
1860 if (!Assert(callCountGP == 0))
1861 {
1862 return NTFR(TFR.FAIL);
1863 }
1864
1865 // - Static proto
1866 if (!Assert(callCountSP == 0))
1867 {
1868 return NTFR(TFR.FAIL);
1869 }
1870
1871 // - proto
1872 if (!Assert(callCountP == 0))
1873 {
1874 return NTFR(TFR.FAIL);
1875 }
1876
1877 // - proto native
1878 if (!Assert(callCountPN == 0))
1879 {
1880 return NTFR(TFR.FAIL);
1881 }
1882
1883 // - static proto native
1884 if (!Assert(callCountSPN == 0))
1885 {
1886 return NTFR(TFR.FAIL);
1887 }
1888
1889 return NTFR(TFR.SUCCESS);
1890 }
1891
1892 //---------------------------------------------------------------------------
1893 // Helpers
1894 //---------------------------------------------------------------------------
1895 // Snore
1896 float Sleep(float timeS)
1897 {
1898 float startTime = GetGame().GetTickTime();
1899 while (GetGame().GetTickTime() - startTime < timeS)
1900 {
1901 // Zzz
1902 }
1903
1904 return GetGame().GetTickTime() - startTime;
1905 }
1906
1907 //---------------------------------------------------------------------------
1908 // Example stress method
1909 float StringFormat()
1910 {
1911 float startTime = GetGame().GetTickTime();
1912
1913 for (int i = 0; i < 1000; ++i)
1914 {
1915 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1916 }
1917
1918 return GetGame().GetTickTime() - startTime;
1919 }
1920
1921 //---------------------------------------------------------------------------
1922 // Example stress method 2
1923 float StringConcat()
1924 {
1925 float startTime = GetGame().GetTickTime();
1926
1927 for (int i = 0; i < 1000; ++i)
1928 {
1929 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1930 }
1931
1932 return GetGame().GetTickTime() - startTime;
1933 }
1934
1935 //---------------------------------------------------------------------------
1936 // To make sure it is only ever called in that test
1938 {
1939 int dummy = 3;
1940 }
1941
1942 //---------------------------------------------------------------------------
1943 // To make sure it is only ever called in that test
1944 static void TestFuncCountDataHelperStatic()
1945 {
1946 int dummy = 3;
1947 }
1948}
1949
1950class EPTHelperClass
1951{
1952 float Sleep2(float timeS)
1953 {
1954 float startTime = GetGame().GetTickTime();
1955 while (GetGame().GetTickTime() - startTime < timeS)
1956 {
1957 // Zzz
1958 }
1959
1960 return GetGame().GetTickTime() - startTime;
1961 }
1962 float SleepAgain(float timeS)
1963 {
1964 float startTime = GetGame().GetTickTime();
1965 while (GetGame().GetTickTime() - startTime < timeS)
1966 {
1967 // Zzz
1968 }
1969
1970 return GetGame().GetTickTime() - startTime;
1971 }
1972
1973 float DoEverything()
1974 {
1975 float startTime = GetGame().GetTickTime();
1976
1977 Sleep2(3);
1978 SleepAgain(3);
1979
1980 return GetGame().GetTickTime() - startTime;
1981 }
1982}

◆ TestClassTimeData()

TFResult TestClassTimeData ( )
1142 : TestFramework
1143{
1146
1147 //---------------------------------------------------------------------------
1148 // Ctor - Decides the tests to run
1149 //---------------------------------------------------------------------------
1150 void EnProfilerTests()
1151 {
1153
1154 AddInitTest("TestToggling");
1155 AddInitTest("TestTogglingImmediate");
1156 AddInitTest("TestSetFlags");
1157 AddInitTest("TestClearFlags");
1158 AddInitTest("TestAddFlags");
1159 AddInitTest("TestModule");
1160 AddInitTest("TestClassTimeData");
1161 AddInitTest("TestClassCountData");
1162 AddInitTest("TestFuncTimeData");
1163 AddInitTest("TestFuncCountData");
1164 }
1165
1166 //---------------------------------------------------------------------------
1167 // Dtor
1168 //---------------------------------------------------------------------------
1169 void ~EnProfilerTests()
1170 {
1172 }
1173
1174 //---------------------------------------------------------------------------
1175 // Tests
1176 //---------------------------------------------------------------------------
1177 // Test toggling state
1179 {
1183 {
1186 }
1187
1188 return NTFR(TFR.FAIL);
1189 }
1190
1191 //---------------------------------------------------------------------------
1192 // Test toggling immediate state
1194 {
1198 {
1201 }
1202
1203 return NTFR(TFR.FAIL);
1204 }
1205
1206 //---------------------------------------------------------------------------
1207 // Test SetFlags/GetFlags
1209 {
1211
1212 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1213 {
1215 EnProfiler.SetFlags(flags);
1216
1217 if (!Assert(EnProfiler.GetFlags() == flags))
1218 {
1220 return NTFR(TFR.FAIL);
1221 }
1222
1223 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1224 {
1226 EnProfiler.SetFlags(flags);
1227
1228 if (!Assert(EnProfiler.GetFlags() == flags))
1229 {
1231 return NTFR(TFR.FAIL);
1232 }
1233 }
1234 }
1235
1236 // Let's test some bogus
1237 EnProfiler.SetFlags(-333);
1240 if (!Assert(bogusFlags == 0))
1241 {
1243 return NTFR(TFR.FAIL);
1244 }
1245
1248 if (!Assert(bogusFlags == 0))
1249 {
1251 return NTFR(TFR.FAIL);
1252 }
1253
1254 // Reset
1256 return NTFR(TFR.SUCCESS);
1257 }
1258
1259 //---------------------------------------------------------------------------
1260 // Test removing of flags
1262 {
1264
1266
1268 {
1270 return NTFR(TFR.FAIL);
1271 }
1272
1275
1276 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1277 {
1279 return NTFR(TFR.FAIL);
1280 }
1281
1284
1285 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1286 {
1288 return NTFR(TFR.FAIL);
1289 }
1290
1291 // Reset
1293 return NTFR(TFR.SUCCESS);
1294 }
1295
1296 //---------------------------------------------------------------------------
1297 // Test adding of flags
1299 {
1301
1303
1304 // Return should match resulting flags
1306 {
1308 return NTFR(TFR.FAIL);
1309 }
1310
1311 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1312 {
1314 return NTFR(TFR.FAIL);
1315 }
1316
1317 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1318 {
1320 return NTFR(TFR.FAIL);
1321 }
1322
1323 // Reset
1325 return NTFR(TFR.SUCCESS);
1326 }
1327
1328 //---------------------------------------------------------------------------
1329 // Test module
1331 {
1332 // File lives in Game, use it while testing
1334
1335 // This was added at the same time as this API, so check if it works as well
1336 string nameOfCurrentModule = Type().GetModule();
1337 if (!Assert(nameOfCurrentModule != ""))
1338 {
1339 return NTFR(TFR.FAIL);
1340 }
1341
1342 // We know we are in Game, so use it as a test
1345 {
1346 return NTFR(TFR.FAIL);
1347 }
1348
1350 {
1351 return NTFR(TFR.FAIL);
1352 }
1353
1354 // Test if setting and getting works
1357
1359 {
1361 return NTFR(TFR.FAIL);
1362 }
1363
1364 // Data to restore
1367
1368 // Make sure we are only profiling Game and that the data is clean
1369 // Only valid for the Get...Per... methods, as they need to be sorted
1371
1372 // GetTickTime() returns in seconds, so gather the results in seconds too
1375
1376 // Time to sleeb
1377 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1378 float timeSlept = Sleep(0.3);
1379 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1380 float diff = postTime - previousTime - timeSlept;
1381
1382 // Restore
1384
1385 // We called the function, so it must have some time
1386 if (!Assert(postTime > 0))
1387 {
1389
1390 if (!wasEnabled)
1391 EnProfiler.Enable(false, true);
1392
1394
1395 return NTFR(TFR.FAIL);
1396 }
1397
1398 if (!Assert(diff < 0.00001))
1399 {
1401
1402 if (!wasEnabled)
1403 EnProfiler.Enable(false, true);
1404
1406
1407 return NTFR(TFR.FAIL);
1408 }
1409
1410 // Clean the session
1412
1413 // Something from a different module should not get sorted, so just fire something from a different module
1414 for (int i = 0; i < 1000; ++i)
1415 {
1416 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1417 }
1418
1419 // Sort and gather the data and validate if it is correct
1423
1424 Debug.TFLog("Game fncs:", this, "TestModule");
1425
1426 int funcCount = timePerFunc.Count();
1427 for (int j = 0; j < funcCount; ++j)
1428 {
1430 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1431 // We are currently profiling Game, so this Core function shouldn't be present
1432 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1433 {
1435
1436 if (!wasEnabled)
1437 EnProfiler.Enable(false, true);
1438
1440
1441 return NTFR(TFR.FAIL);
1442 }
1443 }
1444
1447
1448 int classCount = timePerClass.Count();
1449 for (int k = 0; k < classCount; ++k)
1450 {
1451 typename type = timePerClass[k].param2;
1453 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1454 {
1456
1457 if (!wasEnabled)
1458 EnProfiler.Enable(false, true);
1459
1461
1462 return NTFR(TFR.FAIL);
1463 }
1464
1465 // Only classes from Game should be present
1466 if (!Assert(classModule == eptModule))
1467 {
1469
1470 if (!wasEnabled)
1471 EnProfiler.Enable(false, true);
1472
1474
1475 return NTFR(TFR.FAIL);
1476 }
1477 }
1478
1479 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1482 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1484
1485 bool found = false;
1486
1487 Debug.TFLog("Core fncs:", this, "TestModule");
1488
1489 funcCount = timePerFunc.Count();
1490 for (int l = 0; l < funcCount; ++l)
1491 {
1493 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1494 // We are currently profiling Core, so this Core function should be present
1495 if (tfpc.param2 == "EnumTools::StringToEnum")
1496 {
1497 found = true;
1498 break;
1499 }
1500 }
1501
1502 Assert(found);
1503
1504 // Test some bogus
1506 EnProfiler.SetModule(-333);
1507 bool success = Assert(EnProfiler.GetModule() == mod);
1508 EnProfiler.SetModule(6003);
1510
1513
1514 if (!wasEnabled)
1515 EnProfiler.Enable(false, true);
1516
1517 return BTFR(success && found);
1518 }
1519
1520 //---------------------------------------------------------------------------
1521 // Test to see if class time data is correct
1523 {
1524 // We should restore this when done
1527
1528 // GetTickTime() returns in seconds, so gather the results in seconds too
1530
1531 // Create the classes
1533
1534 // Time to stress
1535 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1536 float timeStressed = clss.DoEverything();
1537 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1538 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1541
1542 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1543
1544 // Restore
1546 if (!wasEnabled)
1547 EnProfiler.Enable(false, true);
1548
1549 // We called the function, so it must have some time
1550 if (!Assert(postTime > 0))
1551 {
1552 return NTFR(TFR.FAIL);
1553 }
1554
1556 {
1557 return NTFR(TFR.FAIL);
1558 }
1559
1560 if (!Assert(diff < 0.001))
1561 {
1562 return NTFR(TFR.FAIL);
1563 }
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 {
1586 instanceArr.Insert(new EPTHelperClass());
1587 }
1588
1589 for (int j = 0; j < releaseAmount; ++j)
1590 {
1591 delete instanceArr[j];
1592 }
1593
1594 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1595 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1596
1599
1600 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1601
1602 // Restore
1603 if (!wasEnabled)
1604 EnProfiler.Enable(false, true);
1605
1606 // Time to check
1607 if (!Assert(alloced == allocAmount))
1608 {
1609 return NTFR(TFR.FAIL);
1610 }
1611
1613 {
1614 return NTFR(TFR.FAIL);
1615 }
1616
1617 return NTFR(TFR.SUCCESS);
1618 }
1619
1620 //---------------------------------------------------------------------------
1621 // Test to see if func time data is correct
1623 {
1624 // We should restore this when done
1627
1628 // GetTickTime() returns in seconds, so gather the results in seconds too
1630
1631 // Time to stress
1632 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1633 float timeStressed = StringFormat();
1634 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1637
1638 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1639 float timeStressed2 = StringConcat();
1640 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1643
1644 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1645
1646 // Restore
1648 if (!wasEnabled)
1649 {
1650 EnProfiler.Enable(false, true);
1651 }
1652
1653 // We called the function, so it must have some time
1654 if (!Assert(postTime > 0))
1655 {
1656 return NTFR(TFR.FAIL);
1657 }
1658
1659 if (!Assert(diff < 0.001))
1660 {
1661 return NTFR(TFR.FAIL);
1662 }
1663
1664 if (!Assert(postTime2 > 0))
1665 {
1666 return NTFR(TFR.FAIL);
1667 }
1668
1669 if (!Assert(diff2 < 0.001))
1670 {
1671 return NTFR(TFR.FAIL);
1672 }
1673
1674 // I know that string.Format is faster than additive concatenation
1676 {
1677 return NTFR(TFR.FAIL);
1678 }
1679
1680 return NTFR(TFR.SUCCESS);
1681 }
1682
1683 //---------------------------------------------------------------------------
1684 // Test to see if func count data is correct
1686 {
1687 // We should restore this when done
1689
1690 // Time to count
1691
1692 // - CallFunction
1693 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1694 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1695 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1696
1698
1699 // - CallFunctionParams
1700 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1701 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1702 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1703
1705
1706 // - Regular call
1707 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1709 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1710
1712
1713 // - Call
1714 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1715 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1716 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1717
1719
1720 // - Garbage
1721 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1722
1723 // - Static
1724 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1726 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1727
1729
1730 // - Global
1731 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1732 GetDayZGame();
1733 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1734
1736
1737 // - Global proto
1738 // Not tracked, so don't need to compare before and after, should always be 0
1739 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1740 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1741
1742 // - Static proto
1743 // Not tracked, so don't need to compare before and after, should always be 0
1744 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1745
1746 // - proto
1747 // Not tracked, so don't need to compare before and after, should always be 0
1748 GetGame().GetHostName();
1749 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1750
1751 // - proto native
1752 // Not tracked, so don't need to compare before and after, should always be 0
1753 GetGame().IsServer();
1754 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1755
1756 // - static proto native
1757 // Not tracked, so don't need to compare before and after, should always be 0
1759 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1760
1761 // Restore
1762 if (!wasEnabled)
1763 {
1764 EnProfiler.Enable(false, true);
1765 }
1766
1767 // Do the checks
1768
1769 // - CallFunction
1770 if (!Assert(callCountCF == 1))
1771 {
1772 return NTFR(TFR.FAIL);
1773 }
1774
1775 // - CallFunctionParams
1776 if (!Assert(callCountCFP == 1))
1777 {
1778 return NTFR(TFR.FAIL);
1779 }
1780
1781 // - Regular call
1782 if (!Assert(callCountRG == 1))
1783 {
1784 return NTFR(TFR.FAIL);
1785 }
1786
1787 // - Call
1788 if (!Assert(callCountC == 1))
1789 {
1790 return NTFR(TFR.FAIL);
1791 }
1792
1793 // - Garbage
1794 if (!Assert(callCountNon == -1))
1795 {
1796 return NTFR(TFR.FAIL);
1797 }
1798
1799 // - Static
1800 if (!Assert(callCountS == 1))
1801 {
1802 return NTFR(TFR.FAIL);
1803 }
1804
1805 // - Global
1806 if (!Assert(callCountG == 1))
1807 {
1808 return NTFR(TFR.FAIL);
1809 }
1810
1811 // - Global proto
1812 if (!Assert(callCountGP == 0))
1813 {
1814 return NTFR(TFR.FAIL);
1815 }
1816
1817 // - Static proto
1818 if (!Assert(callCountSP == 0))
1819 {
1820 return NTFR(TFR.FAIL);
1821 }
1822
1823 // - proto
1824 if (!Assert(callCountP == 0))
1825 {
1826 return NTFR(TFR.FAIL);
1827 }
1828
1829 // - proto native
1830 if (!Assert(callCountPN == 0))
1831 {
1832 return NTFR(TFR.FAIL);
1833 }
1834
1835 // - static proto native
1836 if (!Assert(callCountSPN == 0))
1837 {
1838 return NTFR(TFR.FAIL);
1839 }
1840
1841 return NTFR(TFR.SUCCESS);
1842 }
1843
1844 //---------------------------------------------------------------------------
1845 // Helpers
1846 //---------------------------------------------------------------------------
1847 // Snore
1848 float Sleep(float timeS)
1849 {
1850 float startTime = GetGame().GetTickTime();
1851 while (GetGame().GetTickTime() - startTime < timeS)
1852 {
1853 // Zzz
1854 }
1855
1856 return GetGame().GetTickTime() - startTime;
1857 }
1858
1859 //---------------------------------------------------------------------------
1860 // Example stress method
1861 float StringFormat()
1862 {
1863 float startTime = GetGame().GetTickTime();
1864
1865 for (int i = 0; i < 1000; ++i)
1866 {
1867 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1868 }
1869
1870 return GetGame().GetTickTime() - startTime;
1871 }
1872
1873 //---------------------------------------------------------------------------
1874 // Example stress method 2
1875 float StringConcat()
1876 {
1877 float startTime = GetGame().GetTickTime();
1878
1879 for (int i = 0; i < 1000; ++i)
1880 {
1881 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1882 }
1883
1884 return GetGame().GetTickTime() - startTime;
1885 }
1886
1887 //---------------------------------------------------------------------------
1888 // To make sure it is only ever called in that test
1890 {
1891 int dummy = 3;
1892 }
1893
1894 //---------------------------------------------------------------------------
1895 // To make sure it is only ever called in that test
1896 static void TestFuncCountDataHelperStatic()
1897 {
1898 int dummy = 3;
1899 }
1900}
1901
1902class EPTHelperClass
1903{
1904 float Sleep2(float timeS)
1905 {
1906 float startTime = GetGame().GetTickTime();
1907 while (GetGame().GetTickTime() - startTime < timeS)
1908 {
1909 // Zzz
1910 }
1911
1912 return GetGame().GetTickTime() - startTime;
1913 }
1914 float SleepAgain(float timeS)
1915 {
1916 float startTime = GetGame().GetTickTime();
1917 while (GetGame().GetTickTime() - startTime < timeS)
1918 {
1919 // Zzz
1920 }
1921
1922 return GetGame().GetTickTime() - startTime;
1923 }
1924
1925 float DoEverything()
1926 {
1927 float startTime = GetGame().GetTickTime();
1928
1929 Sleep2(3);
1930 SleepAgain(3);
1931
1932 return GetGame().GetTickTime() - startTime;
1933 }
1934}

◆ TestClearFlags()

TFResult TestClearFlags ( )
881 : TestFramework
882{
885
886 //---------------------------------------------------------------------------
887 // Ctor - Decides the tests to run
888 //---------------------------------------------------------------------------
889 void EnProfilerTests()
890 {
892
893 AddInitTest("TestToggling");
894 AddInitTest("TestTogglingImmediate");
895 AddInitTest("TestSetFlags");
896 AddInitTest("TestClearFlags");
897 AddInitTest("TestAddFlags");
898 AddInitTest("TestModule");
899 AddInitTest("TestClassTimeData");
900 AddInitTest("TestClassCountData");
901 AddInitTest("TestFuncTimeData");
902 AddInitTest("TestFuncCountData");
903 }
904
905 //---------------------------------------------------------------------------
906 // Dtor
907 //---------------------------------------------------------------------------
908 void ~EnProfilerTests()
909 {
911 }
912
913 //---------------------------------------------------------------------------
914 // Tests
915 //---------------------------------------------------------------------------
916 // Test toggling state
918 {
922 {
925 }
926
927 return NTFR(TFR.FAIL);
928 }
929
930 //---------------------------------------------------------------------------
931 // Test toggling immediate state
933 {
937 {
940 }
941
942 return NTFR(TFR.FAIL);
943 }
944
945 //---------------------------------------------------------------------------
946 // Test SetFlags/GetFlags
948 {
950
951 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
952 {
954 EnProfiler.SetFlags(flags);
955
956 if (!Assert(EnProfiler.GetFlags() == flags))
957 {
959 return NTFR(TFR.FAIL);
960 }
961
962 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
963 {
965 EnProfiler.SetFlags(flags);
966
967 if (!Assert(EnProfiler.GetFlags() == flags))
968 {
970 return NTFR(TFR.FAIL);
971 }
972 }
973 }
974
975 // Let's test some bogus
976 EnProfiler.SetFlags(-333);
979 if (!Assert(bogusFlags == 0))
980 {
982 return NTFR(TFR.FAIL);
983 }
984
987 if (!Assert(bogusFlags == 0))
988 {
990 return NTFR(TFR.FAIL);
991 }
992
993 // Reset
995 return NTFR(TFR.SUCCESS);
996 }
997
998 //---------------------------------------------------------------------------
999 // Test removing of flags
1001 {
1003
1005
1007 {
1009 return NTFR(TFR.FAIL);
1010 }
1011
1014
1015 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1016 {
1018 return NTFR(TFR.FAIL);
1019 }
1020
1023
1024 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1025 {
1027 return NTFR(TFR.FAIL);
1028 }
1029
1030 // Reset
1032 return NTFR(TFR.SUCCESS);
1033 }
1034
1035 //---------------------------------------------------------------------------
1036 // Test adding of flags
1038 {
1040
1042
1043 // Return should match resulting flags
1045 {
1047 return NTFR(TFR.FAIL);
1048 }
1049
1050 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1051 {
1053 return NTFR(TFR.FAIL);
1054 }
1055
1056 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1057 {
1059 return NTFR(TFR.FAIL);
1060 }
1061
1062 // Reset
1064 return NTFR(TFR.SUCCESS);
1065 }
1066
1067 //---------------------------------------------------------------------------
1068 // Test module
1070 {
1071 // File lives in Game, use it while testing
1073
1074 // This was added at the same time as this API, so check if it works as well
1075 string nameOfCurrentModule = Type().GetModule();
1076 if (!Assert(nameOfCurrentModule != ""))
1077 {
1078 return NTFR(TFR.FAIL);
1079 }
1080
1081 // We know we are in Game, so use it as a test
1084 {
1085 return NTFR(TFR.FAIL);
1086 }
1087
1089 {
1090 return NTFR(TFR.FAIL);
1091 }
1092
1093 // Test if setting and getting works
1096
1098 {
1100 return NTFR(TFR.FAIL);
1101 }
1102
1103 // Data to restore
1106
1107 // Make sure we are only profiling Game and that the data is clean
1108 // Only valid for the Get...Per... methods, as they need to be sorted
1110
1111 // GetTickTime() returns in seconds, so gather the results in seconds too
1114
1115 // Time to sleeb
1116 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1117 float timeSlept = Sleep(0.3);
1118 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1119 float diff = postTime - previousTime - timeSlept;
1120
1121 // Restore
1123
1124 // We called the function, so it must have some time
1125 if (!Assert(postTime > 0))
1126 {
1128
1129 if (!wasEnabled)
1130 EnProfiler.Enable(false, true);
1131
1133
1134 return NTFR(TFR.FAIL);
1135 }
1136
1137 if (!Assert(diff < 0.00001))
1138 {
1140
1141 if (!wasEnabled)
1142 EnProfiler.Enable(false, true);
1143
1145
1146 return NTFR(TFR.FAIL);
1147 }
1148
1149 // Clean the session
1151
1152 // Something from a different module should not get sorted, so just fire something from a different module
1153 for (int i = 0; i < 1000; ++i)
1154 {
1155 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1156 }
1157
1158 // Sort and gather the data and validate if it is correct
1162
1163 Debug.TFLog("Game fncs:", this, "TestModule");
1164
1165 int funcCount = timePerFunc.Count();
1166 for (int j = 0; j < funcCount; ++j)
1167 {
1169 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1170 // We are currently profiling Game, so this Core function shouldn't be present
1171 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1172 {
1174
1175 if (!wasEnabled)
1176 EnProfiler.Enable(false, true);
1177
1179
1180 return NTFR(TFR.FAIL);
1181 }
1182 }
1183
1186
1187 int classCount = timePerClass.Count();
1188 for (int k = 0; k < classCount; ++k)
1189 {
1190 typename type = timePerClass[k].param2;
1192 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1193 {
1195
1196 if (!wasEnabled)
1197 EnProfiler.Enable(false, true);
1198
1200
1201 return NTFR(TFR.FAIL);
1202 }
1203
1204 // Only classes from Game should be present
1205 if (!Assert(classModule == eptModule))
1206 {
1208
1209 if (!wasEnabled)
1210 EnProfiler.Enable(false, true);
1211
1213
1214 return NTFR(TFR.FAIL);
1215 }
1216 }
1217
1218 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1221 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1223
1224 bool found = false;
1225
1226 Debug.TFLog("Core fncs:", this, "TestModule");
1227
1228 funcCount = timePerFunc.Count();
1229 for (int l = 0; l < funcCount; ++l)
1230 {
1232 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1233 // We are currently profiling Core, so this Core function should be present
1234 if (tfpc.param2 == "EnumTools::StringToEnum")
1235 {
1236 found = true;
1237 break;
1238 }
1239 }
1240
1241 Assert(found);
1242
1243 // Test some bogus
1245 EnProfiler.SetModule(-333);
1246 bool success = Assert(EnProfiler.GetModule() == mod);
1247 EnProfiler.SetModule(6003);
1249
1252
1253 if (!wasEnabled)
1254 EnProfiler.Enable(false, true);
1255
1256 return BTFR(success && found);
1257 }
1258
1259 //---------------------------------------------------------------------------
1260 // Test to see if class time data is correct
1262 {
1263 // We should restore this when done
1266
1267 // GetTickTime() returns in seconds, so gather the results in seconds too
1269
1270 // Create the classes
1272
1273 // Time to stress
1274 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1275 float timeStressed = clss.DoEverything();
1276 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1277 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1280
1281 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1282
1283 // Restore
1285 if (!wasEnabled)
1286 EnProfiler.Enable(false, true);
1287
1288 // We called the function, so it must have some time
1289 if (!Assert(postTime > 0))
1290 {
1291 return NTFR(TFR.FAIL);
1292 }
1293
1295 {
1296 return NTFR(TFR.FAIL);
1297 }
1298
1299 if (!Assert(diff < 0.001))
1300 {
1301 return NTFR(TFR.FAIL);
1302 }
1303
1304 return NTFR(TFR.SUCCESS);
1305 }
1306
1307 //---------------------------------------------------------------------------
1308 // Test to see if class count data is correct
1310 {
1311 const int allocAmount = 9;
1312 const int releaseAmount = 6;
1314
1315 // We should restore this when done
1317
1318 // Time to test
1321
1323 for (int i = 0; i < allocAmount; ++i)
1324 {
1325 instanceArr.Insert(new EPTHelperClass());
1326 }
1327
1328 for (int j = 0; j < releaseAmount; ++j)
1329 {
1330 delete instanceArr[j];
1331 }
1332
1333 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1334 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1335
1338
1339 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1340
1341 // Restore
1342 if (!wasEnabled)
1343 EnProfiler.Enable(false, true);
1344
1345 // Time to check
1346 if (!Assert(alloced == allocAmount))
1347 {
1348 return NTFR(TFR.FAIL);
1349 }
1350
1352 {
1353 return NTFR(TFR.FAIL);
1354 }
1355
1356 return NTFR(TFR.SUCCESS);
1357 }
1358
1359 //---------------------------------------------------------------------------
1360 // Test to see if func time data is correct
1362 {
1363 // We should restore this when done
1366
1367 // GetTickTime() returns in seconds, so gather the results in seconds too
1369
1370 // Time to stress
1371 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1372 float timeStressed = StringFormat();
1373 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1376
1377 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1378 float timeStressed2 = StringConcat();
1379 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1382
1383 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1384
1385 // Restore
1387 if (!wasEnabled)
1388 {
1389 EnProfiler.Enable(false, true);
1390 }
1391
1392 // We called the function, so it must have some time
1393 if (!Assert(postTime > 0))
1394 {
1395 return NTFR(TFR.FAIL);
1396 }
1397
1398 if (!Assert(diff < 0.001))
1399 {
1400 return NTFR(TFR.FAIL);
1401 }
1402
1403 if (!Assert(postTime2 > 0))
1404 {
1405 return NTFR(TFR.FAIL);
1406 }
1407
1408 if (!Assert(diff2 < 0.001))
1409 {
1410 return NTFR(TFR.FAIL);
1411 }
1412
1413 // I know that string.Format is faster than additive concatenation
1415 {
1416 return NTFR(TFR.FAIL);
1417 }
1418
1419 return NTFR(TFR.SUCCESS);
1420 }
1421
1422 //---------------------------------------------------------------------------
1423 // Test to see if func count data is correct
1425 {
1426 // We should restore this when done
1428
1429 // Time to count
1430
1431 // - CallFunction
1432 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1433 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1434 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1435
1437
1438 // - CallFunctionParams
1439 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1440 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1441 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1442
1444
1445 // - Regular call
1446 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1448 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1449
1451
1452 // - Call
1453 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1454 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1455 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1456
1458
1459 // - Garbage
1460 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1461
1462 // - Static
1463 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1465 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1466
1468
1469 // - Global
1470 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1471 GetDayZGame();
1472 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1473
1475
1476 // - Global proto
1477 // Not tracked, so don't need to compare before and after, should always be 0
1478 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1479 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1480
1481 // - Static proto
1482 // Not tracked, so don't need to compare before and after, should always be 0
1483 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1484
1485 // - proto
1486 // Not tracked, so don't need to compare before and after, should always be 0
1487 GetGame().GetHostName();
1488 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1489
1490 // - proto native
1491 // Not tracked, so don't need to compare before and after, should always be 0
1492 GetGame().IsServer();
1493 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1494
1495 // - static proto native
1496 // Not tracked, so don't need to compare before and after, should always be 0
1498 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1499
1500 // Restore
1501 if (!wasEnabled)
1502 {
1503 EnProfiler.Enable(false, true);
1504 }
1505
1506 // Do the checks
1507
1508 // - CallFunction
1509 if (!Assert(callCountCF == 1))
1510 {
1511 return NTFR(TFR.FAIL);
1512 }
1513
1514 // - CallFunctionParams
1515 if (!Assert(callCountCFP == 1))
1516 {
1517 return NTFR(TFR.FAIL);
1518 }
1519
1520 // - Regular call
1521 if (!Assert(callCountRG == 1))
1522 {
1523 return NTFR(TFR.FAIL);
1524 }
1525
1526 // - Call
1527 if (!Assert(callCountC == 1))
1528 {
1529 return NTFR(TFR.FAIL);
1530 }
1531
1532 // - Garbage
1533 if (!Assert(callCountNon == -1))
1534 {
1535 return NTFR(TFR.FAIL);
1536 }
1537
1538 // - Static
1539 if (!Assert(callCountS == 1))
1540 {
1541 return NTFR(TFR.FAIL);
1542 }
1543
1544 // - Global
1545 if (!Assert(callCountG == 1))
1546 {
1547 return NTFR(TFR.FAIL);
1548 }
1549
1550 // - Global proto
1551 if (!Assert(callCountGP == 0))
1552 {
1553 return NTFR(TFR.FAIL);
1554 }
1555
1556 // - Static proto
1557 if (!Assert(callCountSP == 0))
1558 {
1559 return NTFR(TFR.FAIL);
1560 }
1561
1562 // - proto
1563 if (!Assert(callCountP == 0))
1564 {
1565 return NTFR(TFR.FAIL);
1566 }
1567
1568 // - proto native
1569 if (!Assert(callCountPN == 0))
1570 {
1571 return NTFR(TFR.FAIL);
1572 }
1573
1574 // - static proto native
1575 if (!Assert(callCountSPN == 0))
1576 {
1577 return NTFR(TFR.FAIL);
1578 }
1579
1580 return NTFR(TFR.SUCCESS);
1581 }
1582
1583 //---------------------------------------------------------------------------
1584 // Helpers
1585 //---------------------------------------------------------------------------
1586 // Snore
1587 float Sleep(float timeS)
1588 {
1589 float startTime = GetGame().GetTickTime();
1590 while (GetGame().GetTickTime() - startTime < timeS)
1591 {
1592 // Zzz
1593 }
1594
1595 return GetGame().GetTickTime() - startTime;
1596 }
1597
1598 //---------------------------------------------------------------------------
1599 // Example stress method
1600 float StringFormat()
1601 {
1602 float startTime = GetGame().GetTickTime();
1603
1604 for (int i = 0; i < 1000; ++i)
1605 {
1606 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1607 }
1608
1609 return GetGame().GetTickTime() - startTime;
1610 }
1611
1612 //---------------------------------------------------------------------------
1613 // Example stress method 2
1614 float StringConcat()
1615 {
1616 float startTime = GetGame().GetTickTime();
1617
1618 for (int i = 0; i < 1000; ++i)
1619 {
1620 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1621 }
1622
1623 return GetGame().GetTickTime() - startTime;
1624 }
1625
1626 //---------------------------------------------------------------------------
1627 // To make sure it is only ever called in that test
1629 {
1630 int dummy = 3;
1631 }
1632
1633 //---------------------------------------------------------------------------
1634 // To make sure it is only ever called in that test
1635 static void TestFuncCountDataHelperStatic()
1636 {
1637 int dummy = 3;
1638 }
1639}
1640
1641class EPTHelperClass
1642{
1643 float Sleep2(float timeS)
1644 {
1645 float startTime = GetGame().GetTickTime();
1646 while (GetGame().GetTickTime() - startTime < timeS)
1647 {
1648 // Zzz
1649 }
1650
1651 return GetGame().GetTickTime() - startTime;
1652 }
1653 float SleepAgain(float timeS)
1654 {
1655 float startTime = GetGame().GetTickTime();
1656 while (GetGame().GetTickTime() - startTime < timeS)
1657 {
1658 // Zzz
1659 }
1660
1661 return GetGame().GetTickTime() - startTime;
1662 }
1663
1664 float DoEverything()
1665 {
1666 float startTime = GetGame().GetTickTime();
1667
1668 Sleep2(3);
1669 SleepAgain(3);
1670
1671 return GetGame().GetTickTime() - startTime;
1672 }
1673}

◆ TestFuncCountData()

TFResult TestFuncCountData ( )
1305 : TestFramework
1306{
1309
1310 //---------------------------------------------------------------------------
1311 // Ctor - Decides the tests to run
1312 //---------------------------------------------------------------------------
1313 void EnProfilerTests()
1314 {
1316
1317 AddInitTest("TestToggling");
1318 AddInitTest("TestTogglingImmediate");
1319 AddInitTest("TestSetFlags");
1320 AddInitTest("TestClearFlags");
1321 AddInitTest("TestAddFlags");
1322 AddInitTest("TestModule");
1323 AddInitTest("TestClassTimeData");
1324 AddInitTest("TestClassCountData");
1325 AddInitTest("TestFuncTimeData");
1326 AddInitTest("TestFuncCountData");
1327 }
1328
1329 //---------------------------------------------------------------------------
1330 // Dtor
1331 //---------------------------------------------------------------------------
1332 void ~EnProfilerTests()
1333 {
1335 }
1336
1337 //---------------------------------------------------------------------------
1338 // Tests
1339 //---------------------------------------------------------------------------
1340 // Test toggling state
1342 {
1346 {
1349 }
1350
1351 return NTFR(TFR.FAIL);
1352 }
1353
1354 //---------------------------------------------------------------------------
1355 // Test toggling immediate state
1357 {
1361 {
1364 }
1365
1366 return NTFR(TFR.FAIL);
1367 }
1368
1369 //---------------------------------------------------------------------------
1370 // Test SetFlags/GetFlags
1372 {
1374
1375 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1376 {
1378 EnProfiler.SetFlags(flags);
1379
1380 if (!Assert(EnProfiler.GetFlags() == flags))
1381 {
1383 return NTFR(TFR.FAIL);
1384 }
1385
1386 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1387 {
1389 EnProfiler.SetFlags(flags);
1390
1391 if (!Assert(EnProfiler.GetFlags() == flags))
1392 {
1394 return NTFR(TFR.FAIL);
1395 }
1396 }
1397 }
1398
1399 // Let's test some bogus
1400 EnProfiler.SetFlags(-333);
1403 if (!Assert(bogusFlags == 0))
1404 {
1406 return NTFR(TFR.FAIL);
1407 }
1408
1411 if (!Assert(bogusFlags == 0))
1412 {
1414 return NTFR(TFR.FAIL);
1415 }
1416
1417 // Reset
1419 return NTFR(TFR.SUCCESS);
1420 }
1421
1422 //---------------------------------------------------------------------------
1423 // Test removing of flags
1425 {
1427
1429
1431 {
1433 return NTFR(TFR.FAIL);
1434 }
1435
1438
1439 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1440 {
1442 return NTFR(TFR.FAIL);
1443 }
1444
1447
1448 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1449 {
1451 return NTFR(TFR.FAIL);
1452 }
1453
1454 // Reset
1456 return NTFR(TFR.SUCCESS);
1457 }
1458
1459 //---------------------------------------------------------------------------
1460 // Test adding of flags
1462 {
1464
1466
1467 // Return should match resulting flags
1469 {
1471 return NTFR(TFR.FAIL);
1472 }
1473
1474 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1475 {
1477 return NTFR(TFR.FAIL);
1478 }
1479
1480 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1481 {
1483 return NTFR(TFR.FAIL);
1484 }
1485
1486 // Reset
1488 return NTFR(TFR.SUCCESS);
1489 }
1490
1491 //---------------------------------------------------------------------------
1492 // Test module
1494 {
1495 // File lives in Game, use it while testing
1497
1498 // This was added at the same time as this API, so check if it works as well
1499 string nameOfCurrentModule = Type().GetModule();
1500 if (!Assert(nameOfCurrentModule != ""))
1501 {
1502 return NTFR(TFR.FAIL);
1503 }
1504
1505 // We know we are in Game, so use it as a test
1508 {
1509 return NTFR(TFR.FAIL);
1510 }
1511
1513 {
1514 return NTFR(TFR.FAIL);
1515 }
1516
1517 // Test if setting and getting works
1520
1522 {
1524 return NTFR(TFR.FAIL);
1525 }
1526
1527 // Data to restore
1530
1531 // Make sure we are only profiling Game and that the data is clean
1532 // Only valid for the Get...Per... methods, as they need to be sorted
1534
1535 // GetTickTime() returns in seconds, so gather the results in seconds too
1538
1539 // Time to sleeb
1540 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1541 float timeSlept = Sleep(0.3);
1542 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1543 float diff = postTime - previousTime - timeSlept;
1544
1545 // Restore
1547
1548 // We called the function, so it must have some time
1549 if (!Assert(postTime > 0))
1550 {
1552
1553 if (!wasEnabled)
1554 EnProfiler.Enable(false, true);
1555
1557
1558 return NTFR(TFR.FAIL);
1559 }
1560
1561 if (!Assert(diff < 0.00001))
1562 {
1564
1565 if (!wasEnabled)
1566 EnProfiler.Enable(false, true);
1567
1569
1570 return NTFR(TFR.FAIL);
1571 }
1572
1573 // Clean the session
1575
1576 // Something from a different module should not get sorted, so just fire something from a different module
1577 for (int i = 0; i < 1000; ++i)
1578 {
1579 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1580 }
1581
1582 // Sort and gather the data and validate if it is correct
1586
1587 Debug.TFLog("Game fncs:", this, "TestModule");
1588
1589 int funcCount = timePerFunc.Count();
1590 for (int j = 0; j < funcCount; ++j)
1591 {
1593 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1594 // We are currently profiling Game, so this Core function shouldn't be present
1595 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1596 {
1598
1599 if (!wasEnabled)
1600 EnProfiler.Enable(false, true);
1601
1603
1604 return NTFR(TFR.FAIL);
1605 }
1606 }
1607
1610
1611 int classCount = timePerClass.Count();
1612 for (int k = 0; k < classCount; ++k)
1613 {
1614 typename type = timePerClass[k].param2;
1616 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1617 {
1619
1620 if (!wasEnabled)
1621 EnProfiler.Enable(false, true);
1622
1624
1625 return NTFR(TFR.FAIL);
1626 }
1627
1628 // Only classes from Game should be present
1629 if (!Assert(classModule == eptModule))
1630 {
1632
1633 if (!wasEnabled)
1634 EnProfiler.Enable(false, true);
1635
1637
1638 return NTFR(TFR.FAIL);
1639 }
1640 }
1641
1642 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1645 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1647
1648 bool found = false;
1649
1650 Debug.TFLog("Core fncs:", this, "TestModule");
1651
1652 funcCount = timePerFunc.Count();
1653 for (int l = 0; l < funcCount; ++l)
1654 {
1656 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1657 // We are currently profiling Core, so this Core function should be present
1658 if (tfpc.param2 == "EnumTools::StringToEnum")
1659 {
1660 found = true;
1661 break;
1662 }
1663 }
1664
1665 Assert(found);
1666
1667 // Test some bogus
1669 EnProfiler.SetModule(-333);
1670 bool success = Assert(EnProfiler.GetModule() == mod);
1671 EnProfiler.SetModule(6003);
1673
1676
1677 if (!wasEnabled)
1678 EnProfiler.Enable(false, true);
1679
1680 return BTFR(success && found);
1681 }
1682
1683 //---------------------------------------------------------------------------
1684 // Test to see if class time data is correct
1686 {
1687 // We should restore this when done
1690
1691 // GetTickTime() returns in seconds, so gather the results in seconds too
1693
1694 // Create the classes
1696
1697 // Time to stress
1698 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1699 float timeStressed = clss.DoEverything();
1700 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1701 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1704
1705 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1706
1707 // Restore
1709 if (!wasEnabled)
1710 EnProfiler.Enable(false, true);
1711
1712 // We called the function, so it must have some time
1713 if (!Assert(postTime > 0))
1714 {
1715 return NTFR(TFR.FAIL);
1716 }
1717
1719 {
1720 return NTFR(TFR.FAIL);
1721 }
1722
1723 if (!Assert(diff < 0.001))
1724 {
1725 return NTFR(TFR.FAIL);
1726 }
1727
1728 return NTFR(TFR.SUCCESS);
1729 }
1730
1731 //---------------------------------------------------------------------------
1732 // Test to see if class count data is correct
1734 {
1735 const int allocAmount = 9;
1736 const int releaseAmount = 6;
1738
1739 // We should restore this when done
1741
1742 // Time to test
1745
1747 for (int i = 0; i < allocAmount; ++i)
1748 {
1749 instanceArr.Insert(new EPTHelperClass());
1750 }
1751
1752 for (int j = 0; j < releaseAmount; ++j)
1753 {
1754 delete instanceArr[j];
1755 }
1756
1757 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1758 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1759
1762
1763 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1764
1765 // Restore
1766 if (!wasEnabled)
1767 EnProfiler.Enable(false, true);
1768
1769 // Time to check
1770 if (!Assert(alloced == allocAmount))
1771 {
1772 return NTFR(TFR.FAIL);
1773 }
1774
1776 {
1777 return NTFR(TFR.FAIL);
1778 }
1779
1780 return NTFR(TFR.SUCCESS);
1781 }
1782
1783 //---------------------------------------------------------------------------
1784 // Test to see if func time data is correct
1786 {
1787 // We should restore this when done
1790
1791 // GetTickTime() returns in seconds, so gather the results in seconds too
1793
1794 // Time to stress
1795 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1796 float timeStressed = StringFormat();
1797 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1800
1801 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1802 float timeStressed2 = StringConcat();
1803 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1806
1807 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1808
1809 // Restore
1811 if (!wasEnabled)
1812 {
1813 EnProfiler.Enable(false, true);
1814 }
1815
1816 // We called the function, so it must have some time
1817 if (!Assert(postTime > 0))
1818 {
1819 return NTFR(TFR.FAIL);
1820 }
1821
1822 if (!Assert(diff < 0.001))
1823 {
1824 return NTFR(TFR.FAIL);
1825 }
1826
1827 if (!Assert(postTime2 > 0))
1828 {
1829 return NTFR(TFR.FAIL);
1830 }
1831
1832 if (!Assert(diff2 < 0.001))
1833 {
1834 return NTFR(TFR.FAIL);
1835 }
1836
1837 // I know that string.Format is faster than additive concatenation
1839 {
1840 return NTFR(TFR.FAIL);
1841 }
1842
1843 return NTFR(TFR.SUCCESS);
1844 }
1845
1846 //---------------------------------------------------------------------------
1847 // Test to see if func count data is correct
1849 {
1850 // We should restore this when done
1852
1853 // Time to count
1854
1855 // - CallFunction
1856 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1857 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1858 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1859
1861
1862 // - CallFunctionParams
1863 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1864 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1865 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1866
1868
1869 // - Regular call
1870 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1872 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1873
1875
1876 // - Call
1877 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1878 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1879 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1880
1882
1883 // - Garbage
1884 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1885
1886 // - Static
1887 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1889 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1890
1892
1893 // - Global
1894 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1895 GetDayZGame();
1896 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1897
1899
1900 // - Global proto
1901 // Not tracked, so don't need to compare before and after, should always be 0
1902 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1903 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1904
1905 // - Static proto
1906 // Not tracked, so don't need to compare before and after, should always be 0
1907 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1908
1909 // - proto
1910 // Not tracked, so don't need to compare before and after, should always be 0
1911 GetGame().GetHostName();
1912 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1913
1914 // - proto native
1915 // Not tracked, so don't need to compare before and after, should always be 0
1916 GetGame().IsServer();
1917 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1918
1919 // - static proto native
1920 // Not tracked, so don't need to compare before and after, should always be 0
1922 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1923
1924 // Restore
1925 if (!wasEnabled)
1926 {
1927 EnProfiler.Enable(false, true);
1928 }
1929
1930 // Do the checks
1931
1932 // - CallFunction
1933 if (!Assert(callCountCF == 1))
1934 {
1935 return NTFR(TFR.FAIL);
1936 }
1937
1938 // - CallFunctionParams
1939 if (!Assert(callCountCFP == 1))
1940 {
1941 return NTFR(TFR.FAIL);
1942 }
1943
1944 // - Regular call
1945 if (!Assert(callCountRG == 1))
1946 {
1947 return NTFR(TFR.FAIL);
1948 }
1949
1950 // - Call
1951 if (!Assert(callCountC == 1))
1952 {
1953 return NTFR(TFR.FAIL);
1954 }
1955
1956 // - Garbage
1957 if (!Assert(callCountNon == -1))
1958 {
1959 return NTFR(TFR.FAIL);
1960 }
1961
1962 // - Static
1963 if (!Assert(callCountS == 1))
1964 {
1965 return NTFR(TFR.FAIL);
1966 }
1967
1968 // - Global
1969 if (!Assert(callCountG == 1))
1970 {
1971 return NTFR(TFR.FAIL);
1972 }
1973
1974 // - Global proto
1975 if (!Assert(callCountGP == 0))
1976 {
1977 return NTFR(TFR.FAIL);
1978 }
1979
1980 // - Static proto
1981 if (!Assert(callCountSP == 0))
1982 {
1983 return NTFR(TFR.FAIL);
1984 }
1985
1986 // - proto
1987 if (!Assert(callCountP == 0))
1988 {
1989 return NTFR(TFR.FAIL);
1990 }
1991
1992 // - proto native
1993 if (!Assert(callCountPN == 0))
1994 {
1995 return NTFR(TFR.FAIL);
1996 }
1997
1998 // - static proto native
1999 if (!Assert(callCountSPN == 0))
2000 {
2001 return NTFR(TFR.FAIL);
2002 }
2003
2004 return NTFR(TFR.SUCCESS);
2005 }
2006
2007 //---------------------------------------------------------------------------
2008 // Helpers
2009 //---------------------------------------------------------------------------
2010 // Snore
2011 float Sleep(float timeS)
2012 {
2013 float startTime = GetGame().GetTickTime();
2014 while (GetGame().GetTickTime() - startTime < timeS)
2015 {
2016 // Zzz
2017 }
2018
2019 return GetGame().GetTickTime() - startTime;
2020 }
2021
2022 //---------------------------------------------------------------------------
2023 // Example stress method
2024 float StringFormat()
2025 {
2026 float startTime = GetGame().GetTickTime();
2027
2028 for (int i = 0; i < 1000; ++i)
2029 {
2030 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
2031 }
2032
2033 return GetGame().GetTickTime() - startTime;
2034 }
2035
2036 //---------------------------------------------------------------------------
2037 // Example stress method 2
2038 float StringConcat()
2039 {
2040 float startTime = GetGame().GetTickTime();
2041
2042 for (int i = 0; i < 1000; ++i)
2043 {
2044 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
2045 }
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}

◆ TestFuncCountDataHelper()

void TestFuncCountDataHelper ( )
1509 : TestFramework
1510{
1513
1514 //---------------------------------------------------------------------------
1515 // Ctor - Decides the tests to run
1516 //---------------------------------------------------------------------------
1517 void EnProfilerTests()
1518 {
1520
1521 AddInitTest("TestToggling");
1522 AddInitTest("TestTogglingImmediate");
1523 AddInitTest("TestSetFlags");
1524 AddInitTest("TestClearFlags");
1525 AddInitTest("TestAddFlags");
1526 AddInitTest("TestModule");
1527 AddInitTest("TestClassTimeData");
1528 AddInitTest("TestClassCountData");
1529 AddInitTest("TestFuncTimeData");
1530 AddInitTest("TestFuncCountData");
1531 }
1532
1533 //---------------------------------------------------------------------------
1534 // Dtor
1535 //---------------------------------------------------------------------------
1536 void ~EnProfilerTests()
1537 {
1539 }
1540
1541 //---------------------------------------------------------------------------
1542 // Tests
1543 //---------------------------------------------------------------------------
1544 // Test toggling state
1546 {
1550 {
1553 }
1554
1555 return NTFR(TFR.FAIL);
1556 }
1557
1558 //---------------------------------------------------------------------------
1559 // Test toggling immediate state
1561 {
1565 {
1568 }
1569
1570 return NTFR(TFR.FAIL);
1571 }
1572
1573 //---------------------------------------------------------------------------
1574 // Test SetFlags/GetFlags
1576 {
1578
1579 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1580 {
1582 EnProfiler.SetFlags(flags);
1583
1584 if (!Assert(EnProfiler.GetFlags() == flags))
1585 {
1587 return NTFR(TFR.FAIL);
1588 }
1589
1590 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1591 {
1593 EnProfiler.SetFlags(flags);
1594
1595 if (!Assert(EnProfiler.GetFlags() == flags))
1596 {
1598 return NTFR(TFR.FAIL);
1599 }
1600 }
1601 }
1602
1603 // Let's test some bogus
1604 EnProfiler.SetFlags(-333);
1607 if (!Assert(bogusFlags == 0))
1608 {
1610 return NTFR(TFR.FAIL);
1611 }
1612
1615 if (!Assert(bogusFlags == 0))
1616 {
1618 return NTFR(TFR.FAIL);
1619 }
1620
1621 // Reset
1623 return NTFR(TFR.SUCCESS);
1624 }
1625
1626 //---------------------------------------------------------------------------
1627 // Test removing of flags
1629 {
1631
1633
1635 {
1637 return NTFR(TFR.FAIL);
1638 }
1639
1642
1643 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1644 {
1646 return NTFR(TFR.FAIL);
1647 }
1648
1651
1652 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1653 {
1655 return NTFR(TFR.FAIL);
1656 }
1657
1658 // Reset
1660 return NTFR(TFR.SUCCESS);
1661 }
1662
1663 //---------------------------------------------------------------------------
1664 // Test adding of flags
1666 {
1668
1670
1671 // Return should match resulting flags
1673 {
1675 return NTFR(TFR.FAIL);
1676 }
1677
1678 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1679 {
1681 return NTFR(TFR.FAIL);
1682 }
1683
1684 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1685 {
1687 return NTFR(TFR.FAIL);
1688 }
1689
1690 // Reset
1692 return NTFR(TFR.SUCCESS);
1693 }
1694
1695 //---------------------------------------------------------------------------
1696 // Test module
1698 {
1699 // File lives in Game, use it while testing
1701
1702 // This was added at the same time as this API, so check if it works as well
1703 string nameOfCurrentModule = Type().GetModule();
1704 if (!Assert(nameOfCurrentModule != ""))
1705 {
1706 return NTFR(TFR.FAIL);
1707 }
1708
1709 // We know we are in Game, so use it as a test
1712 {
1713 return NTFR(TFR.FAIL);
1714 }
1715
1717 {
1718 return NTFR(TFR.FAIL);
1719 }
1720
1721 // Test if setting and getting works
1724
1726 {
1728 return NTFR(TFR.FAIL);
1729 }
1730
1731 // Data to restore
1734
1735 // Make sure we are only profiling Game and that the data is clean
1736 // Only valid for the Get...Per... methods, as they need to be sorted
1738
1739 // GetTickTime() returns in seconds, so gather the results in seconds too
1742
1743 // Time to sleeb
1744 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1745 float timeSlept = Sleep(0.3);
1746 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1747 float diff = postTime - previousTime - timeSlept;
1748
1749 // Restore
1751
1752 // We called the function, so it must have some time
1753 if (!Assert(postTime > 0))
1754 {
1756
1757 if (!wasEnabled)
1758 EnProfiler.Enable(false, true);
1759
1761
1762 return NTFR(TFR.FAIL);
1763 }
1764
1765 if (!Assert(diff < 0.00001))
1766 {
1768
1769 if (!wasEnabled)
1770 EnProfiler.Enable(false, true);
1771
1773
1774 return NTFR(TFR.FAIL);
1775 }
1776
1777 // Clean the session
1779
1780 // Something from a different module should not get sorted, so just fire something from a different module
1781 for (int i = 0; i < 1000; ++i)
1782 {
1783 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1784 }
1785
1786 // Sort and gather the data and validate if it is correct
1790
1791 Debug.TFLog("Game fncs:", this, "TestModule");
1792
1793 int funcCount = timePerFunc.Count();
1794 for (int j = 0; j < funcCount; ++j)
1795 {
1797 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1798 // We are currently profiling Game, so this Core function shouldn't be present
1799 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1800 {
1802
1803 if (!wasEnabled)
1804 EnProfiler.Enable(false, true);
1805
1807
1808 return NTFR(TFR.FAIL);
1809 }
1810 }
1811
1814
1815 int classCount = timePerClass.Count();
1816 for (int k = 0; k < classCount; ++k)
1817 {
1818 typename type = timePerClass[k].param2;
1820 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1821 {
1823
1824 if (!wasEnabled)
1825 EnProfiler.Enable(false, true);
1826
1828
1829 return NTFR(TFR.FAIL);
1830 }
1831
1832 // Only classes from Game should be present
1833 if (!Assert(classModule == eptModule))
1834 {
1836
1837 if (!wasEnabled)
1838 EnProfiler.Enable(false, true);
1839
1841
1842 return NTFR(TFR.FAIL);
1843 }
1844 }
1845
1846 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1849 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1851
1852 bool found = false;
1853
1854 Debug.TFLog("Core fncs:", this, "TestModule");
1855
1856 funcCount = timePerFunc.Count();
1857 for (int l = 0; l < funcCount; ++l)
1858 {
1860 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1861 // We are currently profiling Core, so this Core function should be present
1862 if (tfpc.param2 == "EnumTools::StringToEnum")
1863 {
1864 found = true;
1865 break;
1866 }
1867 }
1868
1869 Assert(found);
1870
1871 // Test some bogus
1873 EnProfiler.SetModule(-333);
1874 bool success = Assert(EnProfiler.GetModule() == mod);
1875 EnProfiler.SetModule(6003);
1877
1880
1881 if (!wasEnabled)
1882 EnProfiler.Enable(false, true);
1883
1884 return BTFR(success && found);
1885 }
1886
1887 //---------------------------------------------------------------------------
1888 // Test to see if class time data is correct
1890 {
1891 // We should restore this when done
1894
1895 // GetTickTime() returns in seconds, so gather the results in seconds too
1897
1898 // Create the classes
1900
1901 // Time to stress
1902 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1903 float timeStressed = clss.DoEverything();
1904 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1905 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1908
1909 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1910
1911 // Restore
1913 if (!wasEnabled)
1914 EnProfiler.Enable(false, true);
1915
1916 // We called the function, so it must have some time
1917 if (!Assert(postTime > 0))
1918 {
1919 return NTFR(TFR.FAIL);
1920 }
1921
1923 {
1924 return NTFR(TFR.FAIL);
1925 }
1926
1927 if (!Assert(diff < 0.001))
1928 {
1929 return NTFR(TFR.FAIL);
1930 }
1931
1932 return NTFR(TFR.SUCCESS);
1933 }
1934
1935 //---------------------------------------------------------------------------
1936 // Test to see if class count data is correct
1938 {
1939 const int allocAmount = 9;
1940 const int releaseAmount = 6;
1942
1943 // We should restore this when done
1945
1946 // Time to test
1949
1951 for (int i = 0; i < allocAmount; ++i)
1952 {
1953 instanceArr.Insert(new EPTHelperClass());
1954 }
1955
1956 for (int j = 0; j < releaseAmount; ++j)
1957 {
1958 delete instanceArr[j];
1959 }
1960
1961 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1962 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1963
1966
1967 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1968
1969 // Restore
1970 if (!wasEnabled)
1971 EnProfiler.Enable(false, true);
1972
1973 // Time to check
1974 if (!Assert(alloced == allocAmount))
1975 {
1976 return NTFR(TFR.FAIL);
1977 }
1978
1980 {
1981 return NTFR(TFR.FAIL);
1982 }
1983
1984 return NTFR(TFR.SUCCESS);
1985 }
1986
1987 //---------------------------------------------------------------------------
1988 // Test to see if func time data is correct
1990 {
1991 // We should restore this when done
1994
1995 // GetTickTime() returns in seconds, so gather the results in seconds too
1997
1998 // Time to stress
1999 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
2000 float timeStressed = StringFormat();
2001 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
2004
2005 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
2006 float timeStressed2 = StringConcat();
2007 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
2010
2011 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
2012
2013 // Restore
2015 if (!wasEnabled)
2016 {
2017 EnProfiler.Enable(false, true);
2018 }
2019
2020 // We called the function, so it must have some time
2021 if (!Assert(postTime > 0))
2022 {
2023 return NTFR(TFR.FAIL);
2024 }
2025
2026 if (!Assert(diff < 0.001))
2027 {
2028 return NTFR(TFR.FAIL);
2029 }
2030
2031 if (!Assert(postTime2 > 0))
2032 {
2033 return NTFR(TFR.FAIL);
2034 }
2035
2036 if (!Assert(diff2 < 0.001))
2037 {
2038 return NTFR(TFR.FAIL);
2039 }
2040
2041 // I know that string.Format is faster than additive concatenation
2043 {
2044 return NTFR(TFR.FAIL);
2045 }
2046
2047 return NTFR(TFR.SUCCESS);
2048 }
2049
2050 //---------------------------------------------------------------------------
2051 // Test to see if func count data is correct
2053 {
2054 // We should restore this when done
2056
2057 // Time to count
2058
2059 // - CallFunction
2060 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2061 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
2062 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2063
2065
2066 // - CallFunctionParams
2067 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2068 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
2069 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2070
2072
2073 // - Regular call
2074 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2076 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2077
2079
2080 // - Call
2081 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2082 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
2083 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2084
2086
2087 // - Garbage
2088 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
2089
2090 // - Static
2091 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
2093 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
2094
2096
2097 // - Global
2098 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
2099 GetDayZGame();
2100 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
2101
2103
2104 // - Global proto
2105 // Not tracked, so don't need to compare before and after, should always be 0
2106 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
2107 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
2108
2109 // - Static proto
2110 // Not tracked, so don't need to compare before and after, should always be 0
2111 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
2112
2113 // - proto
2114 // Not tracked, so don't need to compare before and after, should always be 0
2115 GetGame().GetHostName();
2116 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
2117
2118 // - proto native
2119 // Not tracked, so don't need to compare before and after, should always be 0
2120 GetGame().IsServer();
2121 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
2122
2123 // - static proto native
2124 // Not tracked, so don't need to compare before and after, should always be 0
2126 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
2127
2128 // Restore
2129 if (!wasEnabled)
2130 {
2131 EnProfiler.Enable(false, true);
2132 }
2133
2134 // Do the checks
2135
2136 // - CallFunction
2137 if (!Assert(callCountCF == 1))
2138 {
2139 return NTFR(TFR.FAIL);
2140 }
2141
2142 // - CallFunctionParams
2143 if (!Assert(callCountCFP == 1))
2144 {
2145 return NTFR(TFR.FAIL);
2146 }
2147
2148 // - Regular call
2149 if (!Assert(callCountRG == 1))
2150 {
2151 return NTFR(TFR.FAIL);
2152 }
2153
2154 // - Call
2155 if (!Assert(callCountC == 1))
2156 {
2157 return NTFR(TFR.FAIL);
2158 }
2159
2160 // - Garbage
2161 if (!Assert(callCountNon == -1))
2162 {
2163 return NTFR(TFR.FAIL);
2164 }
2165
2166 // - Static
2167 if (!Assert(callCountS == 1))
2168 {
2169 return NTFR(TFR.FAIL);
2170 }
2171
2172 // - Global
2173 if (!Assert(callCountG == 1))
2174 {
2175 return NTFR(TFR.FAIL);
2176 }
2177
2178 // - Global proto
2179 if (!Assert(callCountGP == 0))
2180 {
2181 return NTFR(TFR.FAIL);
2182 }
2183
2184 // - Static proto
2185 if (!Assert(callCountSP == 0))
2186 {
2187 return NTFR(TFR.FAIL);
2188 }
2189
2190 // - proto
2191 if (!Assert(callCountP == 0))
2192 {
2193 return NTFR(TFR.FAIL);
2194 }
2195
2196 // - proto native
2197 if (!Assert(callCountPN == 0))
2198 {
2199 return NTFR(TFR.FAIL);
2200 }
2201
2202 // - static proto native
2203 if (!Assert(callCountSPN == 0))
2204 {
2205 return NTFR(TFR.FAIL);
2206 }
2207
2208 return NTFR(TFR.SUCCESS);
2209 }
2210
2211 //---------------------------------------------------------------------------
2212 // Helpers
2213 //---------------------------------------------------------------------------
2214 // Snore
2215 float Sleep(float timeS)
2216 {
2217 float startTime = GetGame().GetTickTime();
2218 while (GetGame().GetTickTime() - startTime < timeS)
2219 {
2220 // Zzz
2221 }
2222
2223 return GetGame().GetTickTime() - startTime;
2224 }
2225
2226 //---------------------------------------------------------------------------
2227 // Example stress method
2228 float StringFormat()
2229 {
2230 float startTime = GetGame().GetTickTime();
2231
2232 for (int i = 0; i < 1000; ++i)
2233 {
2234 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
2235 }
2236
2237 return GetGame().GetTickTime() - startTime;
2238 }
2239
2240 //---------------------------------------------------------------------------
2241 // Example stress method 2
2242 float StringConcat()
2243 {
2244 float startTime = GetGame().GetTickTime();
2245
2246 for (int i = 0; i < 1000; ++i)
2247 {
2248 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
2249 }
2250
2251 return GetGame().GetTickTime() - startTime;
2252 }
2253
2254 //---------------------------------------------------------------------------
2255 // To make sure it is only ever called in that test
2257 {
2258 int dummy = 3;
2259 }
2260
2261 //---------------------------------------------------------------------------
2262 // To make sure it is only ever called in that test
2263 static void TestFuncCountDataHelperStatic()
2264 {
2265 int dummy = 3;
2266 }
2267}
2268
2269class EPTHelperClass
2270{
2271 float Sleep2(float timeS)
2272 {
2273 float startTime = GetGame().GetTickTime();
2274 while (GetGame().GetTickTime() - startTime < timeS)
2275 {
2276 // Zzz
2277 }
2278
2279 return GetGame().GetTickTime() - startTime;
2280 }
2281 float SleepAgain(float timeS)
2282 {
2283 float startTime = GetGame().GetTickTime();
2284 while (GetGame().GetTickTime() - startTime < timeS)
2285 {
2286 // Zzz
2287 }
2288
2289 return GetGame().GetTickTime() - startTime;
2290 }
2291
2292 float DoEverything()
2293 {
2294 float startTime = GetGame().GetTickTime();
2295
2296 Sleep2(3);
2297 SleepAgain(3);
2298
2299 return GetGame().GetTickTime() - startTime;
2300 }
2301}

◆ TestFuncCountDataHelperStatic()

static void TestFuncCountDataHelperStatic ( )
static
1516 : TestFramework
1517{
1520
1521 //---------------------------------------------------------------------------
1522 // Ctor - Decides the tests to run
1523 //---------------------------------------------------------------------------
1524 void EnProfilerTests()
1525 {
1527
1528 AddInitTest("TestToggling");
1529 AddInitTest("TestTogglingImmediate");
1530 AddInitTest("TestSetFlags");
1531 AddInitTest("TestClearFlags");
1532 AddInitTest("TestAddFlags");
1533 AddInitTest("TestModule");
1534 AddInitTest("TestClassTimeData");
1535 AddInitTest("TestClassCountData");
1536 AddInitTest("TestFuncTimeData");
1537 AddInitTest("TestFuncCountData");
1538 }
1539
1540 //---------------------------------------------------------------------------
1541 // Dtor
1542 //---------------------------------------------------------------------------
1543 void ~EnProfilerTests()
1544 {
1546 }
1547
1548 //---------------------------------------------------------------------------
1549 // Tests
1550 //---------------------------------------------------------------------------
1551 // Test toggling state
1553 {
1557 {
1560 }
1561
1562 return NTFR(TFR.FAIL);
1563 }
1564
1565 //---------------------------------------------------------------------------
1566 // Test toggling immediate state
1568 {
1572 {
1575 }
1576
1577 return NTFR(TFR.FAIL);
1578 }
1579
1580 //---------------------------------------------------------------------------
1581 // Test SetFlags/GetFlags
1583 {
1585
1586 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1587 {
1589 EnProfiler.SetFlags(flags);
1590
1591 if (!Assert(EnProfiler.GetFlags() == flags))
1592 {
1594 return NTFR(TFR.FAIL);
1595 }
1596
1597 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1598 {
1600 EnProfiler.SetFlags(flags);
1601
1602 if (!Assert(EnProfiler.GetFlags() == flags))
1603 {
1605 return NTFR(TFR.FAIL);
1606 }
1607 }
1608 }
1609
1610 // Let's test some bogus
1611 EnProfiler.SetFlags(-333);
1614 if (!Assert(bogusFlags == 0))
1615 {
1617 return NTFR(TFR.FAIL);
1618 }
1619
1622 if (!Assert(bogusFlags == 0))
1623 {
1625 return NTFR(TFR.FAIL);
1626 }
1627
1628 // Reset
1630 return NTFR(TFR.SUCCESS);
1631 }
1632
1633 //---------------------------------------------------------------------------
1634 // Test removing of flags
1636 {
1638
1640
1642 {
1644 return NTFR(TFR.FAIL);
1645 }
1646
1649
1650 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1651 {
1653 return NTFR(TFR.FAIL);
1654 }
1655
1658
1659 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1660 {
1662 return NTFR(TFR.FAIL);
1663 }
1664
1665 // Reset
1667 return NTFR(TFR.SUCCESS);
1668 }
1669
1670 //---------------------------------------------------------------------------
1671 // Test adding of flags
1673 {
1675
1677
1678 // Return should match resulting flags
1680 {
1682 return NTFR(TFR.FAIL);
1683 }
1684
1685 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1686 {
1688 return NTFR(TFR.FAIL);
1689 }
1690
1691 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1692 {
1694 return NTFR(TFR.FAIL);
1695 }
1696
1697 // Reset
1699 return NTFR(TFR.SUCCESS);
1700 }
1701
1702 //---------------------------------------------------------------------------
1703 // Test module
1705 {
1706 // File lives in Game, use it while testing
1708
1709 // This was added at the same time as this API, so check if it works as well
1710 string nameOfCurrentModule = Type().GetModule();
1711 if (!Assert(nameOfCurrentModule != ""))
1712 {
1713 return NTFR(TFR.FAIL);
1714 }
1715
1716 // We know we are in Game, so use it as a test
1719 {
1720 return NTFR(TFR.FAIL);
1721 }
1722
1724 {
1725 return NTFR(TFR.FAIL);
1726 }
1727
1728 // Test if setting and getting works
1731
1733 {
1735 return NTFR(TFR.FAIL);
1736 }
1737
1738 // Data to restore
1741
1742 // Make sure we are only profiling Game and that the data is clean
1743 // Only valid for the Get...Per... methods, as they need to be sorted
1745
1746 // GetTickTime() returns in seconds, so gather the results in seconds too
1749
1750 // Time to sleeb
1751 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1752 float timeSlept = Sleep(0.3);
1753 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1754 float diff = postTime - previousTime - timeSlept;
1755
1756 // Restore
1758
1759 // We called the function, so it must have some time
1760 if (!Assert(postTime > 0))
1761 {
1763
1764 if (!wasEnabled)
1765 EnProfiler.Enable(false, true);
1766
1768
1769 return NTFR(TFR.FAIL);
1770 }
1771
1772 if (!Assert(diff < 0.00001))
1773 {
1775
1776 if (!wasEnabled)
1777 EnProfiler.Enable(false, true);
1778
1780
1781 return NTFR(TFR.FAIL);
1782 }
1783
1784 // Clean the session
1786
1787 // Something from a different module should not get sorted, so just fire something from a different module
1788 for (int i = 0; i < 1000; ++i)
1789 {
1790 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1791 }
1792
1793 // Sort and gather the data and validate if it is correct
1797
1798 Debug.TFLog("Game fncs:", this, "TestModule");
1799
1800 int funcCount = timePerFunc.Count();
1801 for (int j = 0; j < funcCount; ++j)
1802 {
1804 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1805 // We are currently profiling Game, so this Core function shouldn't be present
1806 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1807 {
1809
1810 if (!wasEnabled)
1811 EnProfiler.Enable(false, true);
1812
1814
1815 return NTFR(TFR.FAIL);
1816 }
1817 }
1818
1821
1822 int classCount = timePerClass.Count();
1823 for (int k = 0; k < classCount; ++k)
1824 {
1825 typename type = timePerClass[k].param2;
1827 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1828 {
1830
1831 if (!wasEnabled)
1832 EnProfiler.Enable(false, true);
1833
1835
1836 return NTFR(TFR.FAIL);
1837 }
1838
1839 // Only classes from Game should be present
1840 if (!Assert(classModule == eptModule))
1841 {
1843
1844 if (!wasEnabled)
1845 EnProfiler.Enable(false, true);
1846
1848
1849 return NTFR(TFR.FAIL);
1850 }
1851 }
1852
1853 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1856 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1858
1859 bool found = false;
1860
1861 Debug.TFLog("Core fncs:", this, "TestModule");
1862
1863 funcCount = timePerFunc.Count();
1864 for (int l = 0; l < funcCount; ++l)
1865 {
1867 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1868 // We are currently profiling Core, so this Core function should be present
1869 if (tfpc.param2 == "EnumTools::StringToEnum")
1870 {
1871 found = true;
1872 break;
1873 }
1874 }
1875
1876 Assert(found);
1877
1878 // Test some bogus
1880 EnProfiler.SetModule(-333);
1881 bool success = Assert(EnProfiler.GetModule() == mod);
1882 EnProfiler.SetModule(6003);
1884
1887
1888 if (!wasEnabled)
1889 EnProfiler.Enable(false, true);
1890
1891 return BTFR(success && found);
1892 }
1893
1894 //---------------------------------------------------------------------------
1895 // Test to see if class time data is correct
1897 {
1898 // We should restore this when done
1901
1902 // GetTickTime() returns in seconds, so gather the results in seconds too
1904
1905 // Create the classes
1907
1908 // Time to stress
1909 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1910 float timeStressed = clss.DoEverything();
1911 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1912 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1915
1916 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1917
1918 // Restore
1920 if (!wasEnabled)
1921 EnProfiler.Enable(false, true);
1922
1923 // We called the function, so it must have some time
1924 if (!Assert(postTime > 0))
1925 {
1926 return NTFR(TFR.FAIL);
1927 }
1928
1930 {
1931 return NTFR(TFR.FAIL);
1932 }
1933
1934 if (!Assert(diff < 0.001))
1935 {
1936 return NTFR(TFR.FAIL);
1937 }
1938
1939 return NTFR(TFR.SUCCESS);
1940 }
1941
1942 //---------------------------------------------------------------------------
1943 // Test to see if class count data is correct
1945 {
1946 const int allocAmount = 9;
1947 const int releaseAmount = 6;
1949
1950 // We should restore this when done
1952
1953 // Time to test
1956
1958 for (int i = 0; i < allocAmount; ++i)
1959 {
1960 instanceArr.Insert(new EPTHelperClass());
1961 }
1962
1963 for (int j = 0; j < releaseAmount; ++j)
1964 {
1965 delete instanceArr[j];
1966 }
1967
1968 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1969 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1970
1973
1974 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1975
1976 // Restore
1977 if (!wasEnabled)
1978 EnProfiler.Enable(false, true);
1979
1980 // Time to check
1981 if (!Assert(alloced == allocAmount))
1982 {
1983 return NTFR(TFR.FAIL);
1984 }
1985
1987 {
1988 return NTFR(TFR.FAIL);
1989 }
1990
1991 return NTFR(TFR.SUCCESS);
1992 }
1993
1994 //---------------------------------------------------------------------------
1995 // Test to see if func time data is correct
1997 {
1998 // We should restore this when done
2001
2002 // GetTickTime() returns in seconds, so gather the results in seconds too
2004
2005 // Time to stress
2006 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
2007 float timeStressed = StringFormat();
2008 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
2011
2012 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
2013 float timeStressed2 = StringConcat();
2014 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
2017
2018 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
2019
2020 // Restore
2022 if (!wasEnabled)
2023 {
2024 EnProfiler.Enable(false, true);
2025 }
2026
2027 // We called the function, so it must have some time
2028 if (!Assert(postTime > 0))
2029 {
2030 return NTFR(TFR.FAIL);
2031 }
2032
2033 if (!Assert(diff < 0.001))
2034 {
2035 return NTFR(TFR.FAIL);
2036 }
2037
2038 if (!Assert(postTime2 > 0))
2039 {
2040 return NTFR(TFR.FAIL);
2041 }
2042
2043 if (!Assert(diff2 < 0.001))
2044 {
2045 return NTFR(TFR.FAIL);
2046 }
2047
2048 // I know that string.Format is faster than additive concatenation
2050 {
2051 return NTFR(TFR.FAIL);
2052 }
2053
2054 return NTFR(TFR.SUCCESS);
2055 }
2056
2057 //---------------------------------------------------------------------------
2058 // Test to see if func count data is correct
2060 {
2061 // We should restore this when done
2063
2064 // Time to count
2065
2066 // - CallFunction
2067 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2068 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
2069 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2070
2072
2073 // - CallFunctionParams
2074 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2075 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
2076 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2077
2079
2080 // - Regular call
2081 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2083 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2084
2086
2087 // - Call
2088 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2089 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
2090 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
2091
2093
2094 // - Garbage
2095 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
2096
2097 // - Static
2098 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
2100 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
2101
2103
2104 // - Global
2105 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
2106 GetDayZGame();
2107 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
2108
2110
2111 // - Global proto
2112 // Not tracked, so don't need to compare before and after, should always be 0
2113 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
2114 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
2115
2116 // - Static proto
2117 // Not tracked, so don't need to compare before and after, should always be 0
2118 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
2119
2120 // - proto
2121 // Not tracked, so don't need to compare before and after, should always be 0
2122 GetGame().GetHostName();
2123 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
2124
2125 // - proto native
2126 // Not tracked, so don't need to compare before and after, should always be 0
2127 GetGame().IsServer();
2128 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
2129
2130 // - static proto native
2131 // Not tracked, so don't need to compare before and after, should always be 0
2133 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
2134
2135 // Restore
2136 if (!wasEnabled)
2137 {
2138 EnProfiler.Enable(false, true);
2139 }
2140
2141 // Do the checks
2142
2143 // - CallFunction
2144 if (!Assert(callCountCF == 1))
2145 {
2146 return NTFR(TFR.FAIL);
2147 }
2148
2149 // - CallFunctionParams
2150 if (!Assert(callCountCFP == 1))
2151 {
2152 return NTFR(TFR.FAIL);
2153 }
2154
2155 // - Regular call
2156 if (!Assert(callCountRG == 1))
2157 {
2158 return NTFR(TFR.FAIL);
2159 }
2160
2161 // - Call
2162 if (!Assert(callCountC == 1))
2163 {
2164 return NTFR(TFR.FAIL);
2165 }
2166
2167 // - Garbage
2168 if (!Assert(callCountNon == -1))
2169 {
2170 return NTFR(TFR.FAIL);
2171 }
2172
2173 // - Static
2174 if (!Assert(callCountS == 1))
2175 {
2176 return NTFR(TFR.FAIL);
2177 }
2178
2179 // - Global
2180 if (!Assert(callCountG == 1))
2181 {
2182 return NTFR(TFR.FAIL);
2183 }
2184
2185 // - Global proto
2186 if (!Assert(callCountGP == 0))
2187 {
2188 return NTFR(TFR.FAIL);
2189 }
2190
2191 // - Static proto
2192 if (!Assert(callCountSP == 0))
2193 {
2194 return NTFR(TFR.FAIL);
2195 }
2196
2197 // - proto
2198 if (!Assert(callCountP == 0))
2199 {
2200 return NTFR(TFR.FAIL);
2201 }
2202
2203 // - proto native
2204 if (!Assert(callCountPN == 0))
2205 {
2206 return NTFR(TFR.FAIL);
2207 }
2208
2209 // - static proto native
2210 if (!Assert(callCountSPN == 0))
2211 {
2212 return NTFR(TFR.FAIL);
2213 }
2214
2215 return NTFR(TFR.SUCCESS);
2216 }
2217
2218 //---------------------------------------------------------------------------
2219 // Helpers
2220 //---------------------------------------------------------------------------
2221 // Snore
2222 float Sleep(float timeS)
2223 {
2224 float startTime = GetGame().GetTickTime();
2225 while (GetGame().GetTickTime() - startTime < timeS)
2226 {
2227 // Zzz
2228 }
2229
2230 return GetGame().GetTickTime() - startTime;
2231 }
2232
2233 //---------------------------------------------------------------------------
2234 // Example stress method
2235 float StringFormat()
2236 {
2237 float startTime = GetGame().GetTickTime();
2238
2239 for (int i = 0; i < 1000; ++i)
2240 {
2241 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
2242 }
2243
2244 return GetGame().GetTickTime() - startTime;
2245 }
2246
2247 //---------------------------------------------------------------------------
2248 // Example stress method 2
2249 float StringConcat()
2250 {
2251 float startTime = GetGame().GetTickTime();
2252
2253 for (int i = 0; i < 1000; ++i)
2254 {
2255 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
2256 }
2257
2258 return GetGame().GetTickTime() - startTime;
2259 }
2260
2261 //---------------------------------------------------------------------------
2262 // To make sure it is only ever called in that test
2264 {
2265 int dummy = 3;
2266 }
2267
2268 //---------------------------------------------------------------------------
2269 // To make sure it is only ever called in that test
2270 static void TestFuncCountDataHelperStatic()
2271 {
2272 int dummy = 3;
2273 }
2274}
2275
2276class EPTHelperClass
2277{
2278 float Sleep2(float timeS)
2279 {
2280 float startTime = GetGame().GetTickTime();
2281 while (GetGame().GetTickTime() - startTime < timeS)
2282 {
2283 // Zzz
2284 }
2285
2286 return GetGame().GetTickTime() - startTime;
2287 }
2288 float SleepAgain(float timeS)
2289 {
2290 float startTime = GetGame().GetTickTime();
2291 while (GetGame().GetTickTime() - startTime < timeS)
2292 {
2293 // Zzz
2294 }
2295
2296 return GetGame().GetTickTime() - startTime;
2297 }
2298
2299 float DoEverything()
2300 {
2301 float startTime = GetGame().GetTickTime();
2302
2303 Sleep2(3);
2304 SleepAgain(3);
2305
2306 return GetGame().GetTickTime() - startTime;
2307 }
2308}

◆ TestFuncTimeData()

TFResult TestFuncTimeData ( )
1242 : TestFramework
1243{
1246
1247 //---------------------------------------------------------------------------
1248 // Ctor - Decides the tests to run
1249 //---------------------------------------------------------------------------
1250 void EnProfilerTests()
1251 {
1253
1254 AddInitTest("TestToggling");
1255 AddInitTest("TestTogglingImmediate");
1256 AddInitTest("TestSetFlags");
1257 AddInitTest("TestClearFlags");
1258 AddInitTest("TestAddFlags");
1259 AddInitTest("TestModule");
1260 AddInitTest("TestClassTimeData");
1261 AddInitTest("TestClassCountData");
1262 AddInitTest("TestFuncTimeData");
1263 AddInitTest("TestFuncCountData");
1264 }
1265
1266 //---------------------------------------------------------------------------
1267 // Dtor
1268 //---------------------------------------------------------------------------
1269 void ~EnProfilerTests()
1270 {
1272 }
1273
1274 //---------------------------------------------------------------------------
1275 // Tests
1276 //---------------------------------------------------------------------------
1277 // Test toggling state
1279 {
1283 {
1286 }
1287
1288 return NTFR(TFR.FAIL);
1289 }
1290
1291 //---------------------------------------------------------------------------
1292 // Test toggling immediate state
1294 {
1298 {
1301 }
1302
1303 return NTFR(TFR.FAIL);
1304 }
1305
1306 //---------------------------------------------------------------------------
1307 // Test SetFlags/GetFlags
1309 {
1311
1312 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1313 {
1315 EnProfiler.SetFlags(flags);
1316
1317 if (!Assert(EnProfiler.GetFlags() == flags))
1318 {
1320 return NTFR(TFR.FAIL);
1321 }
1322
1323 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1324 {
1326 EnProfiler.SetFlags(flags);
1327
1328 if (!Assert(EnProfiler.GetFlags() == flags))
1329 {
1331 return NTFR(TFR.FAIL);
1332 }
1333 }
1334 }
1335
1336 // Let's test some bogus
1337 EnProfiler.SetFlags(-333);
1340 if (!Assert(bogusFlags == 0))
1341 {
1343 return NTFR(TFR.FAIL);
1344 }
1345
1348 if (!Assert(bogusFlags == 0))
1349 {
1351 return NTFR(TFR.FAIL);
1352 }
1353
1354 // Reset
1356 return NTFR(TFR.SUCCESS);
1357 }
1358
1359 //---------------------------------------------------------------------------
1360 // Test removing of flags
1362 {
1364
1366
1368 {
1370 return NTFR(TFR.FAIL);
1371 }
1372
1375
1376 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1377 {
1379 return NTFR(TFR.FAIL);
1380 }
1381
1384
1385 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1386 {
1388 return NTFR(TFR.FAIL);
1389 }
1390
1391 // Reset
1393 return NTFR(TFR.SUCCESS);
1394 }
1395
1396 //---------------------------------------------------------------------------
1397 // Test adding of flags
1399 {
1401
1403
1404 // Return should match resulting flags
1406 {
1408 return NTFR(TFR.FAIL);
1409 }
1410
1411 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1412 {
1414 return NTFR(TFR.FAIL);
1415 }
1416
1417 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1418 {
1420 return NTFR(TFR.FAIL);
1421 }
1422
1423 // Reset
1425 return NTFR(TFR.SUCCESS);
1426 }
1427
1428 //---------------------------------------------------------------------------
1429 // Test module
1431 {
1432 // File lives in Game, use it while testing
1434
1435 // This was added at the same time as this API, so check if it works as well
1436 string nameOfCurrentModule = Type().GetModule();
1437 if (!Assert(nameOfCurrentModule != ""))
1438 {
1439 return NTFR(TFR.FAIL);
1440 }
1441
1442 // We know we are in Game, so use it as a test
1445 {
1446 return NTFR(TFR.FAIL);
1447 }
1448
1450 {
1451 return NTFR(TFR.FAIL);
1452 }
1453
1454 // Test if setting and getting works
1457
1459 {
1461 return NTFR(TFR.FAIL);
1462 }
1463
1464 // Data to restore
1467
1468 // Make sure we are only profiling Game and that the data is clean
1469 // Only valid for the Get...Per... methods, as they need to be sorted
1471
1472 // GetTickTime() returns in seconds, so gather the results in seconds too
1475
1476 // Time to sleeb
1477 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1478 float timeSlept = Sleep(0.3);
1479 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1480 float diff = postTime - previousTime - timeSlept;
1481
1482 // Restore
1484
1485 // We called the function, so it must have some time
1486 if (!Assert(postTime > 0))
1487 {
1489
1490 if (!wasEnabled)
1491 EnProfiler.Enable(false, true);
1492
1494
1495 return NTFR(TFR.FAIL);
1496 }
1497
1498 if (!Assert(diff < 0.00001))
1499 {
1501
1502 if (!wasEnabled)
1503 EnProfiler.Enable(false, true);
1504
1506
1507 return NTFR(TFR.FAIL);
1508 }
1509
1510 // Clean the session
1512
1513 // Something from a different module should not get sorted, so just fire something from a different module
1514 for (int i = 0; i < 1000; ++i)
1515 {
1516 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1517 }
1518
1519 // Sort and gather the data and validate if it is correct
1523
1524 Debug.TFLog("Game fncs:", this, "TestModule");
1525
1526 int funcCount = timePerFunc.Count();
1527 for (int j = 0; j < funcCount; ++j)
1528 {
1530 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1531 // We are currently profiling Game, so this Core function shouldn't be present
1532 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1533 {
1535
1536 if (!wasEnabled)
1537 EnProfiler.Enable(false, true);
1538
1540
1541 return NTFR(TFR.FAIL);
1542 }
1543 }
1544
1547
1548 int classCount = timePerClass.Count();
1549 for (int k = 0; k < classCount; ++k)
1550 {
1551 typename type = timePerClass[k].param2;
1553 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1554 {
1556
1557 if (!wasEnabled)
1558 EnProfiler.Enable(false, true);
1559
1561
1562 return NTFR(TFR.FAIL);
1563 }
1564
1565 // Only classes from Game should be present
1566 if (!Assert(classModule == eptModule))
1567 {
1569
1570 if (!wasEnabled)
1571 EnProfiler.Enable(false, true);
1572
1574
1575 return NTFR(TFR.FAIL);
1576 }
1577 }
1578
1579 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1582 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1584
1585 bool found = false;
1586
1587 Debug.TFLog("Core fncs:", this, "TestModule");
1588
1589 funcCount = timePerFunc.Count();
1590 for (int l = 0; l < funcCount; ++l)
1591 {
1593 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1594 // We are currently profiling Core, so this Core function should be present
1595 if (tfpc.param2 == "EnumTools::StringToEnum")
1596 {
1597 found = true;
1598 break;
1599 }
1600 }
1601
1602 Assert(found);
1603
1604 // Test some bogus
1606 EnProfiler.SetModule(-333);
1607 bool success = Assert(EnProfiler.GetModule() == mod);
1608 EnProfiler.SetModule(6003);
1610
1613
1614 if (!wasEnabled)
1615 EnProfiler.Enable(false, true);
1616
1617 return BTFR(success && found);
1618 }
1619
1620 //---------------------------------------------------------------------------
1621 // Test to see if class time data is correct
1623 {
1624 // We should restore this when done
1627
1628 // GetTickTime() returns in seconds, so gather the results in seconds too
1630
1631 // Create the classes
1633
1634 // Time to stress
1635 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1636 float timeStressed = clss.DoEverything();
1637 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1638 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1641
1642 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1643
1644 // Restore
1646 if (!wasEnabled)
1647 EnProfiler.Enable(false, true);
1648
1649 // We called the function, so it must have some time
1650 if (!Assert(postTime > 0))
1651 {
1652 return NTFR(TFR.FAIL);
1653 }
1654
1656 {
1657 return NTFR(TFR.FAIL);
1658 }
1659
1660 if (!Assert(diff < 0.001))
1661 {
1662 return NTFR(TFR.FAIL);
1663 }
1664
1665 return NTFR(TFR.SUCCESS);
1666 }
1667
1668 //---------------------------------------------------------------------------
1669 // Test to see if class count data is correct
1671 {
1672 const int allocAmount = 9;
1673 const int releaseAmount = 6;
1675
1676 // We should restore this when done
1678
1679 // Time to test
1682
1684 for (int i = 0; i < allocAmount; ++i)
1685 {
1686 instanceArr.Insert(new EPTHelperClass());
1687 }
1688
1689 for (int j = 0; j < releaseAmount; ++j)
1690 {
1691 delete instanceArr[j];
1692 }
1693
1694 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1695 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1696
1699
1700 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1701
1702 // Restore
1703 if (!wasEnabled)
1704 EnProfiler.Enable(false, true);
1705
1706 // Time to check
1707 if (!Assert(alloced == allocAmount))
1708 {
1709 return NTFR(TFR.FAIL);
1710 }
1711
1713 {
1714 return NTFR(TFR.FAIL);
1715 }
1716
1717 return NTFR(TFR.SUCCESS);
1718 }
1719
1720 //---------------------------------------------------------------------------
1721 // Test to see if func time data is correct
1723 {
1724 // We should restore this when done
1727
1728 // GetTickTime() returns in seconds, so gather the results in seconds too
1730
1731 // Time to stress
1732 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1733 float timeStressed = StringFormat();
1734 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1737
1738 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1739 float timeStressed2 = StringConcat();
1740 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1743
1744 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1745
1746 // Restore
1748 if (!wasEnabled)
1749 {
1750 EnProfiler.Enable(false, true);
1751 }
1752
1753 // We called the function, so it must have some time
1754 if (!Assert(postTime > 0))
1755 {
1756 return NTFR(TFR.FAIL);
1757 }
1758
1759 if (!Assert(diff < 0.001))
1760 {
1761 return NTFR(TFR.FAIL);
1762 }
1763
1764 if (!Assert(postTime2 > 0))
1765 {
1766 return NTFR(TFR.FAIL);
1767 }
1768
1769 if (!Assert(diff2 < 0.001))
1770 {
1771 return NTFR(TFR.FAIL);
1772 }
1773
1774 // I know that string.Format is faster than additive concatenation
1776 {
1777 return NTFR(TFR.FAIL);
1778 }
1779
1780 return NTFR(TFR.SUCCESS);
1781 }
1782
1783 //---------------------------------------------------------------------------
1784 // Test to see if func count data is correct
1786 {
1787 // We should restore this when done
1789
1790 // Time to count
1791
1792 // - CallFunction
1793 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1794 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1795 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1796
1798
1799 // - CallFunctionParams
1800 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1801 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1802 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1803
1805
1806 // - Regular call
1807 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1809 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1810
1812
1813 // - Call
1814 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1815 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1816 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1817
1819
1820 // - Garbage
1821 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1822
1823 // - Static
1824 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1826 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1827
1829
1830 // - Global
1831 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1832 GetDayZGame();
1833 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1834
1836
1837 // - Global proto
1838 // Not tracked, so don't need to compare before and after, should always be 0
1839 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1840 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1841
1842 // - Static proto
1843 // Not tracked, so don't need to compare before and after, should always be 0
1844 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1845
1846 // - proto
1847 // Not tracked, so don't need to compare before and after, should always be 0
1848 GetGame().GetHostName();
1849 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1850
1851 // - proto native
1852 // Not tracked, so don't need to compare before and after, should always be 0
1853 GetGame().IsServer();
1854 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1855
1856 // - static proto native
1857 // Not tracked, so don't need to compare before and after, should always be 0
1859 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1860
1861 // Restore
1862 if (!wasEnabled)
1863 {
1864 EnProfiler.Enable(false, true);
1865 }
1866
1867 // Do the checks
1868
1869 // - CallFunction
1870 if (!Assert(callCountCF == 1))
1871 {
1872 return NTFR(TFR.FAIL);
1873 }
1874
1875 // - CallFunctionParams
1876 if (!Assert(callCountCFP == 1))
1877 {
1878 return NTFR(TFR.FAIL);
1879 }
1880
1881 // - Regular call
1882 if (!Assert(callCountRG == 1))
1883 {
1884 return NTFR(TFR.FAIL);
1885 }
1886
1887 // - Call
1888 if (!Assert(callCountC == 1))
1889 {
1890 return NTFR(TFR.FAIL);
1891 }
1892
1893 // - Garbage
1894 if (!Assert(callCountNon == -1))
1895 {
1896 return NTFR(TFR.FAIL);
1897 }
1898
1899 // - Static
1900 if (!Assert(callCountS == 1))
1901 {
1902 return NTFR(TFR.FAIL);
1903 }
1904
1905 // - Global
1906 if (!Assert(callCountG == 1))
1907 {
1908 return NTFR(TFR.FAIL);
1909 }
1910
1911 // - Global proto
1912 if (!Assert(callCountGP == 0))
1913 {
1914 return NTFR(TFR.FAIL);
1915 }
1916
1917 // - Static proto
1918 if (!Assert(callCountSP == 0))
1919 {
1920 return NTFR(TFR.FAIL);
1921 }
1922
1923 // - proto
1924 if (!Assert(callCountP == 0))
1925 {
1926 return NTFR(TFR.FAIL);
1927 }
1928
1929 // - proto native
1930 if (!Assert(callCountPN == 0))
1931 {
1932 return NTFR(TFR.FAIL);
1933 }
1934
1935 // - static proto native
1936 if (!Assert(callCountSPN == 0))
1937 {
1938 return NTFR(TFR.FAIL);
1939 }
1940
1941 return NTFR(TFR.SUCCESS);
1942 }
1943
1944 //---------------------------------------------------------------------------
1945 // Helpers
1946 //---------------------------------------------------------------------------
1947 // Snore
1948 float Sleep(float timeS)
1949 {
1950 float startTime = GetGame().GetTickTime();
1951 while (GetGame().GetTickTime() - startTime < timeS)
1952 {
1953 // Zzz
1954 }
1955
1956 return GetGame().GetTickTime() - startTime;
1957 }
1958
1959 //---------------------------------------------------------------------------
1960 // Example stress method
1961 float StringFormat()
1962 {
1963 float startTime = GetGame().GetTickTime();
1964
1965 for (int i = 0; i < 1000; ++i)
1966 {
1967 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1968 }
1969
1970 return GetGame().GetTickTime() - startTime;
1971 }
1972
1973 //---------------------------------------------------------------------------
1974 // Example stress method 2
1975 float StringConcat()
1976 {
1977 float startTime = GetGame().GetTickTime();
1978
1979 for (int i = 0; i < 1000; ++i)
1980 {
1981 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1982 }
1983
1984 return GetGame().GetTickTime() - startTime;
1985 }
1986
1987 //---------------------------------------------------------------------------
1988 // To make sure it is only ever called in that test
1990 {
1991 int dummy = 3;
1992 }
1993
1994 //---------------------------------------------------------------------------
1995 // To make sure it is only ever called in that test
1996 static void TestFuncCountDataHelperStatic()
1997 {
1998 int dummy = 3;
1999 }
2000}
2001
2002class EPTHelperClass
2003{
2004 float Sleep2(float timeS)
2005 {
2006 float startTime = GetGame().GetTickTime();
2007 while (GetGame().GetTickTime() - startTime < timeS)
2008 {
2009 // Zzz
2010 }
2011
2012 return GetGame().GetTickTime() - startTime;
2013 }
2014 float SleepAgain(float timeS)
2015 {
2016 float startTime = GetGame().GetTickTime();
2017 while (GetGame().GetTickTime() - startTime < timeS)
2018 {
2019 // Zzz
2020 }
2021
2022 return GetGame().GetTickTime() - startTime;
2023 }
2024
2025 float DoEverything()
2026 {
2027 float startTime = GetGame().GetTickTime();
2028
2029 Sleep2(3);
2030 SleepAgain(3);
2031
2032 return GetGame().GetTickTime() - startTime;
2033 }
2034}

◆ TestModule()

TFResult TestModule ( )
950 : TestFramework
951{
954
955 //---------------------------------------------------------------------------
956 // Ctor - Decides the tests to run
957 //---------------------------------------------------------------------------
958 void EnProfilerTests()
959 {
961
962 AddInitTest("TestToggling");
963 AddInitTest("TestTogglingImmediate");
964 AddInitTest("TestSetFlags");
965 AddInitTest("TestClearFlags");
966 AddInitTest("TestAddFlags");
967 AddInitTest("TestModule");
968 AddInitTest("TestClassTimeData");
969 AddInitTest("TestClassCountData");
970 AddInitTest("TestFuncTimeData");
971 AddInitTest("TestFuncCountData");
972 }
973
974 //---------------------------------------------------------------------------
975 // Dtor
976 //---------------------------------------------------------------------------
977 void ~EnProfilerTests()
978 {
980 }
981
982 //---------------------------------------------------------------------------
983 // Tests
984 //---------------------------------------------------------------------------
985 // Test toggling state
987 {
991 {
994 }
995
996 return NTFR(TFR.FAIL);
997 }
998
999 //---------------------------------------------------------------------------
1000 // Test toggling immediate state
1002 {
1006 {
1009 }
1010
1011 return NTFR(TFR.FAIL);
1012 }
1013
1014 //---------------------------------------------------------------------------
1015 // Test SetFlags/GetFlags
1017 {
1019
1020 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
1021 {
1023 EnProfiler.SetFlags(flags);
1024
1025 if (!Assert(EnProfiler.GetFlags() == flags))
1026 {
1028 return NTFR(TFR.FAIL);
1029 }
1030
1031 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
1032 {
1034 EnProfiler.SetFlags(flags);
1035
1036 if (!Assert(EnProfiler.GetFlags() == flags))
1037 {
1039 return NTFR(TFR.FAIL);
1040 }
1041 }
1042 }
1043
1044 // Let's test some bogus
1045 EnProfiler.SetFlags(-333);
1048 if (!Assert(bogusFlags == 0))
1049 {
1051 return NTFR(TFR.FAIL);
1052 }
1053
1056 if (!Assert(bogusFlags == 0))
1057 {
1059 return NTFR(TFR.FAIL);
1060 }
1061
1062 // Reset
1064 return NTFR(TFR.SUCCESS);
1065 }
1066
1067 //---------------------------------------------------------------------------
1068 // Test removing of flags
1070 {
1072
1074
1076 {
1078 return NTFR(TFR.FAIL);
1079 }
1080
1083
1084 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1085 {
1087 return NTFR(TFR.FAIL);
1088 }
1089
1092
1093 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.NONE))
1094 {
1096 return NTFR(TFR.FAIL);
1097 }
1098
1099 // Reset
1101 return NTFR(TFR.SUCCESS);
1102 }
1103
1104 //---------------------------------------------------------------------------
1105 // Test adding of flags
1107 {
1109
1111
1112 // Return should match resulting flags
1114 {
1116 return NTFR(TFR.FAIL);
1117 }
1118
1119 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
1120 {
1122 return NTFR(TFR.FAIL);
1123 }
1124
1125 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1126 {
1128 return NTFR(TFR.FAIL);
1129 }
1130
1131 // Reset
1133 return NTFR(TFR.SUCCESS);
1134 }
1135
1136 //---------------------------------------------------------------------------
1137 // Test module
1139 {
1140 // File lives in Game, use it while testing
1142
1143 // This was added at the same time as this API, so check if it works as well
1144 string nameOfCurrentModule = Type().GetModule();
1145 if (!Assert(nameOfCurrentModule != ""))
1146 {
1147 return NTFR(TFR.FAIL);
1148 }
1149
1150 // We know we are in Game, so use it as a test
1153 {
1154 return NTFR(TFR.FAIL);
1155 }
1156
1158 {
1159 return NTFR(TFR.FAIL);
1160 }
1161
1162 // Test if setting and getting works
1165
1167 {
1169 return NTFR(TFR.FAIL);
1170 }
1171
1172 // Data to restore
1175
1176 // Make sure we are only profiling Game and that the data is clean
1177 // Only valid for the Get...Per... methods, as they need to be sorted
1179
1180 // GetTickTime() returns in seconds, so gather the results in seconds too
1183
1184 // Time to sleeb
1185 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1186 float timeSlept = Sleep(0.3);
1187 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1188 float diff = postTime - previousTime - timeSlept;
1189
1190 // Restore
1192
1193 // We called the function, so it must have some time
1194 if (!Assert(postTime > 0))
1195 {
1197
1198 if (!wasEnabled)
1199 EnProfiler.Enable(false, true);
1200
1202
1203 return NTFR(TFR.FAIL);
1204 }
1205
1206 if (!Assert(diff < 0.00001))
1207 {
1209
1210 if (!wasEnabled)
1211 EnProfiler.Enable(false, true);
1212
1214
1215 return NTFR(TFR.FAIL);
1216 }
1217
1218 // Clean the session
1220
1221 // Something from a different module should not get sorted, so just fire something from a different module
1222 for (int i = 0; i < 1000; ++i)
1223 {
1224 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1225 }
1226
1227 // Sort and gather the data and validate if it is correct
1231
1232 Debug.TFLog("Game fncs:", this, "TestModule");
1233
1234 int funcCount = timePerFunc.Count();
1235 for (int j = 0; j < funcCount; ++j)
1236 {
1238 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1239 // We are currently profiling Game, so this Core function shouldn't be present
1240 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1241 {
1243
1244 if (!wasEnabled)
1245 EnProfiler.Enable(false, true);
1246
1248
1249 return NTFR(TFR.FAIL);
1250 }
1251 }
1252
1255
1256 int classCount = timePerClass.Count();
1257 for (int k = 0; k < classCount; ++k)
1258 {
1259 typename type = timePerClass[k].param2;
1261 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1262 {
1264
1265 if (!wasEnabled)
1266 EnProfiler.Enable(false, true);
1267
1269
1270 return NTFR(TFR.FAIL);
1271 }
1272
1273 // Only classes from Game should be present
1274 if (!Assert(classModule == eptModule))
1275 {
1277
1278 if (!wasEnabled)
1279 EnProfiler.Enable(false, true);
1280
1282
1283 return NTFR(TFR.FAIL);
1284 }
1285 }
1286
1287 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1290 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1292
1293 bool found = false;
1294
1295 Debug.TFLog("Core fncs:", this, "TestModule");
1296
1297 funcCount = timePerFunc.Count();
1298 for (int l = 0; l < funcCount; ++l)
1299 {
1301 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1302 // We are currently profiling Core, so this Core function should be present
1303 if (tfpc.param2 == "EnumTools::StringToEnum")
1304 {
1305 found = true;
1306 break;
1307 }
1308 }
1309
1310 Assert(found);
1311
1312 // Test some bogus
1314 EnProfiler.SetModule(-333);
1315 bool success = Assert(EnProfiler.GetModule() == mod);
1316 EnProfiler.SetModule(6003);
1318
1321
1322 if (!wasEnabled)
1323 EnProfiler.Enable(false, true);
1324
1325 return BTFR(success && found);
1326 }
1327
1328 //---------------------------------------------------------------------------
1329 // Test to see if class time data is correct
1331 {
1332 // We should restore this when done
1335
1336 // GetTickTime() returns in seconds, so gather the results in seconds too
1338
1339 // Create the classes
1341
1342 // Time to stress
1343 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1344 float timeStressed = clss.DoEverything();
1345 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1346 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1349
1350 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1351
1352 // Restore
1354 if (!wasEnabled)
1355 EnProfiler.Enable(false, true);
1356
1357 // We called the function, so it must have some time
1358 if (!Assert(postTime > 0))
1359 {
1360 return NTFR(TFR.FAIL);
1361 }
1362
1364 {
1365 return NTFR(TFR.FAIL);
1366 }
1367
1368 if (!Assert(diff < 0.001))
1369 {
1370 return NTFR(TFR.FAIL);
1371 }
1372
1373 return NTFR(TFR.SUCCESS);
1374 }
1375
1376 //---------------------------------------------------------------------------
1377 // Test to see if class count data is correct
1379 {
1380 const int allocAmount = 9;
1381 const int releaseAmount = 6;
1383
1384 // We should restore this when done
1386
1387 // Time to test
1390
1392 for (int i = 0; i < allocAmount; ++i)
1393 {
1394 instanceArr.Insert(new EPTHelperClass());
1395 }
1396
1397 for (int j = 0; j < releaseAmount; ++j)
1398 {
1399 delete instanceArr[j];
1400 }
1401
1402 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1403 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1404
1407
1408 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1409
1410 // Restore
1411 if (!wasEnabled)
1412 EnProfiler.Enable(false, true);
1413
1414 // Time to check
1415 if (!Assert(alloced == allocAmount))
1416 {
1417 return NTFR(TFR.FAIL);
1418 }
1419
1421 {
1422 return NTFR(TFR.FAIL);
1423 }
1424
1425 return NTFR(TFR.SUCCESS);
1426 }
1427
1428 //---------------------------------------------------------------------------
1429 // Test to see if func time data is correct
1431 {
1432 // We should restore this when done
1435
1436 // GetTickTime() returns in seconds, so gather the results in seconds too
1438
1439 // Time to stress
1440 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1441 float timeStressed = StringFormat();
1442 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1445
1446 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1447 float timeStressed2 = StringConcat();
1448 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1451
1452 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1453
1454 // Restore
1456 if (!wasEnabled)
1457 {
1458 EnProfiler.Enable(false, true);
1459 }
1460
1461 // We called the function, so it must have some time
1462 if (!Assert(postTime > 0))
1463 {
1464 return NTFR(TFR.FAIL);
1465 }
1466
1467 if (!Assert(diff < 0.001))
1468 {
1469 return NTFR(TFR.FAIL);
1470 }
1471
1472 if (!Assert(postTime2 > 0))
1473 {
1474 return NTFR(TFR.FAIL);
1475 }
1476
1477 if (!Assert(diff2 < 0.001))
1478 {
1479 return NTFR(TFR.FAIL);
1480 }
1481
1482 // I know that string.Format is faster than additive concatenation
1484 {
1485 return NTFR(TFR.FAIL);
1486 }
1487
1488 return NTFR(TFR.SUCCESS);
1489 }
1490
1491 //---------------------------------------------------------------------------
1492 // Test to see if func count data is correct
1494 {
1495 // We should restore this when done
1497
1498 // Time to count
1499
1500 // - CallFunction
1501 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1502 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1503 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1504
1506
1507 // - CallFunctionParams
1508 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1509 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1510 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1511
1513
1514 // - Regular call
1515 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1517 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1518
1520
1521 // - Call
1522 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1523 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1524 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1525
1527
1528 // - Garbage
1529 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1530
1531 // - Static
1532 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1534 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1535
1537
1538 // - Global
1539 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1540 GetDayZGame();
1541 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1542
1544
1545 // - Global proto
1546 // Not tracked, so don't need to compare before and after, should always be 0
1547 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1548 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1549
1550 // - Static proto
1551 // Not tracked, so don't need to compare before and after, should always be 0
1552 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1553
1554 // - proto
1555 // Not tracked, so don't need to compare before and after, should always be 0
1556 GetGame().GetHostName();
1557 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1558
1559 // - proto native
1560 // Not tracked, so don't need to compare before and after, should always be 0
1561 GetGame().IsServer();
1562 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1563
1564 // - static proto native
1565 // Not tracked, so don't need to compare before and after, should always be 0
1567 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1568
1569 // Restore
1570 if (!wasEnabled)
1571 {
1572 EnProfiler.Enable(false, true);
1573 }
1574
1575 // Do the checks
1576
1577 // - CallFunction
1578 if (!Assert(callCountCF == 1))
1579 {
1580 return NTFR(TFR.FAIL);
1581 }
1582
1583 // - CallFunctionParams
1584 if (!Assert(callCountCFP == 1))
1585 {
1586 return NTFR(TFR.FAIL);
1587 }
1588
1589 // - Regular call
1590 if (!Assert(callCountRG == 1))
1591 {
1592 return NTFR(TFR.FAIL);
1593 }
1594
1595 // - Call
1596 if (!Assert(callCountC == 1))
1597 {
1598 return NTFR(TFR.FAIL);
1599 }
1600
1601 // - Garbage
1602 if (!Assert(callCountNon == -1))
1603 {
1604 return NTFR(TFR.FAIL);
1605 }
1606
1607 // - Static
1608 if (!Assert(callCountS == 1))
1609 {
1610 return NTFR(TFR.FAIL);
1611 }
1612
1613 // - Global
1614 if (!Assert(callCountG == 1))
1615 {
1616 return NTFR(TFR.FAIL);
1617 }
1618
1619 // - Global proto
1620 if (!Assert(callCountGP == 0))
1621 {
1622 return NTFR(TFR.FAIL);
1623 }
1624
1625 // - Static proto
1626 if (!Assert(callCountSP == 0))
1627 {
1628 return NTFR(TFR.FAIL);
1629 }
1630
1631 // - proto
1632 if (!Assert(callCountP == 0))
1633 {
1634 return NTFR(TFR.FAIL);
1635 }
1636
1637 // - proto native
1638 if (!Assert(callCountPN == 0))
1639 {
1640 return NTFR(TFR.FAIL);
1641 }
1642
1643 // - static proto native
1644 if (!Assert(callCountSPN == 0))
1645 {
1646 return NTFR(TFR.FAIL);
1647 }
1648
1649 return NTFR(TFR.SUCCESS);
1650 }
1651
1652 //---------------------------------------------------------------------------
1653 // Helpers
1654 //---------------------------------------------------------------------------
1655 // Snore
1656 float Sleep(float timeS)
1657 {
1658 float startTime = GetGame().GetTickTime();
1659 while (GetGame().GetTickTime() - startTime < timeS)
1660 {
1661 // Zzz
1662 }
1663
1664 return GetGame().GetTickTime() - startTime;
1665 }
1666
1667 //---------------------------------------------------------------------------
1668 // Example stress method
1669 float StringFormat()
1670 {
1671 float startTime = GetGame().GetTickTime();
1672
1673 for (int i = 0; i < 1000; ++i)
1674 {
1675 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1676 }
1677
1678 return GetGame().GetTickTime() - startTime;
1679 }
1680
1681 //---------------------------------------------------------------------------
1682 // Example stress method 2
1683 float StringConcat()
1684 {
1685 float startTime = GetGame().GetTickTime();
1686
1687 for (int i = 0; i < 1000; ++i)
1688 {
1689 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1690 }
1691
1692 return GetGame().GetTickTime() - startTime;
1693 }
1694
1695 //---------------------------------------------------------------------------
1696 // To make sure it is only ever called in that test
1698 {
1699 int dummy = 3;
1700 }
1701
1702 //---------------------------------------------------------------------------
1703 // To make sure it is only ever called in that test
1704 static void TestFuncCountDataHelperStatic()
1705 {
1706 int dummy = 3;
1707 }
1708}
1709
1710class EPTHelperClass
1711{
1712 float Sleep2(float timeS)
1713 {
1714 float startTime = GetGame().GetTickTime();
1715 while (GetGame().GetTickTime() - startTime < timeS)
1716 {
1717 // Zzz
1718 }
1719
1720 return GetGame().GetTickTime() - startTime;
1721 }
1722 float SleepAgain(float timeS)
1723 {
1724 float startTime = GetGame().GetTickTime();
1725 while (GetGame().GetTickTime() - startTime < timeS)
1726 {
1727 // Zzz
1728 }
1729
1730 return GetGame().GetTickTime() - startTime;
1731 }
1732
1733 float DoEverything()
1734 {
1735 float startTime = GetGame().GetTickTime();
1736
1737 Sleep2(3);
1738 SleepAgain(3);
1739
1740 return GetGame().GetTickTime() - startTime;
1741 }
1742}

◆ TestSetFlags()

TFResult TestSetFlags ( )
828 : TestFramework
829{
832
833 //---------------------------------------------------------------------------
834 // Ctor - Decides the tests to run
835 //---------------------------------------------------------------------------
836 void EnProfilerTests()
837 {
839
840 AddInitTest("TestToggling");
841 AddInitTest("TestTogglingImmediate");
842 AddInitTest("TestSetFlags");
843 AddInitTest("TestClearFlags");
844 AddInitTest("TestAddFlags");
845 AddInitTest("TestModule");
846 AddInitTest("TestClassTimeData");
847 AddInitTest("TestClassCountData");
848 AddInitTest("TestFuncTimeData");
849 AddInitTest("TestFuncCountData");
850 }
851
852 //---------------------------------------------------------------------------
853 // Dtor
854 //---------------------------------------------------------------------------
855 void ~EnProfilerTests()
856 {
858 }
859
860 //---------------------------------------------------------------------------
861 // Tests
862 //---------------------------------------------------------------------------
863 // Test toggling state
865 {
869 {
872 }
873
874 return NTFR(TFR.FAIL);
875 }
876
877 //---------------------------------------------------------------------------
878 // Test toggling immediate state
880 {
884 {
887 }
888
889 return NTFR(TFR.FAIL);
890 }
891
892 //---------------------------------------------------------------------------
893 // Test SetFlags/GetFlags
895 {
897
898 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
899 {
901 EnProfiler.SetFlags(flags);
902
903 if (!Assert(EnProfiler.GetFlags() == flags))
904 {
906 return NTFR(TFR.FAIL);
907 }
908
909 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
910 {
912 EnProfiler.SetFlags(flags);
913
914 if (!Assert(EnProfiler.GetFlags() == flags))
915 {
917 return NTFR(TFR.FAIL);
918 }
919 }
920 }
921
922 // Let's test some bogus
923 EnProfiler.SetFlags(-333);
926 if (!Assert(bogusFlags == 0))
927 {
929 return NTFR(TFR.FAIL);
930 }
931
934 if (!Assert(bogusFlags == 0))
935 {
937 return NTFR(TFR.FAIL);
938 }
939
940 // Reset
942 return NTFR(TFR.SUCCESS);
943 }
944
945 //---------------------------------------------------------------------------
946 // Test removing of flags
948 {
950
952
954 {
956 return NTFR(TFR.FAIL);
957 }
958
961
963 {
965 return NTFR(TFR.FAIL);
966 }
967
970
972 {
974 return NTFR(TFR.FAIL);
975 }
976
977 // Reset
979 return NTFR(TFR.SUCCESS);
980 }
981
982 //---------------------------------------------------------------------------
983 // Test adding of flags
985 {
987
989
990 // Return should match resulting flags
992 {
994 return NTFR(TFR.FAIL);
995 }
996
997 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
998 {
1000 return NTFR(TFR.FAIL);
1001 }
1002
1003 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
1004 {
1006 return NTFR(TFR.FAIL);
1007 }
1008
1009 // Reset
1011 return NTFR(TFR.SUCCESS);
1012 }
1013
1014 //---------------------------------------------------------------------------
1015 // Test module
1017 {
1018 // File lives in Game, use it while testing
1020
1021 // This was added at the same time as this API, so check if it works as well
1022 string nameOfCurrentModule = Type().GetModule();
1023 if (!Assert(nameOfCurrentModule != ""))
1024 {
1025 return NTFR(TFR.FAIL);
1026 }
1027
1028 // We know we are in Game, so use it as a test
1031 {
1032 return NTFR(TFR.FAIL);
1033 }
1034
1036 {
1037 return NTFR(TFR.FAIL);
1038 }
1039
1040 // Test if setting and getting works
1043
1045 {
1047 return NTFR(TFR.FAIL);
1048 }
1049
1050 // Data to restore
1053
1054 // Make sure we are only profiling Game and that the data is clean
1055 // Only valid for the Get...Per... methods, as they need to be sorted
1057
1058 // GetTickTime() returns in seconds, so gather the results in seconds too
1061
1062 // Time to sleeb
1063 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1064 float timeSlept = Sleep(0.3);
1065 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1066 float diff = postTime - previousTime - timeSlept;
1067
1068 // Restore
1070
1071 // We called the function, so it must have some time
1072 if (!Assert(postTime > 0))
1073 {
1075
1076 if (!wasEnabled)
1077 EnProfiler.Enable(false, true);
1078
1080
1081 return NTFR(TFR.FAIL);
1082 }
1083
1084 if (!Assert(diff < 0.00001))
1085 {
1087
1088 if (!wasEnabled)
1089 EnProfiler.Enable(false, true);
1090
1092
1093 return NTFR(TFR.FAIL);
1094 }
1095
1096 // Clean the session
1098
1099 // Something from a different module should not get sorted, so just fire something from a different module
1100 for (int i = 0; i < 1000; ++i)
1101 {
1102 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1103 }
1104
1105 // Sort and gather the data and validate if it is correct
1109
1110 Debug.TFLog("Game fncs:", this, "TestModule");
1111
1112 int funcCount = timePerFunc.Count();
1113 for (int j = 0; j < funcCount; ++j)
1114 {
1116 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1117 // We are currently profiling Game, so this Core function shouldn't be present
1118 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1119 {
1121
1122 if (!wasEnabled)
1123 EnProfiler.Enable(false, true);
1124
1126
1127 return NTFR(TFR.FAIL);
1128 }
1129 }
1130
1133
1134 int classCount = timePerClass.Count();
1135 for (int k = 0; k < classCount; ++k)
1136 {
1137 typename type = timePerClass[k].param2;
1139 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1140 {
1142
1143 if (!wasEnabled)
1144 EnProfiler.Enable(false, true);
1145
1147
1148 return NTFR(TFR.FAIL);
1149 }
1150
1151 // Only classes from Game should be present
1152 if (!Assert(classModule == eptModule))
1153 {
1155
1156 if (!wasEnabled)
1157 EnProfiler.Enable(false, true);
1158
1160
1161 return NTFR(TFR.FAIL);
1162 }
1163 }
1164
1165 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1168 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1170
1171 bool found = false;
1172
1173 Debug.TFLog("Core fncs:", this, "TestModule");
1174
1175 funcCount = timePerFunc.Count();
1176 for (int l = 0; l < funcCount; ++l)
1177 {
1179 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1180 // We are currently profiling Core, so this Core function should be present
1181 if (tfpc.param2 == "EnumTools::StringToEnum")
1182 {
1183 found = true;
1184 break;
1185 }
1186 }
1187
1188 Assert(found);
1189
1190 // Test some bogus
1192 EnProfiler.SetModule(-333);
1193 bool success = Assert(EnProfiler.GetModule() == mod);
1194 EnProfiler.SetModule(6003);
1196
1199
1200 if (!wasEnabled)
1201 EnProfiler.Enable(false, true);
1202
1203 return BTFR(success && found);
1204 }
1205
1206 //---------------------------------------------------------------------------
1207 // Test to see if class time data is correct
1209 {
1210 // We should restore this when done
1213
1214 // GetTickTime() returns in seconds, so gather the results in seconds too
1216
1217 // Create the classes
1219
1220 // Time to stress
1221 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1222 float timeStressed = clss.DoEverything();
1223 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1224 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1227
1228 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1229
1230 // Restore
1232 if (!wasEnabled)
1233 EnProfiler.Enable(false, true);
1234
1235 // We called the function, so it must have some time
1236 if (!Assert(postTime > 0))
1237 {
1238 return NTFR(TFR.FAIL);
1239 }
1240
1242 {
1243 return NTFR(TFR.FAIL);
1244 }
1245
1246 if (!Assert(diff < 0.001))
1247 {
1248 return NTFR(TFR.FAIL);
1249 }
1250
1251 return NTFR(TFR.SUCCESS);
1252 }
1253
1254 //---------------------------------------------------------------------------
1255 // Test to see if class count data is correct
1257 {
1258 const int allocAmount = 9;
1259 const int releaseAmount = 6;
1261
1262 // We should restore this when done
1264
1265 // Time to test
1268
1270 for (int i = 0; i < allocAmount; ++i)
1271 {
1272 instanceArr.Insert(new EPTHelperClass());
1273 }
1274
1275 for (int j = 0; j < releaseAmount; ++j)
1276 {
1277 delete instanceArr[j];
1278 }
1279
1280 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1281 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1282
1285
1286 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1287
1288 // Restore
1289 if (!wasEnabled)
1290 EnProfiler.Enable(false, true);
1291
1292 // Time to check
1293 if (!Assert(alloced == allocAmount))
1294 {
1295 return NTFR(TFR.FAIL);
1296 }
1297
1299 {
1300 return NTFR(TFR.FAIL);
1301 }
1302
1303 return NTFR(TFR.SUCCESS);
1304 }
1305
1306 //---------------------------------------------------------------------------
1307 // Test to see if func time data is correct
1309 {
1310 // We should restore this when done
1313
1314 // GetTickTime() returns in seconds, so gather the results in seconds too
1316
1317 // Time to stress
1318 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1319 float timeStressed = StringFormat();
1320 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1323
1324 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1325 float timeStressed2 = StringConcat();
1326 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1329
1330 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1331
1332 // Restore
1334 if (!wasEnabled)
1335 {
1336 EnProfiler.Enable(false, true);
1337 }
1338
1339 // We called the function, so it must have some time
1340 if (!Assert(postTime > 0))
1341 {
1342 return NTFR(TFR.FAIL);
1343 }
1344
1345 if (!Assert(diff < 0.001))
1346 {
1347 return NTFR(TFR.FAIL);
1348 }
1349
1350 if (!Assert(postTime2 > 0))
1351 {
1352 return NTFR(TFR.FAIL);
1353 }
1354
1355 if (!Assert(diff2 < 0.001))
1356 {
1357 return NTFR(TFR.FAIL);
1358 }
1359
1360 // I know that string.Format is faster than additive concatenation
1362 {
1363 return NTFR(TFR.FAIL);
1364 }
1365
1366 return NTFR(TFR.SUCCESS);
1367 }
1368
1369 //---------------------------------------------------------------------------
1370 // Test to see if func count data is correct
1372 {
1373 // We should restore this when done
1375
1376 // Time to count
1377
1378 // - CallFunction
1379 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1380 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1381 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1382
1384
1385 // - CallFunctionParams
1386 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1387 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1388 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1389
1391
1392 // - Regular call
1393 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1395 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1396
1398
1399 // - Call
1400 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1401 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1402 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1403
1405
1406 // - Garbage
1407 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1408
1409 // - Static
1410 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1412 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1413
1415
1416 // - Global
1417 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1418 GetDayZGame();
1419 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1420
1422
1423 // - Global proto
1424 // Not tracked, so don't need to compare before and after, should always be 0
1425 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1426 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1427
1428 // - Static proto
1429 // Not tracked, so don't need to compare before and after, should always be 0
1430 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1431
1432 // - proto
1433 // Not tracked, so don't need to compare before and after, should always be 0
1434 GetGame().GetHostName();
1435 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1436
1437 // - proto native
1438 // Not tracked, so don't need to compare before and after, should always be 0
1439 GetGame().IsServer();
1440 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1441
1442 // - static proto native
1443 // Not tracked, so don't need to compare before and after, should always be 0
1445 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1446
1447 // Restore
1448 if (!wasEnabled)
1449 {
1450 EnProfiler.Enable(false, true);
1451 }
1452
1453 // Do the checks
1454
1455 // - CallFunction
1456 if (!Assert(callCountCF == 1))
1457 {
1458 return NTFR(TFR.FAIL);
1459 }
1460
1461 // - CallFunctionParams
1462 if (!Assert(callCountCFP == 1))
1463 {
1464 return NTFR(TFR.FAIL);
1465 }
1466
1467 // - Regular call
1468 if (!Assert(callCountRG == 1))
1469 {
1470 return NTFR(TFR.FAIL);
1471 }
1472
1473 // - Call
1474 if (!Assert(callCountC == 1))
1475 {
1476 return NTFR(TFR.FAIL);
1477 }
1478
1479 // - Garbage
1480 if (!Assert(callCountNon == -1))
1481 {
1482 return NTFR(TFR.FAIL);
1483 }
1484
1485 // - Static
1486 if (!Assert(callCountS == 1))
1487 {
1488 return NTFR(TFR.FAIL);
1489 }
1490
1491 // - Global
1492 if (!Assert(callCountG == 1))
1493 {
1494 return NTFR(TFR.FAIL);
1495 }
1496
1497 // - Global proto
1498 if (!Assert(callCountGP == 0))
1499 {
1500 return NTFR(TFR.FAIL);
1501 }
1502
1503 // - Static proto
1504 if (!Assert(callCountSP == 0))
1505 {
1506 return NTFR(TFR.FAIL);
1507 }
1508
1509 // - proto
1510 if (!Assert(callCountP == 0))
1511 {
1512 return NTFR(TFR.FAIL);
1513 }
1514
1515 // - proto native
1516 if (!Assert(callCountPN == 0))
1517 {
1518 return NTFR(TFR.FAIL);
1519 }
1520
1521 // - static proto native
1522 if (!Assert(callCountSPN == 0))
1523 {
1524 return NTFR(TFR.FAIL);
1525 }
1526
1527 return NTFR(TFR.SUCCESS);
1528 }
1529
1530 //---------------------------------------------------------------------------
1531 // Helpers
1532 //---------------------------------------------------------------------------
1533 // Snore
1534 float Sleep(float timeS)
1535 {
1536 float startTime = GetGame().GetTickTime();
1537 while (GetGame().GetTickTime() - startTime < timeS)
1538 {
1539 // Zzz
1540 }
1541
1542 return GetGame().GetTickTime() - startTime;
1543 }
1544
1545 //---------------------------------------------------------------------------
1546 // Example stress method
1547 float StringFormat()
1548 {
1549 float startTime = GetGame().GetTickTime();
1550
1551 for (int i = 0; i < 1000; ++i)
1552 {
1553 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1554 }
1555
1556 return GetGame().GetTickTime() - startTime;
1557 }
1558
1559 //---------------------------------------------------------------------------
1560 // Example stress method 2
1561 float StringConcat()
1562 {
1563 float startTime = GetGame().GetTickTime();
1564
1565 for (int i = 0; i < 1000; ++i)
1566 {
1567 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1568 }
1569
1570 return GetGame().GetTickTime() - startTime;
1571 }
1572
1573 //---------------------------------------------------------------------------
1574 // To make sure it is only ever called in that test
1576 {
1577 int dummy = 3;
1578 }
1579
1580 //---------------------------------------------------------------------------
1581 // To make sure it is only ever called in that test
1582 static void TestFuncCountDataHelperStatic()
1583 {
1584 int dummy = 3;
1585 }
1586}
1587
1588class EPTHelperClass
1589{
1590 float Sleep2(float timeS)
1591 {
1592 float startTime = GetGame().GetTickTime();
1593 while (GetGame().GetTickTime() - startTime < timeS)
1594 {
1595 // Zzz
1596 }
1597
1598 return GetGame().GetTickTime() - startTime;
1599 }
1600 float SleepAgain(float timeS)
1601 {
1602 float startTime = GetGame().GetTickTime();
1603 while (GetGame().GetTickTime() - startTime < timeS)
1604 {
1605 // Zzz
1606 }
1607
1608 return GetGame().GetTickTime() - startTime;
1609 }
1610
1611 float DoEverything()
1612 {
1613 float startTime = GetGame().GetTickTime();
1614
1615 Sleep2(3);
1616 SleepAgain(3);
1617
1618 return GetGame().GetTickTime() - startTime;
1619 }
1620}

◆ TestToggling()

TFResult TestToggling ( )
798 : TestFramework
799{
802
803 //---------------------------------------------------------------------------
804 // Ctor - Decides the tests to run
805 //---------------------------------------------------------------------------
806 void EnProfilerTests()
807 {
809
810 AddInitTest("TestToggling");
811 AddInitTest("TestTogglingImmediate");
812 AddInitTest("TestSetFlags");
813 AddInitTest("TestClearFlags");
814 AddInitTest("TestAddFlags");
815 AddInitTest("TestModule");
816 AddInitTest("TestClassTimeData");
817 AddInitTest("TestClassCountData");
818 AddInitTest("TestFuncTimeData");
819 AddInitTest("TestFuncCountData");
820 }
821
822 //---------------------------------------------------------------------------
823 // Dtor
824 //---------------------------------------------------------------------------
825 void ~EnProfilerTests()
826 {
828 }
829
830 //---------------------------------------------------------------------------
831 // Tests
832 //---------------------------------------------------------------------------
833 // Test toggling state
835 {
839 {
842 }
843
844 return NTFR(TFR.FAIL);
845 }
846
847 //---------------------------------------------------------------------------
848 // Test toggling immediate state
850 {
854 {
857 }
858
859 return NTFR(TFR.FAIL);
860 }
861
862 //---------------------------------------------------------------------------
863 // Test SetFlags/GetFlags
865 {
867
868 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
869 {
871 EnProfiler.SetFlags(flags);
872
873 if (!Assert(EnProfiler.GetFlags() == flags))
874 {
876 return NTFR(TFR.FAIL);
877 }
878
879 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
880 {
882 EnProfiler.SetFlags(flags);
883
884 if (!Assert(EnProfiler.GetFlags() == flags))
885 {
887 return NTFR(TFR.FAIL);
888 }
889 }
890 }
891
892 // Let's test some bogus
893 EnProfiler.SetFlags(-333);
896 if (!Assert(bogusFlags == 0))
897 {
899 return NTFR(TFR.FAIL);
900 }
901
904 if (!Assert(bogusFlags == 0))
905 {
907 return NTFR(TFR.FAIL);
908 }
909
910 // Reset
912 return NTFR(TFR.SUCCESS);
913 }
914
915 //---------------------------------------------------------------------------
916 // Test removing of flags
918 {
920
922
924 {
926 return NTFR(TFR.FAIL);
927 }
928
931
933 {
935 return NTFR(TFR.FAIL);
936 }
937
940
942 {
944 return NTFR(TFR.FAIL);
945 }
946
947 // Reset
949 return NTFR(TFR.SUCCESS);
950 }
951
952 //---------------------------------------------------------------------------
953 // Test adding of flags
955 {
957
959
960 // Return should match resulting flags
962 {
964 return NTFR(TFR.FAIL);
965 }
966
967 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
968 {
970 return NTFR(TFR.FAIL);
971 }
972
973 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
974 {
976 return NTFR(TFR.FAIL);
977 }
978
979 // Reset
981 return NTFR(TFR.SUCCESS);
982 }
983
984 //---------------------------------------------------------------------------
985 // Test module
987 {
988 // File lives in Game, use it while testing
990
991 // This was added at the same time as this API, so check if it works as well
992 string nameOfCurrentModule = Type().GetModule();
993 if (!Assert(nameOfCurrentModule != ""))
994 {
995 return NTFR(TFR.FAIL);
996 }
997
998 // We know we are in Game, so use it as a test
1001 {
1002 return NTFR(TFR.FAIL);
1003 }
1004
1006 {
1007 return NTFR(TFR.FAIL);
1008 }
1009
1010 // Test if setting and getting works
1013
1015 {
1017 return NTFR(TFR.FAIL);
1018 }
1019
1020 // Data to restore
1023
1024 // Make sure we are only profiling Game and that the data is clean
1025 // Only valid for the Get...Per... methods, as they need to be sorted
1027
1028 // GetTickTime() returns in seconds, so gather the results in seconds too
1031
1032 // Time to sleeb
1033 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1034 float timeSlept = Sleep(0.3);
1035 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1036 float diff = postTime - previousTime - timeSlept;
1037
1038 // Restore
1040
1041 // We called the function, so it must have some time
1042 if (!Assert(postTime > 0))
1043 {
1045
1046 if (!wasEnabled)
1047 EnProfiler.Enable(false, true);
1048
1050
1051 return NTFR(TFR.FAIL);
1052 }
1053
1054 if (!Assert(diff < 0.00001))
1055 {
1057
1058 if (!wasEnabled)
1059 EnProfiler.Enable(false, true);
1060
1062
1063 return NTFR(TFR.FAIL);
1064 }
1065
1066 // Clean the session
1068
1069 // Something from a different module should not get sorted, so just fire something from a different module
1070 for (int i = 0; i < 1000; ++i)
1071 {
1072 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1073 }
1074
1075 // Sort and gather the data and validate if it is correct
1079
1080 Debug.TFLog("Game fncs:", this, "TestModule");
1081
1082 int funcCount = timePerFunc.Count();
1083 for (int j = 0; j < funcCount; ++j)
1084 {
1086 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1087 // We are currently profiling Game, so this Core function shouldn't be present
1088 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1089 {
1091
1092 if (!wasEnabled)
1093 EnProfiler.Enable(false, true);
1094
1096
1097 return NTFR(TFR.FAIL);
1098 }
1099 }
1100
1103
1104 int classCount = timePerClass.Count();
1105 for (int k = 0; k < classCount; ++k)
1106 {
1107 typename type = timePerClass[k].param2;
1109 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1110 {
1112
1113 if (!wasEnabled)
1114 EnProfiler.Enable(false, true);
1115
1117
1118 return NTFR(TFR.FAIL);
1119 }
1120
1121 // Only classes from Game should be present
1122 if (!Assert(classModule == eptModule))
1123 {
1125
1126 if (!wasEnabled)
1127 EnProfiler.Enable(false, true);
1128
1130
1131 return NTFR(TFR.FAIL);
1132 }
1133 }
1134
1135 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1138 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1140
1141 bool found = false;
1142
1143 Debug.TFLog("Core fncs:", this, "TestModule");
1144
1145 funcCount = timePerFunc.Count();
1146 for (int l = 0; l < funcCount; ++l)
1147 {
1149 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1150 // We are currently profiling Core, so this Core function should be present
1151 if (tfpc.param2 == "EnumTools::StringToEnum")
1152 {
1153 found = true;
1154 break;
1155 }
1156 }
1157
1158 Assert(found);
1159
1160 // Test some bogus
1162 EnProfiler.SetModule(-333);
1163 bool success = Assert(EnProfiler.GetModule() == mod);
1164 EnProfiler.SetModule(6003);
1166
1169
1170 if (!wasEnabled)
1171 EnProfiler.Enable(false, true);
1172
1173 return BTFR(success && found);
1174 }
1175
1176 //---------------------------------------------------------------------------
1177 // Test to see if class time data is correct
1179 {
1180 // We should restore this when done
1183
1184 // GetTickTime() returns in seconds, so gather the results in seconds too
1186
1187 // Create the classes
1189
1190 // Time to stress
1191 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1192 float timeStressed = clss.DoEverything();
1193 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1194 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1197
1198 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1199
1200 // Restore
1202 if (!wasEnabled)
1203 EnProfiler.Enable(false, true);
1204
1205 // We called the function, so it must have some time
1206 if (!Assert(postTime > 0))
1207 {
1208 return NTFR(TFR.FAIL);
1209 }
1210
1212 {
1213 return NTFR(TFR.FAIL);
1214 }
1215
1216 if (!Assert(diff < 0.001))
1217 {
1218 return NTFR(TFR.FAIL);
1219 }
1220
1221 return NTFR(TFR.SUCCESS);
1222 }
1223
1224 //---------------------------------------------------------------------------
1225 // Test to see if class count data is correct
1227 {
1228 const int allocAmount = 9;
1229 const int releaseAmount = 6;
1231
1232 // We should restore this when done
1234
1235 // Time to test
1238
1240 for (int i = 0; i < allocAmount; ++i)
1241 {
1242 instanceArr.Insert(new EPTHelperClass());
1243 }
1244
1245 for (int j = 0; j < releaseAmount; ++j)
1246 {
1247 delete instanceArr[j];
1248 }
1249
1250 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1251 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1252
1255
1256 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1257
1258 // Restore
1259 if (!wasEnabled)
1260 EnProfiler.Enable(false, true);
1261
1262 // Time to check
1263 if (!Assert(alloced == allocAmount))
1264 {
1265 return NTFR(TFR.FAIL);
1266 }
1267
1269 {
1270 return NTFR(TFR.FAIL);
1271 }
1272
1273 return NTFR(TFR.SUCCESS);
1274 }
1275
1276 //---------------------------------------------------------------------------
1277 // Test to see if func time data is correct
1279 {
1280 // We should restore this when done
1283
1284 // GetTickTime() returns in seconds, so gather the results in seconds too
1286
1287 // Time to stress
1288 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1289 float timeStressed = StringFormat();
1290 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1293
1294 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1295 float timeStressed2 = StringConcat();
1296 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1299
1300 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1301
1302 // Restore
1304 if (!wasEnabled)
1305 {
1306 EnProfiler.Enable(false, true);
1307 }
1308
1309 // We called the function, so it must have some time
1310 if (!Assert(postTime > 0))
1311 {
1312 return NTFR(TFR.FAIL);
1313 }
1314
1315 if (!Assert(diff < 0.001))
1316 {
1317 return NTFR(TFR.FAIL);
1318 }
1319
1320 if (!Assert(postTime2 > 0))
1321 {
1322 return NTFR(TFR.FAIL);
1323 }
1324
1325 if (!Assert(diff2 < 0.001))
1326 {
1327 return NTFR(TFR.FAIL);
1328 }
1329
1330 // I know that string.Format is faster than additive concatenation
1332 {
1333 return NTFR(TFR.FAIL);
1334 }
1335
1336 return NTFR(TFR.SUCCESS);
1337 }
1338
1339 //---------------------------------------------------------------------------
1340 // Test to see if func count data is correct
1342 {
1343 // We should restore this when done
1345
1346 // Time to count
1347
1348 // - CallFunction
1349 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1350 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1351 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1352
1354
1355 // - CallFunctionParams
1356 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1357 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1358 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1359
1361
1362 // - Regular call
1363 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1365 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1366
1368
1369 // - Call
1370 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1371 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1372 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1373
1375
1376 // - Garbage
1377 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1378
1379 // - Static
1380 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1382 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1383
1385
1386 // - Global
1387 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1388 GetDayZGame();
1389 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1390
1392
1393 // - Global proto
1394 // Not tracked, so don't need to compare before and after, should always be 0
1395 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1396 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1397
1398 // - Static proto
1399 // Not tracked, so don't need to compare before and after, should always be 0
1400 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1401
1402 // - proto
1403 // Not tracked, so don't need to compare before and after, should always be 0
1404 GetGame().GetHostName();
1405 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1406
1407 // - proto native
1408 // Not tracked, so don't need to compare before and after, should always be 0
1409 GetGame().IsServer();
1410 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1411
1412 // - static proto native
1413 // Not tracked, so don't need to compare before and after, should always be 0
1415 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1416
1417 // Restore
1418 if (!wasEnabled)
1419 {
1420 EnProfiler.Enable(false, true);
1421 }
1422
1423 // Do the checks
1424
1425 // - CallFunction
1426 if (!Assert(callCountCF == 1))
1427 {
1428 return NTFR(TFR.FAIL);
1429 }
1430
1431 // - CallFunctionParams
1432 if (!Assert(callCountCFP == 1))
1433 {
1434 return NTFR(TFR.FAIL);
1435 }
1436
1437 // - Regular call
1438 if (!Assert(callCountRG == 1))
1439 {
1440 return NTFR(TFR.FAIL);
1441 }
1442
1443 // - Call
1444 if (!Assert(callCountC == 1))
1445 {
1446 return NTFR(TFR.FAIL);
1447 }
1448
1449 // - Garbage
1450 if (!Assert(callCountNon == -1))
1451 {
1452 return NTFR(TFR.FAIL);
1453 }
1454
1455 // - Static
1456 if (!Assert(callCountS == 1))
1457 {
1458 return NTFR(TFR.FAIL);
1459 }
1460
1461 // - Global
1462 if (!Assert(callCountG == 1))
1463 {
1464 return NTFR(TFR.FAIL);
1465 }
1466
1467 // - Global proto
1468 if (!Assert(callCountGP == 0))
1469 {
1470 return NTFR(TFR.FAIL);
1471 }
1472
1473 // - Static proto
1474 if (!Assert(callCountSP == 0))
1475 {
1476 return NTFR(TFR.FAIL);
1477 }
1478
1479 // - proto
1480 if (!Assert(callCountP == 0))
1481 {
1482 return NTFR(TFR.FAIL);
1483 }
1484
1485 // - proto native
1486 if (!Assert(callCountPN == 0))
1487 {
1488 return NTFR(TFR.FAIL);
1489 }
1490
1491 // - static proto native
1492 if (!Assert(callCountSPN == 0))
1493 {
1494 return NTFR(TFR.FAIL);
1495 }
1496
1497 return NTFR(TFR.SUCCESS);
1498 }
1499
1500 //---------------------------------------------------------------------------
1501 // Helpers
1502 //---------------------------------------------------------------------------
1503 // Snore
1504 float Sleep(float timeS)
1505 {
1506 float startTime = GetGame().GetTickTime();
1507 while (GetGame().GetTickTime() - startTime < timeS)
1508 {
1509 // Zzz
1510 }
1511
1512 return GetGame().GetTickTime() - startTime;
1513 }
1514
1515 //---------------------------------------------------------------------------
1516 // Example stress method
1517 float StringFormat()
1518 {
1519 float startTime = GetGame().GetTickTime();
1520
1521 for (int i = 0; i < 1000; ++i)
1522 {
1523 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1524 }
1525
1526 return GetGame().GetTickTime() - startTime;
1527 }
1528
1529 //---------------------------------------------------------------------------
1530 // Example stress method 2
1531 float StringConcat()
1532 {
1533 float startTime = GetGame().GetTickTime();
1534
1535 for (int i = 0; i < 1000; ++i)
1536 {
1537 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1538 }
1539
1540 return GetGame().GetTickTime() - startTime;
1541 }
1542
1543 //---------------------------------------------------------------------------
1544 // To make sure it is only ever called in that test
1546 {
1547 int dummy = 3;
1548 }
1549
1550 //---------------------------------------------------------------------------
1551 // To make sure it is only ever called in that test
1552 static void TestFuncCountDataHelperStatic()
1553 {
1554 int dummy = 3;
1555 }
1556}
1557
1558class EPTHelperClass
1559{
1560 float Sleep2(float timeS)
1561 {
1562 float startTime = GetGame().GetTickTime();
1563 while (GetGame().GetTickTime() - startTime < timeS)
1564 {
1565 // Zzz
1566 }
1567
1568 return GetGame().GetTickTime() - startTime;
1569 }
1570 float SleepAgain(float timeS)
1571 {
1572 float startTime = GetGame().GetTickTime();
1573 while (GetGame().GetTickTime() - startTime < timeS)
1574 {
1575 // Zzz
1576 }
1577
1578 return GetGame().GetTickTime() - startTime;
1579 }
1580
1581 float DoEverything()
1582 {
1583 float startTime = GetGame().GetTickTime();
1584
1585 Sleep2(3);
1586 SleepAgain(3);
1587
1588 return GetGame().GetTickTime() - startTime;
1589 }
1590}

◆ TestTogglingImmediate()

TFResult TestTogglingImmediate ( )
813 : TestFramework
814{
817
818 //---------------------------------------------------------------------------
819 // Ctor - Decides the tests to run
820 //---------------------------------------------------------------------------
821 void EnProfilerTests()
822 {
824
825 AddInitTest("TestToggling");
826 AddInitTest("TestTogglingImmediate");
827 AddInitTest("TestSetFlags");
828 AddInitTest("TestClearFlags");
829 AddInitTest("TestAddFlags");
830 AddInitTest("TestModule");
831 AddInitTest("TestClassTimeData");
832 AddInitTest("TestClassCountData");
833 AddInitTest("TestFuncTimeData");
834 AddInitTest("TestFuncCountData");
835 }
836
837 //---------------------------------------------------------------------------
838 // Dtor
839 //---------------------------------------------------------------------------
840 void ~EnProfilerTests()
841 {
843 }
844
845 //---------------------------------------------------------------------------
846 // Tests
847 //---------------------------------------------------------------------------
848 // Test toggling state
850 {
854 {
857 }
858
859 return NTFR(TFR.FAIL);
860 }
861
862 //---------------------------------------------------------------------------
863 // Test toggling immediate state
865 {
869 {
872 }
873
874 return NTFR(TFR.FAIL);
875 }
876
877 //---------------------------------------------------------------------------
878 // Test SetFlags/GetFlags
880 {
882
883 for (int i = 0; i < EnumTools.GetEnumSize(EnProfilerFlags); ++i)
884 {
886 EnProfiler.SetFlags(flags);
887
888 if (!Assert(EnProfiler.GetFlags() == flags))
889 {
891 return NTFR(TFR.FAIL);
892 }
893
894 for (int j = 0; j < EnumTools.GetEnumSize(EnProfilerFlags); ++j)
895 {
897 EnProfiler.SetFlags(flags);
898
899 if (!Assert(EnProfiler.GetFlags() == flags))
900 {
902 return NTFR(TFR.FAIL);
903 }
904 }
905 }
906
907 // Let's test some bogus
908 EnProfiler.SetFlags(-333);
911 if (!Assert(bogusFlags == 0))
912 {
914 return NTFR(TFR.FAIL);
915 }
916
919 if (!Assert(bogusFlags == 0))
920 {
922 return NTFR(TFR.FAIL);
923 }
924
925 // Reset
927 return NTFR(TFR.SUCCESS);
928 }
929
930 //---------------------------------------------------------------------------
931 // Test removing of flags
933 {
935
937
939 {
941 return NTFR(TFR.FAIL);
942 }
943
946
948 {
950 return NTFR(TFR.FAIL);
951 }
952
955
957 {
959 return NTFR(TFR.FAIL);
960 }
961
962 // Reset
964 return NTFR(TFR.SUCCESS);
965 }
966
967 //---------------------------------------------------------------------------
968 // Test adding of flags
970 {
972
974
975 // Return should match resulting flags
977 {
979 return NTFR(TFR.FAIL);
980 }
981
982 if (!Assert(EnProfiler.GetFlags() == EnProfilerFlags.RESET))
983 {
985 return NTFR(TFR.FAIL);
986 }
987
988 if (!Assert(EnProfiler.AddFlags(EnProfilerFlags.RECURSIVE) == (EnProfilerFlags.RESET | EnProfilerFlags.RECURSIVE)))
989 {
991 return NTFR(TFR.FAIL);
992 }
993
994 // Reset
996 return NTFR(TFR.SUCCESS);
997 }
998
999 //---------------------------------------------------------------------------
1000 // Test module
1002 {
1003 // File lives in Game, use it while testing
1005
1006 // This was added at the same time as this API, so check if it works as well
1007 string nameOfCurrentModule = Type().GetModule();
1008 if (!Assert(nameOfCurrentModule != ""))
1009 {
1010 return NTFR(TFR.FAIL);
1011 }
1012
1013 // We know we are in Game, so use it as a test
1016 {
1017 return NTFR(TFR.FAIL);
1018 }
1019
1021 {
1022 return NTFR(TFR.FAIL);
1023 }
1024
1025 // Test if setting and getting works
1028
1030 {
1032 return NTFR(TFR.FAIL);
1033 }
1034
1035 // Data to restore
1038
1039 // Make sure we are only profiling Game and that the data is clean
1040 // Only valid for the Get...Per... methods, as they need to be sorted
1042
1043 // GetTickTime() returns in seconds, so gather the results in seconds too
1046
1047 // Time to sleeb
1048 float previousTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1049 float timeSlept = Sleep(0.3);
1050 float postTime = EnProfiler.GetTimeOfFunc("Sleep", Type(), true);
1051 float diff = postTime - previousTime - timeSlept;
1052
1053 // Restore
1055
1056 // We called the function, so it must have some time
1057 if (!Assert(postTime > 0))
1058 {
1060
1061 if (!wasEnabled)
1062 EnProfiler.Enable(false, true);
1063
1065
1066 return NTFR(TFR.FAIL);
1067 }
1068
1069 if (!Assert(diff < 0.00001))
1070 {
1072
1073 if (!wasEnabled)
1074 EnProfiler.Enable(false, true);
1075
1077
1078 return NTFR(TFR.FAIL);
1079 }
1080
1081 // Clean the session
1083
1084 // Something from a different module should not get sorted, so just fire something from a different module
1085 for (int i = 0; i < 1000; ++i)
1086 {
1087 EnumTools.StringToEnum(EnProfilerModule, "MISSION_CUSTOM");
1088 }
1089
1090 // Sort and gather the data and validate if it is correct
1094
1095 Debug.TFLog("Game fncs:", this, "TestModule");
1096
1097 int funcCount = timePerFunc.Count();
1098 for (int j = 0; j < funcCount; ++j)
1099 {
1101 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfp.param1, tfp.param2), this, "TestModule");
1102 // We are currently profiling Game, so this Core function shouldn't be present
1103 if (!Assert(tfp.param2 != "EnumTools::StringToEnum"))
1104 {
1106
1107 if (!wasEnabled)
1108 EnProfiler.Enable(false, true);
1109
1111
1112 return NTFR(TFR.FAIL);
1113 }
1114 }
1115
1118
1119 int classCount = timePerClass.Count();
1120 for (int k = 0; k < classCount; ++k)
1121 {
1122 typename type = timePerClass[k].param2;
1124 if (!Assert(EnProfiler.NameToModule(type.GetModule(), classModule)))
1125 {
1127
1128 if (!wasEnabled)
1129 EnProfiler.Enable(false, true);
1130
1132
1133 return NTFR(TFR.FAIL);
1134 }
1135
1136 // Only classes from Game should be present
1137 if (!Assert(classModule == eptModule))
1138 {
1140
1141 if (!wasEnabled)
1142 EnProfiler.Enable(false, true);
1143
1145
1146 return NTFR(TFR.FAIL);
1147 }
1148 }
1149
1150 // Now let's check if we can gather the data of what we called earlier by switching the profiled module without resetting the session
1153 timePerFunc.Clear(); // Let's reuse the array, but the Get...Per... methods only appends to the array, clearing is our responsibility
1155
1156 bool found = false;
1157
1158 Debug.TFLog("Core fncs:", this, "TestModule");
1159
1160 funcCount = timePerFunc.Count();
1161 for (int l = 0; l < funcCount; ++l)
1162 {
1164 Debug.TFLog(string.Format(" time: %1 | fnc: %2", tfpc.param1, tfpc.param2), this, "TestModule");
1165 // We are currently profiling Core, so this Core function should be present
1166 if (tfpc.param2 == "EnumTools::StringToEnum")
1167 {
1168 found = true;
1169 break;
1170 }
1171 }
1172
1173 Assert(found);
1174
1175 // Test some bogus
1177 EnProfiler.SetModule(-333);
1178 bool success = Assert(EnProfiler.GetModule() == mod);
1179 EnProfiler.SetModule(6003);
1181
1184
1185 if (!wasEnabled)
1186 EnProfiler.Enable(false, true);
1187
1188 return BTFR(success && found);
1189 }
1190
1191 //---------------------------------------------------------------------------
1192 // Test to see if class time data is correct
1194 {
1195 // We should restore this when done
1198
1199 // GetTickTime() returns in seconds, so gather the results in seconds too
1201
1202 // Create the classes
1204
1205 // Time to stress
1206 float previousTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1207 float timeStressed = clss.DoEverything();
1208 float postTime = EnProfiler.GetTimeOfClass(clss.Type(), true);
1209 float postTimeStatic = EnProfiler.GetTimeOfClass(StaticGetType(EPTHelperClass), true);
1212
1213 Debug.TFLog(string.Format("Profiling result: stressed: %1 | profiled: %2 | diff: %3", timeStressed, timeProfiled, diff), this, "TestClassTimeData");
1214
1215 // Restore
1217 if (!wasEnabled)
1218 EnProfiler.Enable(false, true);
1219
1220 // We called the function, so it must have some time
1221 if (!Assert(postTime > 0))
1222 {
1223 return NTFR(TFR.FAIL);
1224 }
1225
1227 {
1228 return NTFR(TFR.FAIL);
1229 }
1230
1231 if (!Assert(diff < 0.001))
1232 {
1233 return NTFR(TFR.FAIL);
1234 }
1235
1236 return NTFR(TFR.SUCCESS);
1237 }
1238
1239 //---------------------------------------------------------------------------
1240 // Test to see if class count data is correct
1242 {
1243 const int allocAmount = 9;
1244 const int releaseAmount = 6;
1246
1247 // We should restore this when done
1249
1250 // Time to test
1253
1255 for (int i = 0; i < allocAmount; ++i)
1256 {
1257 instanceArr.Insert(new EPTHelperClass());
1258 }
1259
1260 for (int j = 0; j < releaseAmount; ++j)
1261 {
1262 delete instanceArr[j];
1263 }
1264
1265 int postAlloc = EnProfiler.GetAllocationsOfClass(StaticGetType(EPTHelperClass), true);
1266 int postInstances = EnProfiler.GetInstancesOfClass(StaticGetType(EPTHelperClass), true);
1267
1270
1271 Debug.TFLog(string.Format("Profiling result: alloc: %1 | instances: %2", alloced, instances), this, "TestClassCountData");
1272
1273 // Restore
1274 if (!wasEnabled)
1275 EnProfiler.Enable(false, true);
1276
1277 // Time to check
1278 if (!Assert(alloced == allocAmount))
1279 {
1280 return NTFR(TFR.FAIL);
1281 }
1282
1284 {
1285 return NTFR(TFR.FAIL);
1286 }
1287
1288 return NTFR(TFR.SUCCESS);
1289 }
1290
1291 //---------------------------------------------------------------------------
1292 // Test to see if func time data is correct
1294 {
1295 // We should restore this when done
1298
1299 // GetTickTime() returns in seconds, so gather the results in seconds too
1301
1302 // Time to stress
1303 float previousTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1304 float timeStressed = StringFormat();
1305 float postTime = EnProfiler.GetTimeOfFunc("StringFormat", Type(), true);
1308
1309 float previousTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1310 float timeStressed2 = StringConcat();
1311 float postTime2 = EnProfiler.GetTimeOfFunc("StringConcat", Type(), true);
1314
1315 Debug.TFLog(string.Format("Profiling result: StringFormat: %1 | StringConcat: %2", timeProfiled, timeProfiled2), this, "TestFuncTimeData");
1316
1317 // Restore
1319 if (!wasEnabled)
1320 {
1321 EnProfiler.Enable(false, true);
1322 }
1323
1324 // We called the function, so it must have some time
1325 if (!Assert(postTime > 0))
1326 {
1327 return NTFR(TFR.FAIL);
1328 }
1329
1330 if (!Assert(diff < 0.001))
1331 {
1332 return NTFR(TFR.FAIL);
1333 }
1334
1335 if (!Assert(postTime2 > 0))
1336 {
1337 return NTFR(TFR.FAIL);
1338 }
1339
1340 if (!Assert(diff2 < 0.001))
1341 {
1342 return NTFR(TFR.FAIL);
1343 }
1344
1345 // I know that string.Format is faster than additive concatenation
1347 {
1348 return NTFR(TFR.FAIL);
1349 }
1350
1351 return NTFR(TFR.SUCCESS);
1352 }
1353
1354 //---------------------------------------------------------------------------
1355 // Test to see if func count data is correct
1357 {
1358 // We should restore this when done
1360
1361 // Time to count
1362
1363 // - CallFunction
1364 int previousCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1365 GetGame().GameScript.CallFunction(this, "TestFuncCountDataHelper", null, 0);
1366 int postCountCF = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1367
1369
1370 // - CallFunctionParams
1371 int previousCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1372 GetGame().GameScript.CallFunctionParams(this, "TestFuncCountDataHelper", null, null);
1373 int postCountCFP = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1374
1376
1377 // - Regular call
1378 int previousCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1380 int postCountRG = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1381
1383
1384 // - Call
1385 int previousCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1386 GetGame().GameScript.Call(this, "TestFuncCountDataHelper", 0);
1387 int postCountC = EnProfiler.GetCountOfFunc("TestFuncCountDataHelper", Type(), true);
1388
1390
1391 // - Garbage
1392 int callCountNon = EnProfiler.GetCountOfFunc("Non Existing Method", Type(), true);
1393
1394 // - Static
1395 int previousCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1397 int postCountS = EnProfiler.GetCountOfFunc("TestFuncCountDataHelperStatic", Type(), true);
1398
1400
1401 // - Global
1402 int previousCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1403 GetDayZGame();
1404 int postCountG = EnProfiler.GetCountOfFuncG("GetDayZGame", true);
1405
1407
1408 // - Global proto
1409 // Not tracked, so don't need to compare before and after, should always be 0
1410 ErrorEx("Testing global proto call", ErrorExSeverity.INFO);
1411 int callCountGP = EnProfiler.GetCountOfFuncG("ErrorEx", true);
1412
1413 // - Static proto
1414 // Not tracked, so don't need to compare before and after, should always be 0
1415 int callCountSP = EnProfiler.GetCountOfFunc("GetCountOfFunc", StaticGetType(EnProfiler), true);
1416
1417 // - proto
1418 // Not tracked, so don't need to compare before and after, should always be 0
1419 GetGame().GetHostName();
1420 int callCountP = EnProfiler.GetCountOfFunc("GetHostName", GetGame().Type(), true);
1421
1422 // - proto native
1423 // Not tracked, so don't need to compare before and after, should always be 0
1424 GetGame().IsServer();
1425 int callCountPN = EnProfiler.GetCountOfFunc("IsServer", GetGame().Type(), true);
1426
1427 // - static proto native
1428 // Not tracked, so don't need to compare before and after, should always be 0
1430 int callCountSPN = EnProfiler.GetCountOfFunc("GetInstance", StaticGetType(ErrorModuleHandler), true);
1431
1432 // Restore
1433 if (!wasEnabled)
1434 {
1435 EnProfiler.Enable(false, true);
1436 }
1437
1438 // Do the checks
1439
1440 // - CallFunction
1441 if (!Assert(callCountCF == 1))
1442 {
1443 return NTFR(TFR.FAIL);
1444 }
1445
1446 // - CallFunctionParams
1447 if (!Assert(callCountCFP == 1))
1448 {
1449 return NTFR(TFR.FAIL);
1450 }
1451
1452 // - Regular call
1453 if (!Assert(callCountRG == 1))
1454 {
1455 return NTFR(TFR.FAIL);
1456 }
1457
1458 // - Call
1459 if (!Assert(callCountC == 1))
1460 {
1461 return NTFR(TFR.FAIL);
1462 }
1463
1464 // - Garbage
1465 if (!Assert(callCountNon == -1))
1466 {
1467 return NTFR(TFR.FAIL);
1468 }
1469
1470 // - Static
1471 if (!Assert(callCountS == 1))
1472 {
1473 return NTFR(TFR.FAIL);
1474 }
1475
1476 // - Global
1477 if (!Assert(callCountG == 1))
1478 {
1479 return NTFR(TFR.FAIL);
1480 }
1481
1482 // - Global proto
1483 if (!Assert(callCountGP == 0))
1484 {
1485 return NTFR(TFR.FAIL);
1486 }
1487
1488 // - Static proto
1489 if (!Assert(callCountSP == 0))
1490 {
1491 return NTFR(TFR.FAIL);
1492 }
1493
1494 // - proto
1495 if (!Assert(callCountP == 0))
1496 {
1497 return NTFR(TFR.FAIL);
1498 }
1499
1500 // - proto native
1501 if (!Assert(callCountPN == 0))
1502 {
1503 return NTFR(TFR.FAIL);
1504 }
1505
1506 // - static proto native
1507 if (!Assert(callCountSPN == 0))
1508 {
1509 return NTFR(TFR.FAIL);
1510 }
1511
1512 return NTFR(TFR.SUCCESS);
1513 }
1514
1515 //---------------------------------------------------------------------------
1516 // Helpers
1517 //---------------------------------------------------------------------------
1518 // Snore
1519 float Sleep(float timeS)
1520 {
1521 float startTime = GetGame().GetTickTime();
1522 while (GetGame().GetTickTime() - startTime < timeS)
1523 {
1524 // Zzz
1525 }
1526
1527 return GetGame().GetTickTime() - startTime;
1528 }
1529
1530 //---------------------------------------------------------------------------
1531 // Example stress method
1532 float StringFormat()
1533 {
1534 float startTime = GetGame().GetTickTime();
1535
1536 for (int i = 0; i < 1000; ++i)
1537 {
1538 string example = string.Format("This %1 is %2 just %3 an %4 example %5", i, Type(), this, startTime, "lorem ipsum 1 2 3");
1539 }
1540
1541 return GetGame().GetTickTime() - startTime;
1542 }
1543
1544 //---------------------------------------------------------------------------
1545 // Example stress method 2
1546 float StringConcat()
1547 {
1548 float startTime = GetGame().GetTickTime();
1549
1550 for (int i = 0; i < 1000; ++i)
1551 {
1552 string example = "This " + i + " is " + Type() + " just " + this + " an " + startTime + " example " + "lorem ipsum 1 2 3";
1553 }
1554
1555 return GetGame().GetTickTime() - startTime;
1556 }
1557
1558 //---------------------------------------------------------------------------
1559 // To make sure it is only ever called in that test
1561 {
1562 int dummy = 3;
1563 }
1564
1565 //---------------------------------------------------------------------------
1566 // To make sure it is only ever called in that test
1567 static void TestFuncCountDataHelperStatic()
1568 {
1569 int dummy = 3;
1570 }
1571}
1572
1573class EPTHelperClass
1574{
1575 float Sleep2(float timeS)
1576 {
1577 float startTime = GetGame().GetTickTime();
1578 while (GetGame().GetTickTime() - startTime < timeS)
1579 {
1580 // Zzz
1581 }
1582
1583 return GetGame().GetTickTime() - startTime;
1584 }
1585 float SleepAgain(float timeS)
1586 {
1587 float startTime = GetGame().GetTickTime();
1588 while (GetGame().GetTickTime() - startTime < timeS)
1589 {
1590 // Zzz
1591 }
1592
1593 return GetGame().GetTickTime() - startTime;
1594 }
1595
1596 float DoEverything()
1597 {
1598 float startTime = GetGame().GetTickTime();
1599
1600 Sleep2(3);
1601 SleepAgain(3);
1602
1603 return GetGame().GetTickTime() - startTime;
1604 }
1605}

◆ ~EnProfilerTests()

Переменные

◆ m_bWasProfilerEnabled

bool m_bWasProfilerEnabled

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