testclient.c 10 KB

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