/* 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; }
static void testMalloc() { MprTime start; char *ptr; int count, i, pin; #if KEEP ssize base; #endif mprPrintf("Alloc/Malloc overhead\n"); count = 2000000 * app->iterations; pin = 0; mprGC(MPR_GC_FORCE); #if MALLOC /* malloc(1) */ base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = malloc(1); if (pin) memset(ptr, 0, 1); } endMark(start, count, "Alloc malloc(1)"); mprPrintf("\tMalloc overhead per block %d\n\n", ((mprGetMem() - base) / count) - 1); /* malloc(8) */ base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = malloc(8); if (pin) memset(ptr, 0, 8); } endMark(start, count, "Alloc malloc(8)"); mprPrintf("\tMalloc overhead per block %d (approx)\n\n", ((mprGetMem() - base) / count) - 8); /* malloc(16) */ base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = malloc(16); if (pin) memset(ptr, 0, 16); } endMark(start, count, "Alloc malloc(16)"); mprPrintf("\tMalloc overhead per block %d (approx)\n\n", ((mprGetMem() - base) / count) - 16); /* malloc(32) */ base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = malloc(32); if (pin) memset(ptr, 0, 32); } endMark(start, count, "Alloc malloc(32)"); mprPrintf("\tMalloc overhead per block %d (approx)\n\n", ((mprGetMem() - base) / count) - 32); /* malloc+free(8) */ start = startMark(); for (i = 0; i < count; i++) { ptr = malloc(8); if (pin) memset(ptr, 0, 8); mprNop(ptr); free(ptr); } endMark(start, count, "Alloc malloc+free(8)"); mprPrintf("\n"); #endif /* mprAlloc(1) */ // base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = mprAlloc(1); if (pin) memset(ptr, 0, 1); } endMark(start, count, "Alloc mprAlloc(1)"); // mprPrintf("\tMpr overhead per block %d (approx)\n\n", ((mprGetMem() - base) / count) - 1); /* mprAlloc(8) */ // base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = mprAlloc(8); if (pin) memset(ptr, 0, 8); } endMark(start, count, "Alloc mprAlloc(8)"); // mprPrintf("\tMpr overhead per block %d (approx)\n\n", ((mprGetMem() - base) / count) - 8); /* mprAlloc(16) */ // base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = mprAlloc(16); if (pin) memset(ptr, 0, 16); } endMark(start, count, "Alloc mprAlloc(16)"); // mprPrintf("\tMpr overhead per block %d (approx)\n\n", ((mprGetMem() - base) / count) - 16); /* mprAlloc(32) */ // base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = mprAlloc(32); if (pin) memset(ptr, 0, 32); } endMark(start, count, "Alloc mprAlloc(32)"); // mprPrintf("\tMpr overhead per block %d (approx)\n\n", ((mprGetMem() - base) / count) - 32); /* mprAlloc(64) */ // base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = mprAlloc(64); if (pin) memset(ptr, 0, 64); } endMark(start, count, "Alloc mprAlloc(32)"); // mprPrintf("\tMpr overhead per block %d (approx)\n\n", ((mprGetMem() - base) / count) - 64); mprPrintf("\n"); }
/* * Do a performance benchmark */ static void doBenchmark(Mpr *mpr, void *thread) { MprTime start; MprList *list; void *mp; int count, i; #if BLD_FEATURE_MULTITHREAD MprMutex *lock; #endif complete = mprCreateCond(mpr); mprPrintf(mpr, "Group\t%-30s\t%13s\t%12s\n", "Benchmark", "Microsec", "Elapsed-sec"); /* * Alloc (1K) */ mprPrintf(mpr, "Alloc Benchmarks\n"); count = 2000000 * iterations; start = startMark(mpr); for (i = 0; i < count; i++) { mp = mprAlloc(mpr, 1024); memset(mp, 0, 1024); mprFree(mp); } endMark(mpr, start, count, "Alloc mprAlloc(1K)|mprFree"); start = startMark(mpr); #if BLD_FEATURE_MULTITHREAD /* * Locking primitives */ mprPrintf(mpr, "Lock Benchmarks\n"); lock = mprCreateLock(mpr); count = 5000000 * iterations; start = startMark(mpr); for (i = 0; i < count; i++) { mprLock(lock); mprUnlock(lock); } endMark(mpr, start, count, "Mutex lock|unlock"); mprFree(lock); /* * Condition signal / wait */ mprPrintf(mpr, "Cond Benchmarks\n"); count = 1000000 * iterations; start = startMark(mpr); mprResetCond(complete); for (i = 0; i < count; i++) { mprSignalCond(complete); mprWaitForCond(complete, -1); } endMark(mpr, start, count, "Cond signal|wait"); #endif /* * List */ mprPrintf(mpr, "List Benchmarks\n"); count = 500000 * iterations; list = mprCreateList(mpr); start = startMark(mpr); for (i = 0; i < count; i++) { mprAddItem(list, (void*) (long) i); mprRemoveItem(list, (void*) (long) i); } endMark(mpr, start, count, "Link insert|remove"); mprFree(list);; /* * Events */ mprPrintf(mpr, "Event Benchmarks\n"); mprResetCond(complete); count = 200000 * iterations; markCount = count; start = startMark(mpr); for (i = 0; i < count; i++) { mprCreateEvent(mprGetDispatcher(mpr), eventCallback, 0, 0, (void*) (long) i, 0); } endMark(mpr, start, count, "Event (create)"); mprWaitForCondWithService(complete, -1); endMark(mpr, start, count, "Event (run|delete)"); /* * Test timer creation, run and delete (make a million timers!) */ mprPrintf(mpr, "Timer\n"); mprResetCond(complete); count = 50000 * iterations; markCount = count; start = startMark(mpr); for (i = 0; i < count; i++) { mprCreateTimerEvent(mprGetDispatcher(mpr), timerCallback, 0, 0, (void*) (long) i, 0); } endMark(mpr, start, count, "Timer (create)"); mprWaitForCondWithService(complete, -1); endMark(mpr, start, count, "Timer (delete)"); testComplete = 1; }