Tuesday, July 30, 2019

Angular + ASP.Net WebAPI Application Performance Optimization - Index and Mind map

This is an index of things which needs to be checked, for performance optimization of a simple ASP.Net + Angular application. The simple application refers to application which read and write to SQL Server. If the application uses some other data store, still the points about other aspects are valid.

The details of each item would be either a link to posts in this blog or link to other resources. If someone is struggling to find where to start or struck on an optimization exercise,hope these questions will light a bulb in their brain. Sorry to say that it is very difficult to find an algorithm which applies every performance optimizations.

Browser side

  • Do we need to support desktop and mobile browsers?
  • Any possibility to avoid Internet Explorer and support only modern browsers:)
  • Do we have separate mobile site with URL or same site is controlled via bootstrap or similar display technique
  • Static assets
    • Are those cached? If not can we cache?
    • If those are cached what is the cache expiry?
    • If those are cached how its done? cache-control or Expires header? Is the cache really working?
    • Is the server properly returning http 304?
    • Are they minified and bundled?
    • Are the images optimized to have small foot print? 
    • Are they served from CDN?
  • Angular best practices applied?
  • If it is AngularJS, what is the feasibility to upgrade to Angular

The network

DNS and routing

  • Hows the DNS resolved to IP?
  • Is the application available public or accessed only via enterprise intranet?
  • What is the network connection band width between clients and server? also between web servers and database server(s)

Network devices

  • CDN
    • Any option for warming up cache?
  • Security / Application Gateways which open packets and scan for attacks.
    • What is the time needed for security inspections? Is it fast, if we bye pass gateway for testing?
  • Network load-balancer
    • Do we have load balancer? Are we reached the height of optimization and need scale out or scale up? In Azure, the best practice is to use LB as the machines gets patches and restarts periodically. Also follow the best practices to group machines so that all of those will not be restarted same time.
    • What is the algorithm used for load balancing?
    • Do we have sticky session need?
  • Common
    • Do we have logs available from these devices?
    • Are the logs enough to confirm that the backing web server is taking time to respond?

Web Server machine

  • Does the requirement of project is practical to the server? eg: One quad core, 8GB VM may never support 100,000 concurrent users even if its a simple company static web site
  • Is the machine / hosting infrastructure in Azure or any cloud? If so do all the best practices followed and coded by considering cloud principles such as retry pattern?
  • What is the status of CPU, Memory, Network etc.. during load test or busy hour?

IIS

  • Logging
    • Does the logging (time-taken field) says requests are taking high time? Please note that that time includes server processing + data transfer
    • Are we logging any custom field which are big in size?
    • Are the extra logging such as failed request tracking enabled?
    • During a load test of busy session
      • If we go to IIS worker process screen are we able to see many requests pending?
      • What is the http queue length performance counter shows ?
      • Are we able to identify one or sub set of requests taking high chunk of processing time other than rest of requests? This can be done by analyzing IIS logs.
  • Static assets - Kind of repetition from previous section. 
    • Are they cached properly?
    • Are we using ASP.Net Bundling or compile time bundled? Are they served in minimized form
    • Can we increase cache expiry?
  • Protocols
    • Any possibility to leverage http/2, if not done?
    • Is it using web sockets?
  • Authentication
    • Is it using windows authentication? If so NTML or Kerberos? Kerberos is recommended for better performance
  • AppPool Recycling 
    • What is the AppPool recycling settings? 
    • Is the recycling causing delay?
    • Any ways to run mock requests so that the cache is populated before real user hit application?
  • Throttling / Limiting
    • What is the MaxConnection Setting and other IIS level throttling?
    • Are there any performance counter showing connections getting queued and rejected?
  • Timeouts
    • Is there any manual test and load test attempted by reducing the timeouts to lowest possible settings? Some times high timeouts cause some requests to occupy entire system resources
  • Payload
    • Does the http compression enabled

ASP.Net Application

ASP.Net pipeline till our code

  • What are the throttling settings at different level?
  • Are there any custom HttpModules which may reduce performance? Any unwanted modules?

Our code

  • Are there anything which can be cached instead of going every time to database? eg: any reference tables which change only by deployment.
  • Are there any retries happening and succeeding after lot of attempts? Try reducing retry count to 3.
  • Are there static variable usage and any potential for lock contention and memory leaks?
  • Are the locks applied to lowest possible code section?
  • JSON optimization
    • Can the property names be shortened?
    • Are the default values serialized into JSON?
  • Are we doing any HTML rendering at server and returning as JSON property? Any possibility to make application as pure client rendered SPA?
  • Do we have high amount of reflection?

.Net General

  • Are there high amount of GC collections? This can be found from analyzing performance counters?
  • What is the fragmentation level in LOH?
  • Can we do NGEN?
  • Any memory leaks in the application? eg: open connections, unwanted static things which may hold objects?

