服务端与客户端验证 • 客户端认证 – 依靠浏览器 – 快速反馈 – 减少页面回发 • 服务器认证 – 具有客户端验证特性 – 可以根据现有数据进行判断 性能即是功能 • 在设计过程中始终将性能放在第一位 – 设计之初便对性能进行规划 • 切勿将“添加性能”作为后续任务! – 一旦项目编写完成,添加性能就会步履维艰 • 在整个项目过程中进行衡量与反复测试 – 性能并不是一蹴而就的 – 反复测试调查是最有效的方法 利用缓存技术的设计 • 充分利用内置 ASP.NET 的内置缓存功能 – 输出缓存 – 部分页面缓存 – 缓存 API • 建议: – 围绕这些功能精心设计您的页面 ––– 可以显著提高性能 ASP.NET 缓存 性能提高的技巧 • 避免不必要的执行操作 – Page_Load 和 IsPostBack void Page_Load(Object sender, EventArgs e) { // ...set up a connection and command here... if (!Page.IsPostBack) { String query = "select * from Authors where FirstName like '%JUSTIN%'"; myCommand.Fill(ds, "Authors"); myDataGrid.DataBind(); } } void Button_Click(Object sender, EventArgs e) { String query = "select * from Authors where FirstName like '%BRAD%'"; myCommand.Fill(ds, "Authors"); myDataGrid.DataBind(); } 性能提高的技巧 • 关闭不必要的Session状态 – <%@ Page EnableSessionState="false" %> • 注意使用Server Control – 不必要时可以不使用Server Control – 不必要时可以关闭ViewState • <asp:datagrid EnableViewState="false“ runat="server"/> • <%@ Page EnableViewState="false" %> 逻辑设计 • 建议: 采用 3层 逻辑模型 – Pages (.aspx) and User Controls (.ascx) UI – Business and Data Access classes in /bin dir – Data within a SQL Database via SPROCs • 设计系统时要考虑到Scale-Out的情形 – 不要假设客户端的请求 永远会返回到同一台机器 性能测试工具 • 微软 Web Application Stress Tool – 可免费进行下载( 10Mb ),适用于 XP、2000、2003 – http://www.microsoft.com/technet/treeview/default.asp?url=/technet/itsolutions/intranet/downloads/webstres.asp • 微软应用程序中心测试工具 (ACT) – 作为 VS.NET Enterprise 的一部分提供给客户 – 启用更丰富的脚本及报告 主要的性能观测项PerfMon Counters • Processor, CPU % Utilization – Low numbers = blocking or lock contention • ASP.NET, Requests In Application Queue – 出现线型增长时表示服务器已达满负荷 • ASP.NET Applications, Requests/Sec – 动态吞吐量(应保持一致—无大的波动) • ASP.NET Application, Errors Total – 预示着功能级错误 (应为 0) • ASP.NET App/Worker Process Restarts – 表示有严重的编程错误 (应为0)