tagging release
[dasher.git] / Src / Tools / UserLogLoadTest / main.cpp
blobb3f20fa222d5fb3452d2371ef4eefd4a5cfda7c3
1 // Command line application that just hammers the user logging
2 // related objects.
3 //
4 // Copyright 2005 by Keith Vertanen
5 //
7 #include "../Common/Common.h"
9 #include <string>
10 #include <vector>
12 #ifdef _WIN32
13 #include <conio.h>
14 #endif
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"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <time.h>
26 #include <iostream>
28 #include "UserLog.h"
29 #include "Utils.h"
31 #ifdef _WIN32
32 // In order to track leaks to line number, we need this at the top of every file
33 #include "MemoryLeak.h"
34 #ifdef _DEBUG
35 #define new DEBUG_NEW
36 #undef THIS_FILE
37 static char THIS_FILE[] = __FILE__;
38 #endif
39 #endif
41 using namespace std;
43 #ifndef VECTOR_STRING
44 typedef vector<string> VECTOR_STRING;
45 #endif
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)
62 int result = 0;
63 int power = 1;
64 for (int i = 0; i < bits; i++)
66 if (rand() % 2 == 0)
67 result += power;
69 power = power * 2;
71 return result;
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);
82 return strResult;
85 int main(int argc, char* argv[])
87 #ifdef _WIN32
88 #ifdef _DEBUG
89 // Windows debug build memory leak detection
90 EnableLeakDetection();
91 #endif
92 #endif
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";
105 if (argc <= 2)
107 cout << "UserLogLoadTest" << endl;
108 cout << " -alphabet <alphabet XML> [" << strAlphabetFilename << "]" << endl;
109 cout << " -alphabetName <alphabet name> [" << strAlphabetName << "]" << endl;
111 cout << endl;
112 return 0;
116 // Parse the command line options
117 int i = 1;
118 while (i < argc)
120 string strParam = makeLower(argv[i]);
121 string strNextParam = "";
122 if (i < argc - 1)
123 strNextParam = argv[i+1];
125 if (strParam.compare("-alphabet") == 0)
127 strAlphabetFilename = strNextParam;
128 i++;
130 else if (strParam.compare("-alphabetname") == 0)
132 strAlphabetName = strNextParam;
133 i++;
135 else
137 // Failure to parse this parameter
138 cout << "Unknown switch: " << strParam << endl;
141 i++;
143 #ifdef _WIN32
144 ::SetPriorityClass(GetCurrentThread(), BELOW_NORMAL_PRIORITY_CLASS);
145 #endif
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);
153 if (pAlphIO == NULL)
155 g_pLogger->Log("Failed to create alphIO from '%s'", logNORMAL, strAlphabetFilename.c_str());
156 return 0;
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());
163 return 0;
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;
175 #ifdef _WIN32
176 cout << "Hit 'q' to quit.\n";
177 #endif
179 while (1)
181 count++;
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");
200 else
202 // Randomly choose something to do
203 int action = GetRandomInt(0, 16);
205 switch (action)
207 case 0:
209 // Output our file and delete the object
210 pUserLog->OutputFile();
212 // delete pDasherInterface;
213 // pDasherInterface = NULL;
214 delete pUserLog;
216 pUserLog = NULL;
217 vectorParams.clear();
218 bWriting = false;
219 break;
221 case 1:
223 pUserLog->AddCanvasSize(GetRandomInt(0, 100), GetRandomInt(0, 100), GetRandomInt(0, 100), GetRandomInt(0, 100));
224 break;
226 case 2:
228 pUserLog->AddWindowSize(GetRandomInt(0, 100), GetRandomInt(0, 100), GetRandomInt(0, 100), GetRandomInt(0, 100));
229 break;
231 case 3:
233 pUserLog->AddMouseLocation(GetRandomInt(0, 100), GetRandomInt(0, 100), GetRandomFloat(-100, 100));
234 break;
236 case 4:
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));
240 break;
242 case 5:
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);
251 else
253 strParamName = (string) vectorParams[GetRandomInt(0, vectorParams.size() - 1)];
256 switch (rand() % 3)
258 case 0:
259 pUserLog->AddParam(strParamName, GetRandomInt(0, 100), GetRandomBitMask(5));
260 break;
261 case 1:
262 pUserLog->AddParam(strParamName, GetRandomFloat(-100, 100), GetRandomBitMask(5));
263 break;
264 case 2:
265 pUserLog->AddParam(strParamName, GetRandomString(4, 40), GetRandomBitMask(5));
266 break;
269 break;
271 case 6:
273 if (!bWriting)
275 pUserLog->StartWriting();
276 bWriting = true;
278 else
280 pUserLog->StopWriting();
281 bWriting = false;
283 break;
285 case 7:
286 case 8:
287 case 9:
288 case 10:
289 case 11:
290 case 12:
292 if (!bWriting)
294 pUserLog->StartWriting();
295 bWriting = true;
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);
308 break;
310 case 13:
311 case 14:
313 if (!bWriting)
315 pUserLog->StartWriting();
316 bWriting = true;
318 pUserLog->DeleteSymbols(GetRandomInt(1, 3));
319 break;
321 case 15:
322 case 16:
324 if (bWriting)
326 pUserLog->StopWriting();
327 bWriting = false;
329 pUserLog->NewTrial();
330 break;
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);
341 #ifndef _WIN32
342 // Look for a file that indicates we should shutdown
343 FILE* fp = NULL;
344 fp = fopen("test.die", "r");
345 if (fp != NULL)
347 fclose(fp);
348 break;
350 #endif
352 #ifdef _WIN32
353 if (kbhit() > 0)
355 if (getch() == 'q')
356 break;
358 #endif
361 if (pUserLog != NULL)
363 delete pUserLog;
364 pUserLog = NULL;
367 if (pAlphabet != NULL)
369 delete pAlphabet;
370 pAlphabet = NULL;
373 if (pAlphIO != NULL)
375 delete pAlphIO;
376 pAlphIO = NULL;
381 if (g_pLogger != NULL)
383 delete g_pLogger;
384 g_pLogger = NULL;
387 return 0;