【问题标题】:Performance difference in IBM Cloud Functions CORS supportIBM Cloud Functions CORS 支持的性能差异
【发布时间】:2018-08-06 02:01:09
【问题描述】:

我正在向我的 OpenWhisk/IBM Cloud 函数添加 CORS 支持。 但是在对函数进行修改( -a web-custom-options true )之后,我注意到性能下降了。 为了隔离问题,我创建了一个简单的函数,如下所示:

public static JsonObject main(JsonObject args) throws IOException {


        String method = args.get("__ow_method").getAsString();
        System.out.println(method+" handle");
        if (method.equalsIgnoreCase("OPTIONS")) {
            JsonObject responseJSON = new JsonObject();
            //add CORS headers

            JsonObject headers = new JsonObject();
            headers.addProperty("Access-Control-Allow-Headers", "*");
            headers.addProperty("Access-Control-Allow-Origin", "https://mjonker.github.io");
            headers.addProperty("Access-Control-Allow-Credentials", "true");

            responseJSON.add("headers", headers);
            responseJSON.addProperty("statusCode", 200);

            return responseJSON;
        } else {

            JsonObject responseJSON = new JsonObject();
            JsonObject headers = new JsonObject();      
            headers.addProperty("Access-Control-Allow-Headers", "*");
            headers.addProperty("Access-Control-Allow-Origin", "https://mjonker.github.io");
            headers.addProperty("Access-Control-Allow-Credentials", "true");
            headers.addProperty("Content-Type", "application/json");
            responseJSON.add("headers", headers);
            Date now = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
            JsonObject answerJSON=new JsonObject();
            JsonArray timeArray = new JsonArray();
            timeArray.add( "It is "+sdf.format(now));
            answerJSON.add("text",timeArray);
            responseJSON.add("body",answerJSON );
            responseJSON.addProperty("statusCode", 200);
            return responseJSON;

        }
    }

有两种情况 1. .http 端点和 web-custom-options true 2. .json 端点和 web-custom-options false

从屏幕截图中可以看出,OPTIONS 的差异很大,但 POST 回复的差异也很明显。 我可以做些什么来获得 CORS 的一些性能支持吗? 我在 JAVA 代码中做错了吗?

【问题讨论】:

  • 我也将该功能部署到了 eu-gb 区域,并且速度要快得多?这(几秒钟)可能是由延迟引起的吗?我在其他基于美国南部的服务方面没有这种经验......

标签: java cors openwhisk ibm-cloud-functions


【解决方案1】:

当您使用自定义选项响应创建 Web 操作时,相应的函数将执行并生成 OPTIONS 响应。但是,如果您允许默认 OPTIONS 响应生效,则不会执行任何函数,并且默认响应由 API 主机提供。

此处显示默认响应:https://github.com/apache/incubator-openwhisk/blob/master/docs/webactions.md#options-requests

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: OPTIONS, GET, DELETE, POST, PUT, HEAD, PATCH
Access-Control-Allow-Headers: Authorization, Content-Type

由于您正在执行 Java 操作,因此启动时间可以解释您所看到的性能。我看到 313 毫秒的初始化时间和 342 毫秒的持续时间只是执行冷启动(当然,这绝不是代表)。

【讨论】:

  • 谢谢,但这并不能解释 7 到 10 秒的响应时间。它在 de eu-gb 区域要好得多,所以现在我可以继续这样做,但它仍然是奇怪的行为。
  • 查看激活记录(wsk激活列表/get),看看等待时间、初始化时间、持续时间是多少。
  • 自从移至 eu-gb 地区以来,我的表现很好,对我来说这是可行的。激活记录确实显示了数百毫秒,这对我们来说很好。
  • 您能否确认如果我们切换到 Python 或 Node,我们的激活时间可能会更短。我可以想象启动 JVM 需要更多时间?
【解决方案2】:

迁移到 eu-gb,不再出现性能问题

【讨论】:

    猜你喜欢
    • 2019-12-01
    • 2021-10-18
    • 2016-06-12
    • 2019-02-10
    • 2014-10-26
    • 2021-06-05
    • 2017-08-13
    • 2020-06-19
    • 1970-01-01
    相关资源
    最近更新 更多