double run_test(int n_fakeswitches, struct fakeswitch * fakeswitches, int mstestlen, int delay) { struct timeval now, then, diff; struct pollfd * pollfds; int i; double sum = 0; double passed; int count; int total_wait = mstestlen + delay; time_t tNow; struct tm *tmNow; pollfds = malloc(n_fakeswitches * sizeof(struct pollfd)); assert(pollfds); gettimeofday(&then, NULL); while (1) { gettimeofday(&now, NULL); timersub(&now, &then, &diff); if ((1000 * diff.tv_sec + (float) diff.tv_usec / 1000) > total_wait) break; #ifdef USE_EPOLL for (i = 0; i < MAX_EVENTS; i++) { events[i].events = EPOLLIN | EPOLLOUT; } int nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1); for (i = 0; i < nfds; i++) { fakeswitch_handle_io(events[i].data.ptr, &(events[i].events)); } #else for(i = 0; i < n_fakeswitches; i++) fakeswitch_set_pollfd(&fakeswitches[i], &pollfds[i]); poll(pollfds, n_fakeswitches, 1000); for(i = 0; i < n_fakeswitches; i++) fakeswitch_handle_io(&fakeswitches[i], &pollfds[i]); #endif } tNow = now.tv_sec; tmNow = localtime(&tNow); printf("%02d:%02d:%02d.%03d %-3d switches: flows/sec: ", tmNow->tm_hour, tmNow->tm_min, tmNow->tm_sec, (int) (now.tv_usec / 1000), n_fakeswitches); usleep(100000); // sleep for 100 ms, to let packets queue for (i = 0; i < n_fakeswitches; i++) { count = fakeswitch_get_count(&fakeswitches[i]); printf("%d ", count); sum += count; } passed = 1000 * diff.tv_sec + (double) diff.tv_usec / 1000; passed -= delay; // don't count the time we intentionally delayed sum /= passed; // is now per ms printf(" total = %lf per ms \n", sum); free(pollfds); return sum; }
double run_test(int n_fakeswitches, struct fakeswitch * fakeswitches, int mstestlen, int delay) { struct timeval now, then, diff; struct pollfd * pollfds; int i; double sum = 0; double passed; int count; int total_wait = mstestlen + delay; time_t tNow; struct tm *tmNow; pollfds = malloc(n_fakeswitches * sizeof(struct pollfd)); assert(pollfds); gettimeofday(&then,NULL); while(1) { gettimeofday(&now, NULL); timersub(&now, &then, &diff); if( (1000* diff.tv_sec + (float)diff.tv_usec/1000)> total_wait) break; for(i = 0; i< n_fakeswitches; i++) fakeswitch_set_pollfd(&fakeswitches[i], &pollfds[i]); poll(pollfds, n_fakeswitches, 1000); // block until something is ready or 100ms passes for(i = 0; i< n_fakeswitches; i++) fakeswitch_handle_io(&fakeswitches[i], &pollfds[i]); } tNow = now.tv_sec; tmNow = localtime(&tNow); printf("%02d:%02d:%02d.%03d %-3d switches: fmods/sec: ", tmNow->tm_hour, tmNow->tm_min, tmNow->tm_sec, (int)(now.tv_usec/1000), n_fakeswitches); usleep(100000); // sleep for 100 ms, to let packets queue for( i = 0 ; i < n_fakeswitches; i++) { count = fakeswitch_get_count(&fakeswitches[i]); printf("%d ", count); sum += count; } passed = 1000 * diff.tv_sec + (double)diff.tv_usec/1000; passed -= delay; // don't count the time we intentionally delayed sum /= passed; // is now per ms printf(" total = %lf per ms \n", sum); free(pollfds); return sum; }