Output Profile Info In A Console Application

Is it possible to output profile info in a console application. It is enabled in my console config correctly and I am using Yii::beginProfile() and Yii::endProfile(). I now need to capture the results some how and display them in the console. Am I missing something? Is this even possible?

Many thanks.

This is the thing that aop can handle it easily and it is for this kind of purpose.

So for yii. If you go to CController class there is two methods:




/**

	 * This method is invoked right before an action is to be executed (after all possible filters.)

	 * You may override this method to do last-minute preparation for the action.

	 * @param CAction $action the action to be executed.

	 * @return boolean whether the action should be executed.

	 */

	protected function beforeAction($action)

	{

		return true;

	}


	/**

	 * This method is invoked right after an action is executed.

	 * You may override this method to do some postprocessing for the action.

	 * @param CAction $action the action just executed.

	 */

	protected function afterAction($action)

	{

	}



What I would do is this:

In Controller class in your component package, I would override with those two methods:




/**

	 * This method is invoked right before an action is to be executed (after all possible filters.)

	 * You may override this method to do last-minute preparation for the action.

	 * @param CAction $action the action to be executed.

	 * @return boolean whether the action should be executed.

	 */

	protected function beforeAction($action)

	{

                echo "Begin:";

		echo "method is:".$action->getId();

                echo "controller is:".$action->getController()->getId();

                return true;  

	}


	/**

	 * This method is invoked right after an action is executed.

	 * You may override this method to do some postprocessing for the action.

	 * @param CAction $action the action just executed.

	 */

	protected function afterAction($action)

	{

                echo "End:";

		echo "method is:".$action->getId();

                echo "controller is:".$action->getController()->getId();

	}



So for any Controller that Extended from the Controller class, the framework will run the above overriden methods and print it. You are going to change the output according to your taste, as long as you have $action object, You have many information.

make sure you are returning true from beforeAction() method, if false yii does not run your real action(I guess that can be handy for security interseption).

Hope this helps

Thanks elbek. I can see that I can extend CConsoleCommand in the same way. However, I think I am nearly there with the following.




class SiteCommand extends CConsoleCommand

{

    

	public function actionDosomething()

	{


		Yii::beginProfile('test');

		//Do stuff that needs profiling.

		Yii::endProfile('test');


		$logger=Yii::getLogger();

		var_dump($logger->profilingResults);

		echo $logger->memoryUsage;

		

		

	}

}



Sorry man, I missed that u are in console :(. But you get the idea.

BTW, CConsoleCommand class has the two above mentioned methods: theck it from here

It is event based:so you will attach to event to the component:

onBeforeAction

onAfterAction

it will run it.

make those two events general for all commands, So for all of them the two events will be run, inside the method profile them.