threadStaticTest.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 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. #ifdef TORQUE_TESTS_ENABLED
  23. #include "testing/unitTesting.h"
  24. // This unit test will blow up without thread static support
  25. #include "core/threadStatic.h"
  26. #ifdef TORQUE_ENABLE_THREAD_STATICS
  27. // Declare a test thread static
  28. DITTS(U32, gUnitTestFoo, 42);
  29. DITTS(F32, gUnitTestF32, 1.0);
  30. TEST(ThreadStatic, BasicAPI)
  31. {
  32. // ThreadStatic list should be initialized right now, so lets see if it has
  33. // any entries.
  34. EXPECT_FALSE(_TorqueThreadStaticReg::getStaticList().empty())
  35. << "Self-registration has failed, or no statics declared";
  36. // Spawn a new copy.
  37. TorqueThreadStaticListHandle testInstance = _TorqueThreadStaticReg::spawnThreadStaticsInstance();
  38. // Test the copy
  39. ASSERT_EQ(_TorqueThreadStaticReg::getStaticList(0).size(), testInstance->size())
  40. << "Spawned static list has a different size from master copy.";
  41. // Traverse the list and compare it to the initial value copy (index 0)
  42. for(S32 i = 0; i < _TorqueThreadStaticReg::getStaticList().size(); i++)
  43. {
  44. _TorqueThreadStatic *master = _TorqueThreadStaticReg::getStaticList()[i];
  45. _TorqueThreadStatic *cpy = (*testInstance)[i];
  46. // Make sure it is not the same memory
  47. EXPECT_NE(master, cpy)
  48. << "Copy not spawned properly.";
  49. // Make sure the sizes are the same
  50. ASSERT_EQ(master->getMemInstSize(), cpy->getMemInstSize())
  51. << "Size mismatch between master and copy";
  52. // Make sure the initialization occurred properly
  53. EXPECT_EQ(0, dMemcmp(master->getMemInstPtr(), cpy->getMemInstPtr(), master->getMemInstSize()))
  54. << "Initial value for spawned list is not correct";
  55. }
  56. // Test metrics if enabled
  57. #ifdef TORQUE_ENABLE_THREAD_STATIC_METRICS
  58. U32 fooHitCount = (*testInstance)[_gUnitTestFooTorqueThreadStatic::getListIndex()]->getHitCount();
  59. #endif
  60. // Set/get some values (If we test static metrics, this is hit 1)
  61. ATTS_(gUnitTestFoo, 1) = 55;
  62. // Test to see that the master copy and the spawned copy differ
  63. // (If we test metrics, this is hit 2, for the instance, and hit 1 for the master)
  64. EXPECT_NE(ATTS_(gUnitTestFoo, 0), ATTS_(gUnitTestFoo, 1))
  65. << "Assignment for spawned instanced memory failed";
  66. #ifdef TORQUE_ENABLE_THREAD_STATIC_METRICS
  67. U32 fooHitCount2 = (*testInstance)[_gUnitTestFooTorqueThreadStatic::getListIndex()]->getHitCount();
  68. EXPECT_EQ(fooHitCount2, (fooHitCount + 2))
  69. << "Thread static metric hit count failed";
  70. #endif
  71. // Destroy instances
  72. _TorqueThreadStaticReg::destroyInstance(testInstance);
  73. }
  74. #ifdef TORQUE_ENABLE_PROFILER
  75. #include "math/mRandom.h"
  76. #include "platform/profiler.h"
  77. // Declare a test thread static
  78. DITTS(U32, gInstancedStaticFoo, 42);
  79. static U32 gTrueStaticFoo = 42;
  80. TEST(ThreadStatic, StressThreadStatic)
  81. {
  82. ASSERT_FALSE(gProfiler->isEnabled())
  83. << "Profiler is currently enabled, test cannot continue";
  84. // Spawn an instance
  85. TorqueThreadStaticListHandle testInstance = _TorqueThreadStaticReg::spawnThreadStaticsInstance();
  86. static const dsize_t TEST_SIZE = 100000;
  87. // What we are going to do in this test is to test some U32 static
  88. // performance. The test will be run TEST_SIZE times, and so first create
  89. // an array of values to standardize the tests on.
  90. U32 testValue[TEST_SIZE];
  91. for(S32 i = 0; i < TEST_SIZE; i++)
  92. testValue[i] = gRandGen.randI();
  93. // Reset the profiler, tell it to dump to console when done
  94. gProfiler->dumpToConsole();
  95. gProfiler->enable(true);
  96. // Value array is initialized, so lets put the foo's through the paces
  97. PROFILE_START(ThreadStaticPerf_TrueStaticAssign);
  98. for(int i = 0; i < TEST_SIZE; i++)
  99. gTrueStaticFoo = testValue[i];
  100. PROFILE_END();
  101. PROFILE_START(ThreadStaticPerf_InstanceStaticAssign);
  102. for(S32 i = 0; i < TEST_SIZE; i++)
  103. ATTS_(gInstancedStaticFoo, 1) = testValue[i];
  104. PROFILE_END();
  105. gProfiler->enable(false);
  106. // Clean up instance
  107. _TorqueThreadStaticReg::destroyInstance(testInstance);
  108. }
  109. #endif
  110. #endif
  111. #endif