M
M
Mors Clamor2019-10-24 23:32:07
Yii
Mors Clamor, 2019-10-24 23:32:07

Why does the API take so long to respond?

Hello. At the moment, api is being done on a local average power machine. To add a user (registration, that is), I use the same RegisterUser model as on the site. As a result, the method looks like this:

public function actionCreate()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $data=(array)json_decode(Yii::$app->getRequest()->getRawBody());
        $newUser=new RegisterForm();
        $newUser->attributes=$data;
          $registerResult=$newUser->register();
  /*В методе register возвращается помимо true/false id созданного пользователя, поэтому массив*/
          if ($registerResult["success"]) {                                      
            Yii::$app->response->statusCode = 201;
            Yii::$app->response->headers->add("Location","/api/user/".$registerResult["id"]);
            return "";
          }
        else {
          Yii::$app->response->statusCode = 409;
          return $newUser->errors;
        }
      }

It seems to be as simple as possible, but even in the case of an error, when the model does not pass validation and 409 is returned, the request takes 200-300 ms, which seems to me a lot, because I read that it's good if about 50 ms. What could be the bottleneck?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2019-10-25
@66demon666

1. Is this all the code that is being used? Show what you have in the form of the register method? Maybe there is some complex cycle - no one guesses.
2. Where does it start to slow down for you? On production? Then the problem is most likely in the server itself. There may be some pitfalls. For example, php version, mysql. Compare local setup and production configuration. For example, if you use php7.2 on the local, and php5.6 on production, then the difference is very big in speed. But that's unlikely, of course. Also see if OPCache is enabled. In general, there are many nuances here
. Update the packages on the production server. composer update --no-dev . This command will disable packages that are needed for development (tests, Fixtures). It shouldn't affect much, but the work will be a little faster.
Also, check if the prod
3 constant is on the production server. If it slows down on the local one, then maybe here. Since in development mode a lot of resource-intensive debugging works.

A
Antonio Solo, 2019-10-25
@solotony

PHP frameworks are slow.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question