jlibtests.cpp 36 KB

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