PerformanceTest.cpp 8.5 KB

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