Presentation is loading. Please wait.

Presentation is loading. Please wait.

Active server pages (ASP.NET)

Similar presentations


Presentation on theme: "Active server pages (ASP.NET)"— Presentation transcript:

1 Active server pages (ASP.NET)
Performance Active server pages (ASP.NET) Chapter-14

2 Topics covered How to design and test for performance
The techniques to use in ASP.NET pages and data handling to ensure the fastest possible pages What caching is and how it can be used create a list of countries using suitable table as example. Create list of cities of the selected country and then show the detail in grid view.

3 Simple Techniques

4 Simple Techniques The things you can do to either existing code or new code that you write. to dispose of resources after they are no longer required to ensure connecting to a database is done in the best possible way using stored procedures can improve data access performance to use generics to improve performance of collections session state can be minimized to allow less processing to be done by ASP.NET view state can be tuned to reduce the amount of data sent to and from the web server

5 Object Disposal The reason for this is that objects need resources to manage them, resources such as CPU and memory. The fewer of these resources used, the less work the server is doing. In general, objects that use expensive resources like the file system, graphics, or databases should be disposed of as soon as they are no longer needed. By default, resources are disposed of automatically by the Garbage Collector. It is possible to improve performance by taking control of object disposal yourself. You can do it in two ways:

6 Object Disposal Disposal with Using Disposal with try/catch try
{ // create resource } catch() // handle exception finally // dispose of resource Disposal with Using using (resource) { // code that uses the resource }

7 Database Connections Each connection takes resources and may stop another application from connecting, so these should be used as sparingly as possible, and only kept open for as short a time as possible. The general rule is to open the connection as late as possible, fetch the data, and close the connection as soon as possible, disposing of the connection once done. In general, if you are manually creating connections, you should dispose of them as soon as you are finished. The Using statement is excellent for this: using (SqlConnection conn = new SqlConnection(“. . .”)) { // code that uses the connection } If you are using the data source controls, object disposal is handled automatically for you.

8 Stored Procedures so far, the data has been fetched either using a data source control such as the SqlDataSource or using code, where a SqlCommand or SqlDataAdapter were used. In all cases, the command that fetched the data, the SQL, was entered directly, such as: SELECT [ProductID], [Name], [Description], [Price], [PictureURL] FROM [Products] it’s not the fastest way to fetch data, because the database has to work out exactly how it is going to fetch the data when the command is executed. This involves creating what’s known as an execution plan—a plan of how the SQL is going to be executed, how tables are to be joined if there are multiple tables, which indexes to use, and so on, and it does this every time.

9 Stored Procedures A much better solution would be to work out the execution plan and store it for subsequent use. So when a query is executed, the plan is ready and doesn’t need recalculating. You can do this by way of stored procedures, which are a way of wrapping up SQL into an easily manageable form. The syntax for creating a stored procedure is as follows: CREATE PROCEDURE ProcedureName AS SqlStatement

10 Strongly Typed Collections
The differences between a normal collection and a generic one. Take the standard ArrayList[normal collection] as an example: the ArrayList stores objects, so when a string is placed into the list, it is converted to an object data type. Likewise, when you’re fetching data from the list, it needs to be converted back to its native type. This conversion between types is an overhead. using System.Collections; ArrayList myList = new ArrayList(); myList.Add(“abc”); myList.Add(“def”); String s = (string)myList[0];

11 Strongly Typed Collections
With a generic collection, the overhead is avoided because the collection is automatically of the correct type. Instead of an ArrayList, a generic List can be used: using System.Collections.Generic; List<string> myList = new List<string>; myList.Add(“abc”); myList.Add(“def”); string s = myList[0]; When declaring the List, you set the type of object it contains within angle brackets—in this case string. When objects are added to the list, they don’t need to be converted because the list is only storing strings. Similarly, no conversion is required when you’re reading a value from the list.

12 Session State The session can be described as the time spent browsing a site, from the moment you first start browsing to the moment you close your browser. This is important to know because session state takes resources, both the processor and memory, and if it’s not required then why have it there? By default, the session is enabled for read and write access, but this can be disabled if your application is never going to use session state. You can do this in the application configuration file (Web.config), by using the sessionState element: <sessionState mode=”Off” />

13 Session State If session state is required, but none of the pages need to update it, then it can be made read-only (or turned off for pages) by changing the enableSessionState attribute of the pages element in Web.config: <pages enableSessionState=”ReadOnly” /> The values can be False to turn off session state, True to enable it, and ReadOnly to make it read-only. The ReadOnly property can also be applied to the Page directive on individual pages as follows: Page enableSessionState=”ReadOnly” %>

14 View State The way ASP.NET server controls retain their state is a great boon for the programmer and makes construction of web sites far easier than it used to be. The retention of state is called the view state and is stored within a hidden control on the page, so no extra programming is needed. This does, however, come with a downside, and that downside is that the size of the page increases, which in turn leads to a slower-loading page. Like session state, view state can also be disabled, both at the page and control level, using the EnableViewState attribute. To enable this for all controls on a page (or the Master page) you modify the Page directive as follows: Page EnableViewState=”False” %>

15 View State For controls, the attribute is the same. For example, the menu control in site.master could have view state disabled like so: <asp:Menu id=”MyMenu” runat=”server” EnableViewState=”False” /> View state can be disabled for the entire application in a similar way to session state, by modifying the pages element in the Web.config file: <pages enableViewState=”false” />

16 Caching

17 Page Caching

18 Page Caching

19 Page Caching

20 Page Caching

21 Caching Output Caching
Output caching is the simplest of the page caching techniques shown in the previous example, whereby the entire page is cached. Can be configured by adding the following line, near the top of the page, underneath directive: OutputCache Duration=”30” VaryByParam=”none” %> Fragment Caching Fragment caching enables portions of a page to be cached while the rest of the page remains dynamic. The user control uses the OutputCache directive to dictate how it should be cached, in the same way that pages use it: Control OutputCache Duration=”30” VaryByParam=”none” %>

22 Caching Post Cache Substitution
Post cache substitution is the opposite of fragment caching, where the page is cached but a portion of the page is dynamic. This is achieved with the Substitution control, which works differently from other forms of caching. You still use the OutputCache directive on the page to determine the caching, but the Substitution control is simply a placeholder into which the content is to be placed, and this content has to be created manually. The Substitution control has a property called MethodName that indicates a function that will return a string of data to be substituted into the cached page. For example, the page would be as follows:

23 Caching The code-behind would be as follows:
Page CodeFile=”PCS.aspx.cs” Inherits=”PCS” %> OutputCache Duration=”30” VaryByParam=”none” %> <html> <form runat=”server”> <div class=”boxFloatRight”><%=DateTime.Now%></div> <asp:Substitution id=”sub1” runat=”server” MethodName=”Substitute” /> </form> </html> The code-behind would be as follows: partial class PCS : System.Web.UI.Page { static string Substitute(HttpContext myContext) return “Date fetched on “ + DateTime.Now.ToString(); } When this page is run, it will be cached, so the div with the date and time will remain the same. But each time the page is requested, the Substitute method will be called, so the data that it returns will not be cached.

24 Q & A ?


Download ppt "Active server pages (ASP.NET)"

Similar presentations


Ads by Google