How to profile a Yii2 based API?

1.2k views Asked by At

I have an API application written in Yii2 framework and I don't know how to measure and track the performance as I am keen to see what is happening behind the curtain. My API is using mongoDB and I also would like to see the queries somehow I just don't know where to start. The Yii2 has its own integrated debugPanel which is great, but works only with browsers and can't have the benefits with using Postman to perform API calls for instance.

How do you guys do it on the dev and live environment?

Cheers

1

There are 1 answers

3
Blizz On

For basic measuring, Yii has some built in profiling. You can call it with \Yii::beginProfile() and Yii::endProfile() for that and view results via the debug toolbar.

For development you can also use Xdebug. It support profiling as well.

For production, that's something else. You want a solution that has as little impact on performance as possible and you want something to run regularly and automatic. You should keep track of routes and their profiled result so you can compare the improvements (or not) of your code over time.

I worked on a couple very high traffic sites and what we used was xhprof that activated randomly.

For example in your index.php you can do something like

if (rand(1, 100) == 50) {
   xhprof_enable();
   // on after_request() or register_shutdown_function(): store route and results
}

Obviously whatever you need may vary but perhaps this gives you some ideas in what direction to look.