jlibtests.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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(testBitsetHelpers);
  77. CPPUNIT_TEST(testSimple);
  78. CPPUNIT_TEST_SUITE_END();
  79. protected:
  80. void testBitsetHelpers()
  81. {
  82. CPPUNIT_ASSERT_EQUAL(0U, countTrailingUnsetBits(1));
  83. CPPUNIT_ASSERT_EQUAL(31U, countLeadingUnsetBits(1));
  84. CPPUNIT_ASSERT_EQUAL(1U, getMostSignificantBit(1));
  85. CPPUNIT_ASSERT_EQUAL(4U, countTrailingUnsetBits(0x110));
  86. CPPUNIT_ASSERT_EQUAL(23U, countLeadingUnsetBits(0x110));
  87. CPPUNIT_ASSERT_EQUAL(9U, getMostSignificantBit(0x110));
  88. CPPUNIT_ASSERT_EQUAL(0U, countTrailingUnsetBits(0xFFFFFFFFU));
  89. CPPUNIT_ASSERT_EQUAL(0U, countLeadingUnsetBits(0xFFFFFFFFU));
  90. CPPUNIT_ASSERT_EQUAL(32U, getMostSignificantBit(0xFFFFFFFFU));
  91. }
  92. void testSet1(bool initial, IBitSet *bs, unsigned start, unsigned numBits, bool setValue, bool clearValue)
  93. {
  94. unsigned end = start+numBits;
  95. if (initial)
  96. bs->incl(start, end-1);
  97. for (unsigned i=start; i < end; i++)
  98. {
  99. ASSERT(bs->test(i) == clearValue);
  100. bs->set(i, setValue);
  101. ASSERT(bs->test(i) == setValue);
  102. bs->set(i+5, setValue);
  103. ASSERT(bs->scan(0, setValue) == i);
  104. ASSERT(bs->scan(i+1, setValue) == i+5);
  105. bs->set(i, clearValue);
  106. bs->set(i+5, clearValue);
  107. //Clearing i+5 above may extend the set - so need to calculate the end carefully
  108. unsigned last = i+5 < end ? end : i + 6;
  109. unsigned match1 = bs->scan(0, setValue);
  110. CPPUNIT_ASSERT_EQUAL((unsigned)(initial ? last : -1), match1);
  111. bs->invert(i);
  112. ASSERT(bs->test(i) == setValue);
  113. bs->invert(i);
  114. ASSERT(bs->test(i) == clearValue);
  115. bool wasSet = bs->testSet(i, setValue);
  116. ASSERT(wasSet == clearValue);
  117. bool wasSet2 = bs->testSet(i, clearValue);
  118. ASSERT(wasSet2 == setValue);
  119. ASSERT(bs->test(i) == clearValue);
  120. bs->set(i, setValue);
  121. unsigned match = bs->scanInvert(0, setValue);
  122. ASSERT(match == i);
  123. ASSERT(bs->test(i) == clearValue);
  124. }
  125. bs->reset();
  126. if (initial)
  127. {
  128. bs->incl(start, end);
  129. bs->excl(start+5, end-5);
  130. }
  131. else
  132. bs->incl(start+5, end-5);
  133. unsigned inclStart = bs->scan(start, setValue);
  134. ASSERT((start+5) == inclStart);
  135. unsigned inclEnd = bs->scan(start+5, clearValue);
  136. ASSERT((end-5) == (inclEnd-1));
  137. }
  138. void testSet(bool initial)
  139. {
  140. unsigned now = msTick();
  141. bool setValue = !initial;
  142. bool clearValue = initial;
  143. const unsigned numBits = 400;
  144. const unsigned passes = 10000;
  145. for (unsigned pass=0; pass < passes; pass++)
  146. {
  147. Owned<IBitSet> bs = createThreadSafeBitSet();
  148. testSet1(initial, bs, 0, numBits, setValue, clearValue);
  149. }
  150. unsigned elapsed = msTick()-now;
  151. fprintf(stdout, "Bit test (%u) time taken = %dms\n", initial, elapsed);
  152. now = msTick();
  153. for (unsigned pass=0; pass < passes; pass++)
  154. {
  155. Owned<IBitSet> bs = createBitSet();
  156. testSet1(initial, bs, 0, numBits, setValue, clearValue);
  157. }
  158. elapsed = msTick()-now;
  159. fprintf(stdout, "Bit test [thread-unsafe version] (%u) time taken = %dms\n", initial, elapsed);
  160. now = msTick();
  161. size32_t bitSetMemSz = getBitSetMemoryRequirement(numBits+5);
  162. MemoryBuffer mb;
  163. void *mem = mb.reserveTruncate(bitSetMemSz);
  164. for (unsigned pass=0; pass < passes; pass++)
  165. {
  166. Owned<IBitSet> bs = createBitSet(bitSetMemSz, mem);
  167. testSet1(initial, bs, 0, numBits, setValue, clearValue);
  168. }
  169. elapsed = msTick()-now;
  170. fprintf(stdout, "Bit test [thread-unsafe version, fixed memory] (%u) time taken = %dms\n", initial, elapsed);
  171. }
  172. class CBitThread : public CSimpleInterfaceOf<IInterface>, implements IThreaded
  173. {
  174. IBitSet &bitSet;
  175. unsigned startBit, numBits;
  176. bool initial, setValue, clearValue;
  177. CThreaded threaded;
  178. Owned<IException> exception;
  179. CppUnit::Exception *cppunitException;
  180. public:
  181. CBitThread(IBitSet &_bitSet, unsigned _startBit, unsigned _numBits, bool _initial)
  182. : threaded("CBitThread", this), bitSet(_bitSet), startBit(_startBit), numBits(_numBits), initial(_initial)
  183. {
  184. cppunitException = NULL;
  185. setValue = !initial;
  186. clearValue = initial;
  187. }
  188. void start() { threaded.start(); }
  189. void join()
  190. {
  191. threaded.join();
  192. if (exception)
  193. throw exception.getClear();
  194. else if (cppunitException)
  195. throw cppunitException;
  196. }
  197. virtual void main()
  198. {
  199. try
  200. {
  201. unsigned endBit = startBit+numBits-1;
  202. if (initial)
  203. bitSet.incl(startBit, endBit);
  204. for (unsigned i=startBit; i < endBit; i++)
  205. {
  206. ASSERT(bitSet.test(i) == clearValue);
  207. bitSet.set(i, setValue);
  208. ASSERT(bitSet.test(i) == setValue);
  209. if (i < (endBit-1))
  210. ASSERT(bitSet.scan(i, clearValue) == i+1); // find next unset (should be i+1)
  211. bitSet.set(i, clearValue);
  212. bitSet.invert(i);
  213. ASSERT(bitSet.test(i) == setValue);
  214. bitSet.invert(i);
  215. ASSERT(bitSet.test(i) == clearValue);
  216. bool wasSet = bitSet.testSet(i, setValue);
  217. ASSERT(wasSet == clearValue);
  218. bool wasSet2 = bitSet.testSet(i, clearValue);
  219. ASSERT(wasSet2 == setValue);
  220. ASSERT(bitSet.test(i) == clearValue);
  221. bitSet.set(i, setValue);
  222. unsigned match = bitSet.scanInvert(startBit, setValue);
  223. ASSERT(match == i);
  224. ASSERT(bitSet.test(i) == clearValue);
  225. }
  226. }
  227. catch (IException *e)
  228. {
  229. exception.setown(e);
  230. }
  231. catch (CppUnit::Exception &e)
  232. {
  233. cppunitException = e.clone();
  234. }
  235. }
  236. };
  237. unsigned testParallelRun(IBitSet &bitSet, unsigned nThreads, unsigned bitsPerThread, bool initial)
  238. {
  239. IArrayOf<CBitThread> bitThreads;
  240. unsigned bitStart = 0;
  241. unsigned bitEnd = 0;
  242. for (unsigned t=0; t<nThreads; t++)
  243. {
  244. bitThreads.append(* new CBitThread(bitSet, bitStart, bitsPerThread, initial));
  245. bitStart += bitsPerThread;
  246. }
  247. unsigned now = msTick();
  248. for (unsigned t=0; t<nThreads; t++)
  249. bitThreads.item(t).start();
  250. Owned<IException> exception;
  251. CppUnit::Exception *cppunitException = NULL;
  252. for (unsigned t=0; t<nThreads; t++)
  253. {
  254. try
  255. {
  256. bitThreads.item(t).join();
  257. }
  258. catch (IException *e)
  259. {
  260. EXCLOG(e, NULL);
  261. if (!exception)
  262. exception.setown(e);
  263. else
  264. e->Release();
  265. }
  266. catch (CppUnit::Exception *e)
  267. {
  268. cppunitException = e;
  269. }
  270. }
  271. if (exception)
  272. throw exception.getClear();
  273. else if (cppunitException)
  274. throw *cppunitException;
  275. return msTick()-now;
  276. }
  277. void testSetParallel(bool initial)
  278. {
  279. unsigned numBits = 1000000; // 10M
  280. unsigned nThreads = getAffinityCpus();
  281. unsigned bitsPerThread = numBits/nThreads;
  282. bitsPerThread = ((bitsPerThread + (BitsPerItem-1)) / BitsPerItem) * BitsPerItem; // round up to multiple of BitsPerItem
  283. numBits = bitsPerThread*nThreads; // round
  284. fprintf(stdout, "testSetParallel, testing bit set of size : %d, nThreads=%d\n", numBits, nThreads);
  285. Owned<IBitSet> bitSet = createThreadSafeBitSet();
  286. unsigned took = testParallelRun(*bitSet, nThreads, bitsPerThread, initial);
  287. fprintf(stdout, "Thread safe parallel bit set test (%u) time taken = %dms\n", initial, took);
  288. size32_t bitSetMemSz = getBitSetMemoryRequirement(numBits);
  289. MemoryBuffer mb;
  290. void *mem = mb.reserveTruncate(bitSetMemSz);
  291. bitSet.setown(createBitSet(bitSetMemSz, mem));
  292. took = testParallelRun(*bitSet, nThreads, bitsPerThread, initial);
  293. fprintf(stdout, "Thread unsafe parallel bit set test (%u) time taken = %dms\n", initial, took);
  294. }
  295. void testSimple()
  296. {
  297. testSet(false);
  298. testSet(true);
  299. testSetParallel(false);
  300. testSetParallel(true);
  301. }
  302. };
  303. CPPUNIT_TEST_SUITE_REGISTRATION( JlibSetTest );
  304. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibSetTest, "JlibSetTest" );
  305. /* =========================================================== */
  306. class JlibFileIOTest : public CppUnit::TestFixture
  307. {
  308. unsigned rs, nr10pct, nr150pct;
  309. char *record;
  310. StringBuffer tmpfile;
  311. StringBuffer server;
  312. CPPUNIT_TEST_SUITE( JlibFileIOTest );
  313. CPPUNIT_TEST(testIOSmall);
  314. CPPUNIT_TEST(testIORemote);
  315. CPPUNIT_TEST(testIOLarge);
  316. CPPUNIT_TEST_SUITE_END();
  317. public:
  318. JlibFileIOTest()
  319. {
  320. HardwareInfo hdwInfo;
  321. getHardwareInfo(hdwInfo);
  322. rs = 65536;
  323. unsigned nr = (unsigned)(1024.0 * (1024.0 * (double)hdwInfo.totalMemory / (double)rs));
  324. nr10pct = nr / 10;
  325. nr150pct = (unsigned)((double)nr * 1.5);
  326. record = (char *)malloc(rs);
  327. for (unsigned i=0;i<rs;i++)
  328. record[i] = 'a';
  329. record[rs-1] = '\n';
  330. tmpfile.set("JlibFileIOTest.txt");
  331. server.set(".");
  332. // server.set("192.168.1.18");
  333. }
  334. ~JlibFileIOTest()
  335. {
  336. free(record);
  337. }
  338. protected:
  339. void testIO(unsigned nr, SocketEndpoint *ep)
  340. {
  341. IFile *ifile;
  342. IFileIO *ifileio;
  343. unsigned fsize = (unsigned)(((double)nr * (double)rs) / (1024.0 * 1024.0));
  344. fflush(NULL);
  345. fprintf(stdout,"\n");
  346. fflush(NULL);
  347. for(int j=0; j<2; j++)
  348. {
  349. if (j==0)
  350. fprintf(stdout, "File size: %d (MB) Cache, ", fsize);
  351. else
  352. fprintf(stdout, "\nFile size: %d (MB) Nocache, ", fsize);
  353. if (ep != NULL)
  354. {
  355. ifile = createRemoteFile(*ep, tmpfile);
  356. fprintf(stdout, "Remote: (%s)\n", server.toCharArray());
  357. }
  358. else
  359. {
  360. ifile = createIFile(tmpfile);
  361. fprintf(stdout, "Local:\n");
  362. }
  363. ifile->remove();
  364. unsigned st = msTick();
  365. IFEflags extraFlags = IFEcache;
  366. if (j==1)
  367. extraFlags = IFEnocache;
  368. ifileio = ifile->open(IFOcreate, extraFlags);
  369. try
  370. {
  371. ifile->setFilePermissions(0666);
  372. }
  373. catch (...)
  374. {
  375. fprintf(stdout, "ifile->setFilePermissions() exception\n");
  376. }
  377. unsigned iter = nr / 40;
  378. __int64 pos = 0;
  379. for (unsigned i=0;i<nr;i++)
  380. {
  381. ifileio->write(pos, rs, record);
  382. pos += rs;
  383. if ((i % iter) == 0)
  384. {
  385. fprintf(stdout,".");
  386. fflush(NULL);
  387. }
  388. }
  389. ifileio->close();
  390. double rsec = (double)(msTick() - st)/1000.0;
  391. unsigned iorate = (unsigned)((double)fsize / rsec);
  392. fprintf(stdout, "\nwrite - elapsed time = %6.2f (s) iorate = %4d (MB/s)\n", rsec, iorate);
  393. st = msTick();
  394. extraFlags = IFEcache;
  395. if (j==1)
  396. extraFlags = IFEnocache;
  397. ifileio = ifile->open(IFOread, extraFlags);
  398. pos = 0;
  399. for (unsigned i=0;i<nr;i++)
  400. {
  401. ifileio->read(pos, rs, record);
  402. pos += rs;
  403. if ((i % iter) == 0)
  404. {
  405. fprintf(stdout,".");
  406. fflush(NULL);
  407. }
  408. }
  409. ifileio->close();
  410. rsec = (double)(msTick() - st)/1000.0;
  411. iorate = (unsigned)((double)fsize / rsec);
  412. fprintf(stdout, "\nread -- elapsed time = %6.2f (s) iorate = %4d (MB/s)\n", rsec, iorate);
  413. ifileio->Release();
  414. ifile->remove();
  415. ifile->Release();
  416. }
  417. }
  418. void testIOSmall()
  419. {
  420. testIO(nr10pct, NULL);
  421. }
  422. void testIOLarge()
  423. {
  424. testIO(nr150pct, NULL);
  425. }
  426. void testIORemote()
  427. {
  428. SocketEndpoint ep;
  429. ep.set(server, 7100);
  430. testIO(nr10pct, &ep);
  431. }
  432. };
  433. CPPUNIT_TEST_SUITE_REGISTRATION( JlibFileIOTest );
  434. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibFileIOTest, "JlibFileIOTest" );
  435. /* =========================================================== */
  436. class JlibStringBufferTest : public CppUnit::TestFixture
  437. {
  438. CPPUNIT_TEST_SUITE( JlibStringBufferTest );
  439. CPPUNIT_TEST(testSwap);
  440. CPPUNIT_TEST_SUITE_END();
  441. public:
  442. JlibStringBufferTest()
  443. {
  444. }
  445. void testSwap()
  446. {
  447. StringBuffer l;
  448. StringBuffer r;
  449. for (unsigned len=0; len<40; len++)
  450. {
  451. const unsigned numIter = 100000000;
  452. cycle_t start = get_cycles_now();
  453. for (unsigned pass=0; pass < numIter; pass++)
  454. {
  455. l.swapWith(r);
  456. }
  457. cycle_t elapsed = get_cycles_now() - start;
  458. fprintf(stdout, "iterations of size %u took %.2f\n", len, (double)cycle_to_nanosec(elapsed) / numIter);
  459. l.append("a");
  460. r.append("b");
  461. }
  462. }
  463. };
  464. CPPUNIT_TEST_SUITE_REGISTRATION( JlibStringBufferTest );
  465. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibStringBufferTest, "JlibStringBufferTest" );
  466. #endif // _USE_CPPUNIT