0

I just register a Google Cloud free tier lately & took interested in Cloud Functions which can make me develope & run code without install OS system or Software Development Tools. So i want to test its performance to its limit with this function run on set up of 2GB & 2.8Ghz CPU

exports.helloWorld = (req, res) => {
  let message = getTheLottery();
  res.status(200).send(message);
};

function getTheLottery(){
for(var i=0,len=10000000;i<len;i++)
{
var ticket=sha256(makeid(5));
if(ticket=='7CD743877911812A45CD7974023A2D1ACA9831C82057902A2300874A951E6E17')
     return true;
    }
    return false;
}

The SHA256 algorithm was from here & makeid from here to generate random string

It take 40 seconds (40311ms) to complete the task, implement this code on C++ or C# add simple parallel & multithread it can easily run the task in less than 7 seconds on my average school PC with 4GB ram & i5 2.5ghz plus it has to run a OS system & few software in the background, let alone a code, when google claim it can make the code faster 75% compare to normal PC

I haven't try Azure cloud function. Assume i brought the "function" to my site & let ppl run 1000000 times, it'll cost 1657$ arcording to the calculator & maybe a fortune to run it few hundreds time for a full day with barely any way to optimize the code or system since it's all limited & the more time it run, the more it cost by SECONDS. With this money i can rent a VM with powerful GPU & run fully developed render or brute force software with maximum performance

Is there any better way for me to run the code? 'cause this feel like a online code compiler with extra steps. How does it make serverless computing the "future"?

  • 2
    Cloud functions are not designed for parallizable, compute-intensive tasks. – SLaks Apr 17 '19 at 16:38
  • @SLaks then why does it available with a such huge configuration? I mean it would be a waste just to use it to send & receive HTTP request – Dan Minh Toan Apr 17 '19 at 16:42
  • I wouldn't consider any Cloud Functions configuration to be "huge". – Doug Stevenson Apr 17 '19 at 16:44
  • Use Pub/Sub with a cloud function that deals with single or a chunk of data. Let each cloud function instance do tiny computations and at last sum up the result. – CSSer Nov 14 '20 at 16:15

1 Answers1

1

Heavy computation is not a use case for Cloud Functions. In fact, there are quite a few things it's not very good for, given that a server instance has only a single CPU, no GPU, and a maximum execution time of 9 minutes.

If you have a heavy compute load, consider using Compute Engine instead. If you want, you can write a Cloud Function that delegates work to Compute Engine.

Cloud Functions is intended to glue together other parts of your system without having to create and manage a formal infrastructure for it. The main benefits is effortless scaling (up and down), paying only for resources used, and the ability to trigger on events that occur in your GCP project.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • i already know about Compute Engine which is the VM i mention. Does Google have any service like Cloud function contribute to run heavy computation. I'm interested in the idea that one can focus alll the hardware just to run & compute code. it's way more convinient than register a VM, install OS & software to run the app – Dan Minh Toan Apr 17 '19 at 16:53
  • No. As I said, what you can do is write a function that delegates to Compute Engine. That's how you get serverless triggers with heavy compute behind it. – Doug Stevenson Apr 17 '19 at 16:56
  • May i ask if you could explain what's "serverless" about this? 'Cause when we rent a VM on an online system, the VM itself was consisded a server, does it make faster than a local PC somehow? – Dan Minh Toan Apr 17 '19 at 17:01
  • The Cloud Functions trigger is serverless. Compute Engine is not. They are different products that solve different problem in a way that's technologically feasible. It's not currently technologically feasible, while being cost effective, to combine them into a single product. – Doug Stevenson Apr 17 '19 at 17:14
  • 1
    Perhaps this might be helpful to get you started: https://medium.com/google-cloud/start-stop-compute-engine-instance-from-cloud-function-bf9ae5199609 – Doug Stevenson Apr 17 '19 at 17:15
  • i want to try this with TPU. can this code possible to run on python with tensorflow? – Dan Minh Toan Apr 19 '19 at 15:22