123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963 |
- /*##############################################################################
- HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ############################################################################## */
- /*
- * Jlib regression tests
- *
- */
- #ifdef _USE_CPPUNIT
- #include "jsem.hpp"
- #include "jfile.hpp"
- #include "jdebug.hpp"
- #include "jset.hpp"
- #include "sockfile.hpp"
- #include "jqueue.hpp"
- #include "unittests.hpp"
- class JlibSemTest : public CppUnit::TestFixture
- {
- public:
- CPPUNIT_TEST_SUITE(JlibSemTest);
- CPPUNIT_TEST(testSetup);
- CPPUNIT_TEST(testSimple);
- CPPUNIT_TEST(testCleanup);
- CPPUNIT_TEST_SUITE_END();
- protected:
- void testSetup()
- {
- }
- void testCleanup()
- {
- }
- void testTimedAvailable(Semaphore & sem)
- {
- unsigned now = msTick();
- sem.wait(100);
- unsigned taken = msTick() - now;
- //Shouldn't cause a reschedule, definitely shouldn't wait for 100s
- ASSERT(taken < 5);
- }
- void testTimedElapsed(Semaphore & sem, unsigned time)
- {
- unsigned now = msTick();
- sem.wait(time);
- unsigned taken = msTick() - now;
- ASSERT(taken >= time && taken < 2*time);
- }
- void testSimple()
- {
- //Some very basic semaphore tests.
- Semaphore sem;
- sem.signal();
- sem.wait();
- testTimedElapsed(sem, 100);
- sem.signal();
- testTimedAvailable(sem);
- sem.reinit(2);
- sem.wait();
- testTimedAvailable(sem);
- testTimedElapsed(sem, 5);
- }
- };
- CPPUNIT_TEST_SUITE_REGISTRATION( JlibSemTest );
- CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibSemTest, "JlibSemTest" );
- /* =========================================================== */
- class JlibSetTest : public CppUnit::TestFixture
- {
- protected:
- void testBitsetHelpers()
- {
- CPPUNIT_ASSERT_EQUAL(0U, countTrailingUnsetBits(1));
- CPPUNIT_ASSERT_EQUAL(31U, countLeadingUnsetBits(1));
- CPPUNIT_ASSERT_EQUAL(1U, getMostSignificantBit(1));
- CPPUNIT_ASSERT_EQUAL(4U, countTrailingUnsetBits(0x110));
- CPPUNIT_ASSERT_EQUAL(23U, countLeadingUnsetBits(0x110));
- CPPUNIT_ASSERT_EQUAL(9U, getMostSignificantBit(0x110));
- CPPUNIT_ASSERT_EQUAL(0U, countTrailingUnsetBits(0xFFFFFFFFU));
- CPPUNIT_ASSERT_EQUAL(0U, countLeadingUnsetBits(0xFFFFFFFFU));
- CPPUNIT_ASSERT_EQUAL(32U, getMostSignificantBit(0xFFFFFFFFU));
- }
- void testSet1(bool initial, IBitSet *bs, unsigned start, unsigned numBits, bool setValue, bool clearValue)
- {
- unsigned end = start+numBits;
- if (initial)
- bs->incl(start, end-1);
- for (unsigned i=start; i < end; i++)
- {
- ASSERT(bs->test(i) == clearValue);
- bs->set(i, setValue);
- ASSERT(bs->test(i) == setValue);
- bs->set(i+5, setValue);
- ASSERT(bs->scan(0, setValue) == i);
- ASSERT(bs->scan(i+1, setValue) == i+5);
- bs->set(i, clearValue);
- bs->set(i+5, clearValue);
- //Clearing i+5 above may extend the set - so need to calculate the end carefully
- unsigned last = i+5 < end ? end : i + 6;
- unsigned match1 = bs->scan(0, setValue);
- CPPUNIT_ASSERT_EQUAL((unsigned)(initial ? last : -1), match1);
- bs->invert(i);
- ASSERT(bs->test(i) == setValue);
- bs->invert(i);
- ASSERT(bs->test(i) == clearValue);
- bool wasSet = bs->testSet(i, setValue);
- ASSERT(wasSet == clearValue);
- bool wasSet2 = bs->testSet(i, clearValue);
- ASSERT(wasSet2 == setValue);
- ASSERT(bs->test(i) == clearValue);
- bs->set(i, setValue);
- unsigned match = bs->scanInvert(0, setValue);
- ASSERT(match == i);
- ASSERT(bs->test(i) == clearValue);
- }
- bs->reset();
- if (initial)
- {
- bs->incl(start, end);
- bs->excl(start+5, end-5);
- }
- else
- bs->incl(start+5, end-5);
- unsigned inclStart = bs->scan(start, setValue);
- ASSERT((start+5) == inclStart);
- unsigned inclEnd = bs->scan(start+5, clearValue);
- ASSERT((end-5) == (inclEnd-1));
- }
- void testSet(bool initial, unsigned passes, bool timed)
- {
- unsigned now = msTick();
- bool setValue = !initial;
- bool clearValue = initial;
- const unsigned numBits = 400;
- for (unsigned pass=0; pass < passes; pass++)
- {
- Owned<IBitSet> bs = createThreadSafeBitSet();
- testSet1(initial, bs, 0, numBits, setValue, clearValue);
- }
- if (timed)
- {
- unsigned elapsed = msTick()-now;
- DBGLOG("Bit test (%u) %d passes time taken = %dms", initial, passes, elapsed);
- }
- now = msTick();
- for (unsigned pass=0; pass < passes; pass++)
- {
- Owned<IBitSet> bs = createBitSet();
- testSet1(initial, bs, 0, numBits, setValue, clearValue);
- }
- if (timed)
- {
- unsigned elapsed = msTick()-now;
- DBGLOG("Bit test [thread-unsafe version] (%u) %d passes time taken = %dms", initial, passes, elapsed);
- }
- now = msTick();
- size32_t bitSetMemSz = getBitSetMemoryRequirement(numBits+5);
- MemoryBuffer mb;
- void *mem = mb.reserveTruncate(bitSetMemSz);
- for (unsigned pass=0; pass < passes; pass++)
- {
- Owned<IBitSet> bs = createBitSet(bitSetMemSz, mem);
- testSet1(initial, bs, 0, numBits, setValue, clearValue);
- }
- if (timed)
- {
- unsigned elapsed = msTick()-now;
- DBGLOG("Bit test [thread-unsafe version, fixed memory] (%u) %d passes time taken = %dms\n", initial, passes, elapsed);
- }
- }
- };
- class JlibSetTestQuick : public JlibSetTest
- {
- public:
- CPPUNIT_TEST_SUITE(JlibSetTestQuick);
- CPPUNIT_TEST(testBitsetHelpers);
- CPPUNIT_TEST(testSimple);
- CPPUNIT_TEST_SUITE_END();
- void testSimple()
- {
- testSet(false, 100, false);
- testSet(true, 100, false);
- }
- };
- class JlibSetTestStress : public JlibSetTest
- {
- public:
- CPPUNIT_TEST_SUITE(JlibSetTestStress);
- CPPUNIT_TEST(testParallel);
- CPPUNIT_TEST(testSimple);
- CPPUNIT_TEST_SUITE_END();
- void testSimple()
- {
- testSet(false, 10000, true);
- testSet(true, 10000, true);
- }
- protected:
- class CBitThread : public CSimpleInterfaceOf<IInterface>, implements IThreaded
- {
- IBitSet &bitSet;
- unsigned startBit, numBits;
- bool initial, setValue, clearValue;
- CThreaded threaded;
- Owned<IException> exception;
- CppUnit::Exception *cppunitException;
- public:
- CBitThread(IBitSet &_bitSet, unsigned _startBit, unsigned _numBits, bool _initial)
- : threaded("CBitThread", this), bitSet(_bitSet), startBit(_startBit), numBits(_numBits), initial(_initial)
- {
- cppunitException = NULL;
- setValue = !initial;
- clearValue = initial;
- }
- void start() { threaded.start(); }
- void join()
- {
- threaded.join();
- if (exception)
- throw exception.getClear();
- else if (cppunitException)
- throw cppunitException;
- }
- virtual void main()
- {
- try
- {
- unsigned endBit = startBit+numBits-1;
- if (initial)
- bitSet.incl(startBit, endBit);
- for (unsigned i=startBit; i < endBit; i++)
- {
- ASSERT(bitSet.test(i) == clearValue);
- bitSet.set(i, setValue);
- ASSERT(bitSet.test(i) == setValue);
- if (i < (endBit-1))
- ASSERT(bitSet.scan(i, clearValue) == i+1); // find next unset (should be i+1)
- bitSet.set(i, clearValue);
- bitSet.invert(i);
- ASSERT(bitSet.test(i) == setValue);
- bitSet.invert(i);
- ASSERT(bitSet.test(i) == clearValue);
- bool wasSet = bitSet.testSet(i, setValue);
- ASSERT(wasSet == clearValue);
- bool wasSet2 = bitSet.testSet(i, clearValue);
- ASSERT(wasSet2 == setValue);
- ASSERT(bitSet.test(i) == clearValue);
- bitSet.set(i, setValue);
- unsigned match = bitSet.scanInvert(startBit, setValue);
- ASSERT(match == i);
- ASSERT(bitSet.test(i) == clearValue);
- }
- }
- catch (IException *e)
- {
- exception.setown(e);
- }
- catch (CppUnit::Exception &e)
- {
- cppunitException = e.clone();
- }
- }
- };
- unsigned testParallelRun(IBitSet &bitSet, unsigned nThreads, unsigned bitsPerThread, bool initial)
- {
- IArrayOf<CBitThread> bitThreads;
- unsigned bitStart = 0;
- unsigned bitEnd = 0;
- for (unsigned t=0; t<nThreads; t++)
- {
- bitThreads.append(* new CBitThread(bitSet, bitStart, bitsPerThread, initial));
- bitStart += bitsPerThread;
- }
- unsigned now = msTick();
- for (unsigned t=0; t<nThreads; t++)
- bitThreads.item(t).start();
- Owned<IException> exception;
- CppUnit::Exception *cppunitException = NULL;
- for (unsigned t=0; t<nThreads; t++)
- {
- try
- {
- bitThreads.item(t).join();
- }
- catch (IException *e)
- {
- EXCLOG(e, NULL);
- if (!exception)
- exception.setown(e);
- else
- e->Release();
- }
- catch (CppUnit::Exception *e)
- {
- cppunitException = e;
- }
- }
- if (exception)
- throw exception.getClear();
- else if (cppunitException)
- throw *cppunitException;
- return msTick()-now;
- }
- void testSetParallel(bool initial)
- {
- unsigned numBits = 1000000; // 10M
- unsigned nThreads = getAffinityCpus();
- unsigned bitsPerThread = numBits/nThreads;
- bitsPerThread = ((bitsPerThread + (BitsPerItem-1)) / BitsPerItem) * BitsPerItem; // round up to multiple of BitsPerItem
- numBits = bitsPerThread*nThreads; // round
- fprintf(stdout, "testSetParallel, testing bit set of size : %d, nThreads=%d\n", numBits, nThreads);
- Owned<IBitSet> bitSet = createThreadSafeBitSet();
- unsigned took = testParallelRun(*bitSet, nThreads, bitsPerThread, initial);
- fprintf(stdout, "Thread safe parallel bit set test (%u) time taken = %dms\n", initial, took);
- size32_t bitSetMemSz = getBitSetMemoryRequirement(numBits);
- MemoryBuffer mb;
- void *mem = mb.reserveTruncate(bitSetMemSz);
- bitSet.setown(createBitSet(bitSetMemSz, mem));
- took = testParallelRun(*bitSet, nThreads, bitsPerThread, initial);
- fprintf(stdout, "Thread unsafe parallel bit set test (%u) time taken = %dms\n", initial, took);
- }
- void testParallel()
- {
- testSetParallel(false);
- testSetParallel(true);
- }
- };
- CPPUNIT_TEST_SUITE_REGISTRATION( JlibSetTestQuick );
- CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibSetTestQuick, "JlibSetTestQuick" );
- CPPUNIT_TEST_SUITE_REGISTRATION( JlibSetTestStress );
- CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibSetTestStress, "JlibSetTestStress" );
- /* =========================================================== */
- class JlibFileIOTestTiming : public CppUnit::TestFixture
- {
- protected:
- unsigned rs, nr10pct, nr150pct;
- char *record;
- StringBuffer tmpfile;
- CPPUNIT_TEST_SUITE( JlibFileIOTestTiming );
- CPPUNIT_TEST(testIOSmall);
- CPPUNIT_TEST(testIOLarge);
- CPPUNIT_TEST_SUITE_END();
- public:
- JlibFileIOTestTiming()
- {
- HardwareInfo hdwInfo;
- getHardwareInfo(hdwInfo);
- rs = 65536;
- unsigned nr = (unsigned)(1024.0 * (1024.0 * (double)hdwInfo.totalMemory / (double)rs));
- nr10pct = nr / 10;
- nr150pct = (unsigned)((double)nr * 1.5);
- record = (char *)malloc(rs);
- for (unsigned i=0;i<rs;i++)
- record[i] = 'a';
- record[rs-1] = '\n';
- tmpfile.set("JlibFileIOTest.txt");
- }
- ~JlibFileIOTestTiming()
- {
- free(record);
- }
- protected:
- void testIO(unsigned nr, const char *server)
- {
- IFile *ifile;
- IFileIO *ifileio;
- unsigned fsize = (unsigned)(((double)nr * (double)rs) / (1024.0 * 1024.0));
- fflush(NULL);
- fprintf(stdout,"\n");
- fflush(NULL);
- for(int j=0; j<2; j++)
- {
- if (j==0)
- fprintf(stdout, "File size: %d (MB) Cache, ", fsize);
- else
- fprintf(stdout, "\nFile size: %d (MB) Nocache, ", fsize);
- if (server != NULL)
- {
- SocketEndpoint ep;
- ep.set(server, 7100);
- ifile = createRemoteFile(ep, tmpfile);
- fprintf(stdout, "Remote: (%s)\n", server);
- }
- else
- {
- ifile = createIFile(tmpfile);
- fprintf(stdout, "Local:\n");
- }
- ifile->remove();
- unsigned st = msTick();
- IFEflags extraFlags = IFEcache;
- if (j==1)
- extraFlags = IFEnocache;
- ifileio = ifile->open(IFOcreate, extraFlags);
- try
- {
- ifile->setFilePermissions(0666);
- }
- catch (...)
- {
- fprintf(stdout, "ifile->setFilePermissions() exception\n");
- }
- unsigned iter = nr / 40;
- __int64 pos = 0;
- for (unsigned i=0;i<nr;i++)
- {
- ifileio->write(pos, rs, record);
- pos += rs;
- if ((i % iter) == 0)
- {
- fprintf(stdout,".");
- fflush(NULL);
- }
- }
- ifileio->close();
- double rsec = (double)(msTick() - st)/1000.0;
- unsigned iorate = (unsigned)((double)fsize / rsec);
- fprintf(stdout, "\nwrite - elapsed time = %6.2f (s) iorate = %4d (MB/s)\n", rsec, iorate);
- st = msTick();
- extraFlags = IFEcache;
- if (j==1)
- extraFlags = IFEnocache;
- ifileio = ifile->open(IFOread, extraFlags);
- pos = 0;
- for (unsigned i=0;i<nr;i++)
- {
- ifileio->read(pos, rs, record);
- pos += rs;
- if ((i % iter) == 0)
- {
- fprintf(stdout,".");
- fflush(NULL);
- }
- }
- ifileio->close();
- rsec = (double)(msTick() - st)/1000.0;
- iorate = (unsigned)((double)fsize / rsec);
- fprintf(stdout, "\nread -- elapsed time = %6.2f (s) iorate = %4d (MB/s)\n", rsec, iorate);
- ifileio->Release();
- ifile->remove();
- ifile->Release();
- }
- }
- void testIOSmall()
- {
- testIO(nr10pct, NULL);
- }
- void testIOLarge()
- {
- testIO(nr150pct, NULL);
- }
- };
- class JlibFileIOTestStress : public JlibFileIOTestTiming
- {
- protected:
- CPPUNIT_TEST_SUITE( JlibFileIOTestStress );
- CPPUNIT_TEST(testIORemote);
- CPPUNIT_TEST_SUITE_END();
- void testIORemote()
- {
- const char * server = ".";
- testIO(nr10pct, server);
- }
- };
- CPPUNIT_TEST_SUITE_REGISTRATION( JlibFileIOTestTiming );
- CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibFileIOTestTiming, "JlibFileIOTestTiming" );
- CPPUNIT_TEST_SUITE_REGISTRATION( JlibFileIOTestStress );
- CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibFileIOTestTiming, "JlibFileIOTestStress" );
- /* =========================================================== */
- class JlibStringBufferTiming : public CppUnit::TestFixture
- {
- CPPUNIT_TEST_SUITE( JlibStringBufferTiming );
- CPPUNIT_TEST(testSwap);
- CPPUNIT_TEST_SUITE_END();
- public:
- void testSwap()
- {
- StringBuffer l;
- StringBuffer r;
- for (unsigned len=0; len<40; len++)
- {
- const unsigned numIter = 100000000;
- cycle_t start = get_cycles_now();
- for (unsigned pass=0; pass < numIter; pass++)
- {
- l.swapWith(r);
- }
- cycle_t elapsed = get_cycles_now() - start;
- DBGLOG("Each iteration of size %u took %.2f nanoseconds", len, (double)cycle_to_nanosec(elapsed) / numIter);
- l.append("a");
- r.append("b");
- }
- }
- };
- CPPUNIT_TEST_SUITE_REGISTRATION( JlibStringBufferTiming );
- CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibStringBufferTiming, "JlibStringBufferTiming" );
- /* =========================================================== */
- static const unsigned split4_2[] = {0, 2, 4 };
- static const unsigned split100_2[] = {0, 50, 100 };
- static const unsigned split100_10[] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
- static const unsigned split7_10[] = {0,1,1,2,3,3,4,5,6,6,7 };
- static const unsigned split10_3[] = {0,3,7,10 };
- static const unsigned split58_10[] = {0,6,12,17,23,29,35,41,46,52,58 };
- static const unsigned split9_2T[] = { 0,5,9 };
- static const unsigned split9_2F[] = { 0,4,9 };
- static const unsigned split15_3[] = { 0,5,10,15 };
- class JlibQuantileTest : public CppUnit::TestFixture
- {
- CPPUNIT_TEST_SUITE( JlibQuantileTest );
- CPPUNIT_TEST(testQuantile);
- CPPUNIT_TEST(testRandom);
- CPPUNIT_TEST_SUITE_END();
- public:
- JlibQuantileTest()
- {
- }
- void testQuantilePos(unsigned numItems, unsigned numDivisions, bool roundUp, const unsigned * expected)
- {
- if (numDivisions == 0)
- return;
- QuantilePositionIterator iter(numItems, numDivisions, roundUp);
- QuantileFilterIterator filter(numItems, numDivisions, roundUp);
- unsigned prevPos = 0;
- iter.first();
- for (unsigned i=0; i <= numDivisions; i++)
- {
- //Check the values from the quantile iterator match those that are expected
- unsigned pos = (unsigned)iter.get();
- #if 0
- printf("(%d,%d) %d=%d\n", numItems, numDivisions, i, pos);
- #endif
- if (expected)
- CPPUNIT_ASSERT_EQUAL(expected[i], pos);
- //Check that the quantile filter correctly returns true and false for subsequent calls.
- while (prevPos < pos)
- {
- CPPUNIT_ASSERT(!filter.get());
- filter.next();
- prevPos++;
- }
- if (prevPos == pos)
- {
- CPPUNIT_ASSERT(filter.get());
- filter.next();
- prevPos++;
- }
- iter.next();
- }
- }
- void testQuantile()
- {
- testQuantilePos(4, 2, false, split4_2);
- testQuantilePos(100, 2, false, split100_2);
- testQuantilePos(100, 10, false, split100_10);
- testQuantilePos(7, 10, false, split7_10);
- testQuantilePos(10, 3, false, split10_3);
- testQuantilePos(10, 3, true, split10_3);
- testQuantilePos(58, 10, false, split58_10);
- //testQuantilePos(9, 2, true, split9_2T);
- testQuantilePos(9, 2, false, split9_2F);
- testQuantilePos(15, 3, false, split15_3);
- testQuantilePos(1231, 57, false, NULL);
- testQuantilePos(1, 63, false, NULL);
- testQuantilePos(10001, 17, false, NULL);
- }
- void testRandom()
- {
- //test various random combinations to ensure the results are consistent.
- for (unsigned i=0; i < 10; i++)
- testQuantilePos(random() % 1000000, random() % 10000, true, NULL);
- }
- };
- CPPUNIT_TEST_SUITE_REGISTRATION( JlibQuantileTest );
- CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibQuantileTest, "JlibQuantileTest" );
- /* =========================================================== */
- class JlibReaderWriterTestTiming : public CppUnit::TestFixture
- {
- CPPUNIT_TEST_SUITE(JlibReaderWriterTestTiming);
- CPPUNIT_TEST(testCombinations);
- CPPUNIT_TEST_SUITE_END();
- const static unsigned spinScaling = 1000;
- static unsigned spinCalculation(unsigned prev, unsigned scale)
- {
- unsigned value = prev;
- for (unsigned i = 0; i < scale*spinScaling; i++)
- {
- value = (value * 0x1234FEDB + 0x87654321);
- }
- return value;
- }
- class Reader : public Thread
- {
- public:
- Reader(IRowQueue & _source, Semaphore & _doneSem, unsigned _workScale)
- : Thread("Reader"), source(_source), doneSem(_doneSem), workScale(_workScale), work(0)
- {
- }
- virtual int run()
- {
- loop
- {
- const void * next;
- if (!source.dequeue(next))
- break;
- if (!next)
- break;
- std::atomic<byte> * value = (std::atomic<byte> *)next;
- (*value)++;
- if (workScale)
- work = spinCalculation(work, workScale);
- }
- doneSem.signal();
- return 0;
- }
- private:
- IRowQueue & source;
- Semaphore & doneSem;
- volatile unsigned work;
- unsigned workScale;
- };
- class WriterBase : public Thread
- {
- public:
- WriterBase(IRowQueue & _target, size_t _len, byte * _buffer, Semaphore & _startSem, Semaphore & _doneSem, unsigned _workScale)
- : Thread("Writer"), target(_target), len(_len), buffer(_buffer), startSem(_startSem), doneSem(_doneSem), workScale(_workScale), work(0)
- {
- }
- protected:
- size_t len;
- byte * buffer;
- IRowQueue & target;
- Semaphore & startSem;
- Semaphore & doneSem;
- volatile unsigned work;
- unsigned workScale;
- };
- class Writer : public WriterBase
- {
- public:
- Writer(IRowQueue & _target, size_t _len, byte * _buffer, Semaphore & _startSem, Semaphore & _doneSem, unsigned _workScale)
- : WriterBase(_target, _len, _buffer, _startSem, _doneSem, _workScale)
- {
- }
- virtual int run()
- {
- startSem.wait();
- for (size_t i = 0; i < len; i++)
- {
- if (workScale)
- work = spinCalculation(work, workScale);
- target.enqueue(buffer + i);
- }
- target.noteWriterStopped();
- doneSem.signal();
- return 0;
- }
- };
- public:
- const static size_t bufferSize = 0x100000;//0x100000*64;
- void testQueue(IRowQueue & queue, unsigned numProducers, unsigned numConsumers, unsigned queueElements, unsigned readerWork, unsigned writerWork)
- {
- const size_t sizePerProducer = bufferSize / numProducers;
- const size_t testSize = sizePerProducer * numProducers;
- OwnedMalloc<byte> buffer(bufferSize, true);
- Semaphore startSem;
- Semaphore writerDoneSem;
- Semaphore stopSem;
- Reader * * consumers = new Reader *[numConsumers];
- for (unsigned i2 = 0; i2 < numConsumers; i2++)
- {
- consumers[i2] = new Reader(queue, stopSem, readerWork);
- consumers[i2]->start();
- }
- WriterBase * * producers = new WriterBase *[numProducers];
- for (unsigned i1 = 0; i1 < numProducers; i1++)
- {
- producers[i1] = new Writer(queue, sizePerProducer, buffer + i1 * sizePerProducer, startSem, writerDoneSem, writerWork);
- producers[i1]->start();
- }
- cycle_t startTime = get_cycles_now();
- //Start the writers
- startSem.signal(numProducers);
- //Wait for the writers to complete
- for (unsigned i7 = 0; i7 < numProducers; i7++)
- writerDoneSem.wait();
- //Wait for the readers to complete
- for (unsigned i3 = 0; i3 < numConsumers; i3++)
- stopSem.wait();
- cycle_t stopTime = get_cycles_now();
- //All bytes should have been changed to 1, if not a queue item got lost.
- unsigned failures = 0;
- unsigned numClear = 0;
- size_t failPos = ~(size_t)0;
- byte failValue = 0;
- for (size_t pos = 0; pos < testSize; pos++)
- {
- if (buffer[pos] != 1)
- {
- failures++;
- if (failPos == ~(size_t)0)
- {
- failPos = pos;
- failValue = buffer[pos];
- }
- }
- if (buffer[pos] == 0)
- numClear++;
- }
- unsigned timeMs = cycle_to_nanosec(stopTime - startTime) / 1000000;
- unsigned expectedReadWorkTime = (unsigned)(((double)unitWorkTimeMs * readerWork) / numConsumers);
- unsigned expectedWriteWorkTime = (unsigned)(((double)unitWorkTimeMs * writerWork) / numProducers);
- unsigned expectedWorkTime = std::max(expectedReadWorkTime, expectedWriteWorkTime);
- if (failures)
- {
- printf("Fail: Test %u producers %u consumers %u queueItems %u(%u) mismatches fail(@%u=%u)\n", numProducers, numConsumers, queueElements, failures, numClear, (unsigned)failPos, failValue);
- ASSERT(failures == 0);
- }
- else
- printf("Pass: Test %u(@%u) producers %u(@%u) consumers %u queueItems in %ums [%dms]\n", numProducers, writerWork, numConsumers, readerWork, queueElements, timeMs, timeMs-expectedWorkTime);
- for (unsigned i4 = 0; i4 < numConsumers; i4++)
- {
- consumers[i4]->join();
- consumers[i4]->Release();
- }
- delete[] consumers;
- for (unsigned i5 = 0; i5 < numProducers; i5++)
- {
- producers[i5]->join();
- producers[i5]->Release();
- }
- delete[] producers;
- }
- void testQueue(unsigned numProducers, unsigned numConsumers, unsigned numElements = 0, unsigned readWork = 0, unsigned writeWork = 0)
- {
- unsigned queueElements = (numElements != 0) ? numElements : (numProducers + numConsumers) * 2;
- Owned<IRowQueue> queue = createRowQueue(numConsumers, numProducers, queueElements, 0);
- testQueue(*queue, numProducers, numConsumers, queueElements, readWork, writeWork);
- }
- void testWorkQueue(unsigned numProducers, unsigned numConsumers, unsigned numElements)
- {
- for (unsigned readWork = 1; readWork <= 8; readWork = readWork * 2)
- {
- for (unsigned writeWork = 1; writeWork <= 8; writeWork = writeWork * 2)
- {
- testQueue(numProducers, numConsumers, numElements, readWork, writeWork);
- }
- }
- }
- void testCombinations()
- {
- // 1:1
- for (unsigned i=0; i < 10; i++)
- testQueue(1, 1, 10);
- //One to Many
- testQueue(1, 10, 5);
- testQueue(1, 5, 5);
- testQueue(1, 5, 10);
- testQueue(1, 127, 10);
- testQueue(1, 127, 127);
- //Many to One
- testQueue(10, 1, 5);
- testQueue(5, 1, 5);
- testQueue(5, 1, 10);
- testQueue(127, 1, 127);
- cycle_t startTime = get_cycles_now();
- volatile unsigned value = 0;
- for (unsigned pass = 0; pass < 10; pass++)
- {
- for (unsigned i2 = 0; i2 < bufferSize; i2++)
- value = spinCalculation(value, 1);
- }
- cycle_t stopTime = get_cycles_now();
- unitWorkTimeMs = cycle_to_nanosec(stopTime - startTime) / (1000000 * 10);
- printf("Work(1) takes %ums\n", unitWorkTimeMs);
- //How does it scale with number of queue elements?
- for (unsigned elem = 16; elem < 256; elem *= 2)
- {
- testQueue(16, 1, elem, 1, 1);
- }
- #if 1
- //Many to Many
- for (unsigned readWork = 1; readWork <= 8; readWork = readWork * 2)
- {
- for (unsigned writeWork = 1; writeWork <= 8; writeWork = writeWork * 2)
- {
- testQueue(1, 1, 63, readWork, writeWork);
- testQueue(1, 2, 63, readWork, writeWork);
- testQueue(1, 4, 63, readWork, writeWork);
- testQueue(1, 8, 63, readWork, writeWork);
- testQueue(1, 16, 63, readWork, writeWork);
- testQueue(2, 1, 63, readWork, writeWork);
- testQueue(4, 1, 63, readWork, writeWork);
- testQueue(8, 1, 63, readWork, writeWork);
- testQueue(16, 1, 63, readWork, writeWork);
- testQueue(2, 2, 63, readWork, writeWork);
- testQueue(4, 4, 63, readWork, writeWork);
- testQueue(8, 8, 63, readWork, writeWork);
- testQueue(16, 8, 63, readWork, writeWork);
- testQueue(16, 16, 63, readWork, writeWork);
- testQueue(32, 1, 63, readWork, writeWork);
- testQueue(64, 1, 63, readWork, writeWork);
- testQueue(1, 32, 63, readWork, writeWork);
- testQueue(1, 64, 63, readWork, writeWork);
- }
- }
- #else
- //Many to Many
- testWorkQueue(1, 1, 63);
- testWorkQueue(1, 2, 63);
- testWorkQueue(1, 4, 63);
- testWorkQueue(1, 8, 63);
- testWorkQueue(1, 16, 63);
- testWorkQueue(2, 1, 63);
- testWorkQueue(4, 1, 63);
- testWorkQueue(8, 1, 63);
- testWorkQueue(16, 1, 63);
- testWorkQueue(2, 2, 63);
- testWorkQueue(4, 4, 63);
- testWorkQueue(8, 8, 63);
- #endif
- testQueue(2, 2, 4);
- testQueue(2, 2, 8);
- testQueue(2, 2, 16);
- testQueue(2, 2, 32);
- testQueue(2, 2, 100);
- }
- protected:
- unsigned unitWorkTimeMs;
- };
- CPPUNIT_TEST_SUITE_REGISTRATION(JlibReaderWriterTestTiming);
- CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(JlibReaderWriterTestTiming, "JlibReaderWriterTestTiming");
- #endif // _USE_CPPUNIT
|