erlang httpc

httpc

默认情况下httpc监控树结构

inets_sup.png

httpc 的瓶颈所在

调用栈
  1. httpc:request/x
  2. httpc:do_request/x
  3. httpc:handle_request/x
  4. httpc_manager:request
  5. httpc_manager:call(gen_server call)
  6. httpc_manager:handle_request(in gen_server)
  7. httpc_manager:start_handler ( httpc_handler_sup:start_child([whereis(httpc_handler_sup), Request, Options, ProfileName]) )
  8. ......

从上面的调用栈可以看到瓶颈在httpc_manager这个gen_server

如何突破上面提到的瓶颈呢?

1. 增加httpc_profile_sup的worker
httpc_profile_sup:start_child([{profile,abc}]).
httpc:request("http://192.168.3.231:8011/public/index",abc).                                      
{ok,{{"HTTP/1.1",200,"OK"},
     [{"date","Tue, 07 Aug 2018 09:46:38 GMT"},
      {"server","Cowboy"},
      {"content-length","5"}],
     "Alive"}}

httpc_abc.png
2.启动文件添加
...
{inets, [
    {services, [
      {httpc, {default, only_session_cookies}},
      {httpc, {foo, only_session_cookies}},
      {httpc, {bar, only_session_cookies}}
    ]}
  ]}
...
httpc_foo_bar.png
性能验证。
% my_httpc.erl

-export([get/3]).

ts()->
  {MegaSecs, Secs, MicroSecs} = os:timestamp(),
  MegaSecs * 1000*1000*1000 + Secs*1000 + (MicroSecs div 1000).

get(URL,Profile,N)->
  P = self(),
  S = ts(),
  do_get(P,URL,Profile,N),
  do_reduce(0,N),
  E = ts(),
  io:format("cost ~p ms with ~p req~n",[(E-S),N]).

do_get(_P,_URL,_Profile,N) when N < 1 -> ok;
do_get(P,URL,Profile,N) ->
  erlang:spawn( fun()->  do_map(P,URL,Profile) end ),
  do_get(P,URL,Profile,N-1).

do_map(Parent,URL,undefined)->
  R = httpc:request(URL),
  Parent ! R;
do_map(Parent,URL,Profile) when is_atom(Profile)->
  R = httpc:request(URL),
  Parent ! R;
do_map(Parent,URL,L) when is_list(L)->
  R = [httpc:request(URL,X) || X<-L],
  Parent ! R.

do_reduce(C,Total) when C >= Total -> ok;
do_reduce(C,Total) ->
  receive
     _ -> do_reduce(C+1,Total)
  end.

验证结果

(myapp_1@127.0.0.1)9> my_httpc:get("http://192.168.3.231:8011/public/index",[default,default],1000).
cost 211 ms with 1000 req
(myapp_1@127.0.0.1)10> my_httpc:get("http://192.168.3.231:8011/public/index",[default,abc],1000).    
cost 162 ms with 1000 req
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 英文文档,一开始我也是抗拒的,边翻译边看,也就花费了1个小时基本就阅读过了,我的英文基础其实很差。附上链接:链接:...
    lonecolonel阅读 10,401评论 3 1
  • 某夏,天甚闷热,云似连地压城,蝉聒叶无采。余行至水潭,汗流浃背,见水清澈且四面竹木环合,便心生澡意,舍鞋衣而入...
    沥竹泣半夏阅读 180评论 0 0
  • 十年前,你在做什么? 十年后,我在做什么? 时光,流水。 ……
    喃喃自语nannan阅读 232评论 0 0

友情链接更多精彩内容