testclient.c 9.6 KB

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