Latest / Talk Python To Me / #479: Designing Effective Load Tests for Your Python App
Transcript
- 0:00You're about to launch your new app or API, or even just a big refactor of your current project.
- 0:04Will it stand up and deliver when you put it in production? Or will it wither and collapse?
- 0:09How do you know? Well, you would test that, of course. We have Anthony Shaw back on the podcast
- 0:14to dive into a wide range of tools and techniques for performance and load testing of web apps.
- 0:20This is Talk Python To Me, episode 479, recorded August 8th, 2024.
- 0:25Are you ready for your host, please?
- 0:29You're listening to Michael Kennedy on Talk Python To Me.
- 0:32Live from Portland, Oregon, and this segment was made with Python.
- 0:35Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:42This is your host, Michael Kennedy.
- 0:44Follow me on Mastodon, where I'm @mkennedy, and follow the podcast using @talkpython,
- 0:49both accounts over at fosstodon.org, and keep up with the show and listen to over nine years of episodes at talkpython.fm.
- 0:57If you want to be part of our live episodes, you can find the live streams over on YouTube.
- 1:02Subscribe to our YouTube channel over at talkpython.fm/youtube and get notified about upcoming shows.
- 1:08This episode is brought to you by Sentry.
- 1:10Don't let those errors go unnoticed.
- 1:12Use Sentry like we do here at Talk Python.
- 1:14Sign up at talkpython.fm/sentry.
- 1:17And it's brought to you by WorkOS.
- 1:19If you're building a B2B SaaS app at some point, your customers will start asking for enterprise features like SAML authentication,
- 1:26SKIM provisioning, audit logs, and fine-grained authorization.
- 1:30WorkOS helps ship enterprise features on day one without slowing down your core product development.
- 1:37Find out more at talkpython.fm/workos.
- 1:41Anthony, welcome back to Talk Python To Me again, again and again.
- 1:46So awesome to have you back.
- 1:48Always good.
- 1:48Yeah, yeah.
- 1:49Great to be here.
- 1:49Good to catch up.
- 1:51Good to have you on the show.
- 1:52I know we've got a really interesting topic to talk about.
- 1:56Performance, load testing.
- 1:58How do you know if your website or your API is going to work when you ship it to the world in a real world way?
- 2:05Not just how many requests per second can it take, but a real use case, as I know.
- 2:10You're going to tell us all about, so that's going to be awesome.
- 2:12Before we do, you know, just a quick introduction to who you are and maybe for most 98% of the world who knows you, what have you been up to?
- 2:20Yeah, so I am the Python advocacy lead at Microsoft.
- 2:24I do a bunch of open source work, maintain some projects and stuff like that.
- 2:29Wrote a book on Python, the Python compiler called CPython Internals.
- 2:33But these days, I'm mostly known as the person that created VS Code Pets, which was a bit of fun, but has become the most popular piece of software I've ever written.
- 2:45So that's been interesting.
- 2:46It's now got over a million active users.
- 2:48I'm not sure how that works.
- 2:50Careful what you create.
- 2:51You might get known for it, you know?
- 2:53Yeah, I know.
- 2:54Yeah, it's interesting when you go to conferences and stuff now and I'm like, oh, I work on this project and this project.
- 2:59And then you'd mention the pets thing and they're like, oh, you're the pets person.
- 3:03Oh, you're the developer.
- 3:04I'm like, you spent a year writing a deep book on the internals of CPython and it's runtime.
- 3:10I don't know who you are, but this pets thing is killer, man.
- 3:14Yeah, there's a cat that runs around in VS Code.
- 3:16So that's cool.
- 3:16Can it be a dog as well?
- 3:18What kind of pets can we have?
- 3:19Oh, there's everything.
- 3:20Yeah, it can be a dog.
- 3:22There's like chickens and turtles and snakes and everything you can think of.
- 3:28It's a pretty active repository as well.
- 3:29We get a lot of feature requests for new shades of fur and new pets and new behaviors and stuff like that.
- 3:36So, yeah, if you haven't checked it out, then check out the VS Code pets extension for VS Code.
- 3:41Yeah, I installed it for a while.
- 3:43I had to uninstall it.
- 3:44It was too much.
- 3:45If you're one of those people that likes having distractions, then it's helpful.
- 3:49If you find it hard to have a little thing running around whilst you're trying to code, then it might be a bit much.
- 3:55A little bit like power mode.
- 3:56Oh, yeah.
- 3:57I don't know.
- 3:58Are you familiar with the power mode?
- 4:00I used the one in JetBrains.
- 4:01Yeah, I used the power mode for JetBrains when that was around.
- 4:04It was pretty cool.
- 4:05Yeah, if you want maximum distraction for your work.
- 4:09Yeah, it reminds me of Unreal Tournament, the sort of power modes for that.
- 4:13That's right.
- 4:14That's awesome.
- 4:15Well, let's start off with some stories.
- 4:19So, I think everyone has a sense of, like, why do you want your website or your API or your microservice thing of API and website, however you combine these things, to work well and understand.
- 4:32But it's always fun to share some stories.
- 4:34I know you got some.
- 4:36Yeah, so we want to talk about load testing.
- 4:37And I think a fun thing to do with load testing is to reflect on times where people haven't done it the right way and it's caused a really public disaster.
- 4:48But the analogy that I use is the halftime problem, which I'm English, and so this is mostly a soccer thing.
- 4:57But we've got really big soccer games.
- 5:00And it would be the same, I guess, with the Super Bowl.
- 5:02Yeah, I was just thinking the American Super Bowl, football Super Bowl.
- 5:05It's got to be.
- 5:06Yeah, but the difference is that in soccer, at 45 minutes, you've got a 15-minute break.
- 5:11Whereas in the Super Bowl, every 15 minutes, you've got a 15-minute break.
- 5:14Well, and it's just loaded with commercials.
- 5:17So, you've got, like, every five minutes, there's, like, oh, here's two more commercial breaks.
- 5:21Where soccer is a flowing game, there's, like, no break until halftime.
- 5:25That's the difference, I think.
- 5:26So, what happens with a big game, so if it's, like, a FA Cup final or, like, a, you know, like, a Champions League final or something, then you basically
- 5:35have 100 million people all stop watching the TV at the same time for 15 minutes, go to the
- 5:42kitchen, turn on the kettle, make a cup of tea, go to the bathroom.
- 5:46And so, the electricity providers actually have to plan for this because the kettle uses, like,
- 5:53a couple of kilowatts of energy.
- 5:55Especially in the UK where it's 240.
- 5:57Yeah, exactly.
- 5:58Yeah.
- 5:59And all of a sudden, you've got, like, tens of millions of people all switching on their two kilowatt kettles at the same time.
- 6:05So, the electricity providers basically have to plan around the soccer games so that they get this massive spike in load in their grid.
- 6:14So, they actually do look at the sports schedules to basically plan around this.
- 6:18Especially if there's something like a World Cup or something like that.
- 6:20And this is kind of like a load testing thing where you kind of need to plan ahead and think about what the load on the system is going to be.
- 6:28And is it a spike in traffic or is it, like, distributed?
- 6:31And I've definitely seen a couple of occasions where it's gone a bit wrong.
- 6:38So, here in Australia, we had a census maybe eight years ago.
- 6:43And normally, it's a paper census.
- 6:45You fill in the form, say who you are, what you do.
- 6:47But this time, they wanted to do it online as well.
- 6:50And they really encouraged people to use the online version.
- 6:52And they set up the system, set up the website, and said, okay, everyone can fill in the census.
- 6:58There's only 20-something million people in Australia.
- 7:01It's not a very big population.
- 7:02But on the last night before the census was due, it crashed.
- 7:06Because everybody logged on at the last minute and tried to fill it in.
- 7:10And they hadn't tested it properly.
- 7:12And it just wasn't ready for the load.
- 7:14And then the postmortem, they said, oh, we did the load testing.
- 7:17And we tested it.
- 7:18And it seemed fine.
- 7:19But we expected everybody to fill in the census over the six months they had to do it.
- 7:24We didn't think that people would leave it to the last minute,
- 7:26which is more like, well, what did you expect to happen?
- 7:29This is human nature that you're going to put it off until the last possible moment.
- 7:33Yeah, I can only see two spikes.
- 7:35A small spike at the beginning and a huge spike at the end.
- 7:38And like nothing in the middle.
- 7:39Yeah, exactly.
- 7:39So they had to kind of delay the deadline and then provision new infrastructure and stuff like that.
- 7:45So yeah, it was interesting.
- 7:46But it kind of highlighted how you need to think about load testing.
- 7:49Yeah, that's nuts.
- 7:51And I guess the mode before, the style before was kind of a distributed queuing system using paper envelopes where the queuing literally was physical.
- 8:01And then it would be processed at the rate at which the system can handle it, right?
- 8:05Which, you know, however fast you can get them in based on paper.
- 8:09But then you try to turn it into an interactive system.
- 8:12And oh, no.
- 8:12Yeah, yeah, exactly.
- 8:13You know, I have another example to share.
- 8:16Like we've seen websites that, you know, fall down like this.
- 8:20It fell down.
- 8:21And when something has to be done, I think it's probably really tricky to communicate.
- 8:25Let's say 10 million people tried to fill it out that day and it crashed on them.
- 8:30Well, how do they know that the deadline's extended and how do they come back, right?
- 8:33Like there's, it creates this cascading chain of really challenging problems all of a sudden to deal with it.
- 8:39You know, we had the healthcare Obamacare stuff in the U.S. where soon as that thing opened, it just completely died.
- 8:45And for weeks, people couldn't get insurance, which was bad.
- 8:47But that's not the story I want to share.
- 8:49I want to share a different one.
- 8:50There was this app, this person who was frustrated about, they're an artist, like a photographer, I believe.
- 8:56And they're frustrated about LLMs and diffusion models taking everyone's art and generating new art from it.
- 9:03And kind of, you know, I guess it's up to people's opinion whether or not it counts as stealing or copyright theft or it's fair use or whatever.
- 9:10But this person wasn't a fan of that.
- 9:12So they came up with a web app based on serverless that would look at stuff and say, this is real art.
- 9:18This is AI generated art.
- 9:20And it's not exactly the same, but they ended up with a $96,000 Vercel bill.
- 9:27Because they didn't really take into account how much it's going to cost per request to handle this on their serverless.
- 9:36It was like 20 cents a user.
- 9:37And everyone's like, you know, this is right at the height of the LLM boom and all the AI art boom and stuff.
- 9:44And people just swarm to it.
- 9:45Yeah, yeah.
- 9:46So that's a different kind of problem, right?
- 9:48Yeah, that's the cost issue.
- 9:49Yeah, there are definitely other ones to keep in mind.
- 9:52And we mentioned like, you know, the census and the Obamacare thing.
- 9:55And you might be listening and thinking, well, you know, my website is not going to get, you know, 15 million people logging in all at the same time.
- 10:03So like, is this really an issue that I need to handle?
- 10:05And like a few months ago, I was doing a load test of an AI app.
- 10:10So like a chat app and designed the load test and ran it with 10 users and it had issues.
- 10:16So like, this doesn't need to be a tens of millions of users problem.
- 10:20And the issue that it uncovered was like, oh, there's a rate limit on the number of tokens you can send to the LLM.
- 10:26And by default, it's a really small number.
- 10:28And if you have 10 people asking questions simultaneously, then that gets exhausted really quickly.
- 10:34And it just throttles you.
- 10:36And then like the app just says, oh, rate limit error.
- 10:39And you just wouldn't have noticed because if you're a single user clicking around, typing in, you know, questions, or even if you've written some fancy like UI testing or something, you're like, oh, we've tested it.
- 10:50And it works great.
- 10:51But, you know, you run 10 people using it at the same time.
- 10:54And that's where you start to notice problems.
- 10:55I've more than once considered putting some kind of LLM AI augment, you know, rag sort of thing in front of Talk Python because, God, at this point, eight, nine years of full transcripts, human corrected with all sorts of, you know, pretty highly accurate stuff.
- 11:11It would be awesome to have people have conversations about it.
- 11:13But I don't know, just the workload.
- 11:15I can see so many people coming to it and go, that's neat.
- 11:18I'm going to ask it about my homework.
- 11:19It has nothing to do with it.
- 11:21Just using it as like a free AI to just, instead of bothering to download something or use ChatGPT, I'll just use yours.
- 11:28And I'm like, eh, probably not worthwhile.
- 11:30All the trouble.
- 11:31Yeah, you can set them up so that they only answer questions about your own stuff.
- 11:35They don't act as a fancy front end for ChatGPT.
- 11:39We've got plenty of demos like that, that you give it your own internal documents and it only answers questions about those.
- 11:45And you can set it up so that if it doesn't, if it can't figure out a reliable answer, it tells you.
- 11:51It says, I can't.
- 11:52Yeah, that's great.
- 11:52Rather than just making something up.
- 11:55Yeah, that's really nice.
- 11:56Yeah, I forgot how closely you guys work with OpenAI and all them, right?
- 12:01Yeah.
- 12:01I mean, the compute in Azure to run and train all that stuff is, it's out of control.
- 12:07Yeah, the GPUs alone are insane.
- 12:09Yeah, it's, you know, I talked to Mark Rosinovich, a bit of a diversion, but about some of the hardware and some of the data center stuff.
- 12:16And it's like, we're going to create distributed GPUs that connect back over fiber to the actual hardware.
- 12:23So you can scale your GPUs and CPUs independently.
- 12:26There's just all kinds of crazy, interesting stuff there.
- 12:28But that's a side, a bit of a side note, a bit of a side note.
- 12:32All right.
- 12:32So let's talk about basically what do you want to do to design a load test?
- 12:37Like it's more than just let me write a loop and see how many requests I can make, you know, while true, call, you know, use requests and call this or call this endpoint or something, right?
- 12:48Yeah, so there's, I guess, what I normally highlight with load testing is that the wrong way to start is to try and work out how many requests per second your server can handle.
- 13:00That's interesting for throughput, but it's not a good measure of real user traffic.
- 13:06And the main reason is that every user is unique.
- 13:10They do different things.
- 13:11They follow different paths.
- 13:13They put in different data.
- 13:15And also when you're running like a benchmark, then it's just it's not waiting between requests.
- 13:21It's just trying to hammer as much as it can.
- 13:23So real users pause and read and click and wait.
- 13:28And there's normally latency.
- 13:30They don't request the same page over and over and over, right?
- 13:34They request a blend of pages.
- 13:36Here they go to the homepage and they do a search and they explore what they find in the search and they go back and do another search and then they go to check out or whatever, right?
- 13:44And as you're pointing out, they don't just hold down control or command R and just flicker the screen as hard as they can.
- 13:50They they're they're reading and they're interacting.
- 13:52And so what you're saying is if you really want to say how many actual users, not just kind of a throughput number, but how many people using the app do you possibly expect it could keep working or right?
- 14:05You got to factor all these things in, right?
- 14:06Yeah.
- 14:07So you got to factor in the randomness of where they go, what they type in the weights between each click or each API request.
- 14:16And then something else that's really important is that most modern applications is not just the initial HTTP request.
- 14:23It's the 95 additional requests to all the scripts and the resources and the Ajax calls and everything like that.
- 14:32So if it's if it's a browser based application, then typically, you know, it's not just the initial request.
- 14:38It's everything that happens after it.
- 14:39And I've definitely seen times where people have done a load test and said, oh, yeah, the website runs great.
- 14:44And then in the app, they had like a JavaScript polar that would like refresh the data or something every minute.
- 14:51And then they didn't test that.
- 14:53And then you get thousands of users who leave the tab in their browser and it's just polling in the background.
- 14:59So you've basically got this continuous stream of traffic to a special API that does like polling.
- 15:05And they hadn't load tested that and that produced a huge like spike in load and that caused the issue.
- 15:09It's like 75 percent of the workload is the polling.
- 15:12Yeah, exactly.
- 15:13What's your browser?
- 15:14What's your browser tab story?
- 15:16Are you a person that just has tons and tons of tabs open?
- 15:18I don't know.
- 15:19I try and clean them up as much as possible.
- 15:21Yeah.
- 15:21And have like a couple open maybe.
- 15:24And there's some researching something.
- 15:26And then when I'm finished, I just do close all tabs.
- 15:28Yeah, that's me as well.
- 15:29I think there's a there's a good number of people that just have like 50 tabs open.
- 15:33They just leave them.
- 15:34And think about what that does for your website.
- 15:36If you've got some kind of timer based deal, right?
- 15:39You got to consider those people that have just left it.
- 15:41Yeah.
- 15:43This portion of Talk Python To Me is brought to you by Sentry.
- 15:46Code breaks.
- 15:47It's a fact of life.
- 15:48With Sentry, you can fix it faster.
- 15:50As I've told you all before, we use Sentry on many of our apps and APIs here at Talk Python.
- 15:56I recently used Sentry to help me track down one of the weirdest bugs I've run into in a long time.
- 16:02Here's what happened.
- 16:03When signing up for our mailing list, it would crash under a non-common execution pass, like situations where someone was already subscribed or entered an invalid email address or something like this.
- 16:15The bizarre part was that our logging of that unusual condition itself was crashing.
- 16:21How is it possible for our log to crash?
- 16:24It's basically a glorified print statement.
- 16:26Well, Sentry to the rescue.
- 16:29I'm looking at the crash report right now, and I see way more information than you'd expect to find in any log statement.
- 16:35And because it's production, debuggers are out of the question.
- 16:38I see the traceback, of course, but also the browser version, client OS, server OS, server OS version, whether it's production or Q&A, the email and name of the person signing up.
- 16:49That's the person who actually experienced the crash.
- 16:52Dictionaries of data on the call stack and so much more.
- 16:54What was the problem?
- 16:56I initialized the logger with the string info for the level rather than the enumeration dot info, which was an integer based enum.
- 17:05So the logging statement would crash saying that I could not use less than or equal to between strings and ints.
- 17:12Crazy town.
- 17:13But with Sentry, I captured it, fixed it, and I even helped the user who experienced that crash.
- 17:19Don't fly blind.
- 17:21Fix code faster with Sentry.
- 17:22Create your Sentry account now at talkpython.fm/sentry.
- 17:26And if you sign up with the code TALKPYTHON, all capital, no spaces, it's good for two free months of Sentry's business plan, which will give you up to 20 times as many monthly events as well as other features.
- 17:39You did mention that the CSS and the, you talked about the AJAX, but I think also things like CSS images, JavaScript files, not JavaScript execution, but just getting the files, right?
- 17:51Some of these frameworks are a couple hundred megs.
- 17:53Like if you're not somehow distributing that through a CDN, that could be worth considering as well, I think.
- 17:59Yeah, it is definitely.
- 18:00And so in Python, for example, you've got in Django, there's an extension called white noise.
- 18:06So in Django, you've got static content.
- 18:09There is a really easy extension you can install because normally you have to kind of configure static and say, okay, my static files are here.
- 18:16And then you have to set up Nginx or whichever web server you're doing to serve that static content directly.
- 18:22So as a bit of a workaround, people often install this white noise extension that basically uses Python as their static server.
- 18:31So like every time you request a CSS or whatever, Python actually is the thing that reads it from disks and serves it back, which is great for like development.
- 18:39But like you should never use that in production because Python is not a very good CDN.
- 18:44So yeah, that's kind of with load testing, you would just test the endpoint and say, oh, yeah, it works great.
- 18:51But then you actually run it in a browser.
- 18:52And if you're using something like white noise, it's actually creating 10 times, 20 times more load than you expected because it's pulling in all this like CSS and JavaScript and images and stuff like that.
- 19:03Yeah, it can be out of control.
- 19:04I ran into an issue with my website where, I don't know, I got tired of being too careful with font awesome fonts.
- 19:12And I had like missed one.
- 19:14I'm like, I'll just put the whole thing.
- 19:15I'll just put the whole CSS file in.
- 19:17I'm sure it'll be fine.
- 19:19It wasn't fine.
- 19:19Like the web app is running at like 10% CPU usage and Nginx trying to serve up all the JavaScript.
- 19:26Is it like 80?
- 19:26I'm like, what is it doing?
- 19:28Like, oh, it was serving up a megabyte size to font stuff.
- 19:32Every request.
- 19:33This is not good.
- 19:34So yeah, in terms of projects that I've worked on in the past, I've worked on load testing some like big campaigns and stuff like that.
- 19:43Particularly around sports events, awesome television.
- 19:46Back when people used to watch live television instead of streaming at all, you know, they'd have like a murder mystery or something on a big soap opera.
- 19:54And so I'd like load test the application so that when everyone wants to find out who murdered the vicar's wife or whatever, then they'd all kind of click on the website at the same time and just trying to load test it.
- 20:05So some of the things I'd seen were trying to make sure you simulate browser load correctly, trying to distribute traffic in a realistic way.
- 20:14And then spikes in traffic are basically a different problem.
- 20:18So the thing we talked about, like the halftime problem where you've got everybody turning on the kettle at the same time, that's a special type of load test.
- 20:25It's a predictable spike as opposed to just out of the blue spike, right?
- 20:30That one is predictable, which is really nice.
- 20:31And then you get things like seasonal traffic.
- 20:34So for a lot of e-commerce applications and stuff like that, you would expect in the lead up to Christmas or in the Black Friday sale in the US, for example, like Cyber Monday, you know, you'd expect like a spike in traffic for those.
- 20:46But it will be distributed over a day or two.
- 20:48So you want to be able to like properly assess those.
- 20:52Another spike that I think you and I can both relate to is the driver of the day, Formula One.
- 20:58Oh, right.
- 20:58You've got 10 laps left within the race.
- 21:01Everyone now go to this thing and press this button and you have five minutes to do it or something, right?
- 21:07Like that's going to be a mega spike.
- 21:09Yeah, there can be some big, big loads in traffic.
- 21:11Sudden spikes are really hard to handle because you get bottlenecks in the network.
- 21:17You get bottlenecks in the database.
- 21:18You get bottlenecks in the web server.
- 21:20So those are kind of a unique type of problem to test for.
- 21:24If you're looking more like waves of traffic, so like, oh, you know, traffic would build up during the weekday or the type of application I have actually gets busier at the weekend or in the evenings.
- 21:35Then those are kind of the ones where you test ramped traffic.
- 21:38So all these load testing tools have a ramp up time or ramp configuration where certain locusts, for example, you say, how many concurrent users do you want to get to?
- 21:48So let's say that's like a thousand or something.
- 21:50And then how much do you want to ramp up?
- 21:52And you generally should always use a ramp because if you've got a thousand users, unless you've put them all in a room and said, okay, everybody click on the button at the same time.
- 22:02Like that's not going to happen.
- 22:04People, people log in gradually over a period of time.
- 22:07And if you don't use ramping and load testing tools, you actually create, you kind of end up simulating stuff that's not realistic.
- 22:14So you'd see like a massive spike in response times as the application gets backed up.
- 22:20But if it's not really realistic that you get those types of spikes, then don't, don't simulate it that way.
- 22:24We're going to talk about it a little bit, but there's a lot of different layers of caching as well.
- 22:29And if the app has just come to life, many of those layers of caching are not set up and warmed up.
- 22:35So that's a big deal.
- 22:37And it could be as simple as the web app parsing the Jinja or Chameleon or Django template, right?
- 22:44That's the very first time that can be slow.
- 22:45But then if you set it up right in production, it will not parse at the second, third and fourth request, right?
- 22:52So if we talk about Locust as an example, you've got on the screen, I think it's my favorite load testing.
- 22:57Locust is so good.
- 22:59It's really flexible and you define the user flows in Python code.
- 23:04So you write a class that represents a user and then you program in the steps that they would take.
- 23:09So like, oh, they start on the homepage, then they click on this page, then they click on this page.
- 23:15And then you can program in like the pauses and the randomness and stuff like that.
- 23:20And actually set up different types of personas or different types of users.
- 23:24I think this is a great way of designing a load test because you're thinking about what would the user do rather than like what's the throughput.
- 23:31So if you set up a Locust test where they start off at a point in your site, if your site has a login, how many of your users would log in?
- 23:39Because I think it's important to start off.
- 23:42If this is a new application, it's really hard to know.
- 23:44So you're going to have to come up with an educated guess or test like a range of parameters.
- 23:49But if this isn't a website that you've had running for, you know, a year or two, and you've got like a history of traffic, then you can look at it and say, okay, how many people on the homepage are logged in?
- 23:59Because there's a big difference.
- 24:00Because when you talk about caching and like CDNs and stuff like that, you know, you can cache the rendered template, but you generally wouldn't cache the whole rendered template if they're logged in.
- 24:11Because at the top, it might say, you know, hello, Anthony, and you wouldn't want to cache that so that when Mike clicks on the website, it says hello, Anthony.
- 24:18And he's like, who the hell's that?
- 24:19That would be very disturbing, wouldn't it?
- 24:21So there's a balance between caching and serving up dynamic content.
- 24:26And then also if you're presenting, you know, lists of products or someone's got shopping cart or something like that, obviously that's unique to them.
- 24:32So you're kind of trying to simulate this as best as possible.
- 24:36Otherwise, you just create these really optimistic load tests where you're like, oh, we tested the homepage and it can handle 50,000 users.
- 24:44But in practice, if a thousand of those log in, then the whole thing falls apart because all of a sudden you're not using the cached version of the homepage.
- 24:51Right.
- 24:52You know, another example that you brought up is, let's just take Talk Python, right?
- 24:56It's got coming up on 500 episodes.
- 24:58If you test and suppose I put really aggressive caching, like on the database results that come back from a single episode for the page and then just render that out of memory, basically, at least just out of the objects that are in memory.
- 25:10If you do your test to just hit the one, like let's just randomly pick episode 300, hit that one a bunch of times.
- 25:16But in reality, people are hitting all four or 500 and it's not all cached the same.
- 25:21Or maybe there's so much memory that each one has to hold that it like putting all 500 in memory and the cache like runs it out of RAM on the server.
- 25:28All sorts of stuff that happens as you get this kind of dynamic mix, right?
- 25:32You want to introduce randomness.
- 25:34So for example, on the login, if you've got a login flow or if you're clicking on a particular product, then try not to hard code which one it is.
- 25:44If you've got caching, you might not even see the caching either.
- 25:47Like databases do a lot of caching.
- 25:49So like if you're always pulling the same thing from the database, the database server is probably going to cache that.
- 25:55So, you know, it's going to be a lot faster.
- 25:57So if you can randomize the like the inputs or the pages that you click on or the flows that people take, then that's great.
- 26:04Locust actually has like an extra when you define a task, which is a decorator on a function.
- 26:09You can have like how often this happens.
- 26:12So you can have some tasks which happen more sort of frequently than others.
- 26:16So you can say, OK, you know, five times more people go to the homepage and then every so often somebody does a search.
- 26:23But then when you do the search, when you want to low test the search, you know, you want to randomize that a bit more than just always searching for the same thing.
- 26:31Right, right, right, right.
- 26:32Yeah.
- 26:33And just so for people who haven't seen Locust, you create a class, you give it a couple of functions.
- 26:38And then, for example, to test the homepage, you just literally put the task decorator on the function.
- 26:43And you say self.client.get slash self.client.get, maybe some additional assets or not.
- 26:48And then you can even hard code into the class what the domain is or localhost or support or whatever.
- 26:54And that's it.
- 26:55And you just you can assign a weight to these tasks like homepage five times more likely than about.
- 27:00So just put task of five instead of task by itself.
- 27:03Right.
- 27:03It's incredible.
- 27:04Yeah, it's really, really helpful.
- 27:05And then there's a library.
- 27:07There's a couple of things you need to keep in mind as well.
- 27:09And like if the user logs in and then it probably creates some sort of session, whether that's like a cookie or a token or something like that.
- 27:17So you need to store that somewhere in the class so that subsequent requests use the same login session.
- 27:23And then also, you know, frameworks like Django and Flask have got cross-site request forgery like protection.
- 27:31So they generate these CSRF tokens in the forms as well.
- 27:35So there's normally like a bit involved in getting the cookie or the session ID, getting the CSRF value.
- 27:42And then like, say, if you're submitting forms or you're like doing a search or something, you need to code a bit in Locust to work around the security controls.
- 27:52Right.
- 27:53For example, you might do a request to the page that has the form and then pull back the CRF token from a cookie and then use that as part of the form submission data.
- 28:03Otherwise, it might just say invalid.
- 28:07This portion of Talk Python is brought to you by WorkOS.
- 28:10If you're building a B2B SaaS app, at some point, your customers will start asking for enterprise features like SAML authentication, skim provisioning, autologs and fine grained authorization.
- 28:20That's where WorkOS comes in with easy to use APIs that help you ship enterprise features on day one without slowing down your core product development.
- 28:30Today, some of the fastest growing startups in the world are powered by WorkOS, including ones you probably know, like Perplexity, Vercel and Webflow.
- 28:38WorkOS also provides a generous free tier of up to one million monthly active users for AuthKit, making it the perfect authentication layer for growing companies.
- 28:48It comes standard with useful features like RBAC, MFA and bot protection.
- 28:53If you're currently looking to build SSO for your first enterprise customer, you should consider using WorkOS.
- 28:59Integrate in minutes and start shipping enterprise plans today.
- 29:04Just visit talkpython.fm/workos.
- 29:06The link is in your podcast player show notes.
- 29:08Thank you to WorkOS for supporting the show.
- 29:11You know, another one that would be really tricky would be like turnstile or reCAPTCHA.
- 29:17You're probably basically not getting that.
- 29:19It's not worth it.
- 29:20Yeah, you're stuck there.
- 29:22You've got to fill in those ones.
- 29:23Just maybe turn it off real quick for your test and then turn it back on.
- 29:27I don't know.
- 29:27I mean, you could do it, turn it off in development or something like that, right?
- 29:30Yeah.
- 29:30So when I look at this, this locus stuff, I see behind the scenes something like requests or HTTPX,
- 29:38where all it does is pull back the string of the HTML, or maybe even it just gets the head and actually throws away the content.
- 29:45I bet it streams it back.
- 29:46But what I don't imagine it does is it doesn't parse the HTML, realize that it's a view front end,
- 29:54execute the JavaScript that then has three API calls to more stuff on the back on the server, right?
- 29:59If it's a rich front end app, it probably doesn't treat it the same if we write it just like this, right?
- 30:05Yeah.
- 30:05So the other one I wanted to highlight is an extension to locust so you can connect it with playwrights.
- 30:11At this thing?
- 30:12It does HTML parsing.
- 30:14That's more like looking at the HTML to see if like a particular, you know, like a beautiful soup or responses was the framework for this.
- 30:23Right.
- 30:24Make sure timeout does not appear in the text or something like that, right?
- 30:27Yeah.
- 30:27Make sure the page doesn't have error in big letters.
- 30:30That's one thing you can do is checking that the content of the page actually contains the right thing.
- 30:34And then playwright is a UI, like a web testing tool.
- 30:39Playwright works really well with pytest.
- 30:42So I recommend using playwright anyway, if you're running a web application, because you can write pytest tests in playwright.
- 30:50Even better is if you want to get started with playwright, it has a code generator.
- 30:55So when you pip install it, you can run it in the code gen mode.
- 30:58It pops up a browser and then you just go on your website and just click around and then do what you would do normally.
- 31:05Type in, you know, fill in the forms, click on the buttons.
- 31:07And then whilst you're doing that in another tab, it actually generates all the Python code for the pytests.
- 31:13So it basically generates the pytest test code in a separate window automatically as you're clicking around in the browser.
- 31:20So like in terms of writing UI tests is quite difficult because often you have to be like, OK, how do I find the button?
- 31:26How do I click on the right button?
- 31:28How do I find the form?
- 31:29Especially with JavaScript, because it's not, you know, often things don't have a specific idea or you've got to figure out what selectors to use and stuff like that.
- 31:37So this makes it a lot easier because you can use the code gen.
- 31:39So that's a browser test.
- 31:41So with a load test normally in Locust, you're just making HTTP requests, but you're not actually rendering the page or running the AJAX or the JavaScript code.
- 31:50Whereas with Playwright, when you run Playwright, it's actually spinning up a browser and then driving the browser from Python.
- 31:56So you can plug Locust and Playwright together.
- 31:58There's a Playwright extension for Locust.
- 32:00So you can say, OK, each load test user, I actually want that to be a browser.
- 32:06And so when you say I want to test 100 concurrent users, it actually spins up 100 browsers.
- 32:14In a very, well, Playwright's actually really interesting how it works, but like there's a headless mode for browsers these days.
- 32:21So it doesn't actually run 100 windows like you can't see them.
- 32:24I can't use my computer while this is running.
- 32:26It's just overwhelmed by.
- 32:28But yeah, I don't recommend running 10,000 concurrents on a single laptop because it's going to run pretty slowly.
- 32:35It's going to have a bad time.
- 32:36But that is really important to actually test.
- 32:39So that will test it.
- 32:40All the stuff that we've been talking about.
- 32:42It'll test.
- 32:42If there's polling, it'll test that.
- 32:44If the CSS is not delivered over a CDN, it's going to go get that potentially white noise or wherever it's coming from.
- 32:52It's going to do all the things.
- 32:54Yeah, the challenge with it is that, you know, like you mentioned, running a browser, even if it's just a tab, uses a lot of resources.
- 33:01Whereas making a HTTP request using a request or something, you know, it doesn't really need anything.
- 33:06So you can quite happily in Locust, you know, make 100,000 requests and your local machine that you're testing on will be fine.
- 33:15It will.
- 33:16So, yeah, you can actually get low testing as a service.
- 33:20And the reason you'd probably want to use that is if you're testing a scenario where your local dev environment or your test environment just isn't big enough, where you need to distribute it, then you need more horsepower, basically, to go and run all the requests.
- 33:36Especially for something like Playwright, you know, where you need a lot of resources for the browser.
- 33:42Yeah, the little screenshot they have for Locust says 21,400 users are currently accessing the website.
- 33:48It's like, that's a lot of browser instances.
- 33:50That's a lot of browser instances.
- 33:52That looks like an API test, which would be a lot easier to do.
- 33:56APIs are easy.
- 33:57Yeah, so we, there's a service called Azure Low Testing.
- 34:00There is other options as well.
- 34:02I'm sure AWS has one and Google probably has one as well.
- 34:06Azure Low Testing, I know.
- 34:07And we are launching Locust support for that.
- 34:12At the moment, it supports Jamie to test.
- 34:14But yeah, we're going to be launching Locust support for that.
- 34:17I think by the time this episode comes out, it will probably be in public preview.
- 34:21So I've been using that and testing that.
- 34:23And the reason you would use it is, like I said, if you can run the Locust test locally on your machine and it runs great and brilliant, but, you know, you can ask us to spin up 50, 100, or even more instances running in parallel.
- 34:37And so if you want to do a really large scale test, then you can just basically get that from a cloud service provider like Azure.
- 34:44Yeah, awesome.
- 34:45I didn't realize you guys were bringing that online.
- 34:46That's cool.
- 34:47Yeah, nobody knows about us.
- 34:48Yeah.
- 34:49Can you do distributed?
- 34:51I think you can do some distributed stuff with the Locust even, can't you?
- 34:54Just, yeah.
- 34:54Yeah, you can.
- 34:56But you've got to have your own infrastructure, right?
- 34:57A lot of it uses SSH and you've got to, like, basically it kind of works over the shell.
- 35:02Yeah.
- 35:02So, yeah, we've kind of got our own distribution and, like, coordination system and stuff like that.
- 35:07And then also when you're running the test, your output, I guess, is what is the response time of the pages?
- 35:15And, like, what does that look like?
- 35:17And I think what confuses a lot of people is the first time they use Locust or JMeeter or one of the other tools, they get all these percentiles back.
- 35:25And they're like, oh, the 90th percentile response time is this and the 95th is this and the 99th is that.
- 35:31And they're like, okay, well, if I go back to my high school math, I think I can remember what percentiles are, but, like, which one matters?
- 35:38Because normally there's a massive difference between the 90th and the 99th percentiles with low testing.
- 35:44And, you know, you might say, oh, well, the 99th is like 10 seconds, but the 90th is like 300 milliseconds.
- 35:51So, you know, was that a good output or a bad output?
- 35:54I'm not really sure how to interpret the results.
- 35:56So, yeah, you'll see in the UI for Locust, it gives you percentiles.
- 36:01All the other low testing tools are very similar.
- 36:03It's basically like a distribution of the response times for those particular pages.
- 36:09And what you're trying to understand is, like, what is the expected response time?
- 36:14So, you know, if it's a bell curve, then what's the center point of that?
- 36:17Because the 99th is interesting, but often, like, it's because the cache was warming up or, you know, there was like one user that took 10 seconds.
- 36:26The cache expired and then it got recreated right then or something.
- 36:30Yeah, exactly.
- 36:31So for 99th percentile, you know, if it's 10 seconds, you might have one user that took 10 seconds and 99 users that took, you know, a couple of hundred milliseconds.
- 36:40So it's, you know, do you want to, you know, factor for that one user or do you want to focus on the other, on the bulk of the group?
- 36:48Yeah.
- 36:48And if you're like my daughter, you'll just say that the Internet's broken.
- 36:50Yeah.
- 36:51It could be, or it could be that YouTube is slow for some odd reason for you for a minute.
- 36:56Like, it's not necessarily the entire Internet that is the fault here.
- 36:59Yeah.
- 36:59Well, most users, I think if it took 10 seconds to respond with just clicking the refresh button.
- 37:05Yeah, exactly.
- 37:08So just generate more load.
- 37:10Yeah.
- 37:10You might have to program that into your load test.
- 37:12If it takes longer than five seconds, then issue another three requests.
- 37:16Yeah.
- 37:16That's a really good point, actually.
- 37:17Let's see.
- 37:18Are there any pictures of the reports here?
- 37:21Let's see.
- 37:22Yeah.
- 37:22You got these, you got these nice graphs that shows you response times.
- 37:26Yeah.
- 37:27They're not just graphs.
- 37:28They're live graphs.
- 37:28You can kind of see it like flowing as it's testing, right?
- 37:32As it's ramping up or whatever.
- 37:33Yeah, definitely.
- 37:35So what we do with the load testing services is we've got that graph.
- 37:39But then you can also say, I also want to see how many crews a second were happening on the database.
- 37:44What was the memory usage of the web applications?
- 37:47Like how many, like what was the pod size if you're using Kubernetes?
- 37:51Like stuff like that.
- 37:53I've got a couple of demos where I've got like parameter-based load testing, where when you're setting up a Kubernetes environment, you're like, oh, I'm not sure how much memory I should allocate the containers or like how many of them I should have in a pod.
- 38:06So you can basically just use something like GitHub Actions to give a matrix and say, okay, I want to test these configurations.
- 38:13Let's see what happens with half a gig of RAM per container or two gigs of RAM per container.
- 38:18Let's do two, four, eight in a cluster.
- 38:21And it will run the same load tests for every configuration.
- 38:23And then you can just compare all the graphs and say, okay, we don't want to over-allocate infrastructure because, you know, people often go a bit nuts.
- 38:31They're like, oh, how many instances in a cluster do we need on the front end?
- 38:34And they allocate like 16.
- 38:36But then when it's actually running, 15 of them are idle.
- 38:39So it's kind of just over-provisioning.
- 38:41So load testing can be a great way of not just like planning for spikes in traffic or being able to like cater for it.
- 38:49But actually the other way around, which is you run a load test and you realize you could probably actually do with less infrastructure.
- 38:55So like things like memory as well, like memory is expensive when you're buying it from a service provider.
- 39:01And, you know, CPUs and stuff like that or the number of instances.
- 39:04And actually, let's see what happens if we turn some of those things down.
- 39:08Will that impact the response times?
- 39:10Can we get away with it basically?
- 39:12Because in a lot of cases, you can actually spend a lot less money and get the same performance or even just a negligible difference.
- 39:19Or maybe you identify somewhere where you could add some level of caching.
- 39:23Yeah.
- 39:23And then all of a sudden you get, you can preview, you get 10x the load per worker process.
- 39:28You know, we could, we could have a smaller machine or smaller cluster or whatever.
- 39:32Yeah, exactly.
- 39:33So one, one thing I want, would like maybe to talk to the folks about is just how do you interpret these graphs?
- 39:39So you have like a request per second.
- 39:42And then you talked about, say, the 95th percentile response time and this ramping up.
- 39:47And usually when you look at these graphs, it's really obvious, like, yeah, we can add more users.
- 39:51But here is where we start to suffer consequences if we add any more users than this.
- 39:57Doesn't always just completely fall over.
- 39:59It just behaves worse until it does.
- 40:01It's kind of a bit like a feedback system for anyone who studies that.
- 40:05When you configure a ramp in Locust, you're saying, okay, how many users per second do we want to add?
- 40:11And you start off with a slow ramp is my suggestion.
- 40:16So like, you know, every 10 seconds, add one user.
- 40:19And would like be a really slow way of doing it.
- 40:21And then what you're looking at is the response times for each page, or you can get an average of everything.
- 40:28So if you're starting off with a load test that just tests one page, you slowly ramp up the users.
- 40:34Then you're looking at the response time graph.
- 40:36Often when you run a load test where it starts, the response times spike up at the beginning.
- 40:42Because, you know, the service was probably asleep.
- 40:45The database server was probably asleep.
- 40:47You know, it needs to like kick a few things into action to get it responding.
- 40:51So you often see like a spike at the beginning.
- 40:53That's fine.
- 40:54Don't worry about that.
- 40:55If as long as it's a short spike and not, it doesn't go on for hours.
- 40:58But we know once everything's warmed up, then you should see a stable graph.
- 41:02So the response times should stick around the same level, even as you add more and more users to a point.
- 41:09So it's basically looking at the two graphs, which is the response time and the number of users,
- 41:14and trying to understand how many users does it need to get to before that response timeline starts going up.
- 41:22And you basically know that's where you've introduced some sort of bottleneck.
- 41:26Something's reaching its limit.
- 41:27Yeah.
- 41:28That's where someone, you know, people are all of a sudden sitting in a queue somewhere.
- 41:33The hard part is actually figuring out where that queue is.
- 41:36If you don't have the time, but you've got the money, you can just throw more infrastructure at it.
- 41:39But often you can fix some of those queues by looking at where, what are we caching and where?
- 41:45Right. Look at the CPU usage of the various aspects.
- 41:48What's the database CPU load?
- 41:51What's the web process?
- 41:52If the web bit is kind of chill, but the database is at 100%, maybe you need better indexes or something.
- 41:59Yeah, because often you look at the infrastructure on the back end.
- 42:02And even though the response times are going up, so there's a bottleneck, the CPU is maybe still at 60%.
- 42:07And the memory might only still be at 50%.
- 42:09So you're like, okay, you know, more CPUs isn't necessarily the issue, but things are getting stuck somewhere.
- 42:15And that might be that, you know, each page, actually, I remember one low test I did where each, it was a PHP application using like some framework.
- 42:25And the performance like was horrible and we couldn't figure out why.
- 42:28And we put on some debugging tools and realized that every single page ran 200 SQL queries.
- 42:34Because of the way they've written it.
- 42:36It was like, oh, that'd be why.
- 42:38Because, you know, you're looking at the CPU and memory of the actual web servers and they're fine.
- 42:43Like the web servers are just like basically just sitting there continually waiting for the database server to respond.
- 42:49So, you know, you look at the resource usage and you might be thinking, well, why is it getting slower?
- 42:54But often it's because you're making calls to the database or a backend API or something.
- 42:59And it's just sitting there idle waiting for the responses.
- 43:02So I know you mentioned the show a few times, but like the N plus one issues and stuff like that for ORMs in particular is where you get those types of scale where you're, you should not be seeing that many SQL queries.
- 43:14It's so easy to do because you write the same code, get the things and then loop over some and interact with some aspect of them.
- 43:21And if you don't eagerly do the query at the first part, each one of those is a separate query.
- 43:26The more you get back, the worse it is.
- 43:28Yeah, exactly.
- 43:28And I mentioned like an AI app, like with this LLM rag app that we did a low test on with like 10 users.
- 43:35And like the CPU and memory were fine on the front end because it's not really doing anything.
- 43:38It's just calling the LLM.
- 43:39But it hit like a token limit, like really, really quickly.
- 43:43And because we were capturing that and tracing it, then we could see like that's what the bottleneck was, that it was getting rate limited on the on the back end.
- 43:50One of the challenges that people can run into, this is really important, is testing with way less data than you're going to have in production.
- 43:58I've got 10 entries in the database because that's what I bothered to test type in while I was playing with the app.
- 44:04But I've got a million in production.
- 44:06Stuff like that, right?
- 44:07I've got three products and there's a million in production.
- 44:11I've seen this a few times where people have done load testing and they're like, oh, on the all users page or on the list products page, like it runs super fast because they've deployed the dev environment and it's got like no products in the database.
- 44:24It doesn't matter if you have indexes because there's only three things, just return them all.
- 44:28Yeah.
- 44:29Or it's good.
- 44:29Yeah.
- 44:29And you don't see things like the N plus one because, you know, there's only like a couple of products, if any at all.
- 44:34So you often want to seed the application with as much fake data as possible.
- 44:40And the library that I absolutely love for this is called Mimesis, which is a fake data generator, kind of similar to Faker, but it's got a lot more support for like different languages and environments and stuff like that.
- 44:53So if you wanted to say, okay, let's create a hundred thousand users and you need to generate names and addresses and locations and stuff like that, you can do that using Mimesis really easily.
- 45:04And also if you want to do like test like different cultures or locales.
- 45:10So, you know, not just testing like English names, but testing all sorts of different countries and stuff like that or different phone numbers, then yeah, you can use Mimesis to do that.
- 45:19Yeah.
- 45:19It's got all kinds of different things.
- 45:21You get credit cards and phone numbers and all kinds of stuff.
- 45:25Yeah.
- 45:25I need a hundred thousand Brazilian phone numbers and it will just give you them in exactly the right format.
- 45:31Yeah.
- 45:31So then you save those to your database once.
- 45:33Yeah.
- 45:34And then you can run your tests and see if you have your plus one problem or you, your indexes don't fit into memory or whatever the problem might be.
- 45:42Right.
- 45:42Yeah.
- 45:42So for Django or for SQLAlchemy, you can do the, like these load and dump commands.
- 45:49So what I kind of recommend is to keep it fast is to use Mimesis to generate a seed data file, even if that's like JSON or something, and then just do like a bulk load in the test environment.
- 46:01And then you can reset it every time.
- 46:02So you can basically just do a rollback and then just reset and do a bulk load.
- 46:06If you're using like a document database, then they've got similar tools for that.
- 46:10So if you're using Mongo, then you could just do a bulk load from like a test command, basically.
- 46:15Right.
- 46:15Just do a load it up with some fake data, do a Mongo dump.
- 46:18And then whenever you're ready to reset it, just do a Mongo restore --drop and it'll.
- 46:22Yeah, exactly.
- 46:23From scratch.
- 46:23Yes.
- 46:24Yep.
- 46:24And you can reuse those for your integration tests as well.
- 46:26If you're writing, if you've got integration tests with Django or Flask, then often you need a database fixture and you want to seed that with some information, then you can just reuse the same data.
- 46:35But also do transactions, right?
- 46:37Like put a, put a transactions that's always rolled back.
- 46:40So once you load it up, you're not, you're not breaking it.
- 46:43But that's tricky if the code itself calls commit, I suppose.
- 46:45Yeah.
- 46:46Some idea.
- 46:48No, it changed it.
- 46:49Darn it.
- 46:49Yeah.
- 46:49There was a question in the chat.
- 46:51How does load testing change when you have non-deterministic processes, e.g. LLM queries, which is a really good question.
- 46:57And I think it's kind of related to this where you've got a, you want to introduce like an element of randomness.
- 47:03But we talked about like a, you know, a user search page, or if you've got like a chat feature or something, then you want to kind of vary the question, especially if you've got any kind of caching.
- 47:14So that is tricky though, because at the moment, like the LLM calls, depending on which model you're using and how it's set up and stuff like that, like, but they can take, you know, a second to 10 seconds to get a response.
- 47:27Easy.
- 47:27Yeah.
- 47:28So, you know, you need to kind of factor that in.
- 47:30It's like, how is your app, how does your app handle sitting and waiting for 10 seconds before it gets a response back?
- 47:37And often you'll find that you'll max out the number of like threads you've got, all the number of workers in like Gunicorn or Uvicorn or something like that, because they're all just sitting there waiting.
- 47:47That's a place where async and await would be pretty awesome.
- 47:50Yeah.
- 47:50Right.
- 47:51Because you can sort of let the thread go and just, you'll get back to it.
- 47:55Also, maybe you just need a different architecture.
- 47:58You know, maybe it's not just more caching or more hardware.
- 48:00It's like, so we're going to put the, there's a pending request in the database and we're going to push that off to somewhere.
- 48:06And then we'll just set up like some kind of JavaScript thing to check if it's done and then pull down the answer or, you know, something that's not necessarily blocking potentially.
- 48:14I think in a way it's great for, like you mentioned, great for that.
- 48:17Actually, there was an announcement pretty recently that you can run Uvicorn now, like as a production.
- 48:23Yeah.
- 48:23Without being wrapped through Gunicorn.
- 48:25Yeah.
- 48:26That's awesome.
- 48:27For this kind of scenario we just talked about where you're waiting on backend calls and stuff like that, use the async version of those libraries.
- 48:33Like if it's OpenAI, there's an async version of the OpenAI SDK.
- 48:37Use that.
- 48:38And then at least like Uvicorn is going to just have, it will quite happily run like hundreds and hundreds of those requests that are just waiting on backend calls.
- 48:46Absolutely.
- 48:47So one thing I want to throw in here, this is coming from me, not from you, but I want to get your thoughts on it.
- 48:53So I recently set up this thing called Uptime Kuma.
- 48:56Are you familiar with Uptime Kuma?
- 48:58No.
- 48:59So this is an open source, self-hosted Uptime monitoring tool.
- 49:03And just run it in Docker.
- 49:06Off it goes.
- 49:06And one of the things that's nice about this is, well, I, over on Talk Python.
- 49:11Now, if you go to the bottom, it's got a server status and you can come in here, you can see the websites behaving well.
- 49:17The RSS feed is behaving well, the courses size, but also the mobile API, all these things.
- 49:22But the reason I think this is, I mean, status, like, is it working or not?
- 49:26It's not really that relevant.
- 49:27That's like an operational thing.
- 49:28But what's cool about it is if you go and dig into the thing, you go to the dashboard, like, let's say, you could even group them, right?
- 49:36So like, let's do this.
- 49:38I'll do the API.
- 49:39So for like the mobile apps API or the mobile app for Talk Python courses, you can actually see over time what it's seen in response time for like days.
- 49:50So with your load tests, you're saying, let me just hammer it and see how long, how it responds to that, right?
- 49:57And if you point something like this, or there's many other tools like this, but this is a nice one.
- 50:01You can just say, just keep a record and let me go back and look for, you know, for 24 hours.
- 50:06How is the load looked?
- 50:08And you can see there's some weird spike there.
- 50:09It probably did like a deployment, like right around then or something.
- 50:12But in general, you know, 40, 50 milliseconds, 35, 81, right?
- 50:17It's handling it.
- 50:18Because if the thing was overwhelmed, then these tests would also suffer the latency that everyone else is suffering, you know?
- 50:25And this is like every 30 seconds.
- 50:27Yeah, that's really cool.
- 50:28I do want to give a shout out to OpenTelemetry.
- 50:30I hope more people, this, every year, more and more people find out about this and they're like, oh, this solves a lot of my problems.
- 50:37It's a CNCF project.
- 50:42So it's a big open source standard.
- 50:44It's basically is a standard for instrumentation, like an observability.
- 50:49So you kind of plug these instrumentation libraries into your app.
- 50:54And there's ones for fast, FastAPI, Django, Flask, like the backends, like Mongo, like SQLAlchemy, stuff like that.
- 51:02And you basically just install these packages and it will just start to capture all this information about, oh, there's a request and it had these parameters and it took this long.
- 51:12And so in Python, like with a very small amount of code and a few packages, you can instrument all this stuff in your application.
- 51:19And then the other thing that OpenTelemetry does is it will have, it's kind of like a pluggable exporter.
- 51:25So wherever you want to send all that data to, you can kind of pick and choose like which platform you want to send it to.
- 51:33And there are some like local ones you can use as well.
- 51:36Like you showed this tool for uptime.
- 51:38There's some Docker containers for that.
- 51:40So like you just spin up a Docker container and it will just take all that telemetry data and give it to you in a GUI.
- 51:46So you can see like all the traces, how long they took, what calls it made on the backend, what queries it was running.
- 51:52Like OpenTelemetry is a brilliant way of doing that.
- 51:55And it's not just Python.
- 51:56Like if you've got some components of your application that are written in other languages, like odds are that it is also supported.
- 52:04So it's kind of like a framework, I guess, for capturing data.
- 52:07And when you're doing load tests, this is how we do like postmortems to figure out or even just looking at the stats and seeing like where did things go slow.
- 52:16And I've got some videos and stuff of demos I've done with this where I've got like applications.
- 52:21I've done a load test on them and then I can see, oh, there was a spike in load.
- 52:25Let's go back and look at the data to see what caused that and which calls was it?
- 52:30You know, what was the resources, the memory usage?
- 52:32And often like, you know, was it the database or was it an API call?
- 52:36And why did it take so long?
- 52:38What were the special parameters, stuff like that?
- 52:40So you.
- 52:40What were we waiting on?
- 52:41Was it the actual web app or was it a database?
- 52:43But like you can retroactively know that, right?
- 52:46Yeah.
- 52:46And I can tell them, she's a great, great way of doing that.
- 52:48Yeah.
- 52:48That's super cool.
- 52:49That's way more holistic than my just give me a graph or response time.
- 52:53Yeah.
- 52:53You know, that's still cool.
- 52:54It's still cool.
- 52:56It's, you know, 20 minutes to set it up in the Docker cluster.
- 52:59Yeah.
- 52:59Awesome.
- 53:00Well, let's talk about, I know we're basically out of time, Anthony.
- 53:03Let's just talk really quickly about one thing that I think is a little bit tricky.
- 53:07And maybe just to get your thoughts on, you know, my Vercel example of cost is sort of
- 53:13in this space.
- 53:13And that's serverless.
- 53:15What are your thoughts on serverless?
- 53:17Like you have way more control when you have a VM or you've got a Kubernetes cluster or whatever
- 53:22it is you're working with.
- 53:23Right.
- 53:23I don't know.
- 53:24I always kind of see serverless.
- 53:25It's a really cool idea, but it's more of a commercial concept than a technical one.
- 53:30Yeah.
- 53:31I'm thinking more about just the, you don't control the warmup.
- 53:34Yeah.
- 53:34You don't control the machine.
- 53:36You know, there's just a lot of stuff that's black box to you.
- 53:39I think most of the platforms and most of the providers have got a, like a range of options.
- 53:45Like I speak to Azure Functions, which is like our serverless Python engine.
- 53:51You can use that as like just pay per request, but you know, it then has certain optimizations
- 53:56so that it has like a warm startup time.
- 53:58So, you know, if it's, you know, if you haven't made any requests for, you know, a certain number
- 54:02of hours, then the application will fall asleep.
- 54:04So it's not using resources and we're not charging you money.
- 54:07But if you don't want to have that, if you want the startup time to be super fast, then
- 54:10you can use a different service level basically and have like faster startups.
- 54:15You can have like pre-allocated resources, which then becomes not serverless, but it's
- 54:20the same framework.
- 54:21It's the same architecture and Lambda is the same.
- 54:23AWS Lambda is the same.
- 54:25You can kind of set it up in those different options.
- 54:27Things kind of get tricky when you get into Kubernetes because you've got real infrastructure
- 54:32on the background.
- 54:33And, you know, when you look at Kubernetes clusters or Kubernetes as a service or however you're
- 54:38setting it up, the first question is like, how many VMs do I need to provision and how
- 54:42big do they need to be?
- 54:43So yes, you're kind of building like a serverless abstraction on top of it, but you've got real
- 54:50infrastructure in the background.
- 54:51That's actually really hard because in a lot of cases, you've just got tons of idle infrastructure.
- 54:56So I think low testing is a good way of looking at like trying to right size what you've got
- 55:01provisioned.
- 55:02In many cases, scaling down some pits and scaling up others.
- 55:06I don't mess with Kubernetes.
- 55:06It's too much for me.
- 55:08I don't need all that.
- 55:09I don't need all that stuff.
- 55:11I do use Docker though, which is really, really nice.
- 55:13And I know a lot of these tools we talked about support running the Docker or working with Docker
- 55:17and so on.
- 55:18Yeah.
- 55:18All right.
- 55:19Well, hopefully people have some really concrete ideas and tools that they can use like Locus.io,
- 55:25which we're both huge fans of.
- 55:27Playwright, same deal, but also some of the philosophy and ideas behind it, which is super
- 55:32important too.
- 55:32So much appreciated you coming on the show to share that and just the chance to catch up.
- 55:36How about you give us a quick wrap up, important takeaways for people who want to go out and
- 55:41test their stuff now?
- 55:42Yeah.
- 55:42So I think step one is to look at your application and understand what your users are likely to
- 55:48do.
- 55:48So if you want to design a low test, start simple and maybe even start with Playwright
- 55:52because you can just, you just spin up the browser recorder, click around in the website
- 55:56and, you know, simulate what a user would be doing.
- 55:59Stitch that together with Locust and test 10 users.
- 56:02Don't go nuts.
- 56:03Start off with a small number, simple test, and you will uncover the things that need optimizing.
- 56:09I don't think I've ever encountered an application that just ran really efficiently the first time
- 56:13and then just keep working on that.
- 56:15So yeah, instead, because often you would end up trying to,
- 56:18kind of optimize things that are not going to get touched or don't really make a difference
- 56:21when you actually test it.
- 56:23So yeah, start simple.
- 56:24I recommend using Locust and Playwright if you want, or you can just write a simple Locust
- 56:29test and then put the instrumentation in the backend so that you can see not just the response
- 56:34times, but you can see as much data as you can, as possible on what's happening and how
- 56:39long it's taking.
- 56:40And I'll share like a couple of links of some simple dashboards you can use with OpenTelemetry,
- 56:46whether it will capture that data locally or in the cloud and show you, you know, a trace
- 56:50of every request.
- 56:51Awesome.
- 56:52And do that with real data.
- 56:54Don't do it with three entries.
- 56:55Yeah.
- 56:57It's not going to, not going to mean what you think it means if you do it with only
- 57:00three entries.
- 57:00Yeah.
- 57:00All right.
- 57:01Well, always great to have you on the show.
- 57:03Thanks for being here, Anthony.
- 57:04Catch you later.
- 57:04Great to be back.
- 57:05Thanks for much.
- 57:06Yeah.
- 57:06Bye.
- 57:06This has been another episode of Talk Python To Me.
- 57:10Thank you to our sponsors.
- 57:12Be sure to check out what they're offering.
- 57:13It really helps support the show.
- 57:15Take some stress out of your life.
- 57:17Get notified immediately about errors and performance issues in your web or mobile applications with
- 57:22Sentry.
- 57:22Just visit talkpython.fm/sentry and get started for free.
- 57:27And be sure to use the promo code talkpython, all one word.
- 57:31This episode is brought to you by WorkOS.
- 57:34If you're building a B2B SaaS app, at some point, your customers will start asking for
- 57:39enterprise features like SAML authentication, skim provisioning, audit logs, and fine-grained
- 57:44authorization.
- 57:45WorkOS helps ship enterprise features on day one without slowing down your core product development.
- 57:51Find out more at talkpython.fm/workos.
- 57:55Want to level up your Python?
- 57:56We have one of the largest catalogs of Python video courses over at Talk Python.
- 58:01Our content ranges from true beginners to deeply advanced topics like memory and async.
- 58:05And best of all, there's not a subscription in sight.
- 58:08Check it out for yourself at training.talkpython.fm.
- 58:11Be sure to subscribe to the show.
- 58:13Open your favorite podcast app and search for Python.
- 58:16We should be right at the top.
- 58:17You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 58:22and the direct RSS feed at /rss on talkpython.fm.
- 58:26We're live streaming most of our recordings these days.
- 58:29If you want to be part of the show and have your comments featured on the air,
- 58:33be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 58:37This is your host, Michael Kennedy.
- 58:39Thanks so much for listening.
- 58:41I really appreciate it.
- 58:42Now get out there and write some Python code.
- 58:44I'll see you next time.