int main(int argc, char *argv[]) { struct timeval programStart, programEnd, calculationStart, calculationEnd; gettimeofday(&programStart, NULL); // set up the glut window glutInit(&argc, argv); glutInitWindowSize(X_WINDOW, Y_WINDOW); glutInitWindowPosition(0, 0); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutCreateWindow("CSC 415 - Parallel Mandelbrot"); glClearColor(0.0, 0.0, 0.0, 0.0); int numSets = 1; if ( numSets > 1 ) { double sum = 0.0, sum2 = 0.0; for (int i = 0; i<numSets; i++) { gettimeofday(&calculationStart, NULL); par_mandelbrotImage(NUM_THREADS, 512); gettimeofday(&calculationEnd, NULL); sum += timeDifference(&calculationStart, &calculationEnd)/1000000; sum2 += (timeDifference(&calculationStart, &calculationEnd)/1000000 )* (timeDifference(&calculationStart, &calculationEnd)/1000000); } printf ("Average Calculation Time (over %d runs) = %.3f sec\n", numSets, sum/numSets); printf ("Standard Deviation = %.3f sec\n", sqrt(sum2/numSets - (sum/numSets)* (sum/numSets))/(numSets-1)); } else { gettimeofday(&calculationStart, NULL); par_mandelbrotImage(NUM_THREADS, 512); gettimeofday(&calculationEnd, NULL); printf("Multi-threaded Mandelbrot Fractal (Calculation): %.3f sec\n", timeDifference (&calculationStart, &calculationEnd)/1000000); } // Specify callback function glutDisplayFunc(display); glutMainLoop(); pthread_exit(NULL); return 0; }
double StopWatch::elapsed(){ if(stopped == true) { return timeDifference(start_tv,stop_tv); } else if(started == true) { gettimeofday(&temp_tv,&temp_tz); return timeDifference(start_tv,temp_tv); } else { std::cerr << "StopWatch has not been started yet.\n"; return (double) 0; } }
void bench(bool longLog) { muduo::Logger::setOutput(asyncOutput); int cnt = 0; const int kBatch = 1000; muduo::string empty = " "; muduo::string longStr(3000, 'X'); longStr += " "; for (int t = 0; t < 30; ++t) { muduo::Timestamp start = muduo::Timestamp::now(); for (int i = 0; i < kBatch; ++i) { LOG_INFO << "Hello 0123456789" << " abcdefghijklmnopqrstuvwxyz " << (longLog ? longStr : empty) << cnt; ++cnt; } muduo::Timestamp end = muduo::Timestamp::now(); printf("%f\n", timeDifference(end, start) * 1000000 / kBatch); struct timespec ts = { 0, 500 * 1000 * 1000 }; nanosleep(&ts, NULL); } }
double StopWatch::lapTime(){ if (started == true) { gettimeofday(&temp_tv,&temp_tz); temp2_tv = lap_tv; lap_tv = temp_tv; // Time zone does not need to be modified. if (lap == true) { return timeDifference(temp2_tv,temp_tv); } else { lap = true; return timeDifference(start_tv,temp_tv); } } else { std::cerr << "StopWatch has not been started yet.\n"; return (double) 0; } }
/* * fileTransferDownload():This function handles the sending of playlist to client. * It communicates with the client and handles file download. */ void fileTransferDownload(int connID) { char metaDataDownload[3000], fileSize[sizeof(long)]; const char *delims = "~"; char file_name[108], noOfFiles[2], cwd[BUF_SIZE], buf[BUF_SIZE], b[2], dname[108] = ""; struct stat stbuf; DIR * dirp; struct dirent * entry; int file_count = 0, i = 0, bytes, count = 0, succ, w, r, changedir; char *fileNameArrayDownload[MAX_FILES], *fileSizeArrayDownload[MAX_FILES]; long lSize = 0L; FILE *fd, *fdTx; time_t t_start, t_end; struct tm *st,*en; struct timeval time_ms,time_end; struct TIME t1,t2,diff; //get the playlist name sent by the user memset(file_name, 0, 108); r = read(connID, file_name, sizeof(file_name)); printf("The playlist transferred : %s \n ", file_name); write(connID, "1", 1); selectdir(); getcwd(cwd, sizeof(cwd)); strcat(cwd, "/"); strcat(cwd, file_name); changedir = chdir(cwd); if (changedir < 0) fatal("Change Directory in download failed"); //Loop through the playlist to get the count of files dirp = opendir(cwd); while ((entry = readdir(dirp)) != NULL) { // If the entry is a regular file if ((strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)) { file_count++; } } //initialize metaData to blank memset(metaDataDownload, 0, 3000); memset(noOfFiles, 0, 2); sprintf(noOfFiles, "%d", file_count); //append number of files to start of metadata strcpy(metaDataDownload, noOfFiles); strcat(metaDataDownload, delims); //Loop through the directory and form the metadata to be used for download dirp = opendir(cwd); while ((entry = readdir(dirp)) != NULL) { if ((strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)) { /* If the entry is a regular file */ fileNameArrayDownload[i] = entry->d_name; //append filename to metadata strcat(metaDataDownload, fileNameArrayDownload[i]); strcat(metaDataDownload, delims); //To read the file size int fdes; fdes = open(fileNameArrayDownload[i], O_RDONLY); if (fdes < 0) fatal("File des open failed"); else printf("File des %d open success\n", i); //Use fstat as it is better for binary files if ((fstat(fdes, &stbuf) != 0) || (!S_ISREG(stbuf.st_mode))) { fatal("Error in fstat"); } lSize = stbuf.st_size; printf("File Size : %ld\n", lSize); close(fdes); memset(fileSize, 0, sizeof(long)); sprintf(fileSize, "%ld", lSize); fileSizeArrayDownload[i] = (char *) malloc(sizeof(lSize)); //append filesize to metadata strcpy(fileSizeArrayDownload[i], fileSize); strcat(metaDataDownload, fileSizeArrayDownload[i]); strcat(metaDataDownload, delims); i++; } } printf("The Metadata is %s : \n", metaDataDownload); char *success; //write the metadata to client w = write(connID, metaDataDownload, 3000); if (w < 0) printf("After metaData call failed\n "); //read ACK from client r = read(connID, success, sizeof(file_name)); //calculate the time taken t_start = time(NULL); st = localtime(&t_start); t1.hours = st->tm_hour; t1.minutes = st->tm_min; t1.seconds = st->tm_sec; gettimeofday(&time_ms, NULL); t1.milliseconds = time_ms.tv_usec/1000; printf("START TIME = %d:%d:%d:%03ld\n",t1.hours,t1.minutes,t1.seconds,t1.milliseconds); //loop through each file in the playlist and send to client while (count < file_count) { fdTx = fopen(fileNameArrayDownload[count], "rb"); if (fdTx < 0) fatal("File open failed"); else printf("File open success\n"); //loop to read the file contents 1024 bytes at a time while ((bytes = fread(buf, BUF_SIZE, 1, fdTx)) >= 0) { w = write(connID, buf, BUF_SIZE); //condition to check if end of file has been reached if (bytes == 0 && feof(fdTx)) { break; } } fclose(fdTx); //check for ACK from client for success of file transfer w = read(connID, b, 1); b[1] = '\0'; succ = atoi(b); if (succ > 0) printf("File [%d] Transfer Result : %d\n", count, succ); else fatal("File transfer failed"); count++; } //calculate the end time and measure the total time taken t_end = time(NULL); en = localtime(&t_end); t2.hours = en->tm_hour; t2.minutes = en->tm_min; t2.seconds = en->tm_sec; gettimeofday(&time_end, NULL); t2.milliseconds = time_end.tv_usec/1000; printf("END TIME = %d:%d:%d:%03ld\n",t2.hours,t2.minutes,t2.seconds, t2.milliseconds); timeDifference(t2,t1, &diff); printf("TIME = %d:%d:%d:%03ld\n",diff.hours,diff.minutes,diff.seconds,diff.milliseconds); }
/* * fileTransfer(): This function is used to handle file transfer from client. * It handles the metadata information and performs necessary upload operations for server */ void fileTransfer(int connID) { char metaData[3000], revbuf[BUF_SIZE], backup[BUF_SIZE], success[1] = "1"; char *fileNameArray[MAX_FILES], *fileSizeArray[MAX_FILES]; char *str1, *result, *nextString; const char *delims = "~"; int anamolyCount = 0, fr_block_sz = 0, remain = 0; int counter = 0, i = 1, noOfFiles = 0, count = 0, w, j; unsigned long lSize = 0L, sizeCheck = 0L; time_t t_start, t_end; struct tm *st,*en; struct timeval time_ms,time_end; struct TIME t1,t2,diff; //read the metadata information sent from client w = read(connID, metaData, sizeof(metaData)); metaData[strlen(metaData)] = '\0'; printf("Metadata : %s\n", metaData); //loop to split the metadata and read the information for (j = 0, str1 = metaData;; j++, str1 = NULL) { result = strtok_r(str1, delims, &nextString); if (result == NULL) break; if (j == 0) { noOfFiles = atoi(result); continue; } if (j % 2 != 0) { fileNameArray[j / 2] = result; } else { fileSizeArray[j / 2 - 1] = result; } } //Send ACK to client on metadata success w = write(connID, success, 1); //calculate the time taken t_start = time(NULL); st = localtime(&t_start); t1.hours = st->tm_hour; t1.minutes = st->tm_min; t1.seconds = st->tm_sec; gettimeofday(&time_ms, NULL); t1.milliseconds = time_ms.tv_usec/1000; printf("START TIME = %d:%d:%d:%03ld\n",t1.hours,t1.minutes,t1.seconds,t1.milliseconds); //Loop to obtain files from client while (count < noOfFiles) { lSize = atol(fileSizeArray[count]); counter = (int) lSize / BUF_SIZE; FILE *fr = fopen(fileNameArray[count], "ab"); if (fr == NULL) fatal("File cannot be opened on server"); printf("File %s opened\n",fileNameArray[count]); while (i <= counter) { fr_block_sz = read(connID, revbuf, BUF_SIZE); //this block is executed if the client does not recieve 1024 bytes sent from server //anamolyCount is used as a performance metric to analyze file chunking anomalies while (fr_block_sz < BUF_SIZE) { anamolyCount++; remain = BUF_SIZE - fr_block_sz; remain = read(connID, backup, remain); strcat(revbuf, backup); fr_block_sz += remain; } sizeCheck += (long) fr_block_sz; fwrite(revbuf, fr_block_sz, 1, fr); bzero(revbuf, BUF_SIZE); i++; remain = 0; } //Obtain last chunk of file as it may not be in the boundary of 1024 bytes if (sizeCheck < lSize) { remain = (int) lSize - (int) sizeCheck; fr_block_sz = read(connID, revbuf, remain); fwrite(revbuf, remain, 1, fr); bzero(revbuf, BUF_SIZE); } //File transfer success printf("Ok received from client!\n"); fclose(fr); //send ACK to server w = write(connID, success, 1); //display the anamoly count to show anomalies in file chunking. printf("Anamoly Count for file[%d] : %d\n", count, anamolyCount); //reset all variables to zero sizeCheck = 0L; lSize = 0L; i = 1; remain = 0; fr_block_sz = 0; anamolyCount = 0; count++; } //measure the end time and calculate the total time taken t_end = time(NULL); en = localtime(&t_end); t2.hours = en->tm_hour; t2.minutes = en->tm_min; t2.seconds = en->tm_sec; gettimeofday(&time_end, NULL); t2.milliseconds = time_end.tv_usec/1000; printf("END TIME = %d:%d:%d:%03ld\n",t2.hours,t2.minutes,t2.seconds, t2.milliseconds); timeDifference(t2,t1, &diff); printf("TIME = %d:%d:%d:%03ld\n",diff.hours,diff.minutes,diff.seconds,diff.milliseconds); //close the connection with client close(connID); }
double cpuTimeImpl::cpuTimeIncrement() const { lastTime_ = newTime_; getTime(newTime_); return timeDifference(lastTime_, newTime_); }
double cpuTimeImpl::elapsedCpuTime() const { getTime(newTime_); return timeDifference(startTime_, newTime_); }
double Foam::cpuTime::elapsedCpuTime() const { getTime(newTime_); return timeDifference(startTime_, newTime_); }
double clockTime::timeIncrement() const { lastTime_ = newTime_; getTime(newTime_); return timeDifference(lastTime_, newTime_); }
double clockTime::elapsedTime() const { getTime(newTime_); return timeDifference(startTime_, newTime_); }
/** Main program: copies a file. @param argc Number of command-line arguments (including program name). @param argv Array of pointers to character arays holding arguments. @return 0 if successful, 1 if fail. */ int main(int argc, char* argv[]) { char** infilenames; // Names of files. char* outfilename; int fileCount; int returnstatus; int paramIndex; char method; int bufferSize; int i; //An iterator struct timeval tcpstart; struct timeval tcpfinish; if (argc < 3) { usage(argv[0]); // Must have at least 3 arguments return 1; } //search for copy type if ((paramIndex = searchArrayForString(argv,argc,"-c")) != -1) { if (paramIndex+1 == argc) { printf("Missing parameter. Program terminated.\n"); return 1; } method = atoi(argv[paramIndex+1]); if (method != 1 && method != 2 && method != 3) { printf("Invalid copymethod argument. Valid options are 1, 2, or 3. You entered \"%s\". Program Terminated.\n",argv[paramIndex+1]); return 1; } } else { method = 3; //set default method } //Set buffer size if (method == 3) { if ((paramIndex = searchArrayForString(argv,argc,"-b")) != -1) { if (paramIndex+1 == argc) { printf("Missing parameter. Program terminated.\n"); return 1; } bufferSize = atoi(argv[paramIndex+1]); if (bufferSize <= 0) { printf("Invalid buffer Size. Program terminatred\n"); return 1; } } else { bufferSize = 1024; //set default buffer size } } //find file names if ((paramIndex = findFirstFile(argv,argc)) == -1) { printf("No file names entered. Program Terminated.\n"); return 1; } if ((fileCount = countInputFiles(argv,argc)) < 1) { printf("At least two file names or locations must be specified. Program terminated\n"); return 1; } infilenames = (char**) malloc(sizeof(char*)*fileCount); //Make space for a list of the files to copy //Fill an array of files to copy for (i = 0; i < fileCount; i++) { infilenames[i] = argv[paramIndex + i]; } outfilename = argv[paramIndex + fileCount]; //Copy the files based on the current method gettimeofday(&tcpstart,NULL); switch(method) { case 1: returnstatus = copyfile1(infilenames, outfilename, fileCount); break; case 2: returnstatus = copyfile2(infilenames, outfilename, fileCount); break; case 3: returnstatus = copyfile3(infilenames, outfilename, fileCount, bufferSize); break; } gettimeofday(&tcpfinish,NULL); struct timeval* tcpDifference = timeDifference(&tcpstart, &tcpfinish); if (!returnstatus) { printf("\nCopy started: %d seconds and %d microseconds from UTC start.\n",tcpstart.tv_sec,tcpstart.tv_usec); printf("Copy finished: %d seconds and %d microseconds from UTC start.\n",tcpfinish.tv_sec,tcpfinish.tv_usec); printf("Time for Copy: %d seconds and %d microseconds.\n",tcpDifference->tv_sec,tcpDifference->tv_usec); } free(tcpDifference); free(infilenames); return returnstatus; }