Long TTFB / slow on custom api call

#1

I have created a custom API endpoint that returns only a few fields (rating, rating_de, rating_en, published) from a collection with around 12 fields (and currently has 93 entries).

I notice, that it takes quite some time (± 3300ms) until ttfb, although the response then is only 3.74kb in total.

What can be a reason why it takes so much time for a custom api call? I use a sqlite db for collection storage. My php knowledge is unfortunately very little. I would be glad for any help to speed up the custom api call.

<?php
  // following was modified from modules/Collections/Controller/RestApi.php:
  $options = [];

  $fields = ['rating'=> 1, 'rating_de'=>1, 'rating_en'=>1, 'published' => 1];
  $options['fields'] = $fields;

  // cast string values if get request
  if ($fields && isset($_GET['fields'])) $options['fields'] = $this->app->helper('utils')->fixStringNumericValues($fields);

  // lang filter
  if ($lang = $this->param('lang', false)) $fieldsFilter['lang'] = $lang;

  if (is_array($fieldsFilter) && count($fieldsFilter)) {
      $options['fieldsFilter'] = $fieldsFilter;
  }

  return cockpit('collections')->find('testimonials', $options);
?>
#2

Try $app->helper('utils')->... instead of $this->app->helper('utils')->....

#3

Thanks for your fast reply! It still takes around the same time though

#4

Ah, sorry. I did a quick test. Use $this->helper('utils')->... The other both methods returned a error message in a few milliseconds on my localhost. So this shouldn’t take 3 seconds, even when it’s wrong.

But I have a few questions:

Do you get your expected values in the response? 4KB sounds fine if you only fetch 4 numeric and boolean fields.

Do other api requests work?

Are $fields hardcoded? So do you care about GET requests, but don’t care about POST requests? Your current code only iterates over the hardcoded fields, that are numeric already.

Do you have a custom event on collections.find.after or collections.find.before, that might produce an endless loop?

#5

No problem. Good idea to indeed try this on localhost :slight_smile: After the change you recommended, there were, no errors.

I did and do get actually get my expected values back (filtered on language), Other api requests work as expected as well.

$fields are hardcoded in this file (ratings.php), since I wanted to send as little as possible to the frontend, and I don’t care for a custom post for now, maybe later. (Right now I only post through the official collection endpoint). I also tried right now with skipping the numeric values and it works as well, but still the big latency.

As for the events: I only have the following in /addons/Moderation/bootstrap.php:

  // force the Draft status when posted by API
  $app->on('collections.save.before.testimonials', function($name, &$entry, $isUpdate) {
    $entry['published'] = 'Draft';
  });
  // -->
}
#6

I’m sorry. I shortened the code and I don’t have any delay when requesting my test collection ‘pages’ without rating fields, but with a published field.

<?php

$options['fields'] = ['rating'=> 1, 'rating_de'=> 1, 'rating_en'=> 1, 'published' => 1];

// lang filter
if ($lang = $this->param('lang', false)) {
    $options['fieldsFilter'] = $lang;
}

return cockpit('collections')->find('pages', $options);

If everything else works, your server settings are fine. So it might be an addon. This should have been my first question: Which addons do you use and do you use the latest versions (addons and cockpit)?

You can disable addons for testing via config.yaml, e. g.:

modules.disabled:
    - Moderation
1 Like
#7

Sorry for my late response, and thank you very much for your instructions!! The speed issues are definitely due to the Moderation plugin, when disabling it’s blazing fast. I tried with updating the addon, but this doesn’t seem to have any effect :confused:

#8

Hey @natemate90, can you raise an issue on the Moderation github repo providing further details (response times, if you are using mongodb or sqlite, etc…) how it is slowing down API requests, on my tests don’t see any particular increase in response times.

The Addon just enabled should not affect the API by himself as it’s only acts when you have a moderation field in your collection.
If you have a moderation field it will trigger on the collections.find.before by adding a filter, and on collections.find.after but only for handling Draft entries (that is the bit that can involve a few more ms as it loads the entry revisions and to check the latest published revision, if you have too many revisions my suggestion is to use the Helpers addon and set a limit on number of revisions that a collection can have).

Something that may also help to alleviate the BE is to use caching, you can look into https://github.com/pauloamgomes/CockpitCMS-RedisCache , that will reduce a lot response times, or if you can’t rely on Redis use the https://github.com/agentejo/SimpleResponseCache addon.

1 Like
#9

Thank you very much @pauloamgomes !! It seems to have resolved with the Helper plugin. limiting the amount of revisions, there were previous revisions with many JPEG base64 entries that have been deleted now, having these stored in an sqlite db was not a smart decision I guess, so it’s totally my fault and not the plugin!:see_no_evil: