erlang httpc

httpc

默認(rèn)情況下httpc監(jiān)控樹結(jié)構(gòu)

inets_sup.png

httpc 的瓶頸所在

調(diào)用棧
  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. ......

從上面的調(diào)用??梢钥吹狡款i在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.

驗證結(jié)果

(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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 英文文檔,一開始我也是抗拒的,邊翻譯邊看,也就花費(fèi)了1個小時基本就閱讀過了,我的英文基礎(chǔ)其實很差。附上鏈接:鏈接:...
    lonecolonel閱讀 10,407評論 3 1
  • 某夏,天甚悶熱,云似連地壓城,蟬聒葉無采。余行至水潭,汗流浹背,見水清澈且四面竹木環(huán)合,便心生澡意,舍鞋衣而入...
    瀝竹泣半夏閱讀 180評論 0 0
  • 十年前,你在做什么? 十年后,我在做什么? 時光,流水。 ……
    喃喃自語nannan閱讀 232評論 0 0

友情鏈接更多精彩內(nèi)容