📄 ndbportlibtest.cpp
字号:
// Check tick functions //#if 0 int sleeptimes[] = {78, 12, 199, 567, 899}; for (int i = 0; i < 5; i++) { ndbout << "*------------------------------- Measure" << i << endl; NDB_TICKS millisec_now; NDB_TICKS millisec_now2; millisec_now = NdbTick_CurrentMillisecond(); NdbSleep_MilliSleep(sleeptimes[i]); millisec_now2 = NdbTick_CurrentMillisecond(); ndbout << " Time before sleep = " << millisec_now << endl; ndbout << " Time after sleep = " << millisec_now2 << endl; ndbout << " Tried to sleep "<<sleeptimes[i]<<" milliseconds." << endl; ndbout << " Sleep time was " << millisec_now2 -millisec_now <<" milliseconds." << endl; } ndbout << "TEST4 completed" << endl; ndbout << "= TEST5 ===============================" << endl; // Check NdbOut ndbout << "Testing hex and dec functions of NdbOut" << endl; for (int i = 0; i<= 0xFF; i++) { ndbout << i << "=" <<hex << i << "="<<dec << i << ", "; } ndbout << endl<< "Testing that hex is reset to dec by endl" << endl; ndbout << hex << 67 << endl; ndbout << 67 << endl; ndbout << "TEST5 completed" << endl; ndbout << "= TEST6 ===============================" << endl; const char* theEnvHostNamePtr; char buf[255]; char theHostHostName[256]; theEnvHostNamePtr = NdbEnv_GetEnv("HOSTNAME", buf, 255); if(theEnvHostNamePtr == NULL) fail("TEST6", "Could not get HOSTNAME from env"); else{ ndbout << "HOSTNAME from GetEnv" << theEnvHostNamePtr << endl; NdbHost_GetHostName(theHostHostName); ndbout << "HOSTNAME from GetHostName" <<theHostHostName << endl; if (strcmp(theEnvHostNamePtr, theHostHostName) != 0) fail("TEST6", "NdbHost_GetHostName or NdbEnv_GetEnv failed"); } ndbout << "= TEST7 ===============================" << endl; testmutex = NdbMutex_Create(); testcond = NdbCondition_Create(); testthreadsdone = 0; for (int i = 0; i < T3_THREADS; i++) { t3args[i] = i; t3threads[i] = NdbThread_Create(testfunc, // Function (void**)&t3args[i],// Arg 2048, // Stacksize (char*)"test7thread", // Thread name NDB_THREAD_PRIO_MEAN); // Thread priority } ndbout << "All threads created" << endl; if (NdbMutex_Lock(testmutex) != 0) fail("TEST7", "NdbMutex_Lock failed"); while (testthreadsdone < T3_THREADS*10) { // just testing the functionality without timing out, therefor 20 sec. if(NdbCondition_WaitTimeout(testcond, testmutex, 20000) != 0) fail("TEST7", "NdbCondition_WaitTimeout failed"); ndbout << "Condition signaled, there are " << testthreadsdone << " completed threads" << endl; } if (NdbMutex_Unlock(testmutex) != 0) fail("TEST7", "NdbMutex_Unlock failed"); for (int i = 0; i < T3_THREADS; i++) { if (NdbThread_WaitFor(t3threads[i], &status3) != 0) fail("TEST7", "NdbThread_WaitFor failed"); NdbThread_Destroy(&t3threads[i]); } NdbMutex_Destroy(testmutex); NdbCondition_Destroy(testcond); ndbout << "TEST7 completed" << endl; ndbout << "= TEST8 ===============================" << endl; ndbout << " NdbCondition_WaitTimeout" << endl; testmutex = NdbMutex_Create(); testcond = NdbCondition_Create(); for (int i = 0; i < 5; i++) { ndbout << "*------------------------------- Measure" << i << endl; NDB_TICKS millisec_now; NDB_TICKS millisec_now2; millisec_now = NdbTick_CurrentMillisecond(); if (NdbCondition_WaitTimeout(testcond, testmutex, sleeptimes[i]) != 0) fail("TEST8", "NdbCondition_WaitTimeout failed"); millisec_now2 = NdbTick_CurrentMillisecond(); ndbout << " Time before WaitTimeout = " << millisec_now << endl; ndbout << " Time after WaitTimeout = " << millisec_now2 << endl; ndbout << " Tried to wait "<<sleeptimes[i]<<" milliseconds." << endl; ndbout << " Wait time was " << millisec_now2 -millisec_now <<" milliseconds." << endl; } ndbout << "TEST8 completed" << endl; ndbout << "= TEST9 ===============================" << endl; ndbout << " NdbTick_CurrentXXXXXsecond compare" << endl; for (int i = 0; i < 5; i++) { ndbout << "*------------------------------- Measure" << i << endl; NDB_TICKS millisec_now; NDB_TICKS millisec_now2; Uint32 usec_now, usec_now2; Uint64 msec_now, msec_now2; millisec_now = NdbTick_CurrentMillisecond(); NdbTick_CurrentMicrosecond( &msec_now, &usec_now); NdbSleep_MilliSleep(sleeptimes[i]); millisec_now2 = NdbTick_CurrentMillisecond(); NdbTick_CurrentMicrosecond( &msec_now2, &usec_now2); Uint64 usecdiff = time_diff(msec_now,msec_now2,usec_now,usec_now2); NDB_TICKS msecdiff = millisec_now2 -millisec_now; ndbout << " Slept "<<sleeptimes[i]<<" milliseconds." << endl; ndbout << " Measured " << msecdiff <<" milliseconds with milli function ." << endl; ndbout << " Measured " << usecdiff/1000 << "," << usecdiff%1000<<" milliseconds with micro function ." << endl; } ndbout << "TEST9 completed" << endl; const int iter = 20; ndbout << "Testing microsecond timer - " << iter << " iterations" << endl; testMicros(iter); ndbout << "Testing microsecond timer - COMPLETED" << endl;#if defined NDB_OSE || defined NDB_SOFTOSE ndbout << "system_tick() = " << system_tick() << " us per tick" << endl;#endif ndbout << "= TEST10 ===============================" << endl; testmutex = NdbMutex_Create(); testcond = NdbCondition_Create(); testthreadsdone = 0; for (int i = 0; i < T3_THREADS; i++) { t3args[i] = i; t3threads[i] = NdbThread_Create(testTryLockfunc, // Function (void**)&t3args[i],// Arg 2048, // Stacksize (char*)"test10thread", // Thread name NDB_THREAD_PRIO_MEAN); // Thread priority } ndbout << "All threads created" << endl; if (NdbMutex_Lock(testmutex) != 0) fail("TEST10", "NdbMutex_Lock failed"); while (testthreadsdone < T3_THREADS*10) { if(NdbCondition_Wait(testcond, testmutex) != 0) fail("TEST10", "NdbCondition_WaitTimeout failed"); ndbout << "Condition signaled, there are " << testthreadsdone << " completed threads" << endl; } if (NdbMutex_Unlock(testmutex) != 0) fail("TEST10", "NdbMutex_Unlock failed"); for (int i = 0; i < T3_THREADS; i++) { if (NdbThread_WaitFor(t3threads[i], &status3) != 0) fail("TEST10", "NdbThread_WaitFor failed"); NdbThread_Destroy(&t3threads[i]); } NdbMutex_Destroy(testmutex); NdbCondition_Destroy(testcond); ndbout << "TEST10 completed" << endl; // Check total status of test if (TestHasFailed == 1) ndbout << endl << "TEST FAILED!" << endl; else ndbout << endl << "TEST PASSED!" << endl; return TestHasFailed;};Uint64 time_diff(Uint64 s1, Uint64 s2, Uint32 m1, Uint32 m2){ Uint64 diff = 0; diff += (s2 - s1) * 1000000; if(m2 >= m1) diff += (m2 - m1); else { diff += m2; diff -= m1; } // if(0) // ndbout("(s1,m1) = (%d, %d) (s2,m2) = (%d, %d) -> diff = %d\n", // (Uint32)s1,m1,(Uint32)s2,m2, (Uint32)diff); return diff;};void testMicros(int count){ Uint32 avg = 0; Uint32 sum2 = 0; for(int i = 0; i<count; i++){ Uint64 s1, s2; Uint32 m1, m2; if(NdbTick_CurrentMicrosecond(&s1, &m1) != 0){ ndbout << "Failed to get current micro" << endl; TestHasFailed = 1; return; } Uint32 r = (rand() % 1000) + 1; NdbSleep_MilliSleep(r); if(NdbTick_CurrentMicrosecond(&s2, &m2) != 0){ ndbout << "Failed to get current micro" << endl; TestHasFailed = 1; return; } Uint64 m = time_diff(s1,s2,m1,m2); if(verbose) ndbout << "Slept for " << r << " ms" << " - Measured " << m << " us" << endl; if(m > (r*1000)){ avg += (m - (r*1000)); sum2 += (m - (r*1000)) * (m - (r*1000)); } else { avg += ((r*1000) - m); sum2 += ((r*1000) - m) * ((r*1000) - m); }#if 0 m /= 1000; if(m > r && ((m - r) > 10)){ ndbout << "Difference to big: " << (m - r) << " - Test failed" << endl; TestHasFailed = 1; } if(m < r && ((r - m) > 10)){ ndbout << "Difference to big: " << (r - m) << " - Test failed" << endl; TestHasFailed = 1; }#endif } Uint32 dev = (avg * avg - sum2) / count; dev /= count; avg /= count; Uint32 t = 0; while((t*t)<dev) t++; ndbout << "NOTE - measure are compared to NdbSleep_MilliSleep(...)" << endl; ndbout << "Average error = " << avg << " us" << endl; ndbout << "Stddev error = " << t << " us" << endl;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -