
- 加为好友
- 发送私信
- 在线聊天
|
| 发表于:2008-05-16 16:58:2835楼 得分:30 |
提高ASPX服务器性能的几大狠招 ∆ 第0招依靠测试工具,以下根据ACT test测试结果,整理。【全部招数凶狠度的依据】 所谓性能优化,必须是建立在测试的基础之上的,ACT Test是比较爽的测试工具,比Load Runner方便,比Web Stress直观,支持脚本编程和录制登陆到注销全过程。 所有优化都要进行对比测试,才是评判的数字依据。 所以,个人认为:不做压力测试,优化是可以做,但是没数据支持,是不严谨的。 ∆ 第一招生成静态。【凶狠度排名第一:性能RPS提升两个数量级(提高速度百倍)】 以下是截取Response的Stream生成文件的代码 protected override void Render(HtmlTextWriter writer) { StringWriter stringWriter = new StringWriter(); HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter); base.Render(htmlTextWriter); if (本页允许生成静态HTML) { //根据aspx的get参数构造出的HTML文件名,如:aaa.aspx?id=1 转化成 aaa_id_1.html string filePath = Server.MapPath(htmlFileName); StreamWriter streamWriter = new StreamWriter(filePath , false, Encoding.UTF8); streamWriter.Write(HTML); streamWriter.Close(); htmlTextWriter.Close(); } } 生成后,以后先判断是否有这个文件,如果有就跳过去。 本页允许生成静态HTML,可以在后台做一个钩选和更新,并且在内容发生变化后删除静态HTML。 ∆ 第二招,图片文件分流服务器。【凶狠度排名第二:性能RPS提升1个数量级(提高速度十倍)】 1,web 1台或多台。 2,图片1台或多台。 3,文件1台或多台。 4,数据库1台或多台。 不同机房要采用Remoting分发文件是个不错的主义,就是工作量大。 以下是同机房多服务器之间可以通过web读写分发文件的关键代码。 using System; using System.Collections; using System.Configuration; using System.Data; using System.IO; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Security.Principal; using System.Runtime.InteropServices; namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { public const int LOGON32_LOGON_INTERACTIVE = 2; public const int LOGON32_PROVIDER_DEFAULT = 0; WindowsImpersonationContext impersonationContext; [DllImport("advapi32.dll", CharSet = CharSet.Auto)] public static extern int LogonUser(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] public extern static int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken); private bool impersonateValidUser(String userName, String domain, String password) { IntPtr token = IntPtr.Zero; IntPtr tokenDuplicate = IntPtr.Zero; if (LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0) { if (DuplicateToken(token, 2, ref tokenDuplicate) != 0) { WindowsIdentity tempWindowsIdentity; tempWindowsIdentity = new WindowsIdentity(tokenDuplicate); impersonationContext = tempWindowsIdentity.Impersonate(); if (impersonationContext != null) return true; else return false; } else return false; } else return false; } private void undoImpersonation() { impersonationContext.Undo();//回退为未更改前账户 } protected void Page_Load(object sender, EventArgs e) { //临时更改为 跟 网络硬盘相同用户名密码的账户(此账户必须在网络盘有写入权限)本机也需要同样帐号密码的帐户 if (impersonateValidUser("administrator", "192.168.1.102", "kuqu123456")) { Response.Write(System.IO.File.Exists(@"\\192.168.1.102\share\C#高级编程\C#高级编程(第四版).pdf")); &nb | |