Skip to content

Instantly share code, notes, and snippets.

@yongboy
Created June 26, 2015 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yongboy/393196dec89f9be8c67e to your computer and use it in GitHub Desktop.
Save yongboy/393196dec89f9be8c67e to your computer and use it in GitHub Desktop.
DNS解析测试脚本
%%%-------------------------------------------------------------------
%%% @author nieyong
%%% @copyright (C) 2015
%%% @doc
%%% DNS解析测试
%%% @end
%%%-------------------------------------------------------------------
-module(test1).
-export([start/2]).
%%
%% 传入域名列表和每一个域名执行时间
%% 使用方式:test1:start(["www.baidu.com", "www.taobao.com", "www.qq.com", "www.sina.com.cn", "www.youku.com"], 1000).
%%
start(Hosts, Num) when is_integer(Num), Num > 0 ->
StartTime = now_timestamp_milliseconds(),
PID = spawn(fun() -> loop(0, 0, Num * length(Hosts), StartTime) end),
[spawn(fun() -> get_host_fn(Host, N, PID), ok end) || N <- lists:seq(1, Num), Host <- Hosts],
ok;
start(_Host, _Other) ->
io:format("Invalid Argument~n"),
ok.
get_host_fn(Host, _Id, PID) ->
case inet_res:gethostbyname(Host) of
{ok, _Hostent} ->
PID ! success;
%%io:format("~p times ~p : ~p~n", [Id, Host, Hostent]);
{error, _Reason} -> PID ! failure %%, throw(Reason) %%throw(Reason)
end,
ok.
%% 等待处理结果,输出最终测试报告
loop(Succ, Fail, Total, StartTime) ->
case (Succ + Fail) == Total of
true ->
UseTime = now_timestamp_milliseconds() - StartTime,
io:format("Summary DNS Query: [Total : ~p, Success : ~p, Failure : ~p, UseTime ~w seconds]~n", [Total, Succ, Fail, UseTime/1000]),
self() ! quit;
false ->
ok
end,
receive
success ->
NewSucc = Succ + 1,
loop(NewSucc, Fail, Total, StartTime);
failure ->
NewFail = Fail + 1,
loop(Succ, NewFail, Total, StartTime);
_ ->
true
end.
%%
%% 当前时间戳,以毫秒表示
%%
now_timestamp_milliseconds() ->
{Mega, Sec, Micro} = os:timestamp(),
(Mega*1000000 + Sec)*1000 + round(Micro/1000).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment