技巧 22:儘可能使用 Server.Transfer 代替 Response.Redirect
Response.Redirect 讓瀏覽器請求另一個頁面。此函數常用來將使用者重新導向到一個登入或錯誤頁面。因為重新導向強制請求新頁面,結果是瀏覽器必須到 Web 服務器往返兩次,且 Web 服務器必須多處理一個請求。IIS 5.0 引入了一個新的函數 Server.Transfer,它將執行轉移到同一台伺服器上的另一個 ASP 頁。這樣就避免多餘的瀏覽器-Web-伺服器的往返,從而改善了總體系統效能以及縮短了使用者的回應時間。檢查“重新導向”中的“新的方向”,上面應該是 Server.Transfer 和 Server.Execute。
另請參見 Leveraging ASP in IIS 5.0,瞭解 IIS 5.0 和 ASP 3.0 新功能的完整列表。
技巧 23:在目錄 URL 中使用後斜杠
一個相關的技巧是確保在指向目錄的 URL 中使用後斜杠 (/)。如果您省略了後斜杠,瀏覽器就會向伺服器發出請求,只是為了告訴伺服器,它在請求目錄。瀏覽器就會發出第二個請求,將斜杠附加到 URL 後面,只有此後,伺服器才能以該目錄的預設文件或目錄列表(如果沒有預設文件且啟用了瀏覽目錄的話)響應。附加斜杠可省去第一個、無用的住返。為便於使用者閱讀,可以省略顯示名稱中的後斜杠。
例如,寫:
<a href=?http://msdn.microsoft.com/workshop/? title=?MSDN Web
Workshop?>http://msdn.microsoft.com/workshop</a>
這也適用於指向 Web 網站上首頁的 URL:使用下面的:<a href=?http://msdn.microsoft.com/?>,而不使用 <a href=?http://msdn.microsoft.com?>。
技巧 24:避免使用伺服器變數
訪問伺服器變數會使 Web 網站向伺服器發出一個特殊請求,並收集所有伺服器變數,而不只是您請求的那個變數。這種情況類似於,在發黴的閣樓上,在一個檔案夾中尋找某個檔案。當您想要找那個檔案時,您必須去閣樓上,先找到檔案夾,然後才能找到這份檔案。當您請求伺服器變數時,發生的情況是一樣的 - 您第一次請求伺服器變數時,就會使效能受到影響。後面的對其它伺服器變數的請求,則不會對效能產生影響。
決不要訪問非限定的 Request 對象(例如,Request("Data"))。對於不在 Request.Cookies、Request.Form、Request.QueryString 或 Request.ClientCertificate 中的項目,則隱式調用 Request.ServerVariables。Request.ServerVariables 集合比其它集合慢得多。
技巧 25:升級到最新和最出色的
系統組件是恒定的,我們建議您將它們升級到最新和最好的配置。最好升級到 Windows 2000(因此,也應升級到 IIS 5.0、ADO 2.5、MSXML 2.5、Internet Explorer 5.0、VBScript 5.1 和 JScript 5.1)。在多處理器電腦上,實施 IIS 5.0 和 ADO 2.5 可顯著改善效能。在 Windows 2000 下,ASP 可以很好地擴充到四個處理器或更多,而在 IIS 4.0 下,ASP 的擴充性不能超出兩個處理器。在應用程式中使用的指令碼代碼和 ADO 越多,升級到 Windows 2000 之後,效能的改善就會越多。
如果目前還不能升級到 Windows 2000,您可以升級到 SQL Server、ADO、VBScript 和 JScript、MSXML、Internet Explorer 和 NT 4 Service Packs 的最新版本。它們均可提高效能和可靠性。
技巧 26:最佳化 Web 服務器
有多種 IIS 最佳化參數可以改善網站效能。例如,對於 IIS 4.0,我們常常發現,增加 ASP ProcessorThreadMax 參數(參見 IIS 文檔)可以顯著改善效能,特別是在傾向於等待後端資源(如資料庫)或其它中間產品(如螢幕刷)的網站上。在 IIS 5.0 中,您可能發現啟用 ASP Thread Gating 比尋找一個 AspProcessorThreadMax 最佳設定效率更高,這一點現在已為大家所熟知。
有關較好的參考資料,參見下面的最佳化 IIS。
最佳的配置設定取決於(其中一些因素)應用程式代碼、運行所在的系統硬體和客戶機工作負載。找到最佳設定的唯一方法是進行效能測試,這是我們在下一個技巧中所要討論的。
技巧 27:進行效能測試
正如我們在前面已經講過,效能是一個特徵。如果您想要改善網站的效能,那麼就制定一個效能目標,然後逐步改進,直到達到目標為止。不要,就不進行任何效能測試。通常,在項目結束時,再作必需的結構調整已經為時太晚,您的客戶將為此感到失望。將效能測試作為您日常測試的一部分來進行。可以對單個組件分別進行效能測試,如針對 ASP 頁或 COM 物件,或將網站作為一個整體來測試。
許多人使用單個瀏覽器請求頁面,來測試 Web 網站的效能。這樣做就會給您一個感覺,即網站的響應能力很好,但這樣做實際上並不能告訴您在負載條件下網站的效能如何。
一般情況下,要想準確地測試效能,您需要一個專門的測試環境。此環境應包括硬體,其處理器速度、處理器數量、記憶體、磁碟、網路設定等方面與生產環境的硬體相似。其次,您必須指定客戶機的工作負載:有多少同時的使用者,他們發出請求的頻率,他們點擊頁面的類型等等。如果您沒有網站實際使用方式的資料,您必須估計一下使用的情況。最後,您需要一個可以類比預期客戶機工作負載的工具。有了這些工具,您就可以開始回答諸如“如果我有 N 個同時的使用者,那麼需要多少伺服器?”之類的問題。您還可以找出出現瓶頸的原因,並以此為目標進行最佳化。
下面列出了一些好的 Web 負載測試工具。我們精選 Microsoft Web Application Stress (WAS) 工具包。WAS 可使您記錄測試指令碼,然後類比數百或成千上萬個使用者訪問 Web 服務器。WAS 報告很多統計資訊,包括每秒鐘的請求數,回應時間分布情況和錯誤計數。WAS 有豐富的客戶機介面和基於 Web 的介面兩種,Web 介面可使您進行遠程測試。
一定要閱讀 IIS 5.0 Tuning Guide。
技巧 28:閱讀資源連結
下面是一些與效能有關的出色的資源連結。如果您想瞭解有關資訊,請閱讀 Developing Scalable Web Applications。
資源
最佳化 ASP 指令碼
Developing Scalable Web Applications
Got Any Cache? Nancy Winnick Cluts 著
Maximizing the Performance of Your Active Server Pages,Nancy Winnick Cluts 著
15 Seconds: Performance Section
Enhancing Performance in ASP - Part I,Wayne Plourde 著
When is Better Worse? Weighing the Technology Trade-Offs,Nancy Winnick Cluts 著
Speed and Optimization Resources,Charles Carroll 著
最佳化 IIS
The Art and Science of Web Server Tuning with Internet Information Services 5.0
Leveraging ASP in IIS 5.0,J.D. Meier 著
Tuning IIS 4.0 for High Volume Sites,Michael Stephenson 著
Tuning Internet Information Server Performance,Mike Moore 著
Navigating the Maze of Settings for Web Server Performance Optimization,Todd Wanke 著
Managing Internet Information Server 4.0 for Performance,Hans Hugli 著
ADO 和 SQL Server
Top Ten Tips: Accessing SQL Through ADO and ASP,J.D. Meier 著
Improve the Performance of your MDAC Application,Suresh Kannan 著
Pooling in the Microsoft Data Access Components,Leland Ahlbeck 和 Don Willits 合著
SQL Server: Performance Benchmarks and Guides
Improving the Performance of Data Access Components with IIS 4.0,Leland Ahlbeck 著
Microsoft Data Access Components (MDAC) and ActiveX Data Objects (ADO) Performance Tips,Leland Ahlbeck 著
Microsoft SQL Server 7.0 Practical Performance Tuning and Optimization - The Server Perspective,Damien Lindauer 著
Microsoft SQL Server 7.0 Practical Performance Tuning and Optimization - The Application Perspective,Damien Lindauer 著
Accessing Recordsets over the Internet,Dino Esposito 著
ASP 組件和執行緒模式
ASP Component Guidelines,J.D. Meier 著
Q243548: INFO: Design Guidelines for VB Components under ASP
Threading Models Explained,Nancy Winnick Cluts 著
So Happy Together? Using ActiveX components with Active Server Pages,Nancy Winnick Cluts 著
Developing Active Server Components with ATL,George Reilly 著
Agility in Server Components,Neil Allain 著
Building High-Performance Middle-Tier Components with C++,Jon Flanders 著
Active Server Pages and COM Apartments,Don Box 著
House of COM: Active Server Pages,Don Box 著
House of COM: Contexts,Don Box 著
House of COM: Performance Trade-offs of the Windows 2000 Component Execution Environment,Don Box 著
Building COM Components That Take Full Advantage of Visual Basic and Scripting,Ivo Salmre 著
Component Design Principles for MTS
詞典組件
Creating a Page Cache Object,Robert Coleridge 著
Abridging the Dictionary Object: The ASP Team Creates a Lookup-Table Object,Robert Carter 著
Caprock Dictionary
Site Server Commerce Edition includes a dictionary component
工作階段狀態
Q175167: HOWTO: Persisting Values Without Sessions
Q157906: HOWTO: How To Maintain State Across Pages with VBScript
XML-based Persistence Behaviors Fix Web Farm Headaches,Aaron Skonnard 著
House of COM: Stateless Programming,Don Box 著
效能和擴充性
Blueprint for Building Web Sites Using the Microsoft Windows DNA Platform
Server Performance and Scalability Killers,George Reilly 著
Microsoft Visual Studio Scalability Center
Fitch & Mather Stocks 2000
Tuning the FMStocks Application
High-Performance Visual Basic Apps,Ken Spencer 著
Duwamish Books,Phase 4
Top Windows DNA Performance Mistakes and How to Prevent Them,Gary Geiger 和 Jon Pulsipher 合著
Building from Static HTML to High-Performance Web-Farms,Shawn Bice 著
工具
Microsoft Web Application Stress Tool
I Can't Stress It Enough -- Load Test Your ASP Application,J.D. Meier 著
Windows DNA Performance Kit
Monitoring Events in Distributed Applications Using Visual Studio Analyzer,Mai-lan Tomsen 著
書目
Professional Active Server Pages 3.0,Wrox Press(特別是第 26 章:Optimizing ASP Performance,George Reilly 和 Matthew Gibbs 合著)。
Microsoft Internet Information Services 5.0 Resource Guide(與 Windows 2000 Server Resource Kit 在一起),Microsoft Press。
Microsoft Internet Information Server Resource Kit(用於 IIS 4.0),Microsoft Press。
Programming Distributed Applications with COM and Microsoft Visual Basic 6.0,Ted Pattison 著,Microsoft Press。
Effective COM,Don Box、Keith Brown、Tim Ewald 和 Chris Sells 合著;Addison-Wesley。
Developing Web Usability: The Practice of Simplicity,Jakob Nielsen 著,New Riders。
ASP Web 網站
Microsoft TechNet for IIS
LearnASP.com
4GuysFromRolla.com
15Seconds.com
AspToday.com
Asp101.com
AspLists.com。許多專業的郵件清單包括:
Fast Code!
ASP Advanced
Not NewbieState Management
Scalability
Visual Basic Components
XML
C++/ATL Component Building
UseIt.com: Web 可用性
ASP 樣式
ASP Best Practices,George Reilly 著
ASP Quick Lessons,Charles Carroll 著
Planning for ASP,John Meade 著
ASP Guidelines,J.D. Meier 著
XML
Inside XML Performance,Chris Lovett 著
Inside MSXML3 Performance,Chris Lovett 著