jlibtests.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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 "jqueue.hpp"
  24. #include "unittests.hpp"
  25. class JlibSemTest : public CppUnit::TestFixture
  26. {
  27. public:
  28. CPPUNIT_TEST_SUITE(JlibSemTest);
  29. CPPUNIT_TEST(testSetup);
  30. CPPUNIT_TEST(testSimple);
  31. CPPUNIT_TEST(testCleanup);
  32. CPPUNIT_TEST_SUITE_END();
  33. protected:
  34. void testSetup()
  35. {
  36. }
  37. void testCleanup()
  38. {
  39. }
  40. void testTimedAvailable(Semaphore & sem)
  41. {
  42. unsigned now = msTick();
  43. sem.wait(100);
  44. unsigned taken = msTick() - now;
  45. //Shouldn't cause a reschedule, definitely shouldn't wait for 100s
  46. ASSERT(taken < 5);
  47. }
  48. void testTimedElapsed(Semaphore & sem, unsigned time)
  49. {
  50. unsigned now = msTick();
  51. sem.wait(time);
  52. unsigned taken = msTick() - now;
  53. ASSERT(taken >= time && taken < 2*time);
  54. }
  55. void testSimple()
  56. {
  57. //Some very basic semaphore tests.
  58. Semaphore sem;
  59. sem.signal();
  60. sem.wait();
  61. testTimedElapsed(sem, 100);
  62. sem.signal();
  63. testTimedAvailable(sem);
  64. sem.reinit(2);
  65. sem.wait();
  66. testTimedAvailable(sem);
  67. testTimedElapsed(sem, 5);
  68. }
  69. };
  70. CPPUNIT_TEST_SUITE_REGISTRATION( JlibSemTest );
  71. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibSemTest, "JlibSemTest" );
  72. /* =========================================================== */
  73. class JlibSetTest : public CppUnit::TestFixture
  74. {
  75. protected:
  76. void testBitsetHelpers()
  77. {
  78. CPPUNIT_ASSERT_EQUAL(0U, countTrailingUnsetBits(1U));
  79. CPPUNIT_ASSERT_EQUAL(31U, countLeadingUnsetBits(1U));
  80. CPPUNIT_ASSERT_EQUAL(1U, getMostSignificantBit(1U));
  81. CPPUNIT_ASSERT_EQUAL(4U, countTrailingUnsetBits(0x110U));
  82. CPPUNIT_ASSERT_EQUAL(23U, countLeadingUnsetBits(0x110U));
  83. CPPUNIT_ASSERT_EQUAL(9U, getMostSignificantBit(0x110U));
  84. CPPUNIT_ASSERT_EQUAL(0U, countTrailingUnsetBits(0xFFFFFFFFU));
  85. CPPUNIT_ASSERT_EQUAL(0U, countLeadingUnsetBits(0xFFFFFFFFU));
  86. CPPUNIT_ASSERT_EQUAL(32U, getMostSignificantBit(0xFFFFFFFFU));
  87. CPPUNIT_ASSERT_EQUAL(52U, countTrailingUnsetBits(I64C(0x1010000000000000U)));
  88. }
  89. void testSet1(bool initial, IBitSet *bs, unsigned start, unsigned numBits, bool setValue, bool clearValue)
  90. {
  91. unsigned end = start+numBits;
  92. if (initial)
  93. bs->incl(start, end-1);
  94. for (unsigned i=start; i < end; i++)
  95. {
  96. ASSERT(bs->test(i) == clearValue);
  97. bs->set(i, setValue);
  98. ASSERT(bs->test(i) == setValue);
  99. bs->set(i+5, setValue);
  100. ASSERT(bs->scan(0, setValue) == i);
  101. ASSERT(bs->scan(i+1, setValue) == i+5);
  102. bs->set(i, clearValue);
  103. bs->set(i+5, clearValue);
  104. //Clearing i+5 above may extend the set - so need to calculate the end carefully
  105. unsigned last = i+5 < end ? end : i + 6;
  106. unsigned match1 = bs->scan(0, setValue);
  107. CPPUNIT_ASSERT_EQUAL((unsigned)(initial ? last : -1), match1);
  108. bs->invert(i);
  109. ASSERT(bs->test(i) == setValue);
  110. bs->invert(i);
  111. ASSERT(bs->test(i) == clearValue);
  112. bool wasSet = bs->testSet(i, setValue);
  113. ASSERT(wasSet == clearValue);
  114. bool wasSet2 = bs->testSet(i, clearValue);
  115. ASSERT(wasSet2 == setValue);
  116. ASSERT(bs->test(i) == clearValue);
  117. bs->set(i, setValue);
  118. unsigned match = bs->scanInvert(0, setValue);
  119. ASSERT(match == i);
  120. ASSERT(bs->test(i) == clearValue);
  121. }
  122. bs->reset();
  123. if (initial)
  124. {
  125. bs->incl(start, end);
  126. bs->excl(start+5, end-5);
  127. }
  128. else
  129. bs->incl(start+5, end-5);
  130. unsigned inclStart = bs->scan(start, setValue);
  131. ASSERT((start+5) == inclStart);
  132. unsigned inclEnd = bs->scan(start+5, clearValue);
  133. ASSERT((end-5) == (inclEnd-1));
  134. }
  135. void testSet(bool initial, unsigned passes, bool timed)
  136. {
  137. unsigned now = msTick();
  138. bool setValue = !initial;
  139. bool clearValue = initial;
  140. const unsigned numBits = 400;
  141. for (unsigned pass=0; pass < passes; pass++)
  142. {
  143. Owned<IBitSet> bs = createThreadSafeBitSet();
  144. testSet1(initial, bs, 0, numBits, setValue, clearValue);
  145. }
  146. if (timed)
  147. {
  148. unsigned elapsed = msTick()-now;
  149. DBGLOG("Bit test (%u) %d passes time taken = %dms", initial, passes, elapsed);
  150. }
  151. now = msTick();
  152. for (unsigned pass=0; pass < passes; pass++)
  153. {
  154. Owned<IBitSet> bs = createBitSet();
  155. testSet1(initial, bs, 0, numBits, setValue, clearValue);
  156. }
  157. if (timed)
  158. {
  159. unsigned elapsed = msTick()-now;
  160. DBGLOG("Bit test [thread-unsafe version] (%u) %d passes time taken = %dms", initial, passes, elapsed);
  161. }
  162. now = msTick();
  163. size32_t bitSetMemSz = getBitSetMemoryRequirement(numBits+5);
  164. MemoryBuffer mb;
  165. void *mem = mb.reserveTruncate(bitSetMemSz);
  166. for (unsigned pass=0; pass < passes; pass++)
  167. {
  168. Owned<IBitSet> bs = createBitSet(bitSetMemSz, mem);
  169. testSet1(initial, bs, 0, numBits, setValue, clearValue);
  170. }
  171. if (timed)
  172. {
  173. unsigned elapsed = msTick()-now;
  174. DBGLOG("Bit test [thread-unsafe version, fixed memory] (%u) %d passes time taken = %dms\n", initial, passes, elapsed);
  175. }
  176. }
  177. };
  178. class JlibSetTestQuick : public JlibSetTest
  179. {
  180. public:
  181. CPPUNIT_TEST_SUITE(JlibSetTestQuick);
  182. CPPUNIT_TEST(testBitsetHelpers);
  183. CPPUNIT_TEST(testSimple);
  184. CPPUNIT_TEST_SUITE_END();
  185. void testSimple()
  186. {
  187. testSet(false, 100, false);
  188. testSet(true, 100, false);
  189. }
  190. };
  191. class JlibSetTestStress : public JlibSetTest
  192. {
  193. public:
  194. CPPUNIT_TEST_SUITE(JlibSetTestStress);
  195. CPPUNIT_TEST(testParallel);
  196. CPPUNIT_TEST(testSimple);
  197. CPPUNIT_TEST_SUITE_END();
  198. void testSimple()
  199. {
  200. testSet(false, 10000, true);
  201. testSet(true, 10000, true);
  202. }
  203. protected:
  204. class CBitThread : public CSimpleInterfaceOf<IInterface>, implements IThreaded
  205. {
  206. IBitSet &bitSet;
  207. unsigned startBit, numBits;
  208. bool initial, setValue, clearValue;
  209. CThreaded threaded;
  210. Owned<IException> exception;
  211. CppUnit::Exception *cppunitException;
  212. public:
  213. CBitThread(IBitSet &_bitSet, unsigned _startBit, unsigned _numBits, bool _initial)
  214. : threaded("CBitThread", this), bitSet(_bitSet), startBit(_startBit), numBits(_numBits), initial(_initial)
  215. {
  216. cppunitException = NULL;
  217. setValue = !initial;
  218. clearValue = initial;
  219. }
  220. void start() { threaded.start(); }
  221. void join()
  222. {
  223. threaded.join();
  224. if (exception)
  225. throw exception.getClear();
  226. else if (cppunitException)
  227. throw cppunitException;
  228. }
  229. virtual void main()
  230. {
  231. try
  232. {
  233. unsigned endBit = startBit+numBits-1;
  234. if (initial)
  235. bitSet.incl(startBit, endBit);
  236. for (unsigned i=startBit; i < endBit; i++)
  237. {
  238. ASSERT(bitSet.test(i) == clearValue);
  239. bitSet.set(i, setValue);
  240. ASSERT(bitSet.test(i) == setValue);
  241. if (i < (endBit-1))
  242. ASSERT(bitSet.scan(i, clearValue) == i+1); // find next unset (should be i+1)
  243. bitSet.set(i, clearValue);
  244. bitSet.invert(i);
  245. ASSERT(bitSet.test(i) == setValue);
  246. bitSet.invert(i);
  247. ASSERT(bitSet.test(i) == clearValue);
  248. bool wasSet = bitSet.testSet(i, setValue);
  249. ASSERT(wasSet == clearValue);
  250. bool wasSet2 = bitSet.testSet(i, clearValue);
  251. ASSERT(wasSet2 == setValue);
  252. ASSERT(bitSet.test(i) == clearValue);
  253. bitSet.set(i, setValue);
  254. unsigned match = bitSet.scanInvert(startBit, setValue);
  255. ASSERT(match == i);
  256. ASSERT(bitSet.test(i) == clearValue);
  257. }
  258. }
  259. catch (IException *e)
  260. {
  261. exception.setown(e);
  262. }
  263. catch (CppUnit::Exception &e)
  264. {
  265. cppunitException = e.clone();
  266. }
  267. }
  268. };
  269. unsigned testParallelRun(IBitSet &bitSet, unsigned nThreads, unsigned bitsPerThread, bool initial)
  270. {
  271. IArrayOf<CBitThread> bitThreads;
  272. unsigned bitStart = 0;
  273. unsigned bitEnd = 0;
  274. for (unsigned t=0; t<nThreads; t++)
  275. {
  276. bitThreads.append(* new CBitThread(bitSet, bitStart, bitsPerThread, initial));
  277. bitStart += bitsPerThread;
  278. }
  279. unsigned now = msTick();
  280. for (unsigned t=0; t<nThreads; t++)
  281. bitThreads.item(t).start();
  282. Owned<IException> exception;
  283. CppUnit::Exception *cppunitException = NULL;
  284. for (unsigned t=0; t<nThreads; t++)
  285. {
  286. try
  287. {
  288. bitThreads.item(t).join();
  289. }
  290. catch (IException *e)
  291. {
  292. EXCLOG(e, NULL);
  293. if (!exception)
  294. exception.setown(e);
  295. else
  296. e->Release();
  297. }
  298. catch (CppUnit::Exception *e)
  299. {
  300. cppunitException = e;
  301. }
  302. }
  303. if (exception)
  304. throw exception.getClear();
  305. else if (cppunitException)
  306. throw *cppunitException;
  307. return msTick()-now;
  308. }
  309. void testSetParallel(bool initial)
  310. {
  311. unsigned numBits = 1000000; // 10M
  312. unsigned nThreads = getAffinityCpus();
  313. unsigned bitsPerThread = numBits/nThreads;
  314. bitsPerThread = ((bitsPerThread + (BitsPerItem-1)) / BitsPerItem) * BitsPerItem; // round up to multiple of BitsPerItem
  315. numBits = bitsPerThread*nThreads; // round
  316. fprintf(stdout, "testSetParallel, testing bit set of size : %d, nThreads=%d\n", numBits, nThreads);
  317. Owned<IBitSet> bitSet = createThreadSafeBitSet();
  318. unsigned took = testParallelRun(*bitSet, nThreads, bitsPerThread, initial);
  319. fprintf(stdout, "Thread safe parallel bit set test (%u) time taken = %dms\n", initial, took);
  320. size32_t bitSetMemSz = getBitSetMemoryRequirement(numBits);
  321. MemoryBuffer mb;
  322. void *mem = mb.reserveTruncate(bitSetMemSz);
  323. bitSet.setown(createBitSet(bitSetMemSz, mem));
  324. took = testParallelRun(*bitSet, nThreads, bitsPerThread, initial);
  325. fprintf(stdout, "Thread unsafe parallel bit set test (%u) time taken = %dms\n", initial, took);
  326. }
  327. void testParallel()
  328. {
  329. testSetParallel(false);
  330. testSetParallel(true);
  331. }
  332. };
  333. CPPUNIT_TEST_SUITE_REGISTRATION( JlibSetTestQuick );
  334. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibSetTestQuick, "JlibSetTestQuick" );
  335. CPPUNIT_TEST_SUITE_REGISTRATION( JlibSetTestStress );
  336. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibSetTestStress, "JlibSetTestStress" );
  337. /* =========================================================== */
  338. class JlibFileIOTestTiming : public CppUnit::TestFixture
  339. {
  340. protected:
  341. unsigned rs, nr10pct, nr150pct;
  342. char *record;
  343. StringBuffer tmpfile;
  344. CPPUNIT_TEST_SUITE( JlibFileIOTestTiming );
  345. CPPUNIT_TEST(testIOSmall);
  346. CPPUNIT_TEST(testIOLarge);
  347. CPPUNIT_TEST_SUITE_END();
  348. public:
  349. JlibFileIOTestTiming()
  350. {
  351. HardwareInfo hdwInfo;
  352. getHardwareInfo(hdwInfo);
  353. rs = 65536;
  354. unsigned nr = (unsigned)(1024.0 * (1024.0 * (double)hdwInfo.totalMemory / (double)rs));
  355. nr10pct = nr / 10;
  356. nr150pct = (unsigned)((double)nr * 1.5);
  357. record = (char *)malloc(rs);
  358. for (unsigned i=0;i<rs;i++)
  359. record[i] = 'a';
  360. record[rs-1] = '\n';
  361. tmpfile.set("JlibFileIOTest.txt");
  362. }
  363. ~JlibFileIOTestTiming()
  364. {
  365. free(record);
  366. }
  367. protected:
  368. void testIO(unsigned nr, const char *server)
  369. {
  370. IFile *ifile;
  371. IFileIO *ifileio;
  372. unsigned fsize = (unsigned)(((double)nr * (double)rs) / (1024.0 * 1024.0));
  373. fflush(NULL);
  374. fprintf(stdout,"\n");
  375. fflush(NULL);
  376. for(int j=0; j<2; j++)
  377. {
  378. if (j==0)
  379. fprintf(stdout, "File size: %d (MB) Cache, ", fsize);
  380. else
  381. fprintf(stdout, "\nFile size: %d (MB) Nocache, ", fsize);
  382. if (server != NULL)
  383. {
  384. SocketEndpoint ep;
  385. ep.set(server, 7100);
  386. ifile = createRemoteFile(ep, tmpfile);
  387. fprintf(stdout, "Remote: (%s)\n", server);
  388. }
  389. else
  390. {
  391. ifile = createIFile(tmpfile);
  392. fprintf(stdout, "Local:\n");
  393. }
  394. ifile->remove();
  395. unsigned st = msTick();
  396. IFEflags extraFlags = IFEcache;
  397. if (j==1)
  398. extraFlags = IFEnocache;
  399. ifileio = ifile->open(IFOcreate, extraFlags);
  400. try
  401. {
  402. ifile->setFilePermissions(0666);
  403. }
  404. catch (...)
  405. {
  406. fprintf(stdout, "ifile->setFilePermissions() exception\n");
  407. }
  408. unsigned iter = nr / 40;
  409. __int64 pos = 0;
  410. for (unsigned i=0;i<nr;i++)
  411. {
  412. ifileio->write(pos, rs, record);
  413. pos += rs;
  414. if ((i % iter) == 0)
  415. {
  416. fprintf(stdout,".");
  417. fflush(NULL);
  418. }
  419. }
  420. ifileio->close();
  421. double rsec = (double)(msTick() - st)/1000.0;
  422. unsigned iorate = (unsigned)((double)fsize / rsec);
  423. fprintf(stdout, "\nwrite - elapsed time = %6.2f (s) iorate = %4d (MB/s)\n", rsec, iorate);
  424. st = msTick();
  425. extraFlags = IFEcache;
  426. if (j==1)
  427. extraFlags = IFEnocache;
  428. ifileio = ifile->open(IFOread, extraFlags);
  429. pos = 0;
  430. for (unsigned i=0;i<nr;i++)
  431. {
  432. ifileio->read(pos, rs, record);
  433. pos += rs;
  434. if ((i % iter) == 0)
  435. {
  436. fprintf(stdout,".");
  437. fflush(NULL);
  438. }
  439. }
  440. ifileio->close();
  441. rsec = (double)(msTick() - st)/1000.0;
  442. iorate = (unsigned)((double)fsize / rsec);
  443. fprintf(stdout, "\nread -- elapsed time = %6.2f (s) iorate = %4d (MB/s)\n", rsec, iorate);
  444. ifileio->Release();
  445. ifile->remove();
  446. ifile->Release();
  447. }
  448. }
  449. void testIOSmall()
  450. {
  451. testIO(nr10pct, NULL);
  452. }
  453. void testIOLarge()
  454. {
  455. testIO(nr150pct, NULL);
  456. }
  457. };
  458. class JlibFileIOTestStress : public JlibFileIOTestTiming
  459. {
  460. protected:
  461. CPPUNIT_TEST_SUITE( JlibFileIOTestStress );
  462. CPPUNIT_TEST(testIORemote);
  463. CPPUNIT_TEST_SUITE_END();
  464. void testIORemote()
  465. {
  466. const char * server = ".";
  467. testIO(nr10pct, server);
  468. }
  469. };
  470. CPPUNIT_TEST_SUITE_REGISTRATION( JlibFileIOTestTiming );
  471. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibFileIOTestTiming, "JlibFileIOTestTiming" );
  472. CPPUNIT_TEST_SUITE_REGISTRATION( JlibFileIOTestStress );
  473. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibFileIOTestTiming, "JlibFileIOTestStress" );
  474. /* =========================================================== */
  475. class JlibStringBufferTiming : public CppUnit::TestFixture
  476. {
  477. CPPUNIT_TEST_SUITE( JlibStringBufferTiming );
  478. CPPUNIT_TEST(testSwap);
  479. CPPUNIT_TEST_SUITE_END();
  480. public:
  481. void testSwap()
  482. {
  483. StringBuffer l;
  484. StringBuffer r;
  485. for (unsigned len=0; len<40; len++)
  486. {
  487. const unsigned numIter = 100000000;
  488. cycle_t start = get_cycles_now();
  489. for (unsigned pass=0; pass < numIter; pass++)
  490. {
  491. l.swapWith(r);
  492. }
  493. cycle_t elapsed = get_cycles_now() - start;
  494. DBGLOG("Each iteration of size %u took %.2f nanoseconds", len, (double)cycle_to_nanosec(elapsed) / numIter);
  495. l.append("a");
  496. r.append("b");
  497. }
  498. }
  499. };
  500. CPPUNIT_TEST_SUITE_REGISTRATION( JlibStringBufferTiming );
  501. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibStringBufferTiming, "JlibStringBufferTiming" );
  502. /* =========================================================== */
  503. static const unsigned split4_2[] = {0, 2, 4 };
  504. static const unsigned split100_2[] = {0, 50, 100 };
  505. static const unsigned split100_10[] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
  506. static const unsigned split7_10[] = {0,1,1,2,3,3,4,5,6,6,7 };
  507. static const unsigned split10_3[] = {0,3,7,10 };
  508. static const unsigned split58_10[] = {0,6,12,17,23,29,35,41,46,52,58 };
  509. static const unsigned split9_2T[] = { 0,5,9 };
  510. static const unsigned split9_2F[] = { 0,4,9 };
  511. static const unsigned split15_3[] = { 0,5,10,15 };
  512. class JlibQuantileTest : public CppUnit::TestFixture
  513. {
  514. CPPUNIT_TEST_SUITE( JlibQuantileTest );
  515. CPPUNIT_TEST(testQuantile);
  516. CPPUNIT_TEST(testRandom);
  517. CPPUNIT_TEST_SUITE_END();
  518. public:
  519. JlibQuantileTest()
  520. {
  521. }
  522. void testQuantilePos(unsigned numItems, unsigned numDivisions, bool roundUp, const unsigned * expected)
  523. {
  524. if (numDivisions == 0)
  525. return;
  526. QuantilePositionIterator iter(numItems, numDivisions, roundUp);
  527. QuantileFilterIterator filter(numItems, numDivisions, roundUp);
  528. unsigned prevPos = 0;
  529. iter.first();
  530. for (unsigned i=0; i <= numDivisions; i++)
  531. {
  532. //Check the values from the quantile iterator match those that are expected
  533. unsigned pos = (unsigned)iter.get();
  534. #if 0
  535. printf("(%d,%d) %d=%d\n", numItems, numDivisions, i, pos);
  536. #endif
  537. if (expected)
  538. CPPUNIT_ASSERT_EQUAL(expected[i], pos);
  539. //Check that the quantile filter correctly returns true and false for subsequent calls.
  540. while (prevPos < pos)
  541. {
  542. CPPUNIT_ASSERT(!filter.get());
  543. filter.next();
  544. prevPos++;
  545. }
  546. if (prevPos == pos)
  547. {
  548. CPPUNIT_ASSERT(filter.get());
  549. filter.next();
  550. prevPos++;
  551. }
  552. iter.next();
  553. }
  554. }
  555. void testQuantile()
  556. {
  557. testQuantilePos(4, 2, false, split4_2);
  558. testQuantilePos(100, 2, false, split100_2);
  559. testQuantilePos(100, 10, false, split100_10);
  560. testQuantilePos(7, 10, false, split7_10);
  561. testQuantilePos(10, 3, false, split10_3);
  562. testQuantilePos(10, 3, true, split10_3);
  563. testQuantilePos(58, 10, false, split58_10);
  564. //testQuantilePos(9, 2, true, split9_2T);
  565. testQuantilePos(9, 2, false, split9_2F);
  566. testQuantilePos(15, 3, false, split15_3);
  567. testQuantilePos(1231, 57, false, NULL);
  568. testQuantilePos(1, 63, false, NULL);
  569. testQuantilePos(10001, 17, false, NULL);
  570. }
  571. void testRandom()
  572. {
  573. //test various random combinations to ensure the results are consistent.
  574. for (unsigned i=0; i < 10; i++)
  575. testQuantilePos(random() % 1000000, random() % 10000, true, NULL);
  576. }
  577. };
  578. CPPUNIT_TEST_SUITE_REGISTRATION( JlibQuantileTest );
  579. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibQuantileTest, "JlibQuantileTest" );
  580. /* =========================================================== */
  581. class JlibReaderWriterTestTiming : public CppUnit::TestFixture
  582. {
  583. CPPUNIT_TEST_SUITE(JlibReaderWriterTestTiming);
  584. CPPUNIT_TEST(testCombinations);
  585. CPPUNIT_TEST_SUITE_END();
  586. const static unsigned spinScaling = 1000;
  587. static unsigned spinCalculation(unsigned prev, unsigned scale)
  588. {
  589. unsigned value = prev;
  590. for (unsigned i = 0; i < scale*spinScaling; i++)
  591. {
  592. value = (value * 0x1234FEDB + 0x87654321);
  593. }
  594. return value;
  595. }
  596. class Reader : public Thread
  597. {
  598. public:
  599. Reader(IRowQueue & _source, Semaphore & _doneSem, unsigned _workScale)
  600. : Thread("Reader"), source(_source), doneSem(_doneSem), workScale(_workScale), work(0)
  601. {
  602. }
  603. virtual int run()
  604. {
  605. loop
  606. {
  607. const void * next;
  608. if (!source.dequeue(next))
  609. break;
  610. if (!next)
  611. break;
  612. std::atomic<byte> * value = (std::atomic<byte> *)next;
  613. (*value)++;
  614. if (workScale)
  615. work = spinCalculation(work, workScale);
  616. }
  617. doneSem.signal();
  618. return 0;
  619. }
  620. private:
  621. IRowQueue & source;
  622. Semaphore & doneSem;
  623. volatile unsigned work;
  624. unsigned workScale;
  625. };
  626. class WriterBase : public Thread
  627. {
  628. public:
  629. WriterBase(IRowQueue & _target, size_t _len, byte * _buffer, Semaphore & _startSem, Semaphore & _doneSem, unsigned _workScale)
  630. : Thread("Writer"), target(_target), len(_len), buffer(_buffer), startSem(_startSem), doneSem(_doneSem), workScale(_workScale), work(0)
  631. {
  632. }
  633. protected:
  634. size_t len;
  635. byte * buffer;
  636. IRowQueue & target;
  637. Semaphore & startSem;
  638. Semaphore & doneSem;
  639. volatile unsigned work;
  640. unsigned workScale;
  641. };
  642. class Writer : public WriterBase
  643. {
  644. public:
  645. Writer(IRowQueue & _target, size_t _len, byte * _buffer, Semaphore & _startSem, Semaphore & _doneSem, unsigned _workScale)
  646. : WriterBase(_target, _len, _buffer, _startSem, _doneSem, _workScale)
  647. {
  648. }
  649. virtual int run()
  650. {
  651. startSem.wait();
  652. for (size_t i = 0; i < len; i++)
  653. {
  654. if (workScale)
  655. work = spinCalculation(work, workScale);
  656. target.enqueue(buffer + i);
  657. }
  658. target.noteWriterStopped();
  659. doneSem.signal();
  660. return 0;
  661. }
  662. };
  663. public:
  664. const static size_t bufferSize = 0x100000;//0x100000*64;
  665. void testQueue(IRowQueue & queue, unsigned numProducers, unsigned numConsumers, unsigned queueElements, unsigned readerWork, unsigned writerWork)
  666. {
  667. const size_t sizePerProducer = bufferSize / numProducers;
  668. const size_t testSize = sizePerProducer * numProducers;
  669. OwnedMalloc<byte> buffer(bufferSize, true);
  670. Semaphore startSem;
  671. Semaphore writerDoneSem;
  672. Semaphore stopSem;
  673. Reader * * consumers = new Reader *[numConsumers];
  674. for (unsigned i2 = 0; i2 < numConsumers; i2++)
  675. {
  676. consumers[i2] = new Reader(queue, stopSem, readerWork);
  677. consumers[i2]->start();
  678. }
  679. WriterBase * * producers = new WriterBase *[numProducers];
  680. for (unsigned i1 = 0; i1 < numProducers; i1++)
  681. {
  682. producers[i1] = new Writer(queue, sizePerProducer, buffer + i1 * sizePerProducer, startSem, writerDoneSem, writerWork);
  683. producers[i1]->start();
  684. }
  685. cycle_t startTime = get_cycles_now();
  686. //Start the writers
  687. startSem.signal(numProducers);
  688. //Wait for the writers to complete
  689. for (unsigned i7 = 0; i7 < numProducers; i7++)
  690. writerDoneSem.wait();
  691. //Wait for the readers to complete
  692. for (unsigned i3 = 0; i3 < numConsumers; i3++)
  693. stopSem.wait();
  694. cycle_t stopTime = get_cycles_now();
  695. //All bytes should have been changed to 1, if not a queue item got lost.
  696. unsigned failures = 0;
  697. unsigned numClear = 0;
  698. size_t failPos = ~(size_t)0;
  699. byte failValue = 0;
  700. for (size_t pos = 0; pos < testSize; pos++)
  701. {
  702. if (buffer[pos] != 1)
  703. {
  704. failures++;
  705. if (failPos == ~(size_t)0)
  706. {
  707. failPos = pos;
  708. failValue = buffer[pos];
  709. }
  710. }
  711. if (buffer[pos] == 0)
  712. numClear++;
  713. }
  714. unsigned timeMs = cycle_to_nanosec(stopTime - startTime) / 1000000;
  715. unsigned expectedReadWorkTime = (unsigned)(((double)unitWorkTimeMs * readerWork) / numConsumers);
  716. unsigned expectedWriteWorkTime = (unsigned)(((double)unitWorkTimeMs * writerWork) / numProducers);
  717. unsigned expectedWorkTime = std::max(expectedReadWorkTime, expectedWriteWorkTime);
  718. if (failures)
  719. {
  720. printf("Fail: Test %u producers %u consumers %u queueItems %u(%u) mismatches fail(@%u=%u)\n", numProducers, numConsumers, queueElements, failures, numClear, (unsigned)failPos, failValue);
  721. ASSERT(failures == 0);
  722. }
  723. else
  724. printf("Pass: Test %u(@%u) producers %u(@%u) consumers %u queueItems in %ums [%dms]\n", numProducers, writerWork, numConsumers, readerWork, queueElements, timeMs, timeMs-expectedWorkTime);
  725. for (unsigned i4 = 0; i4 < numConsumers; i4++)
  726. {
  727. consumers[i4]->join();
  728. consumers[i4]->Release();
  729. }
  730. delete[] consumers;
  731. for (unsigned i5 = 0; i5 < numProducers; i5++)
  732. {
  733. producers[i5]->join();
  734. producers[i5]->Release();
  735. }
  736. delete[] producers;
  737. }
  738. void testQueue(unsigned numProducers, unsigned numConsumers, unsigned numElements = 0, unsigned readWork = 0, unsigned writeWork = 0)
  739. {
  740. unsigned queueElements = (numElements != 0) ? numElements : (numProducers + numConsumers) * 2;
  741. Owned<IRowQueue> queue = createRowQueue(numConsumers, numProducers, queueElements, 0);
  742. testQueue(*queue, numProducers, numConsumers, queueElements, readWork, writeWork);
  743. }
  744. void testWorkQueue(unsigned numProducers, unsigned numConsumers, unsigned numElements)
  745. {
  746. for (unsigned readWork = 1; readWork <= 8; readWork = readWork * 2)
  747. {
  748. for (unsigned writeWork = 1; writeWork <= 8; writeWork = writeWork * 2)
  749. {
  750. testQueue(numProducers, numConsumers, numElements, readWork, writeWork);
  751. }
  752. }
  753. }
  754. void testCombinations()
  755. {
  756. // 1:1
  757. for (unsigned i=0; i < 10; i++)
  758. testQueue(1, 1, 10);
  759. //One to Many
  760. testQueue(1, 10, 5);
  761. testQueue(1, 5, 5);
  762. testQueue(1, 5, 10);
  763. testQueue(1, 127, 10);
  764. testQueue(1, 127, 127);
  765. //Many to One
  766. testQueue(10, 1, 5);
  767. testQueue(5, 1, 5);
  768. testQueue(5, 1, 10);
  769. testQueue(127, 1, 127);
  770. cycle_t startTime = get_cycles_now();
  771. volatile unsigned value = 0;
  772. for (unsigned pass = 0; pass < 10; pass++)
  773. {
  774. for (unsigned i2 = 0; i2 < bufferSize; i2++)
  775. value = spinCalculation(value, 1);
  776. }
  777. cycle_t stopTime = get_cycles_now();
  778. unitWorkTimeMs = cycle_to_nanosec(stopTime - startTime) / (1000000 * 10);
  779. printf("Work(1) takes %ums\n", unitWorkTimeMs);
  780. //How does it scale with number of queue elements?
  781. for (unsigned elem = 16; elem < 256; elem *= 2)
  782. {
  783. testQueue(16, 1, elem, 1, 1);
  784. }
  785. #if 1
  786. //Many to Many
  787. for (unsigned readWork = 1; readWork <= 8; readWork = readWork * 2)
  788. {
  789. for (unsigned writeWork = 1; writeWork <= 8; writeWork = writeWork * 2)
  790. {
  791. testQueue(1, 1, 63, readWork, writeWork);
  792. testQueue(1, 2, 63, readWork, writeWork);
  793. testQueue(1, 4, 63, readWork, writeWork);
  794. testQueue(1, 8, 63, readWork, writeWork);
  795. testQueue(1, 16, 63, readWork, writeWork);
  796. testQueue(2, 1, 63, readWork, writeWork);
  797. testQueue(4, 1, 63, readWork, writeWork);
  798. testQueue(8, 1, 63, readWork, writeWork);
  799. testQueue(16, 1, 63, readWork, writeWork);
  800. testQueue(2, 2, 63, readWork, writeWork);
  801. testQueue(4, 4, 63, readWork, writeWork);
  802. testQueue(8, 8, 63, readWork, writeWork);
  803. testQueue(16, 8, 63, readWork, writeWork);
  804. testQueue(16, 16, 63, readWork, writeWork);
  805. testQueue(32, 1, 63, readWork, writeWork);
  806. testQueue(64, 1, 63, readWork, writeWork);
  807. testQueue(1, 32, 63, readWork, writeWork);
  808. testQueue(1, 64, 63, readWork, writeWork);
  809. }
  810. }
  811. #else
  812. //Many to Many
  813. testWorkQueue(1, 1, 63);
  814. testWorkQueue(1, 2, 63);
  815. testWorkQueue(1, 4, 63);
  816. testWorkQueue(1, 8, 63);
  817. testWorkQueue(1, 16, 63);
  818. testWorkQueue(2, 1, 63);
  819. testWorkQueue(4, 1, 63);
  820. testWorkQueue(8, 1, 63);
  821. testWorkQueue(16, 1, 63);
  822. testWorkQueue(2, 2, 63);
  823. testWorkQueue(4, 4, 63);
  824. testWorkQueue(8, 8, 63);
  825. #endif
  826. testQueue(2, 2, 4);
  827. testQueue(2, 2, 8);
  828. testQueue(2, 2, 16);
  829. testQueue(2, 2, 32);
  830. testQueue(2, 2, 100);
  831. }
  832. protected:
  833. unsigned unitWorkTimeMs;
  834. };
  835. CPPUNIT_TEST_SUITE_REGISTRATION(JlibReaderWriterTestTiming);
  836. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(JlibReaderWriterTestTiming, "JlibReaderWriterTestTiming");
  837. #endif // _USE_CPPUNIT