/*TITLE test program for block classes*/ /****keyword-flag*** "%v %f %n" */ /* "18 9-May-98,15:11:08 HASHTEST.CPP" */ /****keyword-flag*** "%v %f %n" */ char HashtestVersionInfo[] = "18 9-May-98,15:11:08 HASHTEST.CPP"; #include "common.h" extern "C" { #include } int main(int argc, char *argv[]) { QuantumFile QF; MainObjectArrayPtr MOA; ModifiableElement Element; FILE *infile; ofstream ofs; long ilong; int i; char *result; ModifiableElement TestName; String Command; clock_t StartAdd; clock_t StartRead; clock_t StartDelete; clock_t End; clock_t CurrentTicks; clock_t PreviousTicks; AccountNumber AcctNum; ModifiableElement TempElement; long Dup = 0; char buf[4000]; char *KeyPtr = &buf[0]; char *DataPtr = &buf[10]; long TotalKeys = 100000L; ArrayIndex nbuf = 20; long CurrentReadCount; long PreviousReadCount; long ReadCount; long DeleteCount; long CurrentWriteCount; long PreviousWriteCount; String TestFile = "testx.qnt"; ModifiableElement Key; const int KeyOffset = 10; long CurrentTotalKeys; long PreviousTotalKeys; long StartingElement = 0; int NewKeyAdded; PrintStat Statistics; DynamicHashArray DHArray; FlexArray SaveKeys; if (argc == 1) { printf("Syntax: hashtest command buffers qfile total-keys [starting-element] \n"); printf("Where 'command' is one of the following:\n"); printf("%s\n%s\n%s\n", "a (add elements to existing file)", "i (import new file)", "r (read and export elements from existing file)", "d (delete records from file)\n"); printf("'buffers' is the number of buffers to allocate;\n"); printf("'qfile' is the name of the quantum file;\n"); printf("for 'a' or 'i' commands,\n" "'total-keys' is the number of keys the file should contain after the run;\n"); printf("for 'r' commands, 'total-keys' is the number of keys to read\n" "and 'starting-element' is the key number to start with.\n"); printf("for 'd' commands, 'total-keys' is the number of keys to delete\n" "and 'starting-element' is the key number to start with.\n"); exit(1); } Command = argv[1]; if (Command.GetSize() > 2) { printf("Only one command at a time, please!\n"); exit(1); } if (argc > 2) nbuf = atoi(argv[2]); if (argc > 3) TestFile = argv[3]; if (argc > 4) TotalKeys = atol(argv[4]); if (argc > 5) StartingElement = atol(argv[5]); strcpy(buf," "); for (i = 1; i < argc; i ++) { strcat(buf,argv[i]); strcat(buf," "); } Statistics.Init("stats.out"); Statistics.Write("\n\n%s\nArguments:%s\n",HashtestVersionInfo,buf); Statistics.Write("\nBlockSize: %lu\n\n",BlockSize); if (Command == "i") remove(TestFile); TestName = ModifiableElement("Test"); infile = fopen("names.in","rb"); setvbuf(infile,NULL,_IOFBF,16384); ofs.open("names.out",ios::out|ios::binary); QF.Open(TestFile,nbuf); PersistentArrayUlong Save(&QF,"Save"); SaveKeys.Open(&QF,"SaveKeys"); DHArray.Open(&QF,"DHArray"); Element = ""; Statistics.Write("\nSetup\n"); CurrentReadCount = QF.GetReadCount(); CurrentWriteCount = QF.GetWriteCount(); CurrentTicks = clock(); Statistics.Write("%lu:%lu:%lu\n", CurrentTicks, CurrentReadCount, CurrentWriteCount); PreviousTicks = CurrentTicks; PreviousReadCount = CurrentReadCount; PreviousWriteCount = CurrentWriteCount; StartAdd = clock(); if (Command == "i") { Save[0] = KeyOffset; // start storing keys here Save[1] = 0; // offset in input file Command = "a"; // now it's the same as appending } CurrentTotalKeys = Save[0] - KeyOffset; PreviousTotalKeys = CurrentTotalKeys; Statistics.Write("\nPrevious keys stored: %lu",CurrentTotalKeys); if (Command == "a") { Statistics.Write("\nWrite"); fseek(infile,Save[1],SEEK_SET); // skip used input lines Statistics.Write("\n%6lu: ",CurrentTotalKeys); NewKeyAdded = FALSE; while (CurrentTotalKeys < TotalKeys) { if (CurrentTotalKeys % 4000 == 0 && NewKeyAdded == TRUE) { NewKeyAdded = FALSE; Statistics.Write("\n%6lu: ",CurrentTotalKeys); } if (CurrentTotalKeys % 1000 == 999 && NewKeyAdded == TRUE) { NewKeyAdded = FALSE; CurrentReadCount = QF.GetReadCount(); CurrentWriteCount = QF.GetWriteCount(); CurrentTicks = clock(); Statistics.Write("%lu:%lu:%lu ", CurrentTicks-PreviousTicks, CurrentReadCount-PreviousReadCount, CurrentWriteCount-PreviousWriteCount); PreviousTicks = CurrentTicks; PreviousReadCount = CurrentReadCount; PreviousWriteCount = CurrentWriteCount; } memset(buf,0,4000); result = fgets(buf, 4000, infile); if (result == NULL) break; Key = ModifiableElement(9,KeyPtr); // get account number AcctNum = Key; ArrayIndex DataLength = strlen(DataPtr); if (DataLength == 1) DataLength = 0; //get rid of cr/lf Element = ModifiableElement(DataLength,DataPtr); TempElement = DHArray[AcctNum]; DHArray[AcctNum] = Element; if (TempElement != "") Dup++; else { SaveKeys[CurrentTotalKeys] = AcctNum; CurrentTotalKeys++; NewKeyAdded = TRUE; } } Save[0] = CurrentTotalKeys+KeyOffset; // set to next available place Save[1] = ftell(infile); // where to start the next time we add stuff } StartRead = clock(); PreviousTicks = StartRead; if (Command == "r") { Statistics.Write("\nRead"); if (StartingElement % 4000 != 0) Statistics.Write("\n%6lu: ", StartingElement); for (ilong = StartingElement; ilong < TotalKeys; ilong ++) { if (ilong % 4000 == 0) Statistics.Write("\n%6lu: ",ilong); if (ilong % 1000 == 999) { CurrentReadCount = QF.GetReadCount(); CurrentWriteCount = QF.GetWriteCount(); CurrentTicks = clock(); Statistics.Write("%lu:%lu:%lu ", CurrentTicks-PreviousTicks, CurrentReadCount-PreviousReadCount, CurrentWriteCount-PreviousWriteCount); PreviousTicks = CurrentTicks; PreviousReadCount = CurrentReadCount; PreviousWriteCount = CurrentWriteCount; } AcctNum = SaveKeys[ilong]; if (AcctNum == "") break; Element = DHArray[AcctNum]; if (Element == "") ofs << "Key " << AcctNum << " not found." << endl; else ofs << AcctNum << ' ' << Element; } ReadCount = ilong - StartingElement; } StartDelete = clock(); PreviousTicks = StartDelete; if (Command == "d") { Statistics.Write("\nDelete"); if (StartingElement % 4000 != 0) Statistics.Write("\n%6lu: ", StartingElement); for (ilong = StartingElement; ilong < TotalKeys; ilong ++) { if (ilong % 4000 == 0) Statistics.Write("\n%6lu: ",ilong); if (ilong % 1000 == 999) { CurrentReadCount = QF.GetReadCount(); CurrentWriteCount = QF.GetWriteCount(); CurrentTicks = clock(); Statistics.Write("%lu:%lu:%lu ", CurrentTicks-PreviousTicks, CurrentReadCount-PreviousReadCount, CurrentWriteCount-PreviousWriteCount); PreviousTicks = CurrentTicks; PreviousReadCount = CurrentReadCount; PreviousWriteCount = CurrentWriteCount; } AcctNum = SaveKeys[ilong]; if (AcctNum == "") break; Element = DHArray[AcctNum]; DHArray[AcctNum] = ""; if (Element == "") ofs << "Key " << AcctNum << " not found" << endl; } DeleteCount = ilong - StartingElement; } QF.Flush(); Statistics.Write("\n"); CurrentReadCount = QF.GetReadCount(); CurrentWriteCount = QF.GetWriteCount(); End = clock(); fclose(infile); if (Command == "a") { Statistics.Write("\nTimer ticks to add %lu elements: %lu\n", CurrentTotalKeys-PreviousTotalKeys,StartRead-StartAdd); Statistics.Write("%lu duplicates found\n",Dup); Statistics.Write("Number of chain overflows: %lu\n", DynamicHashString::GetChainCount()); } if (Command == "r") Statistics.Write("\nTimer ticks to read %lu elements: %lu\n", ReadCount,End-StartRead); if (Command == "d") Statistics.Write("\nTimer ticks to delete %lu elements: %lu\n", DeleteCount,End-StartDelete); Statistics.Write("\nTotal ticks: %lu, total reads: %lu, total writes: %lu\n", End,CurrentReadCount, CurrentWriteCount); if (Command == "a" || Command == "i") Statistics.Write("\nTotal number of keys stored: %lu\n",CurrentTotalKeys); return(0); }