PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 1, 2022

[FIXED] Why does the first response takes more time in GAS web app?

 November 01, 2022     google-apps-script, performance, web-applications     No comments   

Issue

Edit: I am using clasp. Updated the code to the actual GAS!

I have a GAS deployed as a web app. We send POST requests from Slack via a slash command and it needs a response in less than 3000ms because GAS can't handle asynchronous code.

At the first request, it takes more than 3000ms to send a response but on the following requests, it is around 1500ms.

The doPost function looks like the following.

var exports = exports || {};
var module = module || { exports: exports };
Logger = BetterLog.useSpreadsheet('spreadsheetId');
function doPost(request) {
    var startExecutionDate = new Date();
    var path = request.parameter.path;
    Logger.log("Request received with path: " + path);
    var response = Responses.Error;
    var token = request.parameter.token;
    if (path.startsWith('/slack')) {
        Logger.log("Slack request");
        var slackRouter = new SlackRouter();
        response = slackRouter.post(request);
        // ...
    }
    // ...
}

And this is the code for the Slack Router.

var exports = exports || {};
var module = module || { exports: exports };
var SlackRouter = (function () {
    function SlackRouter() {
    }
    SlackRouter.prototype.post = function (request) {
        var path = request.parameter.path;
        switch (path) {
            case Routes.Team:
                Logger.log("For team");
                // ...
        }
    };
    return SlackRouter;
}());
exports.SlackRouter = SlackRouter;

I have the timestamps for each log.

First attempt

| Timestamp    | Delta in ms | Log Message   |
|--------------|-------------|---------------|
| 11:22:34:164 | 0           | Path: ...     |
| 11:22:35:354 | 1190        | Slack request |
| 11:22:35:462 | 108         | For team      |

Second attempt

| Timestamp    | Delta in ms | Log Message   |
|--------------|-------------|---------------|
| 11:22:45:047 | 0           | Path: ...     |
| 11:22:45:164 | 117         | Slack request |
| 11:22:45:350 | 186         | For team      |

I had several ideas already like the web app goes to a sleep state but since we calculate delta from the first log message it doesn't make sense.

So what is going on behind the scenes? Are you aware of any easy workarounds? If possible I don't want to build a microservice to send a response to Slack in time and later send the actual response.


Solution

The Apps Script servers don't keep every script ever written or deployed loaded in memory, and so scripts that haven't been run in a while need to be loaded from disk first. This is usually referred to as a "cold start time" in Cloud providers.

Answered by Eric Koleda on Google Apps Script Community forum



Answered By - beltekylevi
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing