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.

230 lines
6.5 KiB

  1. //////////////////////////////////////////////////////////////////////
  2. // File: stressMain.cpp
  3. //
  4. // Copyright (c) 2001 Microsoft Corporation. All Rights Reserved.
  5. //
  6. // Purpose:
  7. // This is an empty template for WinHttp stressScheduler stress apps.
  8. // The stress test lives in WinHttp_StressTest() and will be called
  9. // repeatedly in the main function.
  10. //
  11. // This process will inherit a named event handle from
  12. // stressScheduler in the form: "ExitProcessEvent" + <PID of this process>.
  13. // When the stressScheduler sets the object state to signaled, then
  14. // the stress test application must exit immediately.
  15. //
  16. // If this app is running without the stressScheduler, use the
  17. // "/s" switch to run the standalone mode. The app will exit when the user
  18. // sends a break message (CTRL-C).
  19. //
  20. // This stress test will continue to run if:
  21. //
  22. // When not using any switches:
  23. // - The "ExitProcessEvent" object inherited from stressScheduler is in the un-signaled state
  24. // - WinHttp_StressTest() returns true
  25. //
  26. // When using the "/s" standalone switch:
  27. // - WinHttp_StressTest() returns true
  28. //
  29. // History:
  30. // 03/30/01 DennisCh Created
  31. //
  32. //////////////////////////////////////////////////////////////////////
  33. //////////////////////////////////////////////////////////////////////
  34. // Includes
  35. //////////////////////////////////////////////////////////////////////
  36. // Project headers
  37. #include "stressMain.h"
  38. //////////////////////////////////////////////////////////////////////
  39. // Globals and statics
  40. //////////////////////////////////////////////////////////////////////
  41. // ****************************
  42. // ** The name of the stress test and your stress test function
  43. // ** should be declared in a seperate file.
  44. extern LPSTR g_szStressTestName;
  45. extern BOOL WinHttp_StressTest();
  46. // ****************************
  47. // ** hande to the name exit event object inherited from the stressScheduler
  48. HANDLE g_hExitEvent = NULL;
  49. // ****************************
  50. // ** FALSE = run with stressScheduler, TRUE = run without stressScheduler
  51. BOOL g_bStandAloneMode = FALSE;
  52. ////////////////////////////////////////////////////////////
  53. // Function: LogText(DWORD, LPCSTR)
  54. //
  55. // Purpose:
  56. // Prints text.
  57. //
  58. ////////////////////////////////////////////////////////////
  59. VOID
  60. LogText(
  61. LPCSTR szLogText,
  62. ...
  63. )
  64. {
  65. CHAR szBuffer[1024] = {0};
  66. va_list vaList;
  67. if (!szLogText)
  68. return;
  69. va_start( vaList, szLogText );
  70. _vsnprintf( szBuffer, sizeof(szBuffer), szLogText, vaList );
  71. printf("%s\n", szBuffer);
  72. va_end(vaList);
  73. }
  74. ////////////////////////////////////////////////////////////
  75. // Function: GetExitEventHandle()
  76. //
  77. // Purpose:
  78. // This opens a named event object inherited from the stressScheduler.
  79. // The object is in the form: "ExitProcessEvent" + <PID of current process>
  80. // By default this is in the unsignaled state. When the stressScheduler
  81. // sets it to signaled, then it's time for the stress App to exit.
  82. //
  83. ////////////////////////////////////////////////////////////
  84. HANDLE
  85. GetExitEventHandle()
  86. {
  87. CHAR szPID[32];
  88. CHAR szExitProcessName[sizeof(EXIT_PROCESS_EVENT_NAME) + sizeof(szPID)];
  89. HANDLE hExitEvent = NULL;
  90. // if user used the "/S" switch, we run without the stressScheduler and exit when user
  91. // tells us to instead. No need to get inherited event object from stressScheduler
  92. if (g_bStandAloneMode)
  93. return NULL;
  94. // wait for the stressScheduler to create the event object before trying to obtain it.
  95. Sleep(4000);
  96. // Get the processID string
  97. _itoa(_getpid(), szPID, 10);
  98. // build ExitProcess event object name
  99. ZeroMemory(szExitProcessName, sizeof(szExitProcessName));
  100. strcpy(szExitProcessName, EXIT_PROCESS_EVENT_NAME);
  101. strcat(szExitProcessName, szPID);
  102. LogText("\n[ Opening inherited named event object \"%s\". ]", szExitProcessName);
  103. hExitEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, szExitProcessName);
  104. if (!hExitEvent)
  105. LogText("[ ERROR: OpenEvent() failed to open object \"%s\". GetLastError() = %u ]\n", szExitProcessName, GetLastError());
  106. else
  107. LogText("[ OpenEvent() opened object \"%s\". ] \n", szExitProcessName);
  108. return hExitEvent;
  109. }
  110. ////////////////////////////////////////////////////////////
  111. // Function: IsTimeToExitStress()
  112. //
  113. // Purpose:
  114. // Returns TRUE if the exit event object is signaled or NULL. FALSE if not.
  115. // The object is in the form: "ExitProcessEvent" + <PID of current process>
  116. // By default this is in the unsignaled state. When the stressScheduler
  117. // sets it to signaled, then it's time for the stress App to exit.
  118. //
  119. ////////////////////////////////////////////////////////////
  120. BOOL
  121. IsTimeToExitStress()
  122. {
  123. BOOL bResult = FALSE;
  124. // if user used the "/S" switch, we run without the stressScheduler and exit when user
  125. // tells us to instead.
  126. if (g_bStandAloneMode)
  127. return FALSE;
  128. if (!g_hExitEvent)
  129. {
  130. bResult = TRUE;
  131. goto Exit;
  132. }
  133. if (WAIT_OBJECT_0 == WaitForSingleObject(g_hExitEvent, 0))
  134. bResult = TRUE;
  135. Exit:
  136. return bResult;
  137. }
  138. ////////////////////////////////////////////////////////////
  139. // Function: main(INT, LPSTR)
  140. //
  141. // Purpose:
  142. // Program entry point.
  143. //
  144. ////////////////////////////////////////////////////////////
  145. INT
  146. main(
  147. INT argc,
  148. LPSTR argv[]
  149. )
  150. {
  151. DWORD dwIndex = 0;
  152. // **************************
  153. // **************************
  154. // ** Parse command line arguments
  155. // **
  156. if (argc >= 2)
  157. {
  158. // print out options
  159. if (0 == stricmp(argv[1], "/?") || 0 == stricmp(argv[1], "-?"))
  160. {
  161. LogText("USAGE: '/S' to run in standalone mode with the stressScheduler.\n\n");
  162. goto Exit;
  163. }
  164. // run in standalone mode without stressScheduler
  165. if (0 == stricmp(argv[1], "/S") || 0 == stricmp(argv[1], "-S"))
  166. {
  167. LogText("[ Running in standalone mode. \"/S\" switch used. ]\n\n");
  168. g_bStandAloneMode = TRUE;
  169. }
  170. }
  171. // **************************
  172. // **************************
  173. // ** open the exit event object inherited from WinHttpStressScheduler
  174. // **
  175. g_hExitEvent = GetExitEventHandle();
  176. if (!g_bStandAloneMode && !g_hExitEvent)
  177. goto Exit;
  178. // **************************
  179. // **************************
  180. // ** run the stress test until stressScheduler tells us to exit or the stress app does
  181. // **
  182. while (!IsTimeToExitStress() && WinHttp_StressTest())
  183. LogText("[ Running stressExe \"%s\" iteration #%u ]\n", g_szStressTestName, ++dwIndex);
  184. Exit:
  185. if (g_hExitEvent)
  186. CloseHandle(g_hExitEvent);
  187. LogText("[ Exiting test case \"%s\" ]", g_szStressTestName);
  188. return 0;
  189. }