PerformanceTest.cpp 7.2 KB

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