ExecutionQueueTests.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*******************************************************************************
  2. The content of this file includes portions of the proprietary AUDIOKINETIC Wwise
  3. Technology released in source code form as part of the game integration package.
  4. The content of this file may not be used without valid licenses to the
  5. AUDIOKINETIC Wwise Technology.
  6. Note that the use of the game engine is subject to the Unreal(R) Engine End User
  7. License Agreement at https://www.unrealengine.com/en-US/eula/unreal
  8. License Usage
  9. Licensees holding valid licenses to the AUDIOKINETIC Wwise Technology may use
  10. this file in accordance with the end user license agreement provided with the
  11. software or, alternatively, in accordance with the terms contained
  12. in a written agreement between you and Audiokinetic Inc.
  13. Copyright (c) 2023 Audiokinetic Inc.
  14. *******************************************************************************/
  15. #include "Wwise/WwiseUnitTests.h"
  16. #if WWISE_UNIT_TESTS
  17. #include "Wwise/WwiseExecutionQueue.h"
  18. #include <atomic>
  19. TEST_CASE("Audio::Wwise::Concurrency::ExecutionQueue_Smoke", "[ApplicationContextMask][SmokeFilter]")
  20. {
  21. SECTION("Static")
  22. {
  23. static_assert(std::is_constructible<FWwiseExecutionQueue>::value);
  24. static_assert(std::is_constructible<FWwiseExecutionQueue, ENamedThreads::Type>::value);
  25. static_assert(std::is_constructible<FWwiseExecutionQueue, FQueuedThreadPool*>::value);
  26. static_assert(std::is_constructible<FWwiseExecutionQueue, const TCHAR*>::value);
  27. static_assert(std::is_constructible<FWwiseExecutionQueue, const TCHAR*, EThreadPriority>::value);
  28. static_assert(std::is_constructible<FWwiseExecutionQueue, const TCHAR*, EThreadPriority, int32>::value);
  29. static_assert(!std::is_copy_constructible<FWwiseExecutionQueue>::value);
  30. static_assert(!std::is_move_constructible<FWwiseExecutionQueue>::value);
  31. static_assert(!std::is_copy_assignable<FWwiseExecutionQueue>::value);
  32. static_assert(!std::is_move_assignable<FWwiseExecutionQueue>::value);
  33. }
  34. SECTION("Instantiation")
  35. {
  36. FWwiseExecutionQueue NoParam;
  37. FWwiseExecutionQueue NamedThread(ENamedThreads::AnyThread);
  38. FWwiseExecutionQueue Pool(GThreadPool);
  39. FWwiseExecutionQueue NewPool(TEXT("New Execution Queue"));
  40. CHECK(NoParam.ThreadPool);
  41. CHECK(NoParam.NamedThread == ENamedThreads::UnusedAnchor);
  42. CHECK(NoParam.ThreadPool == FWwiseExecutionQueue::GetDefaultThreadPool());
  43. CHECK_FALSE(NamedThread.ThreadPool);
  44. CHECK_FALSE(NamedThread.NamedThread == ENamedThreads::UnusedAnchor);
  45. CHECK(Pool.NamedThread == ENamedThreads::UnusedAnchor);
  46. CHECK_FALSE(Pool.bOwnedPool);
  47. CHECK(Pool.ThreadPool);
  48. CHECK(NewPool.NamedThread == ENamedThreads::UnusedAnchor);
  49. CHECK(NewPool.bOwnedPool);
  50. CHECK(NewPool.ThreadPool);
  51. CHECK(NewPool.ThreadPool && NewPool.ThreadPool->GetNumThreads() == 1);
  52. }
  53. SECTION("Async At Destructor")
  54. {
  55. constexpr const int LoopCount = 10;
  56. std::atomic<int> Value{ 0 };
  57. {
  58. FWwiseExecutionQueue ExecutionQueue;
  59. for (int i = 0; i < LoopCount; ++i)
  60. {
  61. ExecutionQueue.Async([&Value]
  62. {
  63. ++Value;
  64. });
  65. }
  66. }
  67. CHECK(Value.load() == LoopCount);
  68. }
  69. SECTION("AsyncWait")
  70. {
  71. constexpr const int LoopCount = 10;
  72. std::atomic<int> Value{ 0 };
  73. {
  74. const auto CurrentThreadId = FPlatformTLS::GetCurrentThreadId();
  75. FWwiseExecutionQueue ExecutionQueue;
  76. for (int i = 0; i < LoopCount; ++i)
  77. {
  78. ExecutionQueue.AsyncWait([&Value, CurrentThreadId]
  79. {
  80. CHECK_FALSE(CurrentThreadId == FPlatformTLS::GetCurrentThreadId());
  81. ++Value;
  82. });
  83. }
  84. CHECK(Value.load() == LoopCount);
  85. }
  86. }
  87. SECTION("Async in order")
  88. {
  89. constexpr const int LoopCount = 10;
  90. std::atomic<int> Value{ 0 };
  91. {
  92. FWwiseExecutionQueue ExecutionQueue;
  93. for (int i = 0; i < LoopCount; ++i)
  94. {
  95. ExecutionQueue.Async([&Value, ShouldBe = i]
  96. {
  97. CHECK(Value++ == ShouldBe);
  98. });
  99. }
  100. }
  101. CHECK(Value.load() == LoopCount);
  102. }
  103. SECTION("Async at exit")
  104. {
  105. constexpr const int LoopCount = 10;
  106. std::atomic<int> Value{ 0 };
  107. {
  108. FWwiseExecutionQueue::Test::bMockEngineDeletion = true;
  109. FWwiseExecutionQueue ExecutionQueue;
  110. for (int i = 0; i < LoopCount; ++i)
  111. {
  112. ExecutionQueue.Async([&Value, ShouldBe = i]
  113. {
  114. CHECK(Value++ == ShouldBe);
  115. });
  116. }
  117. }
  118. FWwiseExecutionQueue::Test::bMockEngineDeletion = false;
  119. CHECK(Value.load() == LoopCount);
  120. }
  121. SECTION("Async after exit")
  122. {
  123. constexpr const int LoopCount = 10;
  124. std::atomic<int> Value{ 0 };
  125. {
  126. const auto CurrentThreadId = FPlatformTLS::GetCurrentThreadId();
  127. FWwiseExecutionQueue::Test::bMockEngineDeleted = true;
  128. FWwiseExecutionQueue ExecutionQueue;
  129. for (int i = 0; i < LoopCount; ++i)
  130. {
  131. ExecutionQueue.Async([&Value, ShouldBe = i, CurrentThreadId]
  132. {
  133. CHECK(CurrentThreadId == FPlatformTLS::GetCurrentThreadId());
  134. CHECK(Value++ == ShouldBe);
  135. });
  136. }
  137. }
  138. FWwiseExecutionQueue::Test::bMockEngineDeleted = false;
  139. CHECK(Value.load() == LoopCount);
  140. }
  141. }
  142. TEST_CASE("Audio::Wwise::Concurrency::ExecutionQueue_Perf", "[ApplicationContextMask][PerfFilter]")
  143. {
  144. SECTION("AsyncAddingOpPerf")
  145. {
  146. constexpr const int LoopCount = 1000000;
  147. constexpr const int ExpectedUS = 600000;
  148. std::atomic<int> Value{ 0 };
  149. {
  150. FWwiseExecutionQueue ExecutionQueue;
  151. FDateTime StartTime = FDateTime::UtcNow();
  152. for (int i = 0; i < LoopCount; ++i)
  153. {
  154. ExecutionQueue.Async([&Value]
  155. {
  156. ++Value;
  157. });
  158. }
  159. FTimespan Duration = FDateTime::UtcNow() - StartTime;
  160. WWISETEST_LOG("AsyncAddingOpPerf %dus < %dus", (int)Duration.GetTotalMicroseconds(), ExpectedUS);
  161. CHECK(Duration.GetTotalMicroseconds() < ExpectedUS);
  162. }
  163. CHECK(Value.load() == LoopCount);
  164. }
  165. SECTION("AsyncExecutionPerf")
  166. {
  167. constexpr const int LoopCount = 1000000;
  168. constexpr const int ExpectedUS = 200000;
  169. std::atomic<int> Value{ 0 };
  170. FDateTime StartTime;
  171. {
  172. FWwiseExecutionQueue ExecutionQueue;
  173. ExecutionQueue.AsyncWait([&ExecutionQueue, LoopCount, &Value]
  174. {
  175. for (int i = 0; i < LoopCount; ++i)
  176. {
  177. ExecutionQueue.Async([&Value]
  178. {
  179. ++Value;
  180. });
  181. }
  182. });
  183. StartTime = FDateTime::UtcNow();
  184. }
  185. FTimespan Duration = FDateTime::UtcNow() - StartTime;
  186. WWISETEST_LOG("AsyncExecutionPerf %dus < %dus", (int)Duration.GetTotalMicroseconds(), ExpectedUS);
  187. CHECK(Value.load() == LoopCount);
  188. CHECK(Duration.GetTotalMicroseconds() < ExpectedUS);
  189. }
  190. }
  191. TEST_CASE("Audio::Wwise::Concurrency::ExecutionQueue_Stress", "[ApplicationContextMask][StressFilter]")
  192. {
  193. SECTION("AsyncStress")
  194. {
  195. constexpr const int LoopCount = 2000000;
  196. constexpr const int MainLoopCount = 100;
  197. constexpr const int SubLoopCount = 100;
  198. constexpr const int FinalLoopCount = LoopCount / MainLoopCount / SubLoopCount;
  199. constexpr const int ExpectedUS = 1500000;
  200. FDateTime StartTime = FDateTime::UtcNow();
  201. {
  202. FWwiseExecutionQueue MainExecutionQueue;
  203. FWwiseExecutionQueue SubExecutionQueue;
  204. FWwiseExecutionQueue DeletionExecutionQueue;
  205. for (int i = 0; i < MainLoopCount; ++i)
  206. {
  207. MainExecutionQueue.Async([&SubExecutionQueue, &DeletionExecutionQueue, SubLoopCount, FinalLoopCount]
  208. {
  209. for (int i = 0; i < SubLoopCount; ++i)
  210. {
  211. SubExecutionQueue.Async([&DeletionExecutionQueue, FinalLoopCount]
  212. {
  213. auto ExecutionQueue = new FWwiseExecutionQueue;
  214. for (int i = 0; i < FinalLoopCount; ++i)
  215. {
  216. ExecutionQueue->Async([]
  217. {
  218. });
  219. }
  220. DeletionExecutionQueue.Async([ExecutionQueue]
  221. {
  222. delete ExecutionQueue;
  223. });
  224. });
  225. }
  226. });
  227. }
  228. }
  229. FTimespan Duration = FDateTime::UtcNow() - StartTime;
  230. WWISETEST_LOG("AsyncAddingOpPerf %dus < %dus", (int)Duration.GetTotalMicroseconds(), ExpectedUS);
  231. CHECK(Duration.GetTotalMicroseconds() < ExpectedUS);
  232. }
  233. SECTION("AsyncStress at Exit")
  234. {
  235. constexpr const int LoopCount = 100000;
  236. constexpr const int MainLoopCount = 100;
  237. constexpr const int SubLoopCount = 100;
  238. constexpr const int FinalLoopCount = LoopCount / MainLoopCount / SubLoopCount;
  239. constexpr const int ExpectedUS = 3000000;
  240. FDateTime StartTime = FDateTime::UtcNow();
  241. {
  242. FWwiseExecutionQueue MainExecutionQueue;
  243. FWwiseExecutionQueue SubExecutionQueue;
  244. FWwiseExecutionQueue DeletionExecutionQueue;
  245. for (int i = 0; i < MainLoopCount; ++i)
  246. {
  247. MainExecutionQueue.Async([&SubExecutionQueue, &DeletionExecutionQueue, SubLoopCount, FinalLoopCount]
  248. {
  249. for (int i = 0; i < SubLoopCount; ++i)
  250. {
  251. SubExecutionQueue.Async([&DeletionExecutionQueue, FinalLoopCount]
  252. {
  253. auto ExecutionQueue = new FWwiseExecutionQueue;
  254. for (int i = 0; i < FinalLoopCount; ++i)
  255. {
  256. ExecutionQueue->Async([]
  257. {
  258. });
  259. }
  260. DeletionExecutionQueue.Async([ExecutionQueue]
  261. {
  262. delete ExecutionQueue;
  263. });
  264. });
  265. }
  266. });
  267. if (i == MainLoopCount / 3)
  268. {
  269. MainExecutionQueue.Async([]
  270. {
  271. FWwiseExecutionQueue::Test::bMockEngineDeletion = true;
  272. });
  273. }
  274. }
  275. MainExecutionQueue.Async([]
  276. {
  277. FWwiseExecutionQueue::Test::bMockEngineDeleted = true;
  278. });
  279. }
  280. FWwiseExecutionQueue::Test::bMockEngineDeletion = false;
  281. FWwiseExecutionQueue::Test::bMockEngineDeleted = false;
  282. FTimespan Duration = FDateTime::UtcNow() - StartTime;
  283. WWISETEST_LOG("AsyncAddingOpPerf %dus < %dus", (int)Duration.GetTotalMicroseconds(), ExpectedUS);
  284. CHECK(Duration.GetTotalMicroseconds() < ExpectedUS);
  285. }
  286. }
  287. #endif // WWISE_UNIT_TESTS