r/learnprogramming • u/Genft • 11d ago
Some Questions about a Project before Moving Forward
Right, this is pretty specific, and I apologize if it's not really suitable for this subreddit.
At the moment, I'm working on a monte carlo setup using 10k loops for users to run some simulations on it. I'm writing it in python, which I've commonly done for myself, but I've decided to make it usable for a wider crowd as a sort of side project, so I've taken up learning some bootstrap and flask/flaskwtf/jinja so I can get it turned into a website. Once I've got the whole thing written, I intend to set up a VPS and have it accessible to the wider world. That part of the project is moving along just fine.
However, one consideration I've had is figuring out the server load: my simulation generates about 10-20 random numbers per loop and does a clean 10k loops, so about 100k numbers at the highest. By itself, that's peanuts. However, while I don't expect a massive amount of users, it's not unreasonable to expect several hundred or thousand accesses a day, enough that this can balloon into the billions, and in theory those could all happen in a pretty small period rather than spread across the day. This is where my problem comes in: I want to figure out if I need to be worried about size and processing power(in terms of what kind of VPS plan I pick), and possible solutions.
I've arrived at two possible ideas: offload the calculations to the client, or just write more efficient code. So, my question: how big of an issue is this going to be, and is client side a real possibility? From looking online, it seems like 10000+ runs can cause real headaches for javascript, plus, I have to assume my userbase will not only be using computers, but also smartphones. In theory that shouldn't be an issue, but I honestly don't know anything about it, so want to ask.
As far as just making the code more efficient, that's something I wanted to do already, but on that side of things I don't really have a way of measuring output and load for my code: are there tools, methods, or other ways of learning just how intense my code will be so I can plan for my server needs?
2
u/teraflop 11d ago
Benchmark, benchmark, benchmark.
Your question is a reasonable one, but your post is full of vagueness (e.g. forget about whether your code is "intense"). This is something you can quantify. Just measure how much time it takes to do one simulation, and use that to determine how many requests you can handle per second. For instance, in Python you can call
time.time()before and after handling each request, and subtract the two timestamps to find the elapsed time in seconds. (Your OS probably also has its own tools that you can use for measuring resource usage, e.g.perfon Linux.)If your simulation is single-threaded, then you can also multiply by the number of available CPU cores. That is, if a simulation takes 0.1 seconds using one core, and you have an 8-core CPU, then on average you can handle 80 requests per second (10 per second per core). To actually make this happen, you can make your backend server itself multithreaded, or you can run multiple single-threaded instances and load-balance between them. (By default, CPU-intensive Python code is always single-threaded, so the second option is more practical in your case.)
For server-side computation, the simple case is if you're using a "serverless" hosting provider, in which case you can very directly translate CPU time into a monetary cost. For instance, with Google Cloud Run, a process that uses 1GB of memory costs $0.00002 per CPU-second. In this case the hosting platform will automatically spawn as many concurrent instances of your code as necessary to keep up with requests.
If you're using a more traditional hosting paradigm, where you manage your own servers (physical or virtual), then you instead have a fixed cost that you pay regardless of how much CPU time is actually being used. But the number of cores on that machine will limit how fast you can process requests. If they start arriving faster than you can handle them, they'll just get queued up (there's lots more that could be said about where and how this queuing happens, but it's not super relevant to your question).
When it comes to choosing a VPS plan, as a rough ballpark you can assume that a single core of a VPS is probably about as fast as a single core of an average desktop or laptop. There is some variation but it's far less than an order of magnitude. So the number of cores is mostly enough information to make a rough decision.
Of course, as you said, it may make more sense to offload the workload to the clients.
Not sure what you read, but this is pure nonsense. JS doesn't get headaches. Again, this is just a question of resource consumption, which mostly means CPU time. JS can run a loop 10,000 times with absolutely no problems, it just takes about 10,000 times longer than running the loop once, as you'd expect. So you have to measure how much time that is, and decide whether you're OK with it.
With JS running in a browser, one of the main issues that you'll run into is that normally, the browser event loop is blocked while JS code is running. So if any single chunk of code takes longer than a fraction of a second, users will notice their browser interface freezing up. So you want to either break your computation into small chunks, and chain them together across frames using callbacks, or move the workload to a Web Worker which runs in its own background thread.
Presumably you have a smartphone of your own that you can use for testing? Of course there is some variation in performance across phone models. So look up some CPU benchmark scores for your phone, and for a typical phone from (say) 5 years ago, and multiply the measurements you take on your own phone by a corresponding factor to give you a rough margin.
1
u/kschang 11d ago
Force a login, if only to get their email address so you can mail back the answers when done, instead of waiting for immediate feedback. Yes, batching the job instead of going for parallel processing.
1
u/RyanDubois43 11d ago
Set up gunicorn with at least 4 worker processes to handle multiple simulation requests efficiently.
1
u/Aggressive_Ad_5454 11d ago
Your question is, generalized, “how can I provide a compute-intensive service through a web app, at scale?”
You suggested writing tight code for the service. That’s a good idea, but it lets you, maybe, double the amount of service you can provide on the same hardware.
You suggested doing the compute intensive code on your user’s client. I guess you mean in Javascript running in the browser. That’s a GREAT idea but you’ll need to write the Monte Carlo stuff in javascript, not python. It’s a great idea because it sticks your user with the electricity bill for your simulation. That is, it scales up with user-furnished hardware. If running the simulation in the browser UI Javascript thread annoys your users (it might, it makes interaction janky and sluggish) , you could add web worker support. That will keep the user’s browser lively while the computation runs.
If you want to do this simulation server side you can rig your web server to have a number of worker processes slightly less than the number of cores on your web server machine. Then, requests will queue up when many arrive concurrently. In this case, count the number of actual physical cores, not the doubled number that processor makers like to brag about in their marketing.
If you need more capacity it’s generally easy to upgrade a virtual private server to provision more cores. Easy, but not cheap.
1
u/jacksonanna373 11d ago
For code-level hotspots, run Python's built-in cProfile on one full simulation to see where time goes. Optimize the hottest function, then rerun the same workload; now you have evidence for whether a rewrite is worth it.