So you finally have your awesome web application up and running and all seems dandy. Your code is battle-tested, the unit tests worked like a charm and your Acceptance Testing was all but assured. Your website is up and running and users are trickling in slowly. Tens, hundreds, a thousand users login to your system and all of a sudden all hell breaks loose.
Whenever I ask anyone whether they will load test their web application, the answer is almost always this…
Performance Optimization is the process of making something, especially a computer system, work as effectively as possible. So in this context, it’s just more about making our applications run faster and in turn be able to handle a large number of users without any bottlenecks.
So what are some of the things that can cause these said bottlenecks on our web applications?
- Using High-resolution images: I know it feels amazing when your website has amazingly crisp quality images but this impacts the load time of your website. The higher the resolution your image is, the higher the likelihood of the file to be large and in essence, your website taking a longer time to load.
- Importing unused js modules: The importing of modules that are not in use may heavily impact performance. TTI (Time to Interactivity) is heavily impacted because a lot of js files are being loaded.
- Loading scripts from<head>: Like the previous issue, you may have a lot of scripts that you’re using (which is fine), but, loading them on the <head> section of your website will slow a website. This is because when a parser finds these lines with scripts, it fetches said js files and executes them until they are finished then parse the <body> afterward and many scripts can lengthen the time before the <body> is parsed.
- Poorly optimized queries to the database: I believe this to be the most urgent. A well-defined database with lots of relationships can make fetching data a bit complicated i.e. the need to use JOIN statements. By not specifying the fields that are in use, a JOIN statement can include the details of an entire table when only two or three fields are needed from the foreign table. This makes queries very slow and relaying this data to a client-facing app takes time.
- Using outdated language versions: It will always be likely that any update on a language’s version will be better performing than the previous one. i.e. from Node.js, they provide a link that shows the benchmark changes of the various versions and how performance improves per every new iteration. You can find this here.
To be able to identify these issues, load testing plays a huge role.
Load testing is defined as a type of software testing that determines a system’s performance under real-life load conditions.
Load testing improves performance bottlenecks, scalability, and stability of an application before it is available for production.
Why Load Test?
- Load Testing gives us confidence in the system and its reliability and performance
- Load Testing helps identify the bottlenecks in the system under heavy user stress scenarios before they happen in a production environment.
- Load testing gives excellent protection against poor performance and accommodates complementary strategies for performance management and monitoring of a production environment.
Open Source Tools we can use
Locust is an open-source load testing tool that allows users to write Python code to test web applications.
It is a fantastic lightweight testing tool that has been used widely across various large companies. i.e. by DICE, the game developer company of EA’s Battlefield games. No user interfaces are used to configure your tests, just pure Python code.
Loadtest is an npm package that runs a load test on the selected HTTP or WebSockets URL. Unlike Locust, loadtest allows us to only test a URL at a time. This is for quick load testing of a particular URL to see if meets a certain benchmark that is required before deploying to production. The documentation provided will allow you to create more test-cases for your particular needs.
So with load testing done and issues identified, there are a couple of ways you could improve the performance of your applications:
- Use of a CDN: A Content Delivery Network (CDN) is an interconnected system of computers on the internet that provides web content rapidly to numerous users by duplicating or caching the content on multiple servers and directing the content to users on proximity. CDNs reduce the load on the server and in turn, can improve the streaming quality of content on a website.
- Bundling and Minification: Bundling and minification improve load time by reducing the number of requests to the server and reducing the size of requested assets (CSS, JS).
- Image optimization: Yes, you have quality 4k resolution pictures but try and optimize them for your web application. Adobe Photoshop has a cool feature called “Save for Web” that makes your life way easier when optimizing photos. I’m sure there is a lot of software that can do the same function and a lot of tutorials online to help you achieve this.
- Removing duplicate code (JS and CSS): The more code you have, the more likely your application will be slower. Try and remove all the dead code that exists in your project. For this, you can do some research on tree shaking. Info on that you can find here.
- Caching: If you have a lot of data on your APIs that doesn’t change over a long period, caching works perfectly because there is no call to the database that is done. This increases load time exponentially. I have seen requests taking more than 2 seconds to load, take only 10 milliseconds to do so because of caching. Redis has been my go-to cache solution, have a look.
- SQL Optimization: From the base level when designing your database, you can optimize the database schema by:
- Normalizing tables
- Usage of optimal data types
- Avoiding Null values
- Avoiding too many columns
More information on SQL optimization can be found here and here.
So that’s it! I hope this article has given you some insights on the importance of Load Testing and how we can optimize performance through it.
If you have any questions or want to add some comments feel free to do so! Till next time!