zoukankan      html  css  js  c++  java
  • ranch分析学习(一)

      Ranch 是一个tcp处理的程序框架。官方的解释  Ranch is a socket acceptor pool for TCP protocols. 主要目的是提供一个方便,易用,高效,稳定的tcp处理基础程序。前面我也用它作为基础写了个简易的聊天的程序。cowboy底层通信处理也是ranch处理,聊聊数十个个文件做为基础的http服务器。今天我们就来看看它到底有什么魔力。废话不多,接下的几天我将分析它,说说我的学习心得。如果有什么地方说的不对还请大家指正。

      源码下载地址: https://github.com/extend/ranch

      使用例子:https://github.com/extend/ranch/tree/master/examples  

      下载shell : git clone  https://github.com/extend/ranch

      首先我们看一下整个目录树 整个程序总共13个文件。整个程序都是采用otp程序的方式组织的。  

      
    ├── ranch_acceptor.erl
    ├── ranch_acceptors_sup.erl
    ├── ranch_app.erl
    ├── ranch.app.src
    ├── ranch_conns_sup.erl
    ├── ranch.erl
    ├── ranch_listener_sup.erl
    ├── ranch_protocol.erl
    ├── ranch_server.erl
    ├── ranch_ssl.erl
    ├── ranch_sup.erl
    ├── ranch_tcp.erl
    └── ranch_transport.erl
    View Code

      1.首先我们来看看 ranch.app.src  

    {application, ranch, [
        {description, "Socket acceptor pool for TCP protocols."},
        {vsn, "0.10.0"},
        {modules, []},
        {registered, [ranch_sup, ranch_server]},
        {applications, [
            kernel,
            stdlib
        ]},
        {mod, {ranch_app, []}},
        {env, [{profile,true}]}
    ]}.

     这个文件 主要是 Erlang OTP设计原则 ,其余的我都不多叙述。这里主要看看对应的环境配置选项  {env, [{profile,true}]}  这个选项是ranch 对进程的效率分析,在优化提高效率时非常有用。在生产环境中不要配置,否者会影响程序的执行效率。 这个文件是应用程序启动所需,在发布打包后 通过 application:start(ranch). 启动application:stop(ranch).关闭应用程序。

       2.ranch_app.erl 应用程序启动的入口文件  

     1 -module(ranch_app).
     2 -behaviour(application).
     3 
     4 -export([start/2]).
     5 -export([stop/1]).
     6 -export([profile_output/0]).
     7 
     8 start(_, _) ->
     9     _ = consider_profiling(),
    10     ranch_sup:start_link().
    11 
    12 stop(_) ->
    13     ok.
    14 
    15 -spec profile_output() -> ok.
    16 profile_output() ->
    17     eprof:stop_profiling(),
    18     eprof:log("procs.profile"),
    19     eprof:analyze(procs),
    20     eprof:log("total.profile"),
    21     eprof:analyze(total).
    22 
    23 consider_profiling() ->
    24     case application:get_env(profile) of
    25         {ok, true} ->
    26             {ok, _Pid} = eprof:start(),
    27             eprof:start_profiling([self()]);
    28         _ ->
    29             not_profiling
    30     end.

    这个文件是 行为模式 application 的具体实现, start(_, _)  应用启动的入口。stop(_)关闭应用程序对外回调处理函数,主要让我们在关闭前做保存数据,清理程序数据。使用。在这里主要留意两个方法  第一个方法consider_profiling()读取配置文件决定是否启动应用程序性能分析工具。前面提到的 {env, [{profile,true}]}  也就是是否启动性能分析的配置。 第二个方法 profile_output().打印输出具体的性能分析。

      下面我们来具体的看看上面所说的性能分析,编译启动程序 在shell中调用ranch_app:profile_output(). 结果如下

    1> application:start(ranch).
    ok
    2> toolbar:start().
    <0.42.0>
    3> ranch_app:profile_output().
    
    ****** Process <0.37.0>    -- 31.48 % of profiled time *** 
    FUNCTION                            CALLS      %  TIME  [uS / CALLS]
    --------                            -----    ---  ----  [----------]
    gen:where/1                             1   0.00     0  [      0.00]
    gen:timeout/1                           1   0.00     0  [      0.00]
    code:call/1                             1   0.00     0  [      0.00]
    proc_lib:start_link/5                   1   0.00     0  [      0.00]
    proc_lib:sync_wait/2                    1   0.00     0  [      0.00]
    proc_lib:get_ancestors/0                1   0.00     0  [      0.00]
    supervisor:start_link/3                 1   0.00     0  [      0.00]
    lists:member/2                          2   0.00     0  [      0.00]
    error_handler:undefined_function/3      1   1.96     1  [      1.00]
    gen:start/6                             1   1.96     1  [      1.00]

    调用的函数,调用的次数,百分比,时间,调用一次需要的时间。等等都一目了然,清楚明白。这对程序提高效率是非常有帮助的。

      3 接下来我们分析学习 ranch_sup.erl 

     1 -module(ranch_sup).
     2 -behaviour(supervisor).
     3 
     4 -export([start_link/0]).
     5 -export([init/1]).
     6 
     7 -spec start_link() -> {ok, pid()}.
     8 start_link() ->
     9     supervisor:start_link({local, ?MODULE}, ?MODULE, []).
    10 
    11 init([]) ->
    12     ranch_server = ets:new(ranch_server, [
    13         ordered_set, public, named_table]),
    14     Procs = [
    15         {ranch_server, {ranch_server, start_link, []},
    16             permanent, 5000, worker, [ranch_server]}
    17     ],
    18     {ok, {{one_for_one, 10, 10}, Procs}}.

     这个文件是干什么用的呢?如果把ranch_app看成董事长,那程序ranch_sup就相当公司的总经理,它管着下面的人怎么工作,工作的强度,如果出错了怎么办。都归它管理。它就是监督者。supervisor:start_link({local, ?MODULE}, ?MODULE, []). 注册一个名为ranch_sup的本地进程。注册进程类型有 local,global,via三种类型依次为本地,全局,第三个暂时不清楚具体的功能功效,只知道要多导出三个回调函数待我弄明白否补上。今天就暂时说到这,剩下的明天继续。。

       

      

  • 相关阅读:
    The Django Book学习笔记 04 模板
    The Django Book学习笔记
    Python标准库 datetime
    Python %s和%r的区别
    Python转载
    Python while 1 和 while True 速度比较
    Git 时光穿梭鸡 删除文件 以及批量删除文件
    git reset soft,hard,mixed之区别深解
    Git 时光穿梭鸡 撤销修改
    Git 时光穿梭鸡 管理修改
  • 原文地址:https://www.cnblogs.com/codew/p/3833718.html
Copyright © 2011-2022 走看看