简体   繁体   中英

Slim Framework response incredibly slow when returning JSON

I'm building a simple API based on Slim framework 3.8.1. I'm having an issue where the response is taking ~10 seconds to render. I've verified by process of elimination that it's the actual render of the response that's taking ~10 seconds--- it's not the database query, application bootstrap, etc.

I'm loggin the data I'm getting from the endpoint and it retrieves and renders it more or less instantly. The route actually even renders the JSON output right away in the browser, but then continues to spin for ~10,000ms.

Not sure what's going on here, especially since it's a pretty small set of data. Verified that this is happening in various browsers and also tests in REST clients like Postman.

I verified the middleware has long since executed when this waiting has happened, so that doesn't appear to be it.

Here's the minimal version of what I'm doing.

<?php
$api = new Slim();

    $api->any('/{plant}/{noun}', function ($request, $response, $args) {

        return $response->withStatus($status)->withJson(
                            $my_json
                        );
    }

If I use ->write() and just send a short text string instead it's fine. If I pass JSON to ->write() it hangs the same, for ~10 seconds.

Oddly though if I do something simple like return $response->withStatus(404)->withJson(['foo'=>'bar']); it returns a response instantly, with the caveat that the response body is truncated and just shows {" instead of the full {"foo":"bar"} response I'm expecting.

Here's the JSON body I'm passing successfully but that renders for ~10 seconds:

{
  "data": {
    "id": "14",
    "user_id": "1",
    "name": "foozzz",
    "description": "1q234567u12349",
    "sku": "",
    "price": "123.00",
    "shipping": {
      "r1-1": "123.00",
      "r1-1+": "123.00",
      "r2-1": "123.00",
      "r2-1+": "123.00"
    },
    "flexible_price": "1",
    "digital_fulfillment": "1",
    "physical_fulfillment": "1",
    "physical_weight": "0",
    "physical_width": "0",
    "physical_height": "0",
    "physical_depth": "0",
    "available_units": "-1",
    "variable_pricing": "0",
    "fulfillment_asset": "9",
    "descriptive_asset": "64",
    "creation_date": "1499186300",
    "modification_date": "1499707715",
    "variants": {
      "attributes": [],
      "quantities": [
        {
          "id": "13",
          "key": "\"{\\\"123\\\":\\\"PURPLE\\\",\\\"2442\\\":\\\"djdoos\\\"}\"",
          "formatted_name": "",
          "value": "13"
        },
        {
          "id": "14",
          "key": "\"{\\\"123\\\":\\\"PURPLE\\\",\\\"2442\\\":\\\"dskmkdjjd\\\"}\"",
          "formatted_name": "",
          "value": "10"
        },
        {
          "id": "15",
          "key": "\"{\\\"123\\\":\\\"dappsajd\\\",\\\"2442\\\":\\\"djdoos\\\"}\"",
          "formatted_name": "",
          "value": "123"
        },
        {
          "id": "16",
          "key": "\"{\\\"123\\\":\\\"dappsajd\\\",\\\"2442\\\":\\\"dskmkdjjd\\\"}\"",
          "formatted_name": "",
          "value": "81"
        }
      ]
    }
  },
  "status": 200,
  "status_uid": "commerce_item_200"
}

Originally I was thinking it was slow because of the size of the response, but this also takes ~10 seconds to finish loading (once again it renders instantly and then spins for the duration:

{
  "status": 404,
  "status_uid": "general_404",
  "status_message": "Route not found, or server error",
  "error_name": "There was an error while getting a response",
  "error_message": "The request failed."
}

Running this on PHP 5.6.30 on Apache/2.2.15 with no other problems like this. Any thoughts of why this might be occurring?

Just to recap: it's taking ~10 seconds to load, but verified it's not the database or middleware. If I take out the return $response the page loads right away.

Any thoughts? Thanks!

Got it--- apparently Slim was adding a super long Content-Length header size to the $response and it was making the request take a lot longer to load.

I changed the initialization to this and it fixed the problem:

$api = new Slim(['settings' => [
    'addContentLengthHeader' => false,
]]);

Confirmed that the full render + load time is now 113 ms instead of 10,000ms.

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM