jlibtests.cpp 30 KB

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