lib500.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "test.h"
  23. #include "memdebug.h"
  24. #ifdef LIB585
  25. static int counter;
  26. static curl_socket_t tst_opensocket(void *clientp,
  27. curlsocktype purpose,
  28. struct curl_sockaddr *addr)
  29. {
  30. (void)clientp;
  31. (void)purpose;
  32. printf("[OPEN] counter: %d\n", ++counter);
  33. return socket(addr->family, addr->socktype, addr->protocol);
  34. }
  35. static int tst_closesocket(void *clientp, curl_socket_t sock)
  36. {
  37. (void)clientp;
  38. printf("[CLOSE] counter: %d\n", counter--);
  39. return sclose(sock);
  40. }
  41. static void setupcallbacks(CURL *curl)
  42. {
  43. curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, tst_opensocket);
  44. curl_easy_setopt(curl, CURLOPT_CLOSESOCKETFUNCTION, tst_closesocket);
  45. counter = 0;
  46. }
  47. #else
  48. #define setupcallbacks(x) Curl_nop_stmt
  49. #endif
  50. int test(char *URL)
  51. {
  52. CURLcode res;
  53. CURL *curl;
  54. char *ipstr=NULL;
  55. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  56. fprintf(stderr, "curl_global_init() failed\n");
  57. return TEST_ERR_MAJOR_BAD;
  58. }
  59. if ((curl = curl_easy_init()) == NULL) {
  60. fprintf(stderr, "curl_easy_init() failed\n");
  61. curl_global_cleanup();
  62. return TEST_ERR_MAJOR_BAD;
  63. }
  64. test_setopt(curl, CURLOPT_URL, URL);
  65. test_setopt(curl, CURLOPT_HEADER, 1L);
  66. if(libtest_arg3 && !strcmp(libtest_arg3, "activeftp"))
  67. test_setopt(curl, CURLOPT_FTPPORT, "-");
  68. setupcallbacks(curl);
  69. res = curl_easy_perform(curl);
  70. if(!res) {
  71. FILE *moo;
  72. res = curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &ipstr);
  73. moo = fopen(libtest_arg2, "wb");
  74. if(moo) {
  75. double time_namelookup;
  76. double time_connect;
  77. double time_pretransfer;
  78. double time_starttransfer;
  79. double time_total;
  80. fprintf(moo, "IP: %s\n", ipstr);
  81. curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME, &time_namelookup);
  82. curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &time_connect);
  83. curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME, &time_pretransfer);
  84. curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME,
  85. &time_starttransfer);
  86. curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &time_total);
  87. /* since the timing will always vary we only compare relative differences
  88. between these 5 times */
  89. if(time_namelookup >= time_connect) {
  90. fprintf(moo, "namelookup vs connect: %f %f\n",
  91. time_namelookup, time_connect);
  92. }
  93. if(time_connect >= time_pretransfer) {
  94. fprintf(moo, "connect vs pretransfer: %f %f\n",
  95. time_connect, time_pretransfer);
  96. }
  97. if(time_pretransfer >= time_starttransfer) {
  98. fprintf(moo, "pretransfer vs starttransfer: %f %f\n",
  99. time_pretransfer, time_starttransfer);
  100. }
  101. if(time_starttransfer >= time_total) {
  102. fprintf(moo, "starttransfer vs total: %f %f\n",
  103. time_starttransfer, time_total);
  104. }
  105. fclose(moo);
  106. }
  107. }
  108. test_cleanup:
  109. curl_easy_cleanup(curl);
  110. curl_global_cleanup();
  111. return (int)res;
  112. }