1 // Command line application that just hammers the user logging
4 // Copyright 2005 by Keith Vertanen
7 #include "../Common/Common.h"
16 // Declare our global file logging object (declared in DasherInterfaceBase.cpp)
17 #include "FileLogger.h"
18 extern CFileLogger
* g_pLogger
;
20 #include "../../DasherCore/Alphabet/Alphabet.h"
21 #include "../../DasherCore/Alphabet/AlphIO.h"
32 // In order to track leaks to line number, we need this at the top of every file
33 #include "MemoryLeak.h"
37 static char THIS_FILE
[] = __FILE__
;
44 typedef vector
<string
> VECTOR_STRING
;
47 int GetRandomInt(int lower
, int upper
);
48 string
GetRandomString(int lowerLength
, int upperLength
);
50 int GetRandomInt(int lower
, int upper
)
52 return rand() % (upper
- lower
+ 1) + lower
;
55 float GetRandomFloat(int lower
, int upper
)
57 return ((float) rand() / (float) RAND_MAX
) * (float) (upper
- lower
) + (float) lower
;
60 int GetRandomBitMask(int bits
)
64 for (int i
= 0; i
< bits
; i
++)
74 string
GetRandomString(int lowerLength
, int upperLength
)
76 string strResult
= "";
77 int len
= GetRandomInt(lowerLength
, upperLength
);
79 for (int i
= 0; i
< len
; i
++)
80 strResult
+= (char) GetRandomInt(65, 90);
85 int main(int argc
, char* argv
[])
89 // Windows debug build memory leak detection
90 EnableLeakDetection();
94 // Normally this is created in DasherInterfaceBase, but we aren't instantiating
95 // that class for this test harness program.
96 g_pLogger
= new CFileLogger("UserLogLoadTest.log", logDEBUG
, logTimeStamp
| logDateStamp
| logDeleteOldFile
);
98 { // memory leak scoping
100 // Set the defaults for the switched parameters
101 string strAlphabetFilename
= "alphabet.english.xml";
102 string strAlphabetName
= "English alphabet - limited punctuation";
107 cout << "UserLogLoadTest" << endl;
108 cout << " -alphabet <alphabet XML> [" << strAlphabetFilename << "]" << endl;
109 cout << " -alphabetName <alphabet name> [" << strAlphabetName << "]" << endl;
116 // Parse the command line options
120 string strParam
= makeLower(argv
[i
]);
121 string strNextParam
= "";
123 strNextParam
= argv
[i
+1];
125 if (strParam
.compare("-alphabet") == 0)
127 strAlphabetFilename
= strNextParam
;
130 else if (strParam
.compare("-alphabetname") == 0)
132 strAlphabetName
= strNextParam
;
137 // Failure to parse this parameter
138 cout
<< "Unknown switch: " << strParam
<< endl
;
144 ::SetPriorityClass(GetCurrentThread(), BELOW_NORMAL_PRIORITY_CLASS
);
147 // We need an alphabet object in order to create the UserLog object
149 // Load our alphabet from the specified XML file
150 VECTOR_STRING vectorFilenames
;
151 vectorFilenames
.push_back(strAlphabetFilename
);
152 Dasher::CAlphIO
* pAlphIO
= new Dasher::CAlphIO("", "", vectorFilenames
);
155 g_pLogger
->Log("Failed to create alphIO from '%s'", logNORMAL
, strAlphabetFilename
.c_str());
159 Dasher::CAlphabet
* pAlphabet
= new Dasher::CAlphabet(pAlphIO
->GetInfo(strAlphabetName
));
160 if (pAlphabet
== NULL
)
162 g_pLogger
->Log("Failed to create alphabet from '%s'", logNORMAL
, strAlphabetName
.c_str());
166 srand((unsigned) time(NULL
));
168 // CDasherInterface* pDasherInterface = NULL;
169 CUserLog
* pUserLog
= NULL
;
171 unsigned long count
= 0;
172 VECTOR_STRING vectorParams
;
173 bool bWriting
= false;
176 cout
<< "Hit 'q' to quit.\n";
183 if (pUserLog
== NULL
)
186 // pDasherInterface = new CDasherInterface();
187 // pUserLog = pDasherInterface->GetUserLogPtr();
189 pUserLog
= new CUserLog(NULL
, NULL
, GetRandomInt(1, 3), pAlphabet
);
191 // Set a random user log level for use next time we create DasherInterface
192 // pDasherInterface->SetLongParameter(LP_USER_LOG_LEVEL_MASK, GetRandomInt(1, 3));
194 // pDasherInterface->ChangeAlphabet(pDasherInterface->GetStringParameter(SP_ALPHABET_ID));
196 // We need to create a new user log object
197 // pUserLog = new CUserLog(GetRandomInt(1, 3), pAlphabet);
198 pUserLog
->SetOuputFilename("loadtest.xml");
202 // Randomly choose something to do
203 int action
= GetRandomInt(0, 16);
209 // Output our file and delete the object
210 pUserLog
->OutputFile();
212 // delete pDasherInterface;
213 // pDasherInterface = NULL;
217 vectorParams
.clear();
223 pUserLog
->AddCanvasSize(GetRandomInt(0, 100), GetRandomInt(0, 100), GetRandomInt(0, 100), GetRandomInt(0, 100));
228 pUserLog
->AddWindowSize(GetRandomInt(0, 100), GetRandomInt(0, 100), GetRandomInt(0, 100), GetRandomInt(0, 100));
233 pUserLog
->AddMouseLocation(GetRandomInt(0, 100), GetRandomInt(0, 100), GetRandomFloat(-100, 100));
238 pUserLog
->AddCanvasSize(GetRandomInt(0, 100), GetRandomInt(0, 100), GetRandomInt(0, 100), GetRandomInt(0, 100));
239 pUserLog
->AddMouseLocationNormalized(GetRandomInt(0, 100), GetRandomInt(0, 100), (bool) GetRandomInt(0, 1), GetRandomFloat(-100, 100));
244 // Either use a parameter name we've already used, or create a new one
245 string strParamName
= "";
246 if ((vectorParams
.size() <= 0) || (rand() % 2 == 0))
248 strParamName
= GetRandomString(4, 40);
249 vectorParams
.push_back(strParamName
);
253 strParamName
= (string
) vectorParams
[GetRandomInt(0, vectorParams
.size() - 1)];
259 pUserLog
->AddParam(strParamName
, GetRandomInt(0, 100), GetRandomBitMask(5));
262 pUserLog
->AddParam(strParamName
, GetRandomFloat(-100, 100), GetRandomBitMask(5));
265 pUserLog
->AddParam(strParamName
, GetRandomString(4, 40), GetRandomBitMask(5));
275 pUserLog
->StartWriting();
280 pUserLog
->StopWriting();
294 pUserLog
->StartWriting();
298 Dasher::VECTOR_SYMBOL_PROB vectorAdded
;
299 int numToAdd
= GetRandomInt(1, 5);
300 for (int i
= 0; i
< numToAdd
; i
++)
302 Dasher::SymbolProb symProb
;
303 symProb
.prob
= GetRandomFloat(0, 1);
304 symProb
.sym
= rand() % pAlphabet
->GetNumberSymbols();
305 vectorAdded
.push_back(symProb
);
307 pUserLog
->AddSymbols(&vectorAdded
);
315 pUserLog
->StartWriting();
318 pUserLog
->DeleteSymbols(GetRandomInt(1, 3));
326 pUserLog
->StopWriting();
329 pUserLog
->NewTrial();
338 if (count
% 100 == 0)
340 printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bCount: %d", count
);
342 // Look for a file that indicates we should shutdown
344 fp
= fopen("test.die", "r");
361 if (pUserLog
!= NULL
)
367 if (pAlphabet
!= NULL
)
381 if (g_pLogger
!= NULL
)