Is the pain worth the gain? Writing webapps in C (Benchmarks vs Flask and Nodejs)
Tags: python, javascript, webdev, c
Published: 2020-10-29 02:49:34

I wanted to do a performance comparison between __Facil__(a web framework for C) against some more conventional tools, such as __Flask__(Gunicorn server) and __Nodejs__. __Let's dive in!__

![download (7)](https://dev-to-uploads.s3.amazonaws.com/i/rehpvoeobs6b2bleonic.gif)
 
__How the benchmarking was done:__
- A simple webapp was made using each framework. These webapps embed the client's IP address in a page template and then serve it to the client.
- The 3 webapps were __tested__ under __light__, __medium__ and __heavy__ load for __5 minutes__ each, running on a 1GB single core virtual machine(on Digital Ocean)
- They were evaluated on response time, CPU and memory consumption.
- In all of the plots below, the __average value by Facil__ is shown as a __dotted blue line__ for easier comparison. 

## TLDR;
- If you are developing a __high load web application__(over 1000 requests per second), writing it using __Facil__ could definitely __pay its dividends over time__ when it comes to __resource consumption__, __response time__(latency) and the __variance of these two__ factors(they tend to stay more predictable, even under a sudden surge in load)
- For __light load__ applications, __Facil__ might be __overkill__(did not provide significant benefits) unless you are developing for an embedded platform or you prefer to develop in C compared to other languages


## Heavy load (~1100 requests per second)

![download (7)](https://dev-to-uploads.s3.amazonaws.com/i/rehpvoeobs6b2bleonic.gif)
#### Response Time
Facil has come out on top by far when it comes to latency under heavy load. Facil managed to:
- __Have less of a latency spike__ (it responded better to a sudden surge in load at the beginning of the test)
- __Have much less variance in response time__ (more reliable results)
- __Have a lesser average response time__ under heavy load

#### Resource Consumption
![download (15)](https://dev-to-uploads.s3.amazonaws.com/i/ydphnt7en3ywv0u50802.gif) 
When it comes to resource consumption, Facil also managed to:
- Have __lesser resource consumption__ in both ram and CPU, especially compared to Flask
- Have a __lesser variance in resource consumption__ compared to other frameworks

## Medium load (~400 requests/sec)
![download (12)](https://dev-to-uploads.s3.amazonaws.com/i/koonf15x9it5jt2vgf12.gif)
#### Response Time
Under medium load, Facil was:
- __still leading ahead of Flask__ in terms of response time, but __Nodejs is catching up__(Facil was arguably a tiny bit better though).
- __Facil still had a better response to sudden load__ in the beginning of the test than Nodejs


![download (13)](https://dev-to-uploads.s3.amazonaws.com/i/43ahzkqyw02cdv68f4b1.gif)
In terms of resource consumption, under medium load, Facil:
- Is __still significantly leading__ in terms of __RAM__ consumption ahead of both Flask and Nodejs
- Had __less of a CPU spike__ under sudden load
- Had __less variance in CPU consumption__ compared to the other two candidates


## Light load (~100 requests/sec)

![download (10)](https://dev-to-uploads.s3.amazonaws.com/i/2g132bpa9a0a8dqsf30v.gif)
Under light load, Facil:
- Had __seemingly no advantage in response time__ over the other frameworks

![download (14)](https://dev-to-uploads.s3.amazonaws.com/i/fj6nfpvb80jzbed6j1vq.gif)
- __Facil had some minimal advantage__ in RAM consumption compared to other frameworks

## Final notes
__Obviously this was just a simple test__, but it has shown that __if you are expecting large loads__, __want to keep hosting as cheap as possible__ by minimizing resource consumption, or  __doing development for embedded platforms__, a non-conventional framework like Facil might be worth looking into. 

You can find the source code for the 3 webapps under:{% github wickdChromosome/simple_webapp_comparison no-readme %}