Database

  • Does the applications logs showing that the queries are taking time?
  • Does the SQL setup with possible best practices? eg: temp db, flags, connection limiting etc...
  • Are there any subset of SPs always slow?

Mind map

Finally, there is a mind map created using PlantUML for easy reference. Since this is kept textual in GitHub and rendered on demand, this would be living document than the post.

Details on how to get PlantUML working is available in different post including a YouTube video tutorial.

Other similar links

Tuesday, July 23, 2019

Setting up PlantUML for coding C4 architecture diagrams - Video Tutorial

At last I published a video tutorial. As always its very difficult to get the first job done, if its a different type of job.

The tutorial is about setting Visual Studio Code with PlantUML to code C4 architecture diagrams  as explained in one of my previous post.

Since I don't have the right recording equipment and not a native English speaker, I used a text to speech service. It is AWS->Polly service. I didn't feel somehow the voices in Azure are good. The voice is US English Male voice named Matthew.

You tube link is given below and also embedded.
https://www.youtube.com/watch?v=Zt3Bj1HMJ8g

Tuesday, July 16, 2019

Puzzle - Highest product of 3 numbers in an array

Highest Product of triplets in array – Algorithm

Below is a puzzle I got from a colleague when he attended an interview. Trying to solve my way.

Question

  • Find the triplet from integer array whose product is highest
  • Use single loop.
  • Language can be any

Samples

  • Input [1,2,3,4] -> [2,3,4]
  • Input [-5,-7,4,2,1,9]-> [-5,-7,9]
  • Input [-10,-3,-5,0,-20]->[-10,-5,-20]

Approach

Since 2 negative numbers can produce positive product the approach taken was to find the lowest, second lowest, third highest, second highest and first highest numbers and compared their sums as follows.

            if (lowest * secondLowest * highest > highest * secondHighest * thirdHighest)
            {
                return new int[3] { lowest, secondLowest, highest };
            }
            else
            {
                return new int[3] { thirdHighest, secondHighest, highest };
            }


Finding the numbers is the trick. One implementation can be found in below location.
https://github.com/joymon/puzzles/tree/master/Numbers/HighestProductOfTripletInArray

Comment if there are any other simple implementations.

Happy coding...

Tuesday, July 9, 2019

IIS Web server log parsing - URLs which takes highest percentage of time and payload

Identifying where to start fixing performance issues to increase throughput is a tricky question, if we are new to the web application. One way to find out what to fix is to find the costly URL operations in the application. If the IIS logs are available we can check what are the URLs taking highest % time in a given duration they are costly in the system 

Sometimes each URL may be taking 1 second but there are millions of instances and some other times there are less instances but each instance might be taking 10s of seconds. So finding the % of time a particular URL or pattern taking from the total request time if a good start than taking a long running but less in number URL request.

This helps if we need to increase the throughput of existing system or want to reduce the hardware maintaining the same throughput.

Below are the queries to find out the candidate URL. The fix has to be done by analyzing the URL and what and how it does internally.

Percentage of time spent time-taken

/*This script finds out what are the requests taking highest % of time in the entire duration of IIS logs
Better place the URLs to remove ids, GUIDs to find unique URLs*/

SELECT TOP 500  cs-uri-stem,
  COUNT(cs-uri-stem) AS TotalRequests,
  SUM(time-taken) AS SumTime,
  MUL(PROPSUM(time-taken), 100.0) AS PercentTime,
  MAX(time-taken) AS MaxTime,
  MIN(time-taken) AS MinTime,
  AVG(time-taken) AS AvgTime
FROM '[LOGFILEPATH]'
// WHERE condition for proper filtering
GROUP BY cs-uri-stem
ORDER BY PercentTime DESC

Percentage of % payload size 

SELECT TOP 500  cs-uri-stem,

  COUNT(cs-uri-stem) AS TotalRequests,
  SUM(sc-bytes) AS SumSCBytes,
  MUL(PROPSUM(sc-bytes), 100.0) AS PercentSize,
  MAX(sc-bytes) AS MaxSCBytes,
  MIN(sc-bytes) AS MinSCBytes,
  AVG(sc-bytes) AS AvgSCBytes

FROM '[LOGFILEPATH]'
// WHERE condition for proper filtering
GROUP BY cs-uri-stem

ORDER BY PercentSize DESC

Tuesday, July 2, 2019

VS 2019 Zoom out shortcut missing

This is not a brainer. Just documenting a behavior noticed in VS2019 and a quick solution.

Somehow the VS2019 doesn't seems to have the key binding for Zoom Out using the key CTRL+SHIFT+, which essentially becomes CTRL+SHIFT+<. 

We can do research and find out what has happened. Or just add the missing key binding in options as follows.