0

I am adding CORS support to my OpenWhisk/IBM Cloud function. But after making the modification ( -a web-custom-options true ) to the function I noticed a decline in performance. To isolate the issue I created a simple function, see below:

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;

        }
    }

There a two scenario's 1. .http endpoint and web-custom-options true 2. .json endpoint and web-custom-options false

As you can see from the screenshots the difference in OPTIONS is large, but also the difference in the POST replies is notable. Is there something I can do to get CORS support with some performance ? Am I doing something wrong in the JAVA code ?

enter image description here enter image description here

mpjjonker
  • 917
  • 1
  • 6
  • 28
  • I also deployed the function to the eu-gb region and there it is much faster ?Can this (several seconds) be caused by latency ? I don't have this experience with other us-south based services... – mpjjonker Feb 26 '18 at 15:55

2 Answers2

1

When you create a web action with a custom options response, the corresponding function will execute and produce the OPTIONS response. However, if you allow the default OPTIONS response to take affect, no function will execute and the default response is provided by the API host.

The default response is shown here: 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

Since you are executing a Java action, the start up time could explain the performance you're seeing. I see an init time of 313ms and a duration of 342ms just executing a cold start (by no means is this representative, of course).

user6062970
  • 831
  • 4
  • 5
  • Thanks but this does not explain the 7 to 10 seconds respond time. It is much better on de eu-gb region, so for now I can continue with that, but it remains strange behaviour. – mpjjonker Feb 26 '18 at 20:04
  • 1
    Take a look at the activation record (wsk activation list/get) and see what the wait time, init time, and durations are. – user6062970 Feb 26 '18 at 21:06
  • since the move to the eu-gb region I have good performance, for me this works. The activation records show indeed hundreds of milliseconds, which is fine for us. – mpjjonker Feb 27 '18 at 10:19
  • can you confirm that if we would switch to Python or Node that we might have a shorter activation time. I can image that starting a JVM takes a bit more time? – mpjjonker Mar 01 '18 at 13:38
0

Moved to eu-gb and no more performance issues

mpjjonker
  • 917
  • 1
  • 6
  • 28