jlibtests.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. /*
  14. * Jlib regression tests
  15. *
  16. */
  17. #ifdef _USE_CPPUNIT
  18. #include "jsem.hpp"
  19. #include "jfile.hpp"
  20. #include "jdebug.hpp"
  21. #include "sockfile.hpp"
  22. #include "unittests.hpp"
  23. class JlibSemTest : public CppUnit::TestFixture
  24. {
  25. public:
  26. CPPUNIT_TEST_SUITE(JlibSemTest);
  27. CPPUNIT_TEST(testSetup);
  28. CPPUNIT_TEST(testSimple);
  29. CPPUNIT_TEST(testCleanup);
  30. CPPUNIT_TEST_SUITE_END();
  31. protected:
  32. void testSetup()
  33. {
  34. }
  35. void testCleanup()
  36. {
  37. }
  38. void testTimedAvailable(Semaphore & sem)
  39. {
  40. unsigned now = msTick();
  41. sem.wait(100);
  42. unsigned taken = msTick() - now;
  43. //Shouldn't cause a reschedule, definitely shouldn't wait for 100s
  44. ASSERT(taken < 5);
  45. }
  46. void testTimedElapsed(Semaphore & sem, unsigned time)
  47. {
  48. unsigned now = msTick();
  49. sem.wait(time);
  50. unsigned taken = msTick() - now;
  51. ASSERT(taken >= time && taken < 2*time);
  52. }
  53. void testSimple()
  54. {
  55. //Some very basic semaphore tests.
  56. Semaphore sem;
  57. sem.signal();
  58. sem.wait();
  59. testTimedElapsed(sem, 100);
  60. sem.signal();
  61. testTimedAvailable(sem);
  62. sem.reinit(2);
  63. sem.wait();
  64. testTimedAvailable(sem);
  65. testTimedElapsed(sem, 5);
  66. }
  67. };
  68. CPPUNIT_TEST_SUITE_REGISTRATION( JlibSemTest );
  69. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibSemTest, "JlibSemTest" );
  70. /* =========================================================== */
  71. class JlibFileIOTest : public CppUnit::TestFixture
  72. {
  73. unsigned rs, nr10pct, nr150pct;
  74. char *record;
  75. StringBuffer tmpfile;
  76. StringBuffer server;
  77. CPPUNIT_TEST_SUITE( JlibFileIOTest );
  78. CPPUNIT_TEST(testIOSmall);
  79. CPPUNIT_TEST(testIORemote);
  80. CPPUNIT_TEST(testIOLarge);
  81. CPPUNIT_TEST_SUITE_END();
  82. public:
  83. JlibFileIOTest()
  84. {
  85. HardwareInfo hdwInfo;
  86. getHardwareInfo(hdwInfo);
  87. rs = 65536;
  88. unsigned nr = (unsigned)(1024.0 * (1024.0 * (double)hdwInfo.totalMemory / (double)rs));
  89. nr10pct = nr / 10;
  90. nr150pct = (unsigned)((double)nr * 1.5);
  91. record = (char *)malloc(rs);
  92. for (unsigned i=0;i<rs;i++)
  93. record[i] = 'a';
  94. record[rs-1] = '\n';
  95. tmpfile.set("JlibFileIOTest.txt");
  96. server.set(".");
  97. // server.set("192.168.1.18");
  98. }
  99. ~JlibFileIOTest()
  100. {
  101. free(record);
  102. }
  103. protected:
  104. void testIO(unsigned nr, SocketEndpoint *ep)
  105. {
  106. IFile *ifile;
  107. IFileIO *ifileio;
  108. unsigned fsize = (unsigned)(((double)nr * (double)rs) / (1024.0 * 1024.0));
  109. fflush(NULL);
  110. fprintf(stdout,"\n");
  111. fflush(NULL);
  112. for(int j=0; j<2; j++)
  113. {
  114. if (j==0)
  115. fprintf(stdout, "File size: %d (MB) Cache, ", fsize);
  116. else
  117. fprintf(stdout, "\nFile size: %d (MB) Nocache, ", fsize);
  118. if (ep != NULL)
  119. {
  120. ifile = createRemoteFile(*ep, tmpfile);
  121. fprintf(stdout, "Remote: (%s)\n", server.toCharArray());
  122. }
  123. else
  124. {
  125. ifile = createIFile(tmpfile);
  126. fprintf(stdout, "Local:\n");
  127. }
  128. ifile->remove();
  129. unsigned st = msTick();
  130. IFEflags extraFlags = IFEcache;
  131. if (j==1)
  132. extraFlags = IFEnocache;
  133. ifileio = ifile->open(IFOcreate, extraFlags);
  134. unsigned iter = nr / 40;
  135. __int64 pos = 0;
  136. for (unsigned i=0;i<nr;i++)
  137. {
  138. ifileio->write(pos, rs, record);
  139. pos += rs;
  140. if ((i % iter) == 0)
  141. {
  142. fprintf(stdout,".");
  143. fflush(NULL);
  144. }
  145. }
  146. ifileio->close();
  147. double rsec = (double)(msTick() - st)/1000.0;
  148. unsigned iorate = (unsigned)((double)fsize / rsec);
  149. fprintf(stdout, "\nwrite - elapsed time = %6.2f (s) iorate = %4d (MB/s)\n", rsec, iorate);
  150. st = msTick();
  151. extraFlags = IFEcache;
  152. if (j==1)
  153. extraFlags = IFEnocache;
  154. ifileio = ifile->open(IFOread, extraFlags);
  155. pos = 0;
  156. for (unsigned i=0;i<nr;i++)
  157. {
  158. ifileio->read(pos, rs, record);
  159. pos += rs;
  160. if ((i % iter) == 0)
  161. {
  162. fprintf(stdout,".");
  163. fflush(NULL);
  164. }
  165. }
  166. ifileio->close();
  167. rsec = (double)(msTick() - st)/1000.0;
  168. iorate = (unsigned)((double)fsize / rsec);
  169. fprintf(stdout, "\nread -- elapsed time = %6.2f (s) iorate = %4d (MB/s)\n", rsec, iorate);
  170. ifileio->Release();
  171. ifile->remove();
  172. ifile->Release();
  173. }
  174. }
  175. void testIOSmall()
  176. {
  177. testIO(nr10pct, NULL);
  178. }
  179. void testIOLarge()
  180. {
  181. testIO(nr150pct, NULL);
  182. }
  183. void testIORemote()
  184. {
  185. SocketEndpoint ep;
  186. ep.set(server, 7100);
  187. testIO(nr10pct, &ep);
  188. }
  189. };
  190. CPPUNIT_TEST_SUITE_REGISTRATION( JlibFileIOTest );
  191. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibFileIOTest, "JlibFileIOTest" );
  192. #endif // _USE_CPPUNIT