![]() | ||||||||||||||||||||||
|
|
![]() Useful advice{councils} on optimization of ASP-applications Introduction Productivity is a feature of the application. You need to think of productivity at once at designing the application, differently you will copy your initial code later. The material published below represents the first advice{council} from the whole series of advice{councils} on optimization of the applications using technologies ASP and Visual Basic Scripting Edition (VBScript). The majority of them have been repeatedly discussed and c by success are checked up on website Microsoft Corporation and other ASP-sites. Authors of a material mean, that you are already familiar with bases of development of ASP-applications, switching VBScript and-or JScript, ASP-sessions, etc. the important objects (Request, Response and Server). Kehshirujte often the used data on the server The typical ASP-page obtains the data from a database and then deduces them in format HTML. Irrespective of speed of your database, data acquisition from memory of the server is much faster, than processing of sql-search to a final database. The data acquisition, saved on a local hard disk, also usually is faster, than reception of the information from a DB. Therefore one of the basic ways of increase in speed of job of your ASP-page is caching often the used information in memory or on a hard disk. Caching of the data is a classical compromise " a place or time ". If you have selected a correct material for caching, you can see impressive increase of productivity of your application. That caching effective needed to be used for time storage only those data which are repeatedly used for a conclusion and each time are labour-consuming for repeated calculations. The cache full of the out-of-date information, will be waste of memory of the server. The data which do not change often, are the good candidate for caching because you should not worry be relative their synchronization through any time with a final database. Dropping out lists (sombo-box), tables of links, items{points} of the menu, and variable configurations of a site (including names DSN, IP addresses and URL) - the first candidates for storage in a cache. Notice, that you can kehshirovat` data presentation much faster, rather than the data yourself. If the ASP-page changes not so often and its{her} temporary cache will be rather impressive (for example, the full catalogue of products of firm), try to use the generated HTML-pages, than each time to load the server generation of ASP-pages. Kehshirujte often the used data in objects Application or Session Objects Application and Session serve for a data storage in memory which values can be accessible between several HTTP-searches (as against usual variables, whose values are accessible only in a body of one ASP-page). The data of object Session are accessible only to one user (during his session) while data Application are accessible to all users of a website. Therefore often before the developer there is a question: in what from objects to save often the used data. Usually, for initialization of variable these objects procedures of file Global.asa - Application_OnStart () or Session_OnStart () accordingly are used. If in yours Global.asa still there are no these procedures you can add them or initialize variables when it will be necessary. An example can be the following procedure using Application for storage of values of repeatedly used variable EmploymentStatusList. Procedure checks existence of the data in EmploymentStatusList and if necessary raschityvaet them anew: <% Function GetEmploymentStatusList Dim d d = Application ("EmploymentStatusList") If d = " " Then If values no - we execute calculation d = FetchEmploymentStatusList () Application ("EmploymentStatusList") = d End If GetEmploymentStatusList = d End Function %> It is possible to write similar function for any problem where it is not necessary to carry out anew each time trodoemkij calculation. Thus the data of any format (type variant) can be saved. For example, you can use line values, integers, files or a set of recordings. For example: ' To save value recordset as a file Function FetchEmploymentStatusList Dim rs Set rs = CreateObject ("ADODB.Recordset") rs. Open " select StatusName, StatusID from EmployeeStatus ", _ " dsn=employees; uid=sa; pwd =; " To receive all the line long FetchEmploymentStatusList = rs. GetRows () rs. Close Set rs = Nothing End Function If the received file will be used often then it is better to store{keep} it at once as the HTML-list, than a file which needs to be transformed each time in HTML: ' To save value recordset as the HTML-list Function FetchEmploymentStatusList Dim rs, fldName, s Set rs = CreateObject ("ADODB.Recordset") rs. Open " select StatusName, StatusID from EmployeeStatus ", _ " dsn=employees; uid=sa; pwd =; " s = " <select name = "" EmploymentStatus ">" and vbCrLf Set fldName = rs. Fields ("StatusName") Do Until rs. EOF s = s and "<option>" and fldName _ "</option>" and vbCrLf rs. MoveNext Loop s = s and "</select>" and vbCrLf rs. Close Set rs = Nothing FetchEmploymentStatusList = s End Function Kehshirujte the data on a disk of the web - server In memory of your web - server there can be too a plenty of the data. "Too much", certainly, is a question at issue is depends on that, how much you want to use memories, and also number of elements for caching and frequency from which these elements will be requested. In any case if you have too a plenty of the data for caching in memory, think of carry of a cache in text or a XML-file on a hard disk of the web - server. You can simultaneously combine caching on a disk and in memory to generate optimum strategy for your site. Notice, that at measurement of productivity of single ASP-page, data acquisition from a disk can be not always faster, than reception of the equivalent data from a DB. But "file" caching reduces loading a DB and a network, and at high loading a DB up to, will considerably improve the general{common} productivity the ambassador. Caching can be very effective at caching results of complex searches (for example, connection of tables), labour-consuming procedures of preservation, the big sets of recordings. That it will be convinced, as far as this decision there will be is required to test various circuits of preservation. ASP and COM provide some tool means for creation of circuits of caching on a disk. Functions of a set of recordings ADO Save () and Open () save and load recordset c a disk. Using these methods you can copy a code from the last advice{council}, replacing recording in object Application on method Save () for recording in a file. There are some other components which work with files: * Scripting. FileSystemObject allows to create, read and write down a file. * MSXML, MicrosoftR XML parser supports preservation and loading of XML-documents. * Object LookupTable (for example, used on MSN.com) - the best choice for loading simple lists from a disk. At last, consider the problem compulsory caching of the information on a disk. The generated HTML-code can be saved on a disk as .htm or .asp a file; hyperlinks can specify directly this file. You can automate process of generation HTML, using commercial tool means such as XBuilder or means of the publication in the Internet, included in MicrosoftR SQL ServerT. Besides by means of *include directive it is possible to include separate HTML-parts in file ASP or to read a HTML-file from a disk using FileSystemObject. For example, on a home page vbCode last 10 subjects of discussion of two debatable forums (http://vbcity.com/vbcode/ru/home.asp) are resulted. To display these lists it is possible by means of creation of two sets of recordings ADO at each reference{manipulation} to the given page or, following the given advice{council} to save them once as a HTML-file list.inc, and then to include in home.asp: <! - *include virtual = "/list.inc "-> The second way works much faster Avoid kehshirovat` slow components in objects Application or Session In spite of the fact that kehshirovanie the data in objects Application or Session can be good idea, caching of COM-objects can have serious traps. Entering of the most used COM-objects in objects Application or Session often tempts, but, unfortunately, it is a lot of COM-objects, including everything written in Visual Basic 6.0 or earlier, can cause serious critical problems after preservation in objects Application or Session. In particular, any component which is carried out slowly, will call critical problems when kehshiruetsja in objects Session or Application. Fast (quick non-agile) a component - a component marked ThreadingModel=Both which is incorporated Free-threaded marshaler (FTM), or - a component marked ThreadingModel=Neutral. (Neutral - new model in WindowsR 2000 and COM +). The following components are not quick: * Free-threaded components. * Apartment-threaded components. * Single-threaded component. * Configured components (library Microsoft Transaction Server (MTS)/COM + and server applications) are not quick while they Neutral-threaded. Apartment-threaded components and others not quick components well work within the limits of page (i.e. are created and collapse within the limits of one ASP-page). In IIS 4.0 component marked ThreadingModel=Both is carried out quickly. In IIS 5.0 any more so it is enough. The component should not be marked only as Both, he should also is incorporated FTM. IIS carries out check of components, but if you want her to cancel (i.e. want to allow to be to slow components saved in objects Application or Session), you can establish AspTrackThreadingModel in metabase in value True. But it (change AspTrackThreadingModel) is not recommended. IIS 5.0 will give out the message on a mistake if you try to save the slow component created with use Server. CreateObject, in object Application. You can bypass it, using <object runat=server scope=application...> in Global.asa, but it also is not recommended, as it conducts to problems (to turn and serialization), explained below. What all the same it is wrong if you kehshiruete slow components? The slow component, kehshiruemyj in object Session blocks Session from other working streams (thread) ASP. ASP serves a pool (container) of the working streams required by other services. Usually, the new search is processed by the first accessible stream. If Session it is blocked, the search should wait a stream when he becomes accessible. We shall lead{we shall carry out} analogy which will help to understand this situation: you go in shop, choose some rolls, and pay for them in cash department *3. Every time after you have chosen rolls in that shop, you always pay them in cash department *3, even in that case when in other cash departments the turn is shorter or even in general there are no buyers. Preservation of slow components in object Application imposes so negative effect on productivity. ASP creates a special stream for performance medelennykh components in limits Application. It has two consequences: all searches are built in turn to this stream and all searches serializujutsja. Forming in turn means, that parameters have been saved in shared area of memory; searches are switched to a special stream; the method of a component is executed; results are built in shared area. Serialization () means transformation to the consecutive form, that all methods are carried out during one time. For two various streams ASP not probably simultaneous performance of methods of a shared component. It destroys mnogopotokovost` (parallelism), is especial on multiprocessor systems. Worst of all that all slow components in limits Application in common use one stream (" Host STA ") so negative results of serialization it is available. Are confused? There are some general{common} rules. If you write objects in Visual Basic (6.0 or earlier), do not store{keep} them in objects Application or Session. If you do not know potokovuju model of object, do not store{keep} it in a cache. Instead of caching somewhere slow objects, you should create and remove them on each page. Objects will be executed directly in working stream ASP and there will be no turn or serialization. Proizvodimost` it will be adequate, if COM-objects are started under IIS and if they do not use a lot of time to be initialized and destroyed. Notice, that odnopotokovye (single-threaded) objects should not be used this way. Be close{attentive} - VB can create odnopotokovye objects! If you use odnopotokovye objects, this way (such as table Microsoft Excel) does not count on high efficiency. Sets of recordings (recordset) ADO can safely kehshirovat`sja when ADO is marked as Free-threaded. To make ADO as Free-threaded use a file Makfre15.bat which is usually fixed in the catalogue \Program FilesCommon FilesSystemADO. The prevention{warning}: ADO should not be Free-threaded if you use Microsoft Access as a DB. The set of recordings ADO should be disconnected in general also if you cannot operate configuration ADO on your website. Not kehshirujte connection of a DB in objects Application or Session Caching of connections ADO - usually bad strategy. If one object Connection is saved in object Application and used on all pages all pages will struggle for use of this connection. If object Connection is saved in ASP-object Session connection of a DB will be created for each user. It creates excessive loading the web - server and a DB. Instead of caching connections of a DB, create and destroy objects ADO on everyone ASP to page which uses ADO. It is effective, because IIS has the built - in connection of a DB. More precisely, IIS automatically supposes association of connections OLEDB and ODBC. It guarantees, that creation and destruction of communications{connections} on each page will be effective. As the connected sets store{keep} links to connection of a DB, it follows, that you should not kehshirovat` the connected sets in objects Application or Session. However, you can safely kehshirovat` the disconnected sets which do not hold the link to connection. To disconnect a set of recordings, make the following two steps: Set rs = Server. CreateObject ("ADODB.RecordSet") rs. CursorLocation = adUseClient ' a step 1 ' Fill recordset with the data rs. Open strQuery, strProv ' Now disconnect recordset from a source of the data rs. ActiveConnection = Nothing ' a step 2 The detailed information concerning connections look in directories on ADO and SQL Server. Reasonable use of object Session Now, when in the previous advice{councils} advantages of caching of the data in objects Applications and Sessions have been opened, we are going to suggest to avoid to you use of object Session. Sessions have some traps when are used on the loaded sites. Under "loaded" are available in view of sites with hundreds required pages a second or thousand users simultaneously. This advice{council} also is important for sites which should be scaled is horizontal - i.e. those sites which use some servers for distribution of loading and maintenance of fault tolerance at failures. For smaller sites, such as intranet-sites, advantages of application Sessions nevertheless outweigh. Generalizing, ASP automatically creates Session for each user who addresses to the web - server. Each session borrows{occupies} approximately 10 KB of memory (over any data saved in Session) and slows down performance of all searches a little. Session remains working before the termination{ending} tajmauta (timeout), 20 minutes are usual. But the biggest problem at use of sessions is not productivity, and expansibility. Sessions do not cover all involved the web - server; as soon as Session it has been created on one server its{her} data remain there. It means, that if you use sessions on a multiserver website, you should think up strategy for transaction processing each user which should be always directed on the server on which there is a session of this user. It is called as "jamming" of the user on the server (or " sticky session "). Object Application also does not cover all of the server: if you need to use and update in common data Application through the web - server, you need to use a final database. However unchangeable (read-only) data Application nevertheless are useful on multiserver sites. By the speed sites usually use the most critical not less than two webs - servers. Therefore at designing your ASP-application you should or use " sticky sessions ", or simply avoid application of object Session, as well as any other methods saving the information of the user on different webs - servers. If you do not use Session be convinced, that have disconnected them. It can be made by means of Internet Services Manager (see the documentation on ISM). But if all the same it is necessary for you to use sessions, that is some ways to reduce their impacts about productivity. You can move contents which does not demand sessions (for example, pages help, etc.) to the separate ASP-application at which sessions are switched off. Besides on pages where object Session is not used, apply the following directive, pomehhehhaemuju at the top of page: <% @EnableSessionState=False %> One of principal causes of its{her} application - guarantees that Session creates an interesting problem in case of use of frames (frameset.) ASP, that only one search from Session at any time will be carried out. It is done{made} that at simultaneous search by one user of several pages, only one ASP-search has been processed by session that helps to avoid problems of multiline access to object Session. Unfortunately, as a result of it all pages in frameset will be loaded consistently, instead of simultaneously, and the user should wait long time for full loading. Morals of this history: if you are not sure, that with use of frames and Session your application correctly works, use: <% @EnableSessionState=False %> Alternative to use of object Session are numerous parameters of management Session. By transfer of small volumes given (less than 4 KB) are usually recommended to be used Cookies, variable QueryString and latent (hidden) variables of forms. At use of a plenty of transmitted parameters (for example, a basket of the made orders in online shop) most the best choice - a final database. |
|||||||||||||||||||||