Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 80 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
80
Dung lượng
1,84 MB
Nội dung
before the change was made (8.758217) to the slowest speed with the change (0.046475), the speed has increased by over 180 times. After this change, it’s time to run ACT to see what kind of performance can be obtained on the tests that were defined. The original test was run with three browser connections, because that was the only test that did not generate errors. The result of the new test also had no errors. Figure 14.15 shows the dif- ference between the two tests. Comparing the graph of the original test against the new test shows a sub- stantial difference in performance. Neither of these tests generated errors. What was once an extremely slow site now responds significantly better. The original test delivered a mere 12 responses, whereas the latest test delivered 2,204 responses. This is over 180 times the original test. Caching Caching data can result in substantial performance gains. In situations where many users would normally make calls to the database for data that rarely changes, caching the data on the Web server can completely bypass the call to the database server. The cache has a global scope and includes the necessary locking mechanism to allow items to be added and read from the cache by many users. Caching specifics are covered in more detail in Chapter 12, “ASP.NET Applications.” This section explores some of the performance gains derived from caching. Figure 14.15 The original three-browser connection test overlaid with a new three- browser connection test. 608 Chapter 14 p 430234 Ch14.qxd 7/1/03 9:06 AM Page 608 Page Caching Page caching is probably the easiest caching method to implement. This is a compelling choice for many Web applications. To enable Web page caching, the following information must be added to the HTML at the top of the Web page: <%@ OutputCache Duration=”60” VaryByParam=”customerId;pageNumber” %> This setting caches the page output for 60 seconds, after which, the cache is invalidated, and the next request for the page results in execution of the code that is on the page. The VaryByParam setting can be set to none, *, valid querystring, or form parameter names, separated by a semicolon. If VaryByParam is set to none, only one copy of the page is cached. If VaryByParam is set to *, there will be a cached copy of the page for each combination of parameters that changes when the page is retrieved. If the parameter is set to customerId;pageNumber, there will be a cached copy of the page for each customerId and pageNumber combination. Figure 14.16 shows the ACT output after running the same test that was used with the StringBuilder, but the page was cached using the following statement in the Web page HTML: <%@ OutputCache Duration=”60” VaryByParam=”*” %> Figure 14.16 The Application Center Test report when implementing caching. Performance Tuning and Application Instrumentation 609 p 430234 Ch14.qxd 7/1/03 9:06 AM Page 609 Caching improved performance significantly over the last test where the StringBuilder was implemented. This required only a single line of code and resulted in a substantial gain. The test was done using three browser connections, and no errors were reported when running the test. Notice that the request count went from 12 to 2,204 to 15,837. This represents an increase of over seven times the previous test. It’s worth noting that if the code were not changed to use the String- Builder, the change would have been from 12 requests to 15,837 requests. This would represent an increase of over 1,300 times. Object Caching In the previous example, caching was implemented with a single line of code. In some cases, this type of caching is wasteful. For example, if a large amount of data is being sent to the browser and that same data is being sent to the browser from a different page, multiple copies of the same data could be held by the Web server. Often, part of the Web page needs to be dynamic, whereas another part of the page is cached. Object caching involves writing the code to cache objects rather than the entire page. The Cache object is used to add items into the cache. The follow- ing code shows how the BuildString method’s result can be cached: Public Function BuildString() Dim retString As String If Cache(“BuildString”) Is Nothing Then Dim x As Integer = 0 Dim s As New System.Text.StringBuilder() Dim pcStringLength As PerformanceCounter Dim pcLoopValue As PerformanceCounter pcStringLength = CType(Session(“pcStringLength”), _ PerformanceCounter) pcLoopValue = CType(Session(“pcLoopVaue”), _ PerformanceCounter) Trace.Warn(“BuildString”, “Start of loop”) For x = 1 To 3000 s.Append(“abcdefghijklmnopqrstuvwxyz The value of x=”) s.Append(x.ToString()) s.Append(“<br>”) ‘Update the counters. pcStringLength.RawValue = s.Length pcLoopValue.RawValue = x Next Trace.Warn(“BuildString”, “End of loop”) ‘Clear the counters. pcStringLength.RawValue = 0 pcLoopValue.RawValue = 0 610 Chapter 14 p 430234 Ch14.qxd 7/1/03 9:06 AM Page 610 retString = s.ToString() Cache(“BuildString”) = retString Else retString = CType(Cache(“BuildString”), String) End If Return retString End Function The OutputCache directive was removed from the HTML and the test was run again. Figure 14.17 shows the output of the test, overlaid with the pre- vious test. Test number 4 represents the object-caching test, which did not perform as well as the page cache test (test 3). The benefit is that other parts of the page are still dynamic. Figure 14.18 shows the metrics of the test. The page cache test (test 3) was over seven times faster than the StringBuilder test (test 2), but this test (test 4) is only about five times faster than the StringBuilder test. Graphics Caching When working with images, be sure to deliver the image to the browser using the same size that the browser uses to display the image. If a page is display- ing thumbnail images that are 75 x 75 pixels, don’t send the image to the browser at 1,200 x 1,200 pixels, because doing so uses all available network bandwidth. Images should be cached where possible, especially when the image is being loaded from a database. Figure 14.17 The output of the object-caching test, overlaid with previous tests. Performance Tuning and Application Instrumentation 611 p 430234 Ch14.qxd 7/1/03 9:06 AM Page 611 Figure 14.18 The metrics of the object-caching test show a decrease in performance over page caching, but parts of the page can still be dynamic. ViewState ViewState should be monitored, and a test should be run to determine whether it is better to store information in the ViewState or in the Session, Application, or Cache state. If a control is read-only, ViewState may not be necessary for the control. ViewState is turned on at the page level by default, but you can turn it off when it’s not required. Also, ViewState should be reviewed on a per control basis to determine the control’s impact on performance. Use a combination of Web Trace and ACT to determine what the impact of ViewState is for the current Web application. Performance will vary between Web pages. Database Performance In many situations, database performance can become the bottleneck of a Web application. SQL Server is a fast product, but you still need to be aware of items that can impact SQL Server performance. 612 Chapter 14 p 430234 Ch14.qxd 7/1/03 9:06 AM Page 612 Stored Procedures Whenever possible, use stored procedures for making calls to the database. When a stored procedure is created, it is checked for syntax errors, compiled, and saved to disk. When SQL commands are sent to SQL Server from a Web application, SQL Server must check the SQL statement for syntax errors and compile the SQL before the SQL command can be executed. When a call is made to the stored procedure, the first time it runs, it may be slower than sending a SQL statement directly to SQL Server, because the stored procedure must load into memory from the disk. After the stored procedure is in memory, the stored procedure will outperform ad hoc SQL statements. Indexes When creating SQL Server tables and relationships, be sure to designate a pri- mary key for each table. Although the primary key can be an existing field or fields that identify uniqueness for a row, a surrogate primary key should be considered. A surrogate primary key exists solely to be a row identifier, which is a field that is added to the table and is usually an autonumber (also known as an identity) field. It’s faster for SQL Server to maintain an index on a single numeric column than to maintain composite indexes. When a primary key is identified, a unique index is created for the key. When a relationship is created between two tables, the relationship is usu- ally between the primary key of one table and a foreign key of another table. Although the creation of a primary key automatically creates an index for the primary key, the creation of a foreign key does not create an index automati- cally. Big performance gains can be realized by adding indexes for all foreign key fields. Calculated Fields If a stored procedure or view is constantly performing mathematical opera- tions on certain fields, a calculated field can be created that performs the math operation once prior to executing the stored procedure. This addition can lead to large gains when complex formulas are involved, such as trigonometry functions performed on the columns. Performance Tuning and Application Instrumentation 613 p 430234 Ch14.qxd 7/1/03 9:06 AM Page 613 Lab 14.1: Using Application Center Test In this lab, you explore the performance increase that can be obtained by using page caching. An Application Center Test will be used as the pri- mary tool to record performance changes as caching is implemented. Establishing Baseline Performance Data In this section, you add a new Web page called login.aspx: 1. To start this lab, open the OrderEntrySystemSolution from Lab 13.1. 2. Right-click the OrderEntrySystemSolution in the Solution Explorer, and click Check Out. 3. Set Customer as the startup project. 4. Set the CustomerList.aspx page as the startup page. 5. Right-click the CustomerList.aspx page, and click View In Browser to ensure that the Web application has been started. Notice that you are redirected to the login page. Enter a name and password, and click the Login button to redirect the page to the CustomerList.aspx page. Close the browser. 6. Open Application Center Test by clicking Start, All Programs, Microsoft Visual Studio .NET, Visual Studio .NET Enterprise Fea- tures, Microsoft Application Center Test. 7. Click File, New Project to create a new project. When prompted for the project name, type Customer. Your screen should look like Figure 14.19. 8. Create a new test by clicking Actions, New Test to start the New Test Wizard. Click Record a New Test. Select VBScript as the language. Click Start Recording to start recording a test. 9. When the browser window is displayed, enter the following URL: http://localhost/Customer/CustomerList.aspx 10. This code redirects the page to the login.aspx page. Type a valid name and password, and click the Login button to redirect the page to the CustomerList.aspx page. 11. Click the browser’s Refresh button to retrieve another copy of the CustomerList.aspx page. 12. Close the browser, and click the Stop Recording button. 13. On the next screen, type CustomerListTest for the test name. 14. After recoding the test, open the Tests node in Application Center Test. The CustomerListTest should be available; click CustomerListTest. 614 Chapter 14 p 430234 Ch14.qxd 7/1/03 9:06 AM Page 614 The upper-right window contains a message stating that notes can be entered. Enter the following note: Baseline CustomerList Test with 3 Browser Connections for 3 Minutes. Figure 14.19 The Application Center Test screen after creating a new project called Customer. 15. Right-click the CustomerListTest, and click Properties. Set the Browser Connections to 3 and the Duration to 3 minutes. 16. Right-click the CustomerListTest, and click Start Test. 17. After the test is completed, add the following line to the HTML of the CustomerList.aspx page: <%@ OutputCache Duration=”600” VaryByParam=”*” %> 18. Right-click the CustomerListTest, and click Copy. Rename the copy CustomerListCacheTest. In the upper-right pane, add the following note: CustomerList Cache Test with 3 Browser Connections for 3 Minutes. 19. Right-click the CustomerListCacheTest, and click Start Test. Figure 14.20 shows the baseline test overlaid with the cache test. Actual numbers will vary, but there should be a significant difference in performance between the two tests. 20. Scroll to the bottom of the report. Figure 14.21 shows the metrics of the test. There were no errors in either test. Your metrics will vary, but there should be a substantial difference between the two tests. Notice that there were two different response codes: The 200 is a success, and the Performance Tuning and Application Instrumentation 615 p 430234 Ch14.qxd 7/1/03 9:06 AM Page 615 302 represents the redirect to the login page as well as the redirect back to the CustomerList.aspx page upon successful login. There were a total of 1,581 requests by the baseline test and a total of 20,222 requests by the cache test, which represents speed increase of over 12 times. Figure 14.20 The baseline and the cached CustomerList.aspx page results. 21. Save your changes, and check the final solution back into Visual SourceSafe. Figure 14.21 The metrics of the baseline and cached CustomerList.aspx page. 616 Chapter 14 p 430234 Ch14.qxd 7/1/03 9:06 AM Page 616 Summary ■■ Performance tuning is the process of running specific tests on isolated parts of the software, making changes to the software, and rerunning the tests to identify bottlenecks and increase the software’s performance. ■■ Identifying bottlenecks is the process of interpreting load test data and investigating system performance to locate the slowest parts of the sys- tem. Eliminating a bottleneck can result in substantial gains in perfor- mance for the overall system. ■■ The System.dll file contains the Debug class, which can be used to obtain information when running an application that was compiled using the debug switch. ■■ The System.dll file contains the Trace class, which can be used to obtain information while the system is running, especially in a multitier or multithreaded application. ■■ Trace provides the ability to perform page- or application-level tracing. Page-level tracing is configured at the Web page, whereas application- level tracing is configured in the Web.config file. ■■ Performance Monitor can be used to monitor system resources, such as memory usage, processor utilization, disk access, and network band- width. In addition, the .NET Framework provides many counters, and many applications, such as SQL Server and Internet Information Server, provide counters. ■■ Strings are immutable in the .NET Framework. Concatenation of large strings should be avoided due to the resources that are required to move this data. ■■ The StringBuilder class can be used when string concatenation is required, because the StringBuilder class contains an Append method, which does not require excessive resources. ■■ Caching can increase Web performance substantially. For pages that are relatively static, page caching can be used. For pages that can’t be cached, object caching can be implemented. Performance Tuning and Application Instrumentation 617 p 430234 Ch14.qxd 7/1/03 9:06 AM Page 617 [...]... associated with an assembly ‘ Review the values of the assembly attributes ... is exposed to COM ‘ Version information for an assembly consists of the ‘ following four values: ‘ Major Version ‘ Minor Version ‘ Build Number ‘ Revision ‘ You can specify all the values or you can default the ‘ Build and Revision Numbers ‘ by using the ‘*’ as shown below: ... solution is built Assembly Versioning When an assembly is built, its current version is stored in the manifest metadata of the assembly The version can be set by using an assembly attribute called AssemblyVersion By default, this attribute is located in the AssemblyInfo.vb file of each project The default value for this attribute is as follows: The assembly version... .assembly extern DataComponent { ver 1:0:1073:39385 } Although the version is recorded, the version is not used to enforce versioning because this is not a strong-named assembly When a strong-named assembly is referenced in an application, the record of the assembly contains the friendly name, the public-key token, and the version The presence of the public-key token indicates that this is a strong-named... %SystemRoot%\ Assembly folder For example, on most Windows 2000 computers, this is the C:\Winnt \Assembly folder, while on Windows XP computers this is the C:\Windows \Assembly folder Installing an assembly into the Global Assembly Cache requires the assembly to have a strong name The assembly can be dragged and dropped into the Global Assembly Cache folder Figure 15.9 shows the Global Assembly Cache... because the System.Reflection namespace contains the Assembly class and the AssemblyName class The Assembly class is enclosed within brackets because Assembly is also a keyword The Assembly class has several shared methods for obtaining a reference to an assembly In this case, GetExecutingAssembly method returns a reference to the DataComponent assembly The Version class contains properties called... versioning enforcement is provided as well as naming protection Also, strong-named assemblies can reference only other strong-named assemblies Creating a Strong-Named Assembly Creating a strong-named assembly requires a public and private key pair The key pair can be generated by using the strong-name utility (sn.exe) The strong-name utility is a console application that can be run by starting the Visual... visible in the assembly folder The DEVPATH environment variable only works with strong-named assemblies This setting is ignored for assemblies without strong names Assembly- Binding Policies Although strong-named assemblies provide enforcement of versioning between the Web application and the referenced assembly, there are many cases where it is desirable to copy a new version of an assembly to the... runtime to skip the publisher policy: The oldVersion also... create this assembly as a strong-named assembly The strong name, or full name, of the compiled assembly is as follows: Building and Versioning NET Components DataComponent, Version=2.0.0.0, _ Culture=neutral, PublicKeyToken=4fa8f612df7c8110 The strong name consists of the friendly name, the version, the culture (which should always be set to neutral for the main assembly file), and the public-key token . gains derived from caching. Figure 14. 15 The original three-browser connection test overlaid with a new three- browser connection test. 608 Chapter 14 p 430234 Ch14.qxd 7/1/03 9:06 AM Page 608 Page. database. Figure 14. 17 The output of the object-caching test, overlaid with previous tests. Performance Tuning and Application Instrumentation 611 p 430234 Ch14.qxd 7/1/03 9:06 AM Page 611 Figure 14. 18. test name. 14. After recoding the test, open the Tests node in Application Center Test. The CustomerListTest should be available; click CustomerListTest. 614 Chapter 14 p 430234 Ch14.qxd 7/1/03