2
0

threadStaticTest.cpp 5.1 KB

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