所以我想知道在C程序中查找DNS需要多长时间。我使用函数getaddrinfo()来进行DNS查找,所以我认为我必须简单地测量这个函数返回所需的时间,以便获得dns查找时间。然而,情况似乎并非如此。我还有另一个C程序,它使用libcurl测量DNS查找时间,这两个程序在同一时间查找相同的when服务器时返回不同的时间。
getaddrinfo()只需4-5毫秒即可返回,而libcurl则告诉我DNS查找平均需要15 ms。我在linux和Windows上测试了这一点,结果也很相似。值得注意的是,在Visual发布模式下启动程序时,getaddrinfo()以15 5ms的速度返回,但是一旦在醚linux或windows中从控制台启动它,它就有这4到5ms的时间。
而且,即使我查找的服务器很远,而libcurl却高达60 is,getaddrinfo的返回时间仍然相同(在控制台执行中)。
除了getaddrinfo()之外,我想不出要测量什么,所以我的问题是如何在C中正确地度量DNS查找(没有外部库的帮助,比如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));
...[医]附生
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);
...发布于 2018-07-03 14:53:11
为了回答我自己的问题,问题是解决URL问题的标准方法是它们的线程解析器,其中头几次超时太慢了。切换到非线程解析器模式解决了这个问题,开发人员还修复了他们的github,用于线程解析器(请参阅:https://curl.haxx.se/mail/lib-2018-06/0117.html )。
https://stackoverflow.com/questions/50800934
复制相似问题