📄 webclient.c
字号:
{ case 0: numclients = 1; ClientThread(0); exit(0); break; case -1: errexit("Error forking child processes\n"); exit(1); default: break; } */ select(0,(fd_set *)0,(fd_set *)0, (fd_set *)0, &sleeptime); } /* * Wait for all children to exit. */ while (thr_join(0, 0, 0) == 0); /* for(;;) { int pid = wait((int*)0); if ((pid == -1) && errno == ECHILD) break; } */#else /* start threads on NT */ for (i = 0; i < numclients; i++) { if (_beginthread(ClientThread, 0, 0) == -1) { errexit("_beginthread failed: %d", GetLastError()); } }#endif /* WIN32 */#ifdef WIN32 /* wait for children to get to sync point */ while (CounterSemaphore < numclients) sleep(1); CounterSemaphore = 0; /* start all children simultaneously */ ReleaseSemaphore(hSemaphore, 1, 0); if (testtime) { sleep(testtime); alarmhandler(); /* signal end of test to threads */ } /* * Wait for all threads to exit. */ while (CounterSemaphore < numclients) sleep(1); CloseHandle(hSemaphore);#endif /* WIN32 */ return;} /* end main() */void ClientThread(void *dummy){ THREAD FILE *logfile; THREAD stats_t timestat; THREAD rqst_timer_t timerarray[MAXNUMOFFILES]; THREAD SOCKET mastersock = BADSOCKET_VALUE; /* connection to webmaster */ THREAD page_stats_t *page_stats; /* actually a dynamic array */ int loopcnt = 0; int filecnt; int loop; int ran_number; int page_index; int page_number; int file_count = 0; char file_name[50]; struct timeval runningtime; time_t junk; int i; int returnval; /* * INITIALIZE DATA */ page_stats = (page_stats_t *)mymalloc((number_of_pages)*sizeof(page_stats_t)); for (i=0; i < number_of_pages; i++) { page_stats_init(&(page_stats[i])); } if (debug) { /* * OPEN A DEBUG FILE */ fflush(stderr); sprintf(file_name, "%s.%d", DEBUG_FILE, (int)getpid()); debugfile = fopen(file_name, "w+"); if (debugfile == 0) errexit("Can't open debug file\n"); D_PRINTF( "Running in debug mode, %d\n",amclient ); } if (record_all_transactions) { /* * OPEN A LOG FILE. */ sprintf(file_name, "%s%d", LOG_FILE, (int)getpid()); returnerr("Log file is %s\n", file_name); logfile = fopen(file_name, "w+"); } /* Initialize random number generator */ junk = getpid (); rand_r(&junk); D_PRINTF( "Random seed: %d\n", junk ); for (i=0; i < MAXNUMOFFILES; i++) { rqtimer_init(&(timerarray[i])); } stats_init(×tat); D_PRINTF( "Number of files %d\n", numfiles ); timestat.total_num_of_files = numfiles; if (amclient) { /* * WE ARE A CLIENT PROCESS. (i.e. WE ARE NOT RUN BY A USER, BUT BY * THE MASTER WWWSTONE PROCESS. WE NEED TO CONNECT TO THE * MASTER WHO WILL SYNCHRONIZE ALL THE CLIENTS. */ D_PRINTF( "Trying to connect with %s\n",connectstr ); mastersock = connecttomaster(connectstr); D_PRINTF( "connecttomaster returns %d, %s\n", mastersock, neterrstr() ); if(BADSOCKET(mastersock)) { /* * ERROR CONNECTING TO THE MASTER. ABORT. */ errexit("Error connecting to the master: %s\n", neterrstr()); } } /* END IF CLIENT */#ifdef WIN32 /* Tell parent we're ready */ InterlockedIncrement(&CounterSemaphore); /* Wait for main() thread to release us */ WaitForSingleObject(hSemaphore, INFINITE); ReleaseSemaphore(hSemaphore, 1, 0);#endif /* WIN32 */ if (testtime != 0) { /* * IF RUNNING A TIMED TEST, WE WILL LOOP * UNTIL THE ALARM GOES OFF. * WE'LL ALSO NEED TO SET THE SIGNAL HANDLER */#ifndef WIN32 /*signal(SIGALRM, alarmhandler);*/ /* * SEND SIGALRM IN testtime SECONDS */ /*alarm(testtime);*/#endif /* WIN32 */ } /* * AND THEY'RE OFF... */ if (testtime) numloops = INFINITY; GETTIMEOFDAY(&(timestat.starttime), &(timestat.starttimezone)); /* LOOP UNTIL WE HIT numloops, OR WE RUN OUT OF TIME */ for(loopcnt = 0; (loopcnt < numloops) && !timeexpired; loopcnt++) { /* * THIS IS WHERE LOAD TESTING IS DONE. * GET A RANDOM NUMBER, THEN INDEX INTO THE * PAGE, AND THEN REQUEST THAT SET OF FILES. */ if (uil_filelist_f) /* HAVE FILELIST */ { D_PRINTF( "Have filelist\n" ); /* if (testtime != 0) /* RUNNING IN TIMED MODE */ if (1) { D_PRINTF( "Running in timed mode\n" ); /* random number between 0 and totalweight-1 */ junk = getpid (); ran_number = (rand_r(&junk) % total_weight); D_PRINTF( "random %ld\n", ran_number ); /* loop through pages, find correct one * while ran_number is positive, decrement it * by the load_num of the current page * example: ran_number is 5, pages have weights of 10 and 10 * first iteration page_index = 0, ran_number = -5 * iteration halted, page_index = 0 */ page_index = -1; while (ran_number >= 0) { page_index++; D_PRINTF( "Current page index %d: %ld - %d\n", page_index, ran_number, load_file_list[page_index].load_num ); ran_number -= load_file_list[page_index].load_num; } if (page_index >= number_of_pages) { page_index--; } D_PRINTF( "Final page index %d\n", page_index ); filecnt = makeload(load_file_list[page_index].num_of_files, page_index, timerarray, ×tat, mastersock, page_stats); testtime = 1; } else /* NOT RUNNING IN TIMED MODE */ { for (page_number = 0; page_number < number_of_pages; page_number++) { filecnt = makeload(load_file_list[page_number].num_of_files, page_number, timerarray, ×tat, mastersock, page_stats); } /* END for page_number */ } /* END if/else TIMED MODE */ } else /* NO FILELIST */ { D_PRINTF( "No filelist\n" ); /* * LOOP THROUGH UNTIL numfiles TIMES OR UNTIL TIMER EXPIRES * AND ALARM SETS filecnt TO INFINITY. */ /* does this still work?? */ /* filecnt = makeload(numfiles, -1, timerarray); */ } /* END if HAVE FILELIST */ if (filecnt > 0) file_count += filecnt; } /* END while loopcnt */ GETTIMEOFDAY(&(timestat.endtime), &(timestat.endtimezone)); D_PRINTF( "Test run complete\n" ); signal(SIGALRM, 0); if (testtime == 0) { numfiles = loopcnt; if (uil_filelist_f) { numfiles = file_count; } } /* This option ( "-R" ) looks broken (e.g. l > 50) -- JEF 2/15/96 */ if (record_all_transactions) { /* * DUMP THE LOG FILE INFORMATION. */ for (loop=0; loop < (loopcnt * file_count); loop++) { fprintf(logfile, " entertime \t%d.%d\n" " beforeconnect \t%d.%d\n" " afterconnect \t%d.%d\n" " beforeheader \t%d.%d\n" " afterheader \t%d.%d\n" " afterbody \t%d.%d\n" " exittime \t%d.%d\n" " total bytes \t%d\n" " body bytes\t%d\n", timerarray[loop].entertime.tv_sec, timerarray[loop].entertime.tv_usec, timerarray[loop].beforeconnect.tv_sec, timerarray[loop].beforeconnect.tv_usec, timerarray[loop].afterconnect.tv_sec, timerarray[loop].afterconnect.tv_usec, timerarray[loop].beforeheader.tv_sec, timerarray[loop].beforeheader.tv_usec, timerarray[loop].afterheader.tv_sec, timerarray[loop].afterheader.tv_usec, timerarray[loop].afterbody.tv_sec, timerarray[loop].afterbody.tv_usec, timerarray[loop].exittime.tv_sec, timerarray[loop].exittime.tv_usec, timerarray[loop].totalbytes, timerarray[loop].bodybytes); } /* end for loop */ } /* end if recording all transactions */ D_PRINTF( "total errors: %d\n",timestat.rs.totalerrs ); /* gethostname(timestat.hostname,MAXHOSTNAMELEN); */ /* D_PRINTF( "Test for host: %s\n",timestat.hostname ); */ D_PRINTF( "Server is: %s running at port number: %d\n", webserver,portnum ); /* sprintf(timestat.hostname,"%s:%d",timestat.hostname,getpid()); */ if (amclient) /* CLIENT TO A WEBMASTER */ { char *stats_as_text; /* * SEND THE TIMING DATA TO THE MASTER */ stats_as_text = stats_to_text(×tat); D_PRINTF( "stats_to_text returned %s\n", stats_as_text ); returnval = senddata(mastersock, stats_as_text, SIZEOF_STATSTEXTBASE + number_of_pages*SIZEOF_DOUBLETEXT); D_PRINTF( "Wrote time stats to master %d\n", returnval ); if (returnval < 1) { D_PRINTF( "Error while writing time stats: %s\n", neterrstr() ); errexit("Error while writing time stats: %s\n", neterrstr()); } if (uil_filelist_f) /* write pagestats */ { char *page_stats_as_text; for (i = 0; i < number_of_pages; i++) { D_PRINTF( "On page_stats[%d]\n", i ); page_stats_as_text = page_stats_to_text(&page_stats[i]); returnval = strlen(page_stats_as_text); D_PRINTF( "page_stats_to_text[%d] returned %d\n", i, returnval ); returnval = senddata(mastersock, page_stats_as_text, SIZEOF_PAGESTATSTEXT); if (returnval < 1) { D_PRINTF( "Error while writing page_stats[%d]: %s\n", i, neterrstr() ); errexit("Error while writing page_stats[%d]: %s\n", i, neterrstr()); } /* end if */ D_PRINTF( "Wrote %d bytes of page_stats[%d] to master\n", returnval, i ); } /* end for */ } /* end if filelist */ D_PRINTF( "About to close socket\n" ); if (NETCLOSE(mastersock)) D_PRINTF( "Close socket error: %s\n", neterrstr() ); } else /* NOT A CLIENT TO A WEBMASTER */ { if (testtime) { printf("Test ran for: %d minutes\n",(testtime/60)); } else { printf("Test ran for: %d iterations.\n",numloops); } compdifftime(&(timestat.endtime), &(timestat.starttime), &(runningtime)); printf("Total time of test (sec) %d.%d\n", runningtime.tv_sec, runningtime.tv_usec); printf("Files retrieved per iteration: %d\n",numfiles); /* 'per iteration' */ printf("----------------------------------\n"); printf("Totals:\n\n"); rqstat_print(&(timestat.rs)); if (timestat.rs.totalconnects == 0) goto end; printf("Thruput = %5.2lf Kbytes/sec\n", thruputpersec(timestat.rs.totalbytes, &runningtime) / 1000); if (uil_filelist_f && numloops && verbose) { for (loop = 0; loop < number_of_pages; loop++) { if (timestat.page_numbers[loop] != 0) { printf ("===============================================================================\n"); printf ("Page # %d\n\n", loop); printf ("Total number of times page was hit %d\n", page_stats[loop].totalpages); rqstat_print(&(page_stats[loop].rs)); printf ("Page size %d \n", page_stats[loop].page_size); printf ("===============================================================================\n\n"); } /* END if timestat */ } /* END for loop */ } /* END if filelist */ } /* END if client */end: if(record_all_transactions) fclose(logfile); if(debug) { D_PRINTF( "Client exiting.\n" ); fclose(debugfile); }#ifdef WIN32 /* tell parent we're done */ InterlockedIncrement(&CounterSemaphore);#endif /* WIN32 */} /* END ClientThread() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -