static void copy_file(const char *zFrom, const char *zTo){ if( access(zFrom, F_OK) ){ unlink(zTo); }else{ int fd1; int fd2; off_t sz; off_t i; struct stat buf; u8 *aBuf; fd1 = open(zFrom, O_RDONLY, 0644); fd2 = open(zTo, O_RDWR | O_CREAT, 0644); fstat(fd1, &buf); sz = buf.st_size; ftruncate(fd2, sz); aBuf = testMalloc(4096); for(i=0; i<sz; i+=4096){ int nByte = MIN(4096, sz - i); read(fd1, aBuf, nByte); write(fd2, aBuf, nByte); } testFree(aBuf); close(fd1); close(fd2); } }
int test_mem(int argc, char *argv[]) { unsigned int *array = NULL; int arraySize = 10; PKIX_UInt32 actualMinorVersion; PKIX_UInt32 j = 0; PKIX_TEST_STD_VARS(); startTests("Memory Allocation"); PKIX_TEST_EXPECT_NO_ERROR( PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext)); subTest("PKIX_PL_Malloc"); testMalloc(&array); subTest("PKIX_PL_Realloc"); testRealloc(&array); subTest("PKIX_PL_Free"); testFree(array); /* --Negative Test Cases------------------- */ /* Create an integer array of size 10 */ PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Malloc( (PKIX_UInt32)(arraySize*sizeof (unsigned int)), (void **) &array, plContext)); (void) printf("Attempting to reallocate 0 sized memory...\n"); PKIX_TEST_EXPECT_NO_ERROR (PKIX_PL_Realloc(array, 0, (void **) &array, plContext)); (void) printf("Attempting to allocate to null pointer...\n"); PKIX_TEST_EXPECT_ERROR(PKIX_PL_Malloc(10, NULL, plContext)); (void) printf("Attempting to reallocate to null pointer...\n"); PKIX_TEST_EXPECT_ERROR(PKIX_PL_Realloc(NULL, 10, NULL, plContext)); PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(array, plContext)); cleanup: PKIX_Shutdown(plContext); endTests("Memory Allocation"); return (0); }
/* * Unit test framework entry point for this set of unit tests. * */ void testMem_runTests() { pcsl_mem_initialize(NULL, 5000); testAllocation(); testMalloc(); testAllocateChunk(); /* * comment out this test. The implementation changes the * specified max size, to align with a page * need revisit: what the new max size is. So we can't * compare against it. */ /* testModifyChunkSize(); */ testDoubleInit(); testHeapSize(); testCalloc(); testRealloc(); testStrdup(); pcsl_mem_finalize(); }
/* Do a performance benchmark */ static void doBenchmark(void *thread) { MprTime start; MprList *list; int count, i; MprMutex *lock; MprSpin *spin; mprPrintf("Group\t%-30s\t%13s\t%12s\n", "Benchmark", "Microsec", "Elapsed-sec"); testMalloc(); if (!app->testAllocOnly) { /* Locking primitives */ mprPrintf("Lock Benchmarks\n"); lock = mprCreateLock(); count = 5000000 * app->iterations; start = startMark(); for (i = 0; i < count; i++) { mprLock(lock); mprUnlock(lock); } endMark(start, count, "Mutex lock|unlock"); /* Locking primitives */ mprPrintf("Lock Benchmarks\n"); spin = mprCreateSpinLock(); count = 5000000 * app->iterations; start = startMark(); for (i = 0; i < count; i++) { mprSpinLock(spin); mprSpinUnlock(spin); } endMark(start, count, "Spin lock|unlock"); /* Condition signal / wait */ mprPrintf("Cond Benchmarks\n"); count = 1000000 * app->iterations; start = startMark(); mprResetCond(app->complete); for (i = 0; i < count; i++) { mprSignalCond(app->complete); mprWaitForCond(app->complete, -1); } endMark(start, count, "Cond signal|wait"); /* List */ mprPrintf("List Benchmarks\n"); count = 2000000 * app->iterations; list = mprCreateList(count, 0); start = startMark(); for (i = 0; i < count; i++) { mprAddItem(list, (void*) (long) i); mprRemoveItem(list, (void*) (long) i); } endMark(start, count, "Link insert|remove"); /* Events */ mprPrintf("Event Benchmarks\n"); mprResetCond(app->complete); count = 30000 * app->iterations; app->markCount = count; start = startMark(); for (i = 0; i < count; i++) { mprCreateEvent(NULL, "eventBenchmark", 0, eventCallback, ITOP(i), MPR_EVENT_QUICK); } mprWaitForCond(app->complete, -1); endMark(start, count, "Event (create|run|delete)"); /* Test timer creation, run and remove These create a new dispatcher and run a worker thread. */ mprPrintf("Timer\n"); mprResetCond(app->complete); count = 20000 * app->iterations; app->markCount = count; start = startMark(); for (i = 0; i < count; i++) { mprCreateTimerEvent(NULL, "timerBenchmark", 0, timerCallback, (void*) (long) i, 0); } mprWaitForCond(app->complete, -1); endMark(start, count, "Timer (create|delete)"); /* Alloc (1K) */ mprPrintf("Alloc 1K Benchmarks\n"); count = 2000000 * app->iterations; start = startMark(); for (i = 0; i < count; i++) { mprAlloc(1024); if ((i % 128) == 0) { mprGC(0); } } endMark(start, count, "Alloc mprAlloc(1K)"); } testComplete = 1; }