This contest started as a fun chat with an employee at HackerRank and how it ended up with a coding contest. One of the engineers from our team, Aditi Tayal, has recently started learning to build a web app using Mysql and Redis. She built a simple task manager and shared this link with me https://aditiflaskcrudapptutorial.herokuapp.com/.
My first instinct is to figure out how well the app scales and does it have any security issue. I built a small script to insert tasks at a rapid rate and she wrote a code to delete the entries I made. Soon, we were trying to figure out which code runs faster. And ended up planning for a contest to see which script runs fast.
The rules of the contest were as follows
- The script/code we write will run for 120 seconds. We will use timeout to make sure the code runs exactly for 120 seconds
- We will run the script from my personal server in linode.com, so that we there is no unfairness because of home internet speed.
- The script which can make the maximum number of insert within the 2 minutes wins.
- We still haven’t figured out what the winner is going to get.
My initial plan was to write a script using go (something which I’ve been planning to learn for a while), but ended up just using ab
My only worry is that since this is a single web server with not many scaling options if I hit the server with a very high concurrency, it might end up chocking. So, I ended up running ab with multiple concurrent options.
|Concurrency Level||Total Time||Request count||Requests per sec||Time per request||Connection Times|
The only number to worry about is time taken. Here is the plot of the time taken to hit 12K requests vs concurrency level.
Looking at the graph above, guessing 300 concurrency is the best.
We ran the script thrice for 2 minutes. Each of the run was a completely different where thing were good, bad and finally evil.
I let her script run for 2 minutes. A very fair game. I didn’t do anything to disrupt the overall timings or anything at all. In this one, I was able to run 12923 rows. Aditi was able to get only 12140 (though the script had a few extra print commands in it). When we ran the same script in her machine, she was able to hit close 11k+, and my ab script reached only 8k. We both mutually decided to consider the first result which ran in my Linode server and agreed that I won the contest. We may do a re-run soon after we fix some issues in our code.
It is hard to be fair in a game, where you have so many ways to break the rules. This time, I was running another script to fetch the data while she ran her script to insert new records. The reads were hit at a high rate, that the simple SQLite server couldn’t handle both the reads and writes. And all I had to run was
ab -n 10000 -c 500 https://aditiflaskcrudapptutorial.herokuapp.com/
Taking the unfair game to the next level, this time I tried to be pure evil. I was running another script to actually delete the rows she was adding while she ran her script. In fact, she even wrote this 3 line python script, which made the job a lot easier for me. This was fun 🙂 The only catch is to delete only a few rows so that she doesn’t get suspicious.
import requests for i in range(1, 5000): content = requests.get('https://aditiflaskcrudapptutorial.herokuapp.com/delete/'+str(i))
Overall, this was a fun experiment. Even though I won, what matters the most is that we learned a bunch of things about ab, timeout, and how a single web-server behaves under pressure.