testThreadStaticPerformance.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "unit/test.h"
  24. #include "core/threadStatic.h"
  25. #include "math/mRandom.h"
  26. #include "platform/profiler.h"
  27. using namespace UnitTesting;
  28. //-----------------------------------------------------------------------------
  29. // This unit test will blow up without thread static support
  30. #if defined(TORQUE_ENABLE_THREAD_STATICS) && defined(TORQUE_ENABLE_PROFILER)
  31. // Declare a test thread static
  32. DITTS( U32, gInstancedStaticFoo, 42 );
  33. static U32 gTrueStaticFoo = 42;
  34. CreateUnitTest( TestThreadStaticPerformance, "Core/ThreadStaticPerformance" )
  35. {
  36. void run()
  37. {
  38. // Bail if the profiler is turned on right now
  39. if( !test( !gProfiler->isEnabled(), "Profiler is currently enabled, test cannot continue" ) )
  40. return;
  41. // Spawn an instance
  42. TorqueThreadStaticListHandle testInstance = _TorqueThreadStaticReg::spawnThreadStaticsInstance();
  43. static const dsize_t TEST_SIZE = 100000;
  44. // What we are going to do in this test is to test some U32 static
  45. // performance. The test will be run TEST_SIZE times, and so first create
  46. // an array of values to standardize the tests on.
  47. U32 testValue[TEST_SIZE];
  48. for( S32 i = 0; i < TEST_SIZE; i++ )
  49. testValue[i] = gRandGen.randI();
  50. // Reset the profiler, tell it to dump to console when done
  51. gProfiler->dumpToConsole();
  52. gProfiler->enable( true );
  53. // Value array is initialized, so lets put the foo's through the paces
  54. PROFILE_START(ThreadStaticPerf_TrueStaticAssign);
  55. for( int i = 0; i < TEST_SIZE; i++ )
  56. gTrueStaticFoo = testValue[i];
  57. PROFILE_END();
  58. PROFILE_START(ThreadStaticPerf_InstanceStaticAssign);
  59. for( S32 i = 0; i < TEST_SIZE; i++ )
  60. ATTS_( gInstancedStaticFoo, 1 ) = testValue[i];
  61. PROFILE_END();
  62. gProfiler->enable( false );
  63. // Clean up instance
  64. _TorqueThreadStaticReg::destroyInstance( testInstance );
  65. }
  66. };
  67. #endif // TORQUE_ENABLE_THREAD_STATICS