#100DaysOfWeb in Python Transcripts
Chapter: Days 21-24: Async Flask APIs with Quart
Lecture: Performance comparison: Flask vs Quart
0:00 We've done all this work
0:01 and converted our web app to be asynchronous friendly
0:05 and we've converted it to use Quart
0:08 which will actually execute the stuff in parallel.
0:11 So here's the moment of truth
0:12 did we actually get anything out of that?
0:14 Was it any better at all?
0:16 The truth is it depends a lot
0:18 on a lot of different conditions.
0:20 Much of, under certain circumstances, absolutely
0:23 so let's find out.
0:24 We're going to use this tool called wrk
0:26 which as a modern HTTP benchmarking tool
0:29 let's you on the command line
0:30 sort of hit a URL or set or URLs
0:32 and say, test them with this many clients
0:34 and this many connections
0:35 and see how fast you can do requests
0:37 and see if there are any errors, things like that.
0:40 So this is kind of hard to get if you're on Mac OS
0:42 like I am, it's pretty easy.
0:44 You can brew install it.
0:45 You can see the bottom initial link
0:46 on how to install it at these various locations.
0:50 So on Mac OS, brew install is pretty easy
0:52 if you're familiar with Homebrew.
0:54 On Linux, you have to compile it from source.
0:57 You check it out from GitHub and you compile it.
0:58 It's not too hard in Linux.
1:00 And on Windows 10, you can also install it
1:03 but the way you install it in Windows 10
1:05 is you install the Ubuntu subsystem for Windows 10
1:08 and then follow the Ubuntu Instructions.
1:10 So pretty much, that is probably the most work.
1:14 You don't have to install us
1:15 you can use any load testing tool.
1:16 I'm just going to show you some results.
1:18 So here is what we got when we run wrk
1:21 against our Flask version of the sun API call.
1:26 So we're saying here, run with 20 threads
1:29 20 concurrent connections
1:33 and pound the heck out of this URL for 15 seconds.
1:38 Run it for 15 seconds, 20 threads, 20 connections
1:41 and see what you get. Well it turns out we got not such great response.
1:45 We got 4.72 requests per second.
1:48 That sounds okay, but if you look over here
1:51 actually we got 71 requests per second, but see that red?
1:56 We got 65 timeouts.
1:59 That's bad, that's not really amazing at all to me actually.
2:03 If I had a site, and half of the time it was timing out
2:06 it would be basically considered to be down.
2:09 Why is it timing out?
2:10 We talked about as many, many requests come in
2:12 if you can't keep up, they backup, and backup, and backup
2:16 until eventually we got to the point
2:18 where those requests actually timed out.
2:21 So this is the straight Flask version of that code
2:24 and remember these calls take pretty long.
2:25 It's like a half a second per request
2:28 to do the API interaction and stuff.
2:30 How about switching to Quart
2:32 and running the exact same thing?
2:33 Here it is with Quart.
2:35 First of all, notice the red line is gone.
2:37 The red line is gone, timeouts are gone, that's awesome.
2:40 We also, instead of having 71 requests per second
2:43 we have, sorry, 71 total requests, we have 311
2:47 which is instead of four requests a second
2:49 20 requests a second
2:51 so actually it's about four times better
2:54 in terms of performance, so that is pretty awesome.
2:57 One word of warning if you go and do this.
3:00 This is basically a deployment for Quart in general.
3:04 If you want to do this testing
3:06 and you run Quart under a standard WSGI based web server
3:11 uWSGI, gunicorn, something like that
3:15 without configuring gunicorn in a certain way
3:18 it's still going to just act exactly like Flask.
3:20 You have to run it in a different kind of web server
3:23 that understands a different asynchronous API.
3:26 The guy who made Quart
3:27 created a variant of Gunicorn called Hypercorn
3:31 so if you wanted to take advantage of this asynchronous bit
3:34 and if you're using Quart, of course you do
3:36 otherwise why would you not just use Flask?
3:38 If you want to take advantage of that
3:40 you need to run with Hypercorn
3:42 which we got when we installed Quart itself.
3:46 So just be really careful around doing this testing
3:48 that you actually run your app in Hypercorn
3:50 and not just run it like we did out of PyCharm
3:53 because then you'll get Flask performance for both of them.
3:56 This isn't change the world levels of performance
3:59 but all those errors are going away
4:01 and much better response time.
4:02 I would say that that's actually a really big deal.
4:05 Thumbs up to Quart.
4:06 I definitely improved the performance here.