论坛首页 综合技术论坛

tsung源码学习之xmerl

浏览 3754 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-10-25  
开始规模研究tsung的源代码,代码不多,看起来应该不会太费力,先参考这些资料

老大的2篇博客:
http://mryufeng.iteye.com/blog/355827
http://mryufeng.iteye.com/blog/355716
其中还提到一个writing tsung plugin
http://www.process-one.net/en/wiki/Writing_a_Tsung_plugin/
都看了一遍,然后基本明白怎么用后,就该研究如何编写plugin了

tsung 基本就分3部分,controller,client和recorder,如果不是自己录制http压力测试脚本,基本就不用看recorder的代码了

程序启动时先读取的tsung.xml配置文件,这个配置文件由controller里的ts_config来读取,用的是xmerl库,和ejabbered不同,那个是自己写的driver调用libexpat的c的库

xmerl读取xml文件后,是将xml信息存储在由xmerl.hrl定义的一些record里,主要包括
xmlElement,xmlText,xmlAttribute等

通过xmerl_scan:file("filename",[args])读取后,会生成下面的结构
#xmlElement{name=Name,
            ...
            parents=Parents,
            ...
            attributes=Attrs,
            content=Content,
            ...}   

比如下面一段xml代码
<tsung2 v="2" v2="3"><clients><client host="localhost" use_controller_vm="true"/></clients></tsung2>

对应的
name = tsung2
parents = []
attributes = [#xmlAttribute{name = v,value = "2",...},
              #xmlAttribute{name = v2,value = "3",...}],

content = [#xmlElement{},#xmlElement{},...]

所以tsung的入口parse函数是

parse(Element = #xmlElement{parents = [], attributes=Attrs}, Conf=#config{}) ->

因为根元素的parents肯定是[],然后开始依次遍历这个record

获得attributes的代码
%%%-------------------------------------------------------------------
%%% Function: getAttr/2
%%% Purpose:  search the attribute list for the given one
%%%-------------------------------------------------------------------
getAttr(Attr, Name) -> getAttr(string, Attr, Name, "").
getAttr(Type, Attr, Name) -> getAttr(Type, Attr, Name, "").

getAttr(Type, [Attr = #xmlAttribute{name=Name}|_], Name, _Default) ->
    case { Attr#xmlAttribute.value, Type}  of
        {[], string } -> "" ;
        {[], list } -> [] ;
        {[], float_or_integer } -> 0 ;
        {A,_}  -> getTypeAttr(Type,A)
    end;

getAttr(Type, [_H|T], Name, Default) ->
    getAttr(Type, T, Name, Default);

getAttr(_Type, [], _Name, Default) ->
    Default.


基本结构很清楚的
tsung.xml前面部分的clients,server,load等元素都是通用的,由框架本身处理,但是后面具体的session定义就不同了,根据具体要测试的协议不同,会调用不同的模块,比如tsung已经提供的ts_http,ts_mysql等模块

具体是在tsung 处理session根据type指定的,这个是http_simple的tsung配置样例
<session name="http-example" probability="100" type="ts_http">
...
</session>
模块其实也就是ts_http了,我们可以自定义自己的模块

处理代码可以参考ts_config.erl里的这段
%%% Parsing the request element
parse(Element = #xmlElement{name=request, attributes=Attrs},
      Conf = #config{sessions=[CurSess|_], curid=Id}) ->

    Type  = CurSess#session.type,
    SubstitutionFlag  = getAttr(atom, Attrs, subst, false),

    lists:foldl( fun(A,B) ->Type:parse_config(A,B) end,
                 Conf#config{curid=Id+1, cur_req_id=Id+1,
                             subst=SubstitutionFlag,
                             match=[]
                            },
                 Element#xmlElement.content);

这里的Type就是在读取session的type属性时获得的,然后会有一个Type名字对应的erl文件处理parse_config,就是读取自己模块特定的一些配置,这个没法写在通用的代码里
,在erlang里处处可见这样的代码,学习下












   发表时间:2009-11-01  
看起不错,不知这玩意儿生成测试结果报表的功能咋样啊?
0 请登录后投票
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics