Thursday 1 April 2021

Numbers Game on Azure Functions

Porting the Numbers Game onto Azure Function is so easy!
Just like in the AWS Lambda approach, I created two Azure Functions in the same Function App:
  • GetNumbersGameHtml - triggered by HTTP GET
  • SolveNumbersGame - triggered by HTTP POST
Both functions are configured with the same Function Key value.

Code for GetNumbersGameHtml:


module.exports = async function (context, req) {
    const key= req.headers["x-functions-key"] || req.query.code;

    context.res = {
        // status: 200, /* Defaults to 200 */
        headers: {
            "Content-Type": "text/html"
        },
        body: myHtml+key+myHtml2
    };
}

const myHtml = '<!DOCTYPE html>\n'+
'<html>\n'+...
Code for SolveNumbersGame:

module.exports = async function (context, req) {
    const e=req.body;
    var numbers=[e.n1,e.n2,e.n3,e.n4,e.n5,e.n6], target=e.target;
    context.log("solving: "+JSON.stringify(e));
	var answer=solveAllGames(numbers, target);
    
    context.res = {
        // status: 200, /* Defaults to 200 */
        body: JSON.stringify(answer)
    };
}
...
Although the speed of Azure's CLI/Powershell/Portal is very slow comparing to AWS, the Function execution is blazingly fast - between 500ms-800ms - this is on the lowest App Service Plan (F1). The AWS Lambda function can be 10x as slow, usually between 3-8s - I had to increase the timeout setting of the function. In fact, to cater for the long wait time on the web GUI, I downloaded and customised a nifty spinner like below. I kind of enjoy seeing it going 4-5 cycles before it disappears when the solution is displayed; but with Azure, I can hardly see it complete one cycle! (a bit disappointed 😋).

Please wait while getting the solutions...

100
25
5
4
7
9

AWS REST APIs offer fine-grained control such as Model, Mapping Template, which are missing in Azure Function. On the other hand, the way Azure uses Keys for the Function App and Function is very simple to use and effective.

1 comment:

sachchitearp said...

Top 25 Hotels Near Harrah's Cherokee Casino, NC
Hotels near Harrah's Cherokee Casino · Casino at Harrah's Cherokee Casino 경산 출장샵 · 김해 출장마사지 Harrah's Cherokee Casino · Hotel Accommodations by Wyndham Cherokee · 당진 출장마사지 Hotels 전주 출장안마 with 제주 출장안마