所以我想知道在C程序中查找DNS需要多长时间。 我使用函数getaddrinfo()来进行DNS查找,所以我认为我必须简单地测量此函数返回的时间,以便获得dns查找时间。 然而,情况似乎并非如此。 我还有另一个使用libcurl测量DNS查找时间的C程序,当同时查找同一个Web服务器时,两个程序返回不同的时间。
getaddrinfo()只需要4-5毫秒即可返回,而libcurl告诉我DNS查找平均需要15毫秒。我在linux和Windows上测试了这个,结果类似。 令人困惑的是,当我在Visual Studio发布模式下启动程序时,getaddrinfo()在〜15ms内返回,但是一旦我从Ether linux或windows中的控制台启动它,它就会有4-5ms的时间。
getaddrinfo的返回时间也保持不变(在控制台执行时),即使我查找远离服务器而libcurl上升到60ms。
除了getaddrinfo()之外,我无法想出任何可以测量的内容,所以我的问题是如何在C中正确测量DNS查找(没有libcurl等外部库的帮助)?
此处参考的是我测量时间的代码片段:
没有libcurl:
...
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_NUMERICSERV;
if (prog->prog_ipver == 4)
hints.ai_family = AF_INET;
else if (prog->prog_ipver == 6)
hints.ai_family = AF_INET6;
struct timespec ts_dns_start, ts_dns_end, ts_dns_result;
timespec_get(&ts_dns_start, TIME_UTC);
e = getaddrinfo(host, port_str, &hints, &res);
timespec_get(&ts_dns_end, TIME_UTC);
timespec_diff(&ts_dns_start,&ts_dns_end, &ts_dns_result);
printf("%.3lf;", (ts_dns_result.tv_nsec/(double) 1000000));
...
使用libcurl
curl_global_init(CURL_GLOBAL_DEFAULT);
CURL *curl;
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
curl_easy_setopt(curl, CURLOPT_PORT, port);
curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
res = curl_easy_perform(curl);
if(res == CURLE_OK)
{
double connect_dns;
res = curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME, &connect_dns);
if(CURLE_OK == res)
{
printf("%.3lf" , connect_dns * 1000.0);
...
答案 0 :(得分:0)
要回答我自己的问题,问题是libcurl解决URL的标准方法是他们的线程化解析器,其中前几个超时太慢了。 切换到非线程解析器模式解决了这个问题,开发人员还在其github上对其线程解析器进行了修复,请参见:https://curl.haxx.se/mail/lib-2018-06/0117.html