performance improvement tips

how to improve page performance and performance tips and best practices
JohenCorner Profile Pic
Published Date:02-08-2017
Your Website URL(Optional)
50 ways to AVOID, AND FIX ASP. NET PERFORMANCE ISSUES  HJOPUNPZHSHZYLZVY (UOVU`+HUN 'HUOVU`KVUL Projects that use multiple levels of cache often demonstrate a misunderstanding of why caching is required in the first place. Caching is not synonymous with performance. Your code should already be ecient. Caching should only be used as a last resort, after you’ve made all possible (and sensible) code optimizations. (/05. )9,(2.3(:: 7) =PL,UNPULZ9LTVL\U\ZLK 2LPU.YPMÄU 'RLNYPMMRLNYPMÄUJVT If you’re an ASP.NET MVC developer, you might not know that ASP.NET still loads the View Engines for both Razor and Web Forms by default. This can cause performance issues because MVC will normally look for Web Forms views first, before switching over to the Razor views if it can’t find them. You can quickly eliminate this performance issue by adding the following two lines to your Global.asax, in the Application_Start(): ViewEngines.Engines.Clear(); ViewEngines.Engines.Add( newRazorViewEngine()); Goodbye Web Forms View Engine 7) Z7+)ZVKLI\NZL4PJYVZVM» VYWYVÄSLL_LYUHSHZZLTISPLZ VYSPIYHYPLZ +HPK/HUL` SPURLKPUJVTPUKHPKHOHUL`OHUL`JVKLZUL To accurately debug or profile an external assembly or library (i.e. one you’re not directly compiling), you need the PDB files that accompany each of the DLLs. These files give your debugger or profiler access to information such as function names, line numbers, and other related metadata. One thing that sucks in particular is debugging and profiling native Microsoft .NET assemblies without this kind of information. Fortunately, there’s a solution for this very issue. With a little-known feature in Visual Studio 2012 (and 2010 too), you can connect to Microsoft’s Symbol Servers and obtain most of the debugging symbols for their assemblies and libraries. 7)Just go to Tools – Options – (expand) Debugging – Symbols, and select the Microsoft Symbol Servers as your source for Symbols. Getting the symbols from Microsoft every time you debug or profile is slow and painful. It’ll even give you a pop-up saying as much once you check the Microsoft Symbol Servers, so be sure to specify a directory under “Cache symbols in this directory”. It will keep a local copy of the PDBs and check for updates every so often. As a result, you get your regular debugging/profiling AND you can see the function names of the Microsoft assemblies. 7)(ZLSLJPVUVMPWZ YV`/\U; 4PJYVZVM4=7  Make sure HTTP compression is turned on for any uncompressed content. HTML in particular compresses significantly, and in this era of mobile friendliness and slow 3G connections, that’s essential.  Always set the CacheControlMaxAge attribute in web.config to a high number (a year is good). You don’t want people pulling down the same static content they did last week. It’ll also save on the bandwidth you’re paying for. 7) Make use of the OutputCache annotation on MVC controllers. If the server can serve from memory, rather than going to disk or database, that’s a good win.  Always profile your ORM database hits with SQL Profiler during development. ORMs get away from you very quickly. Before you know it, you’ve run a query 2000 times in a loop, when you could have retrieved all your data with a single database hit.  Watch out for lazy loading in ORMs. You shouldn’t lazy load any entities that could be retrieved with a single database hit. 7) Implement different database queries in different contexts. In the API and on a webpage, you’ll inevitably require different entity properties, so don’t load things you don’t need just because it’s convenient to reuse a query. Get MiniProfiler and configure it to always run when you hit your site (just don’t enable it for the general public). You’ll get detailed execution times and a big red warning if the same database query is running multiple times. 7) 4HRLZ\YLWHNPUNPZJVUK\JLKH OLKHHIHZLSH`LY (UOVU`HUKLY/VVYU 'HUOVU`FKO When using grid UI controls (framework based, or 3 party owned), you should carefully consider how paging is implemented. Many controls implement paging in a simplistic fashion, where the database is required to return all available data and the control limits what is shown. This strategy is fraught with performance problems, as it means that all the data in the given set must be extracted from the database (e.g. all customers or orders). Depending on the records involved, this could cause significant problems. 7) VYHZUHWW`\ZLYL_WLYPLUJL- SPLUHSH`ZHSPKHLVUOLJ :PTVU,SSPZVU)HSS 'ZPYLI To avoid unnecessary round trips to the server, validate form entries on the client using JavaScript before posting them. This provides quick feedback and makes your application feel more responsive. Always make sure you explain your validation errors as well. If you use complex password rules or regex patterns, include a message to explain what the rules are to prevent user frustration. 7) (SH`ZWLYMVYTHSPKHPVUVUOL ZLYLYHZLSS (UOVU`HUKLY/VVYU 'HUOVU`FKO This isn’t exactly a performance tip but rather a security tip for when people think that they could improve performance by cutting out server-side validation. These days, client-side validation can be bypassed with ease, so you can’t trust what comes from the browser. So if you think you can save some processing cycles and bypass these steps, don’t, as you’re actually opening massive security holes. 7) SPLUZJYPWZ`V\9LPLOHJ HYL\ZPUN (UOVU`HUKLY/VVYU 'HUOVU`FKO Out of the box, many ASP.NET projects include client script libraries which you may or may not be using. It’s always a good idea to check what you are using, and when. 9LK\JLTLTVY`SLHRZKYHTHPJHSS` ZHLTLU¸\ZPUN¹POOL 4PJOHLS:VYLUZ SPURLKPUJVTPUTPJOHLSZVYLUZ If a type implements IDisposable, wrap the use of it in a “using” statement, so that it automatically disposes of objects properly when the block exits. 7)    4HOL2 'T\KU\N Reducing the amount of data sent across the network can improve application performance significantly. Compressing CSS and JavaScript is possible using bundling and minification. This will reduce the number of server requests and the amount of code sent across the wire. stop 7) ULVYR 9LK\JLOLKHHZLUHJYVZZOL (VPKY\UUPUNZPLZPUKLI\NTVKL HYK+HL ',UJVZPH When it comes to ASP.NET, one of the most common performance blunders I see on a regular basis is accidentally or intentionally running sites in debug mode. Language-level optimizations, such as using StringBuilders, Arrays instead of Lists, Switch instead of If-Then-Else, and so on, are popular, but when you measure their real impact, they usually pale in comparison to optimizations at the framework level. 7) JHYLM\SS`OLUPUWYVK\JPVU JVUZPKLYOH`V\ULLKVSVN (UOVU`HUKLY/VVYU 'HUOVU`FKO Many people deploy to production without checking how logging is currently configured. It is always advisable to check whether your policy is to have logging on or off by default and, if on, what level you should be targeting. In addition, you should check which targets you are outputting to, what archiving strategy you have, and whether your logging infrastructure allows for async logging.  7) (ZLSLJPVUVMPWZ 5PJR/HYYPZVU 'ULO\Z Including height and width in img / tags will allow your page to render more quickly, because space can be allocated for the image before it is downloaded. Add script references at the bottom of the page, because asynchronous downloads halt when a script reference is reached. Style sheets and images can be downloaded asynchronously. Use a content delivery network (CDN) for hosting images and scripts. They may be cached and it will reduce load on your server. 7)    Use image sprites to retrieve smaller images in one download. Use AJAX to retrieve components asynchronously that may not be needed immediately, such as the content of a collapsed panel, content behind a tab, and so on. Make sure you’ve removed HTTP modules that aren’t being used (Windows authentication, for example), and that you’ve disabled services such as FTP and SMTP, if you’re not using them. 7)   ZLOLZHY4VKLHYPI\LV YLK\JLOLSVHKPTLMVY`V\Y 5,;ZPL(:7 1LYLT`1HYYLSS 'QLYLT`QHYYLSS Every time you update your site, IIS must recompile it during the first request, so the initial request takes significantly longer than subsequent ones. An easy solution is to tell IIS to automatically recompile your site as part of the update process. This can be done using the startMode attribute in the ApplicationHost.config file. You can even specify a custom action to run on start-up, such as pre-populating a data cache. 7) +VU»\UKLYLZPTHLOLHS\LVM OL0OLUHJRSPUNWLYMVYTHUJL WYVISLTZ 1LYLT`1HYYLSS 'QLYLT`QHYYLSS Simple UI tricks, such as progress bars, redirecting users’ attention using animation, or placing slower loading sections at the bottom of a page or off- screen, can often ‘fix’ a performance problem without the need to tune the underlying code. These UI tricks are an important tool to have in your performance tuning toolbox and can be much quicker and easier than addressing the underlying issue. They can act as a holdover until you have the time to devote to the core problem. 7)  UV;OYVOHYKHYLHOLWYVISLT KLLSVWLYZ 1LYLT`1HYYLSS 'QLYLT`QHYYLSS 7)  As developers, we often want to fix problems with code, but don’t be afraid to ‘put the compiler down’ and throw some hardware at the problem. Performance problems caused by disk I/O bottlenecks or paging out of RAM can often be solved by a faster hard drive or more RAM. CPU- bound bottlenecks can often be solved by a new machine with a faster processor. As counter- intuitive as it sounds, addressing problems by buying a new machine or upgrading an aging one is often much cheaper than having a developer troubleshoot, diagnose, and correct a deep performance problem. And the rest of your website will get a performance kick to boot