testclient.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #include <WinSock2.h>
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include <time.h>
  5. char * HOST = "127.0.0.1";
  6. unsigned short PORT = 8080;
  7. static const char * RESOURCELIST[] = {
  8. "/hello.txt",
  9. "/imagetest/00.png",
  10. "/"
  11. };
  12. static const char * METHODLIST[] = {
  13. "GET",
  14. "POST"
  15. };
  16. static int CLIENTCOUNT = 20;
  17. static int TESTCYCLES = 50;
  18. static int RESOURCEINDEX = 0;
  19. static int METHODINDEX = 0;
  20. int sockvprintf(SOCKET soc, const char * fmt, va_list vl) {
  21. char buf[1024*8];
  22. int len = vsprintf_s(buf, sizeof(buf), fmt, vl);
  23. int ret = send(soc, buf, len, 0);
  24. return ret;
  25. }
  26. int sockprintf(SOCKET soc, const char * fmt, ...) {
  27. int ret = -1;
  28. va_list vl;
  29. va_start(vl, fmt);
  30. ret = sockvprintf(soc, fmt, vl);
  31. va_end(vl);
  32. return ret;
  33. }
  34. static struct sockaddr_in target = {0};
  35. static CRITICAL_SECTION cs = {0};
  36. static size_t expectedData = 0;
  37. static DWORD_PTR availableCPUs = 1;
  38. static DWORD_PTR totalCPUs = 1;
  39. static unsigned good = 0;
  40. static unsigned bad = 0;
  41. unsigned long postSize = 0;
  42. unsigned long extraHeadSize = 0;
  43. unsigned long queryStringSize = 0;
  44. unsigned long keep_alive = 1;
  45. int WINAPI ClientMain(void * clientNo) {
  46. SOCKET soc;
  47. time_t lastData;
  48. size_t totalData = 0;
  49. int isBody = 0;
  50. int isTest = (clientNo == 0);
  51. int cpu = ((int)clientNo) % 1000;
  52. int timeOut = 10;
  53. const char * resource = 0;
  54. const char * method = 0;
  55. unsigned long i;
  56. // Method: PUT or GET
  57. if (METHODINDEX < sizeof(METHODLIST)/sizeof(METHODLIST[0])) {
  58. method = METHODLIST[METHODINDEX];
  59. }
  60. if (method == 0) {
  61. EnterCriticalSection(&cs);
  62. printf("\r\nClient %u: bad method\a\r\n", (int)clientNo);
  63. LeaveCriticalSection(&cs);
  64. return 1;
  65. }
  66. // Resource
  67. if (RESOURCEINDEX < sizeof(RESOURCELIST)/sizeof(RESOURCELIST[0])) {
  68. resource = RESOURCELIST[RESOURCEINDEX];
  69. }
  70. if (resource == 0) {
  71. EnterCriticalSection(&cs);
  72. printf("\r\nClient %u: bad resource\a\r\n", (int)clientNo);
  73. LeaveCriticalSection(&cs);
  74. return 2;
  75. }
  76. // CPU
  77. if ((!isTest) && (((1ULL<<cpu) & availableCPUs)!=0)) {
  78. SetThreadAffinityMask(GetCurrentThread(), 1ULL<<cpu);
  79. }
  80. // TCP
  81. soc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  82. if (soc==INVALID_SOCKET) {
  83. EnterCriticalSection(&cs);
  84. printf("\r\nClient %u: cannot create socket\a\r\n", (int)clientNo);
  85. LeaveCriticalSection(&cs);
  86. return 3;
  87. }
  88. // comment in to disable Nagle:
  89. {int disable_Nagle = 1; setsockopt(soc, IPPROTO_TCP, TCP_NODELAY, (char *) &disable_Nagle, sizeof(disable_Nagle));}
  90. if (connect(soc, (SOCKADDR*)&target, sizeof(target))) {
  91. EnterCriticalSection(&cs);
  92. printf("\r\nClient %u: cannot connect to server %s:%u\a\r\n", (int)clientNo, HOST, PORT);
  93. LeaveCriticalSection(&cs);
  94. return 4;
  95. }
  96. for (i=0; i<((keep_alive>0)?keep_alive:1); i++) {
  97. // HTTP request
  98. if (queryStringSize>0) {
  99. sockprintf(soc, "%s %s?", method, resource);
  100. for (i=0;i<(queryStringSize/10);i++) {sockprintf(soc, "1234567890");}
  101. for (i=0;i<(queryStringSize%10);i++) {sockprintf(soc, "_");}
  102. sockprintf(soc, " HTTP/1.1\r\nHost: %s\r\n", HOST);
  103. } else {
  104. sockprintf(soc, "%s %s HTTP/1.1\r\nHost: %s\r\nConnection: Close\r\n", method, resource, HOST);
  105. }
  106. if (keep_alive) {
  107. sockprintf(soc, "Connection: Keep-Alive\r\n");
  108. } else {
  109. sockprintf(soc, "Connection: Close\r\n");
  110. }
  111. for (i=0;i<(extraHeadSize/25);i++) {sockprintf(soc, "Comment%04u: 1234567890\r\n", i % 10000);} /* omit (extraHeadSize%25) */
  112. if (!strcmp(method,"GET")) {
  113. sockprintf(soc, "\r\n");
  114. } else {
  115. // not GET
  116. sockprintf(soc, "Content-Length: %u\r\n\r\n", postSize);
  117. for (i=0;i<postSize/10;i++) {sockprintf(soc, "1234567890");}
  118. for (i=0;i<postSize%10;i++) {sockprintf(soc, ".");}
  119. timeOut += postSize/10000;
  120. }
  121. if (!keep_alive) {
  122. shutdown(soc, SD_SEND);
  123. } else {
  124. timeOut = 2;
  125. }
  126. // wait for response from the server
  127. totalData = 0;
  128. lastData = time(0);
  129. for (;;) {
  130. char buf[2048];
  131. int chunkSize = 0;
  132. unsigned long dataReady = 0;
  133. Sleep(1);
  134. if (ioctlsocket(soc, FIONREAD, &dataReady) < 0) break;
  135. if (dataReady) {
  136. chunkSize = recv(soc, buf, sizeof(buf), 0);
  137. if (chunkSize<0) {
  138. printf("Error: recv failed for client %i\r\n", (int)clientNo);
  139. break;
  140. } else if (!isBody) {
  141. char * headEnd = strstr(buf,"\xD\xA\xD\xA");
  142. if (headEnd) {
  143. headEnd+=4;
  144. chunkSize -= ((int)headEnd - (int)buf);
  145. if (chunkSize>0) {
  146. totalData += chunkSize;
  147. lastData = time(0);
  148. //fwrite(headEnd,1,got,STORE);
  149. }
  150. isBody=1;
  151. }
  152. } else {
  153. totalData += chunkSize;
  154. lastData = time(0);
  155. //fwrite(buf,1,got,STORE);
  156. }
  157. } else {
  158. time_t current = time(0);
  159. if (difftime(current, lastData) > timeOut) break;
  160. }
  161. }
  162. if (keep_alive) {
  163. Sleep(1000);
  164. }
  165. }
  166. shutdown(soc, SD_BOTH);
  167. closesocket(soc);
  168. EnterCriticalSection(&cs);
  169. if (isTest) {
  170. expectedData = totalData;
  171. } else if (totalData != expectedData) {
  172. printf("Error: Client %u got %u bytes instead of %u\r\n", (int)clientNo, totalData, expectedData);
  173. bad++;
  174. } else {
  175. good++;
  176. }
  177. LeaveCriticalSection(&cs);
  178. return 0;
  179. }
  180. void RunMultiClientTest(int loop) {
  181. HANDLE *hThread = calloc(CLIENTCOUNT, sizeof(hThread[0]));
  182. int i;
  183. DWORD res;
  184. for (i=0;i<CLIENTCOUNT;i++) {
  185. DWORD dummy;
  186. hThread[i] = CreateThread(NULL, 1024*32, (LPTHREAD_START_ROUTINE)ClientMain, (void*)(1000*loop+i), 0, &dummy);
  187. }
  188. WaitForMultipleObjects(CLIENTCOUNT, hThread, TRUE, 15000);
  189. for (i=0;i<CLIENTCOUNT;i++) {
  190. res = WaitForSingleObject(hThread[i], 0);
  191. if (res == WAIT_OBJECT_0) {
  192. CloseHandle(hThread[i]);
  193. hThread[i]=0;
  194. }
  195. }
  196. for (i=0;i<CLIENTCOUNT;i++) {
  197. if (hThread[i]) {
  198. EnterCriticalSection(&cs);
  199. SuspendThread(hThread[i]); // -> check this thread in the debugger
  200. printf("Thread %i did not finish!\r\n", (int)(1000*loop+i));
  201. LeaveCriticalSection(&cs);
  202. }
  203. }
  204. EnterCriticalSection(&cs);
  205. printf("Test cylce %u completed\r\n\r\n", loop);
  206. LeaveCriticalSection(&cs);
  207. free(hThread);
  208. }
  209. int MultiClientTestAutomatic(unsigned long initialPostSize) {
  210. FILE * log;
  211. int cycle;
  212. postSize = initialPostSize;
  213. do {
  214. printf("Preparing test with %u bytes of data ...", postSize);
  215. ClientMain(0);
  216. if (expectedData==0) {
  217. printf(" Error: Could not read any data\a\r\n");
  218. return 1;
  219. }
  220. printf(" OK: %u bytes of data\r\n", expectedData);
  221. printf("Starting multi client test: %i cycles, %i clients each\r\n\r\n", (int)TESTCYCLES, (int)CLIENTCOUNT);
  222. good=bad=0;
  223. for (cycle=1;cycle<=TESTCYCLES;cycle++) {
  224. RunMultiClientTest(cycle);
  225. }
  226. printf("\r\n--------\r\n%u errors\r\n%u OK\r\n--------\r\n\r\n", bad, good);
  227. log = fopen("testclient.log", "at");
  228. if (log) {
  229. fprintf(log, "%u\t%u\t%u\r\n", postSize, good, bad);
  230. fclose(log);
  231. }
  232. postSize = (postSize!=0) ? (postSize<<1) : 1;
  233. } while (postSize!=0);
  234. return 0;
  235. }
  236. int SingleClientTestAutomatic(unsigned long initialPostSize) {
  237. FILE * log;
  238. int cycle;
  239. int i;
  240. postSize = initialPostSize;
  241. for (cycle=0;;cycle++) {
  242. good=bad=0;
  243. for (i=0;i<1000;i++) {
  244. expectedData=3;
  245. ClientMain((void*)1);
  246. }
  247. log = fopen("testclient.log", "at");
  248. if (log) {
  249. fprintf(log, "Cylce<%u>\t%u\t%u\r\n", cycle, good, bad);
  250. fclose(log);
  251. }
  252. printf("test cycle %u: %u good, %u bad\r\n", cycle, good, bad);
  253. }
  254. return 0;
  255. }
  256. int main(int argc, char * argv[]) {
  257. WSADATA wsaData = {0};
  258. HOSTENT * lpHost = 0;
  259. if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) {
  260. printf("\r\nCannot init WinSock\a\r\n");
  261. return 1;
  262. }
  263. lpHost = gethostbyname(HOST);
  264. if (lpHost == NULL) {
  265. printf("\r\nCannot find host %s\a\r\n",HOST);
  266. return 2;
  267. }
  268. target.sin_family = AF_INET;
  269. target.sin_addr.s_addr = *((u_long FAR *) (lpHost->h_addr));
  270. target.sin_port = htons(PORT);
  271. GetProcessAffinityMask(GetCurrentProcess(), &availableCPUs, &totalCPUs);
  272. printf("CPUs (bit masks): process=%x, system=%x\r\n", availableCPUs, totalCPUs);
  273. InitializeCriticalSectionAndSpinCount(&cs, 100);
  274. /* Do the actual test here */
  275. if (CLIENTCOUNT>0) {
  276. MultiClientTestAutomatic(2000);
  277. } else {
  278. SingleClientTestAutomatic(2000);
  279. }
  280. /* Cleanup */
  281. DeleteCriticalSection(&cs);
  282. WSACleanup();
  283. return 0;
  284. }