PerformanceTest.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. // Jolt includes
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/RegisterTypes.h>
  6. #include <Jolt/Core/Factory.h>
  7. #include <Jolt/Core/TempAllocator.h>
  8. #include <Jolt/Core/JobSystemThreadPool.h>
  9. #include <Jolt/Physics/PhysicsSettings.h>
  10. #include <Jolt/Physics/PhysicsSystem.h>
  11. #include <Jolt/Physics/Collision/NarrowPhaseStats.h>
  12. #ifdef JPH_DEBUG_RENDERER
  13. #include <Jolt/Renderer/DebugRendererRecorder.h>
  14. #include <Jolt/Core/StreamWrapper.h>
  15. #endif // JPH_DEBUG_RENDERER
  16. // STL includes
  17. #include <iostream>
  18. #include <thread>
  19. #include <chrono>
  20. #include <memory>
  21. #include <cstdarg>
  22. using namespace JPH;
  23. using namespace std;
  24. // Disable common warnings triggered by Jolt
  25. JPH_SUPPRESS_WARNINGS
  26. // Local includes
  27. #include "RagdollScene.h"
  28. #include "ConvexVsMeshScene.h"
  29. // Time step for physics
  30. constexpr float cDeltaTime = 1.0f / 60.0f;
  31. static void TraceImpl(const char *inFMT, ...)
  32. {
  33. // Format the message
  34. va_list list;
  35. va_start(list, inFMT);
  36. char buffer[1024];
  37. vsnprintf(buffer, sizeof(buffer), inFMT, list);
  38. // Print to the TTY
  39. cout << buffer << endl;
  40. }
  41. // Program entry point
  42. int main(int argc, char** argv)
  43. {
  44. // Register allocation hook
  45. RegisterDefaultAllocator();
  46. // Parse command line parameters
  47. int specified_quality = -1;
  48. int specified_threads = -1;
  49. uint max_iterations = 500;
  50. bool disable_sleep = false;
  51. bool enable_profiler = false;
  52. #ifdef JPH_DEBUG_RENDERER
  53. bool enable_debug_renderer = false;
  54. #endif // JPH_DEBUG_RENDERER
  55. bool enable_per_frame_recording = false;
  56. unique_ptr<PerformanceTestScene> scene;
  57. for (int argidx = 1; argidx < argc; ++argidx)
  58. {
  59. const char *arg = argv[argidx];
  60. if (strncmp(arg, "-s=", 3) == 0)
  61. {
  62. // Parse scene
  63. if (strcmp(arg + 3, "Ragdoll") == 0)
  64. scene = unique_ptr<PerformanceTestScene>(new RagdollScene);
  65. else if (strcmp(arg + 3, "ConvexVsMesh") == 0)
  66. scene = unique_ptr<PerformanceTestScene>(new ConvexVsMeshScene);
  67. else
  68. {
  69. cerr << "Invalid scene" << endl;
  70. return 1;
  71. }
  72. }
  73. else if (strncmp(arg, "-i=", 3) == 0)
  74. {
  75. // Parse max iterations
  76. max_iterations = (uint)atoi(arg + 3);
  77. }
  78. else if (strncmp(arg, "-q=", 3) == 0)
  79. {
  80. // Parse quality
  81. if (strcmp(arg + 3, "Discrete") == 0)
  82. specified_quality = 0;
  83. else if (strcmp(arg + 3, "LinearCast") == 0)
  84. specified_quality = 1;
  85. else
  86. {
  87. cerr << "Invalid quality" << endl;
  88. return 1;
  89. }
  90. }
  91. else if (strncmp(arg, "-t=", 3) == 0)
  92. {
  93. // Parse threads
  94. specified_threads = atoi(arg + 3);
  95. }
  96. else if (strcmp(arg, "-no_sleep") == 0)
  97. {
  98. disable_sleep = true;
  99. }
  100. else if (strcmp(arg, "-p") == 0)
  101. {
  102. enable_profiler = true;
  103. }
  104. #ifdef JPH_DEBUG_RENDERER
  105. else if (strcmp(arg, "-r") == 0)
  106. {
  107. enable_debug_renderer = true;
  108. }
  109. #endif // JPH_DEBUG_RENDERER
  110. else if (strcmp(arg, "-f") == 0)
  111. {
  112. enable_per_frame_recording = true;
  113. }
  114. else if (strcmp(arg, "-h") == 0)
  115. {
  116. // Print usage
  117. cerr << "Usage:" << endl
  118. << "-s=<scene>: Select scene (Ragdoll, ConvexVsMesh)" << endl
  119. << "-i=<num physics steps>: Number of physics steps to simulate (default 500)" << endl
  120. << "-q=<quality>: Test only with specified quality (Discrete, LinearCast)" << endl
  121. << "-t=<num threads>: Test only with N threads (default is to iterate over 1 .. num hardware threads)" << endl
  122. << "-p: Write out profiles" << endl
  123. << "-r: Record debug renderer output for JoltViewer" << endl
  124. << "-f: Record per frame timings" << endl
  125. << "-no_sleep: Disable sleeping" << endl;
  126. return 0;
  127. }
  128. }
  129. // Install callbacks
  130. Trace = TraceImpl;
  131. // Create a factory
  132. Factory::sInstance = new Factory();
  133. // Register all Jolt physics types
  134. RegisterTypes();
  135. // Create temp allocator
  136. TempAllocatorImpl temp_allocator(10 * 1024 * 1024);
  137. // Load the scene
  138. if (scene == nullptr)
  139. scene = unique_ptr<PerformanceTestScene>(new RagdollScene);
  140. if (!scene->Load())
  141. return 1;
  142. // Output scene we're running
  143. cout << "Running scene: " << scene->GetName() << endl;
  144. // Create mapping table from object layer to broadphase layer
  145. BPLayerInterfaceImpl broad_phase_layer_interface;
  146. // Start profiling this program
  147. JPH_PROFILE_START("Main");
  148. // Trace header
  149. cout << "Motion Quality, Thread Count, Steps / Second, Hash" << endl;
  150. // Iterate motion qualities
  151. for (uint mq = 0; mq < 2; ++mq)
  152. {
  153. // Skip quality if another was specified
  154. if (specified_quality != -1 && mq != (uint)specified_quality)
  155. continue;
  156. // Determine motion quality
  157. EMotionQuality motion_quality = mq == 0? EMotionQuality::Discrete : EMotionQuality::LinearCast;
  158. String motion_quality_str = mq == 0? "Discrete" : "LinearCast";
  159. // Determine which thread counts to test
  160. Array<uint> thread_permutations;
  161. if (specified_threads > 0)
  162. thread_permutations.push_back((uint)specified_threads - 1);
  163. else
  164. for (uint num_threads = 0; num_threads < thread::hardware_concurrency(); ++num_threads)
  165. thread_permutations.push_back(num_threads);
  166. // Test thread permutations
  167. for (uint num_threads : thread_permutations)
  168. {
  169. // Create job system with desired number of threads
  170. JobSystemThreadPool job_system(cMaxPhysicsJobs, cMaxPhysicsBarriers, num_threads);
  171. // Create physics system
  172. PhysicsSystem physics_system;
  173. physics_system.Init(10240, 0, 65536, 10240, broad_phase_layer_interface, BroadPhaseCanCollide, ObjectCanCollide);
  174. // Start test scene
  175. scene->StartTest(physics_system, motion_quality);
  176. // Disable sleeping if requested
  177. if (disable_sleep)
  178. {
  179. const BodyLockInterface &bli = physics_system.GetBodyLockInterfaceNoLock();
  180. BodyIDVector body_ids;
  181. physics_system.GetBodies(body_ids);
  182. for (BodyID id : body_ids)
  183. {
  184. BodyLockWrite lock(bli, id);
  185. if (lock.Succeeded())
  186. {
  187. Body &body = lock.GetBody();
  188. if (!body.IsStatic())
  189. body.SetAllowSleeping(false);
  190. }
  191. }
  192. }
  193. // Optimize the broadphase to prevent an expensive first frame
  194. physics_system.OptimizeBroadPhase();
  195. // A tag used to identify the test
  196. String tag = ToLower(motion_quality_str) + "_th" + ConvertToString(num_threads + 1);
  197. #ifdef JPH_DEBUG_RENDERER
  198. // Open renderer output
  199. ofstream renderer_file;
  200. if (enable_debug_renderer)
  201. renderer_file.open(("performance_test_" + tag + ".jor").c_str(), ofstream::out | ofstream::binary | ofstream::trunc);
  202. StreamOutWrapper renderer_stream(renderer_file);
  203. DebugRendererRecorder renderer(renderer_stream);
  204. #endif // JPH_DEBUG_RENDERER
  205. // Open per frame timing output
  206. ofstream per_frame_file;
  207. if (enable_per_frame_recording)
  208. {
  209. per_frame_file.open(("per_frame_" + tag + ".csv").c_str(), ofstream::out | ofstream::trunc);
  210. per_frame_file << "Frame, Time (ms)" << endl;
  211. }
  212. chrono::nanoseconds total_duration(0);
  213. // Step the world for a fixed amount of iterations
  214. for (uint iterations = 0; iterations < max_iterations; ++iterations)
  215. {
  216. JPH_PROFILE_NEXTFRAME();
  217. // Start measuring
  218. chrono::high_resolution_clock::time_point clock_start = chrono::high_resolution_clock::now();
  219. // Do a physics step
  220. physics_system.Update(cDeltaTime, 1, 1, &temp_allocator, &job_system);
  221. // Stop measuring
  222. chrono::high_resolution_clock::time_point clock_end = chrono::high_resolution_clock::now();
  223. chrono::nanoseconds duration = chrono::duration_cast<chrono::nanoseconds>(clock_end - clock_start);
  224. total_duration += duration;
  225. #ifdef JPH_DEBUG_RENDERER
  226. if (enable_debug_renderer)
  227. {
  228. // Draw the state of the world
  229. BodyManager::DrawSettings settings;
  230. physics_system.DrawBodies(settings, &renderer);
  231. // Mark end of frame
  232. renderer.EndFrame();
  233. }
  234. #endif // JPH_DEBUG_RENDERER
  235. // Record time taken this iteration
  236. if (enable_per_frame_recording)
  237. per_frame_file << iterations << ", " << (1.0e-6 * duration.count()) << endl;
  238. // Dump profile information every 100 iterations
  239. if (enable_profiler && iterations % 100 == 0)
  240. {
  241. JPH_PROFILE_DUMP(tag + "_it" + ConvertToString(iterations));
  242. }
  243. }
  244. // Calculate hash of all positions and rotations of the bodies
  245. size_t hash = 0;
  246. BodyInterface &bi = physics_system.GetBodyInterfaceNoLock();
  247. BodyIDVector body_ids;
  248. physics_system.GetBodies(body_ids);
  249. for (BodyID id : body_ids)
  250. {
  251. Vec3 pos = bi.GetPosition(id);
  252. Quat rot = bi.GetRotation(id);
  253. hash_combine(hash, pos.GetX(), pos.GetY(), pos.GetZ(), rot.GetX(), rot.GetY(), rot.GetZ(), rot.GetW());
  254. }
  255. // Stop test scene
  256. scene->StopTest(physics_system);
  257. // Trace stat line
  258. cout << motion_quality_str << ", " << num_threads + 1 << ", " << double(max_iterations) / (1.0e-9 * total_duration.count()) << ", " << hash << endl;
  259. }
  260. }
  261. #ifdef JPH_TRACK_NARROWPHASE_STATS
  262. NarrowPhaseStat::sReportStats();
  263. #endif // JPH_TRACK_NARROWPHASE_STATS
  264. // Destroy the factory
  265. delete Factory::sInstance;
  266. Factory::sInstance = nullptr;
  267. // End profiling this program
  268. JPH_PROFILE_END();
  269. return 0;
  270. }