PerformanceTest.cpp 7.7 KB

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