Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

347 lines
7.7 KiB

  1. //+------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1994.
  5. //
  6. // File: bm_parse.cxx
  7. //
  8. // Contents: Implementation of Base class for generic parsers
  9. //
  10. // Classes: CTimerBase
  11. //
  12. // Functions:
  13. //
  14. // History: 16-June-94 t-vadims Created
  15. //
  16. //
  17. //--------------------------------------------------------------------------
  18. #include <benchmrk.hxx>
  19. #include <bm_parse.hxx>
  20. #define MAX_INSTR_LENGTH 150
  21. #define BLANK_LINE (FIRST_INTERNALID + 2)
  22. //
  23. // Structure for linked list of instructions with their timings
  24. //
  25. struct SInstruction
  26. {
  27. ULONG ulID;
  28. ULONG ulTime[TEST_MAX_ITERATIONS];
  29. SInstruction *pNext;
  30. };
  31. //+-------------------------------------------------------------------
  32. //
  33. // Member: CTimerBase::Setup, public
  34. //
  35. // Synopsis: Makes all neccessary initializations.
  36. //
  37. // History: 16-June-94 t-vadims Created
  38. //
  39. //--------------------------------------------------------------------
  40. SCODE CTimerBase::Setup (CTestInput *pInput)
  41. {
  42. SCODE sc;
  43. char szFileName[80];
  44. TCHAR szBuf[80];
  45. CTestBase::Setup(pInput);
  46. m_iIterations = pInput->GetIterations(Name());
  47. // get name of the script file
  48. pInput->GetConfigString(Name(), TEXT("ScriptName"), TEXT("script.txt"), szBuf, 80);
  49. #ifdef UNICODE
  50. wcstombs(szFileName, szBuf, 80);
  51. #else
  52. strcpy(szFileName, szBuf);
  53. #endif
  54. m_fpIn = fopen(szFileName, "r");
  55. if(m_fpIn == NULL)
  56. {
  57. Log(TEXT("Can't open script file"), STG_E_FILENOTFOUND);
  58. return STG_E_FILENOTFOUND;
  59. }
  60. m_pParser = NULL;
  61. sc = SetParserObject(); // virtual call to setup m_pParser object.
  62. if(m_pParser == NULL)
  63. sc = E_FAIL;
  64. if(FAILED(sc))
  65. {
  66. Log(TEXT("Setup failed to initialize parser object"), sc);
  67. fclose(m_fpIn);
  68. return sc;
  69. }
  70. #ifdef THREADING_SUPPORT
  71. OleInitializeEx(NULL, pInput->GetOleInitFlag());
  72. #else
  73. OleInitialize(NULL);
  74. #endif
  75. sc = m_pParser->Setup(pInput);
  76. if(FAILED(sc))
  77. {
  78. Log(TEXT("Setup of Parser object failed"), sc);
  79. DeleteParserObject();
  80. OleUninitialize();
  81. fclose(m_fpIn);
  82. return sc;
  83. }
  84. m_pHead = NULL;
  85. m_iLine = 0;
  86. return S_OK;
  87. }
  88. //+-------------------------------------------------------------------
  89. //
  90. // Member: CTimerBase::Cleanup, public
  91. //
  92. // Synopsis: Clean everything up.
  93. //
  94. // History: 16-June-94 t-vadims Created
  95. //
  96. //--------------------------------------------------------------------
  97. SCODE CTimerBase::Cleanup ()
  98. {
  99. SInstruction *pInstr, *pNextInstr;
  100. pInstr = m_pHead;
  101. while (pInstr != NULL)
  102. {
  103. pNextInstr = pInstr->pNext;
  104. delete pInstr;
  105. pInstr = pNextInstr;
  106. }
  107. m_pHead = NULL;
  108. m_pParser->Cleanup();
  109. DeleteParserObject();
  110. fclose (m_fpIn);
  111. OleUninitialize();
  112. return S_OK;
  113. }
  114. //+-------------------------------------------------------------------
  115. //
  116. // Member: CTimerBase::Run, public
  117. //
  118. // Synopsis: Read and execute the script file.
  119. //
  120. // History: 16-June-94 t-vadims Created
  121. //
  122. //--------------------------------------------------------------------
  123. SCODE CTimerBase::Run ()
  124. {
  125. ReadFile ();
  126. ExecuteFile ();
  127. return S_OK;
  128. }
  129. BOOL CTimerBase::IsEmptyLine (LPTSTR pszLine)
  130. {
  131. while (*pszLine)
  132. {
  133. if ( *pszLine != TEXT(' ') && *pszLine != TEXT('\n') &&
  134. *pszLine != TEXT('\t'))
  135. return FALSE;
  136. pszLine++;
  137. }
  138. return TRUE;
  139. }
  140. BOOL CTimerBase::IsInternalID (ULONG ulID)
  141. {
  142. return (ulID >= FIRST_INTERNALID);
  143. }
  144. //+-------------------------------------------------------------------
  145. //
  146. // Member: CTimerBase::ReadFile, private
  147. //
  148. // Synopsis: Reads script file, adding each instruction to the
  149. // link list.
  150. //
  151. // History: 16-June-94 t-vadims Created
  152. //
  153. //--------------------------------------------------------------------
  154. SCODE CTimerBase::ReadFile ()
  155. {
  156. TCHAR szBuf[MAX_INSTR_LENGTH];
  157. SInstruction *pTail = NULL;
  158. ULONG ulID;
  159. while ( SUCCEEDED(GetNextLine(szBuf))) // get line from file to szBuf
  160. {
  161. m_iLine ++;
  162. if (IsEmptyLine(szBuf))
  163. {
  164. pTail = AddNewInstruction(pTail, BLANK_LINE);
  165. }
  166. else
  167. {
  168. ulID = m_pParser->ParseNewInstruction(szBuf);
  169. if (ulID == INVALID_INSTRUCTION)
  170. {
  171. wsprintf(szBuf, TEXT("Invalid instruction on line %d"), m_iLine );
  172. Log(szBuf, E_FAIL);
  173. }
  174. else if(ulID != NOT_INSTRUCTION) // valid instruction
  175. {
  176. pTail = AddNewInstruction(pTail, ulID);
  177. }
  178. }
  179. }
  180. return S_OK;
  181. }
  182. //+-------------------------------------------------------------------
  183. //
  184. // Member: CTimerBase::AddNewInstruction, private
  185. //
  186. // Synopsis: Adds new instruction to linked list
  187. //
  188. // History: 16-June-94 t-vadims Created
  189. //
  190. //--------------------------------------------------------------------
  191. SInstruction *CTimerBase::AddNewInstruction(SInstruction *pTail, ULONG ulID)
  192. {
  193. SInstruction *pInstruction = new SInstruction;
  194. pInstruction->ulID = ulID;
  195. pInstruction->pNext = NULL;
  196. INIT_RESULTS(pInstruction->ulTime);
  197. if (m_pHead == NULL) // first instruction
  198. {
  199. m_pHead = pTail = pInstruction;
  200. }
  201. else
  202. {
  203. pTail->pNext = pInstruction;
  204. pTail = pInstruction;
  205. }
  206. return pTail;
  207. }
  208. //+-------------------------------------------------------------------
  209. //
  210. // Member: CTimerBase::GetNextLine, private
  211. //
  212. // Synopsis: Reads the next line from the file.
  213. // Returns E_FAIL on end of file
  214. //
  215. // History: 16-June-94 t-vadims Created
  216. //
  217. //--------------------------------------------------------------------
  218. SCODE CTimerBase::GetNextLine(LPTSTR pszLine)
  219. {
  220. #ifdef UNICODE
  221. CHAR szBuf[MAX_INSTR_LENGTH];
  222. if (fgets(szBuf, MAX_INSTR_LENGTH, m_fpIn) != NULL)
  223. {
  224. mbstowcs(pszLine, szBuf, MAX_INSTR_LENGTH);
  225. return S_OK;
  226. }
  227. #else
  228. if (fgets(pszLine, MAX_INSTR_LENGTH, m_fpIn) != NULL)
  229. {
  230. return S_OK;
  231. }
  232. #endif
  233. else
  234. return E_FAIL;
  235. }
  236. //+-------------------------------------------------------------------
  237. //
  238. // Member: CTimerBase::ExecuteFile, private
  239. //
  240. // Synopsis: Loops throug the linked list execute each command, and
  241. // recording timings.
  242. //
  243. // History: 16-June-94 t-vadims Created
  244. //
  245. //--------------------------------------------------------------------
  246. SCODE CTimerBase::ExecuteFile()
  247. {
  248. ULONG iIter;
  249. SInstruction *pInstr;
  250. for (iIter = 0; iIter < m_iIterations; iIter++)
  251. {
  252. pInstr = m_pHead;
  253. while (pInstr != NULL)
  254. {
  255. if (!IsInternalID(pInstr->ulID))
  256. pInstr->ulTime[iIter] = m_pParser->ExecuteInstruction(pInstr->ulID);
  257. pInstr = pInstr->pNext;
  258. }
  259. }
  260. return S_OK;
  261. }
  262. //+-------------------------------------------------------------------
  263. //
  264. // Member: CTimerBase::Report, public
  265. //
  266. // Synopsis: Loops throug the linked list, outputing timings of each command.
  267. //
  268. // History: 16-June-94 t-vadims Created
  269. //
  270. //--------------------------------------------------------------------
  271. SCODE CTimerBase::Report (CTestOutput &output)
  272. {
  273. SInstruction *pInstr = m_pHead;
  274. output.WriteSectionHeader (Name(), SectionHeader(), *m_pInput);
  275. output.WriteString (TEXT("\n"));
  276. while (pInstr != NULL)
  277. {
  278. if (pInstr->ulID == BLANK_LINE)
  279. output.WriteString (TEXT("\n"));
  280. else
  281. output.WriteResults (m_pParser->InstructionName(pInstr->ulID),
  282. m_iIterations, pInstr->ulTime);
  283. pInstr = pInstr->pNext;
  284. }
  285. return S_OK;
  286. }