jlibtests.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 "jset.hpp"
  22. #include "sockfile.hpp"
  23. #include "unittests.hpp"
  24. class JlibSemTest : public CppUnit::TestFixture
  25. {
  26. public:
  27. CPPUNIT_TEST_SUITE(JlibSemTest);
  28. CPPUNIT_TEST(testSetup);
  29. CPPUNIT_TEST(testSimple);
  30. CPPUNIT_TEST(testCleanup);
  31. CPPUNIT_TEST_SUITE_END();
  32. protected:
  33. void testSetup()
  34. {
  35. }
  36. void testCleanup()
  37. {
  38. }
  39. void testTimedAvailable(Semaphore & sem)
  40. {
  41. unsigned now = msTick();
  42. sem.wait(100);
  43. unsigned taken = msTick() - now;
  44. //Shouldn't cause a reschedule, definitely shouldn't wait for 100s
  45. ASSERT(taken < 5);
  46. }
  47. void testTimedElapsed(Semaphore & sem, unsigned time)
  48. {
  49. unsigned now = msTick();
  50. sem.wait(time);
  51. unsigned taken = msTick() - now;
  52. ASSERT(taken >= time && taken < 2*time);
  53. }
  54. void testSimple()
  55. {
  56. //Some very basic semaphore tests.
  57. Semaphore sem;
  58. sem.signal();
  59. sem.wait();
  60. testTimedElapsed(sem, 100);
  61. sem.signal();
  62. testTimedAvailable(sem);
  63. sem.reinit(2);
  64. sem.wait();
  65. testTimedAvailable(sem);
  66. testTimedElapsed(sem, 5);
  67. }
  68. };
  69. CPPUNIT_TEST_SUITE_REGISTRATION( JlibSemTest );
  70. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibSemTest, "JlibSemTest" );
  71. /* =========================================================== */
  72. class JlibSetTest : public CppUnit::TestFixture
  73. {
  74. public:
  75. CPPUNIT_TEST_SUITE(JlibSetTest);
  76. CPPUNIT_TEST(testSimple);
  77. CPPUNIT_TEST_SUITE_END();
  78. protected:
  79. void testSet(bool initial)
  80. {
  81. unsigned now = msTick();
  82. bool setValue = !initial;
  83. bool clearValue = initial;
  84. const unsigned numBits = 400;
  85. for (unsigned pass=0; pass< 10000; pass++)
  86. {
  87. Owned<IBitSet> bs = createBitSet();
  88. if (initial)
  89. bs->incl(0, numBits);
  90. for (unsigned i=0; i < numBits; i++)
  91. {
  92. ASSERT(bs->test(i) == clearValue);
  93. bs->set(i, setValue);
  94. ASSERT(bs->test(i) == setValue);
  95. bs->set(i+5, setValue);
  96. ASSERT(bs->scan(0, setValue) == i);
  97. ASSERT(bs->scan(i+1, setValue) == i+5);
  98. bs->set(i, clearValue);
  99. bs->set(i+5, clearValue);
  100. unsigned match1 = bs->scan(0, setValue);
  101. ASSERT(match1 == initial ? -1 : numBits);
  102. bs->invert(i);
  103. ASSERT(bs->test(i) == setValue);
  104. bs->invert(i);
  105. ASSERT(bs->test(i) == clearValue);
  106. bool wasSet = bs->testSet(i, setValue);
  107. ASSERT(wasSet == clearValue);
  108. bool wasSet2 = bs->testSet(i, clearValue);
  109. ASSERT(wasSet2 == setValue);
  110. ASSERT(bs->test(i) == clearValue);
  111. bs->set(i, setValue);
  112. unsigned match = bs->scanInvert(0, setValue);
  113. ASSERT(match == i);
  114. ASSERT(bs->test(i) == clearValue);
  115. }
  116. }
  117. unsigned elapsed = msTick()-now;
  118. fprintf(stdout, "Bit test (%u) time taken = %dms\n", initial, elapsed);
  119. }
  120. void testSimple()
  121. {
  122. testSet(false);
  123. testSet(true);
  124. }
  125. };
  126. CPPUNIT_TEST_SUITE_REGISTRATION( JlibSetTest );
  127. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibSetTest, "JlibSetTest" );
  128. /* =========================================================== */
  129. class JlibFileIOTest : public CppUnit::TestFixture
  130. {
  131. unsigned rs, nr10pct, nr150pct;
  132. char *record;
  133. StringBuffer tmpfile;
  134. StringBuffer server;
  135. CPPUNIT_TEST_SUITE( JlibFileIOTest );
  136. CPPUNIT_TEST(testIOSmall);
  137. CPPUNIT_TEST(testIORemote);
  138. CPPUNIT_TEST(testIOLarge);
  139. CPPUNIT_TEST_SUITE_END();
  140. public:
  141. JlibFileIOTest()
  142. {
  143. HardwareInfo hdwInfo;
  144. getHardwareInfo(hdwInfo);
  145. rs = 65536;
  146. unsigned nr = (unsigned)(1024.0 * (1024.0 * (double)hdwInfo.totalMemory / (double)rs));
  147. nr10pct = nr / 10;
  148. nr150pct = (unsigned)((double)nr * 1.5);
  149. record = (char *)malloc(rs);
  150. for (unsigned i=0;i<rs;i++)
  151. record[i] = 'a';
  152. record[rs-1] = '\n';
  153. tmpfile.set("JlibFileIOTest.txt");
  154. server.set(".");
  155. // server.set("192.168.1.18");
  156. }
  157. ~JlibFileIOTest()
  158. {
  159. free(record);
  160. }
  161. protected:
  162. void testIO(unsigned nr, SocketEndpoint *ep)
  163. {
  164. IFile *ifile;
  165. IFileIO *ifileio;
  166. unsigned fsize = (unsigned)(((double)nr * (double)rs) / (1024.0 * 1024.0));
  167. fflush(NULL);
  168. fprintf(stdout,"\n");
  169. fflush(NULL);
  170. for(int j=0; j<2; j++)
  171. {
  172. if (j==0)
  173. fprintf(stdout, "File size: %d (MB) Cache, ", fsize);
  174. else
  175. fprintf(stdout, "\nFile size: %d (MB) Nocache, ", fsize);
  176. if (ep != NULL)
  177. {
  178. ifile = createRemoteFile(*ep, tmpfile);
  179. fprintf(stdout, "Remote: (%s)\n", server.toCharArray());
  180. }
  181. else
  182. {
  183. ifile = createIFile(tmpfile);
  184. fprintf(stdout, "Local:\n");
  185. }
  186. ifile->remove();
  187. unsigned st = msTick();
  188. IFEflags extraFlags = IFEcache;
  189. if (j==1)
  190. extraFlags = IFEnocache;
  191. ifileio = ifile->open(IFOcreate, extraFlags);
  192. unsigned iter = nr / 40;
  193. __int64 pos = 0;
  194. for (unsigned i=0;i<nr;i++)
  195. {
  196. ifileio->write(pos, rs, record);
  197. pos += rs;
  198. if ((i % iter) == 0)
  199. {
  200. fprintf(stdout,".");
  201. fflush(NULL);
  202. }
  203. }
  204. ifileio->close();
  205. double rsec = (double)(msTick() - st)/1000.0;
  206. unsigned iorate = (unsigned)((double)fsize / rsec);
  207. fprintf(stdout, "\nwrite - elapsed time = %6.2f (s) iorate = %4d (MB/s)\n", rsec, iorate);
  208. st = msTick();
  209. extraFlags = IFEcache;
  210. if (j==1)
  211. extraFlags = IFEnocache;
  212. ifileio = ifile->open(IFOread, extraFlags);
  213. pos = 0;
  214. for (unsigned i=0;i<nr;i++)
  215. {
  216. ifileio->read(pos, rs, record);
  217. pos += rs;
  218. if ((i % iter) == 0)
  219. {
  220. fprintf(stdout,".");
  221. fflush(NULL);
  222. }
  223. }
  224. ifileio->close();
  225. rsec = (double)(msTick() - st)/1000.0;
  226. iorate = (unsigned)((double)fsize / rsec);
  227. fprintf(stdout, "\nread -- elapsed time = %6.2f (s) iorate = %4d (MB/s)\n", rsec, iorate);
  228. ifileio->Release();
  229. ifile->remove();
  230. ifile->Release();
  231. }
  232. }
  233. void testIOSmall()
  234. {
  235. testIO(nr10pct, NULL);
  236. }
  237. void testIOLarge()
  238. {
  239. testIO(nr150pct, NULL);
  240. }
  241. void testIORemote()
  242. {
  243. SocketEndpoint ep;
  244. ep.set(server, 7100);
  245. testIO(nr10pct, &ep);
  246. }
  247. };
  248. CPPUNIT_TEST_SUITE_REGISTRATION( JlibFileIOTest );
  249. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibFileIOTest, "JlibFileIOTest" );
  250. /* =========================================================== */
  251. class JlibStringBufferTest : public CppUnit::TestFixture
  252. {
  253. CPPUNIT_TEST_SUITE( JlibStringBufferTest );
  254. CPPUNIT_TEST(testSwap);
  255. CPPUNIT_TEST_SUITE_END();
  256. public:
  257. JlibStringBufferTest()
  258. {
  259. }
  260. void testSwap()
  261. {
  262. StringBuffer l;
  263. StringBuffer r;
  264. for (unsigned len=0; len<40; len++)
  265. {
  266. const unsigned numIter = 100000000;
  267. cycle_t start = get_cycles_now();
  268. for (unsigned pass=0; pass < numIter; pass++)
  269. {
  270. l.swapWith(r);
  271. }
  272. cycle_t elapsed = get_cycles_now() - start;
  273. fprintf(stdout, "iterations of size %u took %.2f\n", len, (double)cycle_to_nanosec(elapsed) / numIter);
  274. l.append("a");
  275. r.append("b");
  276. }
  277. }
  278. };
  279. CPPUNIT_TEST_SUITE_REGISTRATION( JlibStringBufferTest );
  280. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibStringBufferTest, "JlibStringBufferTest" );
  281. #endif // _USE_CPPUNIT