• 开发高性能的WebService应用


    一、      WebService概述
    1、      性能

    设计时就考虑性能
    不要在事后再加入性能!
    在项目开发的整个过程中反复测试
    两种量化Web性能的方法:
    1) 机器吞吐率(requests/sec)
    2) 响应时间(time tofirst/last bytes)
     

    2、面向对象

    应用于同一平台和运行环境
    共享类型而不是schemas
    采用便宜, 透明通信
    对象的标识与生命周期由系统维护
    客户机和服务器的同步的部署
    容易概念化,因而提供一个自然的模型
    一般不需要状态管理
    应用于一个可预测的序列、期限和结果
    目标是远程透明地使用方法和类型

    3、面向服务

    应用于异构平台和运行环境
    共享schemas而不是类型
    采用高成本, 明确通信
    服务是自治的: 安全和失效是隔离
    允许的连续, 分离部署客户机和服务器
    基于软件组件和分布的对象,依赖服务的协议
    拥有并维护状态
    基于消息, 异步的, 和长通信
    目标是提供服务隔离和调用的标准。

    4、Web Service性能设计的考虑

    设计"大块头"的接口减少往返。
    基于消息的编程而不是远程过程调用(RPC) 。
    使用XML字串作为参数。
    尽量使用原始数据类型参数。
    避免在调用之间维护服务器状态。
    考虑为复杂的WebMethod提供输入校验。
    考虑对WebMethod的结果进行缓存。
    选择适用的大数据包传送方式。
    避免调用本地的WebService。

    5、基于消息编程和RPC

    消息
    //Client
    string msg = "<Items>...</Items>";
    MyMethod(msg);
    //Server
    [WebMethod]
    void MyMethod(string msg){ . . . }

    RPC
    Serv.SendItemsToBePurchased(Array[] items);
    Serv.ShippingAddress(string Address);
    Serv.CheckOut();

    二、WebService性能最佳实践

    连接与线程优化
    WebMethod的优化
    高效地调用WebMethod
    设置超时
    缓存
    状态管理

    1、连接

    配置maxconnection 属性。

    示例:

    <connectionManagement>
    <add address="*" maxconnection="12"/>
    </connectionManagement>

    修改:
    <connectionManagement>
    <add address="WebServiceA" maxconnection="8">
    <add address="WebServiceB" maxconnection="4">
    </connectionManagement>


    对WebService按优先级分配连接资源
    使用单一身份进行调用。

    serv = new WebService1();
    serv.PreAuthenticate=true;
    ICredentials conCredentials =new
    NetworkCredential("UId","Pwd","NPSTest" );
    serv.Credentials = conCredentials;
    serv.AllowAutoRedirect=false;
    serv.ConnectionGroupName = "SameForAllUsers";


    为集成认证使用UnsafeAuthenticatedConnectionSharing 。
    为基本验证使用PreAuthenticate 。

    2、WebMethods

    使用原始类型参数。
    考虑使用缓冲。
    将WebMethod的返回缓存。
    只在需要会话状态时才使用它。

    大数据包的处理

    使用字节数组作为参数或返回值
    返回一个URL
    使用串行化流
    <configuration>
    <system.web>
    <httpRuntime maxRequestLength="8096"
    useFullyQualifiedRedirectUrl="true"
    executionTimeout="45"/>
    </system.web>
    </configuration>

    串行化

    使用XmlIgnore减少串行化。
    减少往返。
    考虑使用XML 压缩。
    public class MyClass
    {
    // The str1 value will be serialized.
    public string str1;
    /* This field will be ignored when
    serialized--unless it's overridden. */
    [XmlIgnoreAttribute]
    public string str2;
    }

    异步WebMethod

    用异步Webmethods对I/O 操作。
    当Webmethods依赖于工作线程时,不要使用异步。
    //webservice
    [WebMethod]
    IAsyncResult BeginMyProc(...)           //the WSDL will show the method as MyProc(...)
    [WebMethod]
    EndMyProc(...)

    单工通信

    如果不需要返回,考虑使用OneWay属性。
    [SoapDocumentMethod(OneWay=true)]
    [WebMethod(Description="Return immediately")]
    public void SomeMethod()
    {...}

    COM交互

    建议: 尽量避免交互
    最好将COM代码移植到.NET
       可能会很昂贵, 尤其对于数组操作
       “受管” 与“不受管” 转换
    注意单元线程组件
       缺省情况ASP.NET 使用MTA线程
       损害STA 组件性能
    注意所有VB6组件!

    3、异步调用

    当有并发任务时例用异步调用WebService。
    使用异步调用多个不相关的WebService。
    异步叫调用WebService可以增加UI 响应。

    4、超时

    适当地设置代理的超时。
    设置您的ASP.NET 超时大于您的WebService的超时。
    如果页面超时,则放弃页面与WebService的连接。
    MyWebServ obj = new MyWebServ();
    obj.Timeout = 15000; // in milliseconds

    5、缓存

    对时效性不强的数据进行缓存。
    考虑提供与缓存所相关的信息给客户。
    考虑使用边界缓存,如ISA上的缓存。

    6、状态管理

    只在需要会话状态时才使用它。
    减少应用状态与服务器的亲合力。
      InProc
      StateServer
      SQLServer

    注:主要资料来自王兴明的WebCast。

    Demo下载

    OscarXie.net

    关注质量与体验——电子商务与自动化测试
    http://www.cnblogs.com/oscarxie/

  • 相关阅读:
    【bzoj1704】[Usaco2007 Mar]Face The Right Way 自动转身机 贪心
    【poj2104】K-th Number 主席树
    【bzoj3772】精神污染 STL+LCA+主席树
    【bzoj3932】[CQOI2015]任务查询系统 离散化+主席树
    【bzoj3545/bzoj3551】[ONTAK2010]Peaks/加强版 Kruskal+树上倍增+Dfs序+主席树
    【bzoj3524】[Poi2014]Couriers 主席树
    【bzoj2223】[Coci 2009]PATULJCI 主席树
    【bzoj2588】Spoj 10628. Count on a tree 离散化+主席树
    【bzoj1901】Zju2112 Dynamic Rankings 离散化+主席树+树状数组
    【bzoj1552】[Cerc2007]robotic sort Splay
  • 原文地址:https://www.cnblogs.com/oscarxie/p/619493.html
Copyright © 2020-2023  润新知