这几天配置dns集群,为了测试各种场景下的服务影响时间,需要自己写个脚本进行统计。没有什么现成的好的工具,就用python里面的dnspython模块写了个小的脚本[python]#!/usr/bin/python#****************************************************************## ScriptName: dnsquery.py# Author: GNUer# Create Date: 2013-06-28 12:52# Modify Author: GNUer# Modify Date: 2013-06-28 12:52# Function:#***************************************************************#import dns.resolverimport dns.exceptionimport timeimport datetimeimport signalimport sysdef get_time():t=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")return tdef get_mtime():t=datetime.datetime.now().strftime("%H:%M:%S.%f")return tdef get_second():return time.time()def do_exit(sig,arg):print "exit dns test"sys.exit(1)def dns_test(num,sleep_time):resolver = dns.resolver.Resolver()#resolver.timeout = 0.01resolver.lifetime =0.0013#change this time accord to reponse timeresolver.nameservers=[‘7.7.7.7′,’6.6.6.6’]testlist=("search.xxx.com","obs.xxx.com","if.xxx.com")last_flag=Truefail_start=0fail_finish=0for i in range(num):for test in testlist:try:for target in resolver.query(qname=test):print "\x1b[32m",get_time(),test,target,"\x1b[m"if last_flag == False:last_flag= Truefail_finish=get_second()errortime=fail_finish-fail_startprint "\x1b[1;
31m","error time is:",errortime,"\x1b[m"except dns.exception.Timeout:print "\x1b[31m",get_time(),test,"failed","\x1b[m"if last_flag == True:fail_start=get_second()last_flag = Falsetime.sleep(sleep_time)signal.signal(signal.SIGINT,do_exit)dns_test(10000000,0.33)[/python]需要比较注意的是需要根据自己的实际情况把resolver.lifetime设置为比dns服务器的响应时间稍微大一点的值,但是需要小于平均响应时间的2倍。不然测试的时间不是太准确。因为总共测试的域名是3个,所以我把每次sleep的时间设置为0.32左右,使得1s总共能发3个请求左右。测试的时候可以先把这个脚本一直跑着,然后去做各种操作,看中间的影响时间是多少。虽然这个脚本是测试dns的,但是也可以修改一下做HTTP请求等等,方便在做各种HA切换的时候测试影响的时间